dsa.draw module — ucxdsa documentation

# dsa.draw module

Module to access graphic drawing functions for Trees, Heaps, Tries and Graphs.

class dsa.draw.Draw

  Bases: `object`

  A base class for drawing various data structures.

  This class provides basic functionalities for rendering, saving, and displaying visual representations of data structures.

  draw(**kwargs)

    Display the rendered plot.

    Parameters:

      **kwargs – Additional keyword arguments.

  render(**kwargs)

    Render the visual representation of the data structure.

    This method should be overridden by subclasses to provide specific rendering logic. :type **kwargs: :param **kwargs: Additional keyword arguments.

  save(filename, **kwargs)

    Save the rendered plot to a file.

    Parameters:

      - filename (str) – The name of the file to save the plot to.

      - **kwargs – Additional keyword arguments.

  set_figsize(figsize)

    Set the figure size for the plot.

    Parameters:

      figsize (tuple) – A tuple representing the figure size (width, height).

class dsa.draw.GraphDraw(graph, directed=None, weighted=None)

  Bases: `Draw`

  A class for drawing graphs using the NetworkX library.

  This class extends the Draw class to visualize graphs. It supports both directed and undirected graphs, as well as weighted and unweighted graphs. Additionally, it provides an option to display the Minimum Spanning Tree (MST) of the graph.

  If directed or weighted parameters are not provided, they will be inferred from the graph type.

  graph

    The graph to be drawn

  directed

    Specifies if the graph is directed. Defaults to None

    Type:

      bool

  weighted

    Specifies if the graph has weighted edges. Defaults to None

    Type:

      bool

  Usage Example:

    gd = GraphDraw(g) # g is a Graph type (AdjacencyMatrixGraph, AdjacencyMatrixWeightedGraph, AdjacencyListWeightedGraph, AdjacencyListGraph) gd.draw()

  render(pos=None, show_mst=False, mst_only=False, **kwargs)

    Renders the graph using Matplotlib. Not to be called directly. Call draw() instead.

class dsa.draw.HeapDraw(heap, **kwargs)

  Bases: `Draw`

  A class for drawing a heap structure using the NetworkX library.

  This class extends the Draw class to visualize heap structures, such as binary heaps or min-heaps.

  heap

    The heap structure to be drawn.

    Type:

      Heap

  Usage Example:

    h = MinHeap() # Define your heap, e.g., MinHeap or Heap hd = HeapDraw(h) hd.draw()

  array_to_node(index, array)

    Converts an array-based heap into a tree node structure.

    This helper function recursively constructs a tree from the array representation of the heap, reflecting the binary tree structure of the heap.

    Parameters:

      - index (int) – The current index in the array representing the node.

      - array (list) – The array containing heap values, organized as a complete binary tree.

    Returns:

      The root node of the constructed subtree.

    Return type:

      TreeNode

  render(**kwargs)

    Renders the heap as a tree using Matplotlib. Not to be called directly. Call draw() instead.

    This method converts the heap into a tree structure and then uses the TreeDraw class to render it visually. Customization options can be provided via keyword arguments.

    Returns:

      The Matplotlib plot object for further customization or display.

    Return type:

      matplotlib.pyplot

class dsa.draw.TreeDraw(tree)

  Bases: `Draw`

  A class for drawing a tree structure using the NetworkX library.

  This class extends the Draw class to visualize tree structures. It organizes the nodes in a hierarchical tree layout and provides options for customization through the render method.

  tree

    The tree structure to be drawn.

    Type:

      Tree

  Usage Example:

    t = Tree(root_node) # Define your tree structure with a root node td = TreeDraw(t) td.draw()

  add_edges(graph, node, pos=None, x=0, y=0, layer=1)

    Recursively adds edges to the graph and positions the nodes in a tree layout.

    Parameters:

      - graph (networkx.DiGraph) – The graph object where edges are added.

      - node (TreeNode) – The current node in the tree.

      - pos (dict, optional) – A dictionary to store the positions of the nodes. Defaults to None.

      - x (float) – The x-coordinate for the current node. Defaults to 0.

      - y (float) – The y-coordinate for the current node. Defaults to 0.

      - layer (int) – The current layer/level of the node in the tree. Defaults to 1.

    Returns:

      A dictionary containing the positions of all nodes in the tree.

    Return type:

      dict

  render(**kwargs)

    Renders the tree using Matplotlib. Not to be called directly. Call draw() instead.

    This method generates a graphical representation of the tree with nodes positioned in a hierarchical layout. Customization options can be provided via keyword arguments.

    Parameters:

      **kwargs – Additional keyword arguments for customization.

    Returns:

      The Matplotlib plot object for further customization or display.

    Return type:

      matplotlib.pyplot

class dsa.draw.TrieDraw(trie, **kwargs)

  Bases: `Draw`

  A class for drawing a Trie structure using the NetworkX library.

  This class extends the Draw class to visualize Trie structures, commonly used for storing strings or prefix trees. It provides methods to convert the Trie into a networkx graph, arrange nodes hierarchically, and render the visualization using Matplotlib.

  trie

    The Trie structure to be drawn.

    Type:

      Trie

  Usage Example:

    trie = Trie() # Initialize your Trie and populate it with words trd = TrieDraw(trie) trd.draw()

  hierarchical_pos(G, root=None, width=1.0, vert_gap=0.2, vert_loc=0, xcenter=0.5)

    Computes the hierarchical position of nodes in the graph for visualization.

    This method arranges the nodes of the Trie in a hierarchical layout, which is particularly useful for visualizing tree-like structures such as Tries.

    Parameters:

      - G (networkx.Graph) – The graph for which to compute positions.

      - root (str, optional) – The root node of the graph. Defaults to None, which means the root will be determined automatically.

      - width (float) – The width of the entire drawing. Defaults to 1.

      - vert_gap (float) – The gap between levels in the hierarchy. Defaults to 0.2.

      - vert_loc (float) – The vertical location of the root node. Defaults to 0.

      - xcenter (float) – The horizontal center of the root node. Defaults to 0.5.

    Returns:

      A dictionary mapping each node to its (x, y) position in the layout.

    Return type:

      dict

  render(**kwargs)

    Renders the Trie as a hierarchical graph using Matplotlib. Not to be called directly. Call draw() instead.

    This version uses NetworkX’s default drawing tools with circular nodes for simplicity and clarity.

  render_rectangle(**kwargs)

    Renders the Trie as a hierarchical graph using Matplotlib. Not to be called directly. Call draw() instead.

    This method uses the hierarchical positions of the nodes to render the Trie as a directed graph. Nodes are drawn as rectangles, and edges represent the transitions between prefixes.

    Returns:

      The Matplotlib plot object for further customization or display.

    Return type:

      matplotlib.pyplot

  to_networkx()

    Converts the Trie into a NetworkX directed graph (DiGraph).

    This method creates a networkx graph representation of the Trie, where each node represents a prefix and each edge represents a character transition.

    Returns:

      The networkx graph representation of the Trie.

    Return type:

      networkx.DiGraph