1

Graph-tool offers a lot of tools for assessing a graph: https://graph-tool.skewed.de/static/doc/topology.html. However, I can't find any method for calculating the girth, i.e., the shortest cycle in the graph.

Do you know if there exists an appropriate method or if I can use the existing ones to come up with an efficient calculation?

blindeyes
  • 409
  • 3
  • 13

1 Answers1

1

I think that this can be easily calculated using all_paths function.

g = gt.collection.data["karate"]
min_cycle_lengths = []
for v in g.vertices():
    cycles_v = list(gt.all_paths(g, source = v, target = v))
    min_cycle_lengths.append(min([len(x)-1 for x in cycles_v if len(x) > 3]))

girth = min(min_cycle_lengths)
Peaceful
  • 4,920
  • 15
  • 54
  • 79
  • Thank you! Needs to handle empty lists in case there are no cycles and is very computationally expensive, but does the job, as you probably can't get away without finding all the paths while keeping the solution simple. – blindeyes Nov 07 '18 at 11:33
  • @blindeyes There was an error in the code; corrected now. Basically, you must remove paths which just go to the neighboring vertices and come back immediately. I think this is an implementation bug in the graph tool. – Peaceful Nov 08 '18 at 07:28