1

I create a directed tree using pydot and visualize it. That is fine but I would also like to retrieve the leaf nodes of the tree. How can I do that?

In general the question is how to access the nodes and edges that are stored inside the pydot object. Does pydot provide this functionality or is it just a visualisation engine?

Bikash Gyawali
  • 969
  • 2
  • 15
  • 33

2 Answers2

3

Essentially is just a visualization engine. But there some functions that can be used to solve your problem:

>>> import pydot
>>> g = pydot.Dot(graph_type="digraph")
>>> g.add_node(pydot.Node(name="A"))
>>> g.add_node(pydot.Node(name="B"))
>>> g.add_node(pydot.Node(name="C"))
>>> g.add_node(pydot.Node(name="D"))
>>> g.add_edge(pydot.Edge("A","B"))
>>> g.add_edge(pydot.Edge("A","C"))
>>> g.add_edge(pydot.Edge("C","D"))
>>> g.get_nodes()
[<pydot.Node object at 0x1de1510>, <pydot.Node object at 0x1de1590>, <pydot.Node object at 0x7fb93407afd0>, <pydot.Node object at 0x1dd20d0>]
>>> leafs = {n.get_name():True for n in g.get_nodes()}
>>> for e in g.get_edge_list():
...     leafs[e.get_source()] = False
... 
>>> leafs
{'A': False, 'C': False, 'B': True, 'D': True}

It should work for you.

Michele d'Amico
  • 22,111
  • 8
  • 69
  • 76
1

This may fail when a graph is build only from edges without specify nodes directly.

I suggest the following procedure:

leafs = set(g.get_nodes())
remove = set()
for e in g.get_edges():
    remove.add(e.get_source())
    leafs.add(e.get_destination())

leafs.difference_update(remove)

This include isolated nodes as well

asterio gonzalez
  • 1,056
  • 12
  • 12