3

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"])
sth
  • 222,467
  • 53
  • 283
  • 367

2 Answers2

1

Well you have the whole graph structure, via graph.get_edge_list() you can implement standard depth first search to find shortest path between nodes. Finding loops is likewise done with standard graph algorithms. See this article on Graph implementations in Python for sourcecode on how to do shortest path between two nodes.

If you're looking for the pydot library do do this for you, you might be out of luck.

I82Much
  • 26,901
  • 13
  • 88
  • 119
1

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.

Jochen Ritzel
  • 104,512
  • 31
  • 200
  • 194
  • For future reference, I have an example of a python script using networkx to identify cycles in dot graphs at: http://blog.jasonantman.com/2012/03/python-script-to-find-dependency-cycles-in-graphviz-dot-files/ Thanks Jochen for giving me the links to come up with the script. – Jason Antman Mar 29 '12 at 03:06
  • Your links are long dead. – Cerin Jun 17 '21 at 00:38