Ejemplo n.º 1
0
def wr_png_array(bh_st, kwargs):
  """Given an array for a binary heap, draw tree."""
  import pydot
  prt = sys.stdout if 'prt' not in kwargs else kwargs['prt']
  fout_png = "binary_heap_{}.png".format('_'.join(str(e) for e in bh_st))
  label = get_png_label(bh_st, kwargs)
  # 1. Create/initialize Graph
  G = pydot.Dot(label=label, graph_type='digraph') # Directed Graph
  edge_idxs = get_edges(bh_st)
  badcol = {c:'#fe2f4a' for p, c in edge_idxs if bh_st[p] < bh_st[c]}
  # 2. Create Nodes
  mknode = lambda i, v: pydot.Node(
    "{V}[{I}]".format(I=i+1, V=v), 
    style = "filled",
    fillcolor = badcol.get(i, "beige"))
  nodes = [mknode(i,v) for i, v in enumerate(bh_st) if v is not None]
  # 3. Add nodes to Graph
  for node in nodes:
    G.add_node(node)
  # 4. Add Edges between Nodes to Graph
  for p, c in edge_idxs:
    G.add_edge(pydot.Edge(nodes[p], nodes[c]))
  # 5. Write Graph to png file
  G.write_png(fout_png)
  prt.write("  WROTE: {}\n".format(fout_png))
Ejemplo n.º 2
0
 def wr_png(self, fout_png="components.png", prt=sys.stdout, **kwargs):
     """Make a png showing a diagram of the connected components."""
     import pydot
     label = get_png_label(self.ID, kwargs)
     # 1. Create/initialize Graph
     G = pydot.Dot(label=label, graph_type='digraph')  # Directed Graph
     # 2. Create Nodes
     nodes = [pydot.Node(str(idx)) for idx in range(len(self.ID))]
     # 3. Add nodes to Graph
     for node in nodes:
         G.add_node(node)
     # 4. Add Edges between Nodes to Graph
     for child, parent in enumerate(self.ID):
         if child != parent:  # Print only edges from one node to another (not to self)
             G.add_edge(pydot.Edge(nodes[parent], nodes[child]))
     # 5. Write Graph to png file
     G.write_png(fout_png)
     prt.write("  WROTE: {}\n".format(fout_png))
Ejemplo n.º 3
0
 def wr_png(self, fout_png="components.png", prt=sys.stdout, **kwargs):
   """Make a png showing a diagram of the connected components."""
   import pydot
   label = get_png_label(self.ID, kwargs)
   # 1. Create/initialize Graph
   G = pydot.Dot(label=label, graph_type='digraph') # Directed Graph
   # 2. Create Nodes
   nodes = [pydot.Node(str(idx)) for idx in range(len(self.ID))]
   # 3. Add nodes to Graph
   for node in nodes:
     G.add_node(node)
   # 4. Add Edges between Nodes to Graph
   for child, parent in enumerate(self.ID):
     if child != parent: # Print only edges from one node to another (not to self)
       G.add_edge(pydot.Edge(nodes[parent], nodes[child]))
   # 5. Write Graph to png file
   G.write_png(fout_png)
   prt.write("  WROTE: {}\n".format(fout_png))
Ejemplo n.º 4
0
def wr_png_array(bh_st, kwargs):
    """Given an array for a binary heap, draw tree."""
    import pydot
    prt = sys.stdout if 'prt' not in kwargs else kwargs['prt']
    fout_png = "binary_heap_{}.png".format('_'.join(str(e) for e in bh_st))
    label = get_png_label(bh_st, kwargs)
    # 1. Create/initialize Graph
    G = pydot.Dot(label=label, graph_type='digraph')  # Directed Graph
    edge_idxs = get_edges(bh_st)
    badcol = {c: '#fe2f4a' for p, c in edge_idxs if bh_st[p] < bh_st[c]}
    # 2. Create Nodes
    mknode = lambda i, v: pydot.Node("{V}[{I}]".format(I=i + 1, V=v),
                                     style="filled",
                                     fillcolor=badcol.get(i, "beige"))
    nodes = [mknode(i, v) for i, v in enumerate(bh_st) if v is not None]
    # 3. Add nodes to Graph
    for node in nodes:
        G.add_node(node)
    # 4. Add Edges between Nodes to Graph
    for p, c in edge_idxs:
        G.add_edge(pydot.Edge(nodes[p], nodes[c]))
    # 5. Write Graph to png file
    G.write_png(fout_png)
    prt.write("  WROTE: {}\n".format(fout_png))