简体   繁体   English

基于边的两个节点之间的所有路径

[英]all paths between two nodes on the basis of edges

My problem is all simple paths problem on hallucinogens. 我的问题是致幻剂的所有简单路径问题。 I need to find all the paths between two nodes on the basis of the edges and not nodes 我需要根据边缘而不是节点找到两个节点之间的所有路径

I found this as a solution but this approach is way too slow considering the size of my actual graph. 我发现这是一种解决方案,但是考虑到实际图形的大小,这种方法太慢了。 I would like to know what are the optimizations that could be done to better this. 我想知道可以做哪些优化来改善这一点。 I have modified the code given in the link to use deque() This is not of too much help either 我已经修改了链接中给出的代码以使用deque(), 这也没有太大帮助

  g=nx.MultiGraph()
  g.add_edge(1,1,key='a')
  g.add_edge(1,2,key='b')
  g.add_edge(2,4,key='c')
  g.add_edge(2,4,key='d')
  g.add_edge(3,2,key='e')
  g.add_edge(3,3,key='f')
  g.add_edge(1,3,key='g')
  g.add_edge(1,5,key='h')
  g.add_edge(5,5,key='i')
The Answer for all_path(1,4): all_path(1,4)的答案:

 Path: 0 --> [(1, 1, 'a'), (1, 2, 'b'), (2, 4, 'c')] Path: 1 --> [(1, 1, 'a'), (1, 2, 'b'), (2, 4, 'd')] Path: 2 --> [(1, 1, 'a'), (1, 3, 'g'), (3, 2, 'e'), (2, 4, 'c')] Path: 3 --> [(1, 1, 'a'), (1, 3, 'g'), (3, 2, 'e'), (2, 4, 'd')] Path: 4 --> [(1, 1, 'a'), (1, 3, 'g'), (3, 3, 'f'), (3, 2, 'e'), (2, 4, 'c')] Path: 5 --> [(1, 1, 'a'), (1, 3, 'g'), (3, 3, 'f'), (3, 2, 'e'), (2, 4, 'd')] Path: 6 --> [(1, 2, 'b'), (2, 4, 'c')] Path: 7 --> [(1, 2, 'b'), (2, 4, 'd')] Path: 8 --> [(1, 3, 'g'), (3, 2, 'e'), (2, 4, 'c')] Path: 9 --> [(1, 3, 'g'), (3, 2, 'e'), (2, 4, 'd')] Path: 10 --> [(1, 3, 'g'), (3, 3, 'f'), (3, 2, 'e'), (2, 4, 'c')] Path: 11 --> [(1, 3, 'g'), (3, 3, 'f'), (3, 2, 'e'), (2, 4, 'd')] 

Try to build new graph with where current edges are transformed into nodes and there is an edge beetween two nodes if we can use their corresponding edges consequently in a path. 如果可以在路径中使用它们对应的边,则尝试构建新图,其中将当前边转换为节点,并且两个节点之间存在边。 After it you can solve your problem with DFS. 之后,您可以使用DFS解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM