我有一个有向的、多重的、加权的图。我想找到 a -> b -> c -> a 的循环
我的图表样本。我希望它清楚:
v1 -> v2
v2 -> v3
v3 -> v1
v1 -> v4
v2 -> v5
如何仅迭代作为目标的节点?这是我的短
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"
我认为应该通过使用 Gython 语法来做出决定,摘自 Jython 教程:
v1 -> v2 (or v2 <- v1): selects the directed edge from node v1 to node v2.
我的最终结果应该是:
results = [[v1,v2,v3]]