Search code examples
pythondirected-graphpydot

get all leaf nodes of a directed graph in pydot


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?


Solution

  • 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.