Search code examples
pythongraphvizpydot

Finding starts, ends, and loops with pydot


Is there a way to achieve this in pydot?

Take the following example:

[Outputted Dot File]

strict graph g{
"A" -> "B";
"B" -> "C";
"C" -> "D";
"D" -> "E";
}

[Python]

print(num.start)
>>> A
print(num.steps)
>>> ["a,b","b,c","c,d","d,e"]
print(num.end)
>>> E

or with the following case:

[Outputted Dot File]

strict graph g{
"A" -> "B";
"B" -> "C";
"C" -> "A";
}

[Python]

if num["A"] == num.loop:
print("[%s] loop detected")%(num["A"])

Solution

  • Pydot can write dot files, but it is not for analyzing graphs.

    You want NetworkX instead. It can read and write dot files, find circles, find reachable nodes and do topological sort.

    Look up the terminology of graphs on wikipedia and NetworkX can do the rest.