I have a directed, multi, weighted graph. I want to find cycles where a -> b -> c -> a
A sample of my graph. I hope it it clear:
v1 -> v2
v2 -> v3
v3 -> v1
v1 -> v4
v2 -> v5
How to iterate only nodes that are targets? This is my shor
results = []
for n in g.nodes: # iterates all nodes
x = n #marks the first node
for n1 in n.neighbors: #iterates neighbors, but this part should include only neighbors that are targets.
y = n1 # marks second node
for n2 in n.neighbors: #same as above, should select only target neighbors.
if n2 == x:
print "FOUND"
I believe the decision should come up by using Gython grammar, excerpt from Jython tutorial:
v1 -> v2 (or v2 <- v1): selects the directed edge from node v1 to node v2.
My end result should be:
results = [[v1,v2,v3]]
Sure some graph lib will bring this functionality. If you want to do it by hand, maybe this (quick and dirty, Dijkstra would kill me) snippet might give you some hints:
(I added another vertex from v5 to v2 in order to get more than one cycle)