네트워크에서이 작업을 수행하는 방법입니다. 그것은 대략 here에 주어진 솔루션을 기반으로합니다. 나는 a->b
과 a<-b
이 당신이 원하는 두 개의 뚜렷한 경로라고 가정하고 있습니다. 이 목록을 목록으로 반환 할 것입니다. 각 하위 목록은 경로의 (정렬 된) 모서리입니다.
import networkx as nx
import itertools
def getPaths(G,source,target, maxLength, excludeSet=None):
#print source, target, maxLength, excludeSet
if excludeSet== None:
excludeSet = set([source])
else:
excludeSet.add(source)# won't allow a path starting at source to go through source again.
if maxLength == 0:
excludeSet.remove(source)
return []
else:
if G.has_edge(source,target):
paths=[[(source,target)]]
else:
paths = []
if G.has_edge(target,source):
paths.append([(target,source)])
#neighbors_iter is a big iterator that will give (neighbor,edge) for each successor of source and then for each predecessor of source.
neighbors_iter = itertools.chain(((neighbor,(source,neighbor)) for neighbor in G.successors_iter(source) if neighbor != target),((neighbor,(neighbor,source)) for neighbor in G.predecessors_iter(source) if neighbor != target))
#note that if a neighbor is both a predecessor and a successor, it shows up twice in this iteration.
paths.extend([[edge] + path for (neighbor,edge) in neighbors_iter if neighbor not in excludeSet for path in getPaths(G,neighbor,target,maxLength-1,excludeSet)])
excludeSet.remove(source) #when we move back up the recursion, don't want to exclude this source any more
return paths
G=nx.DiGraph()
G.add_edges_from([(1,2),(2,3),(1,3),(1,4),(3,4),(4,3)])
print getPaths(G,1,3,2)
>[[(1, 3)], [(1, 2), (2, 3)], [(1, 4), (4, 3)], [(1, 4), (3, 4)]]
난 당신이 더 효율적인 알고리즘에 도착합니다 networkx에 다 익스트라 알고리즘을 수정하여 (익스트라 알고리즘이 컷오프를 가지고 있습니다 만, 기본적으로는 최단 경로를 반환 할 것, 그리고 그 기대 그것은 가장자리 방향을 따라 갈 것입니다).
다음은 전체 경로의 대체 버전입니다. 확장명 : paths.extend ([가장자리] (인접)에 대한 경로 (인접 항목) _ 경로가 제외 항목에있는 인접 항목 인 경우 getPaths (G, 인접 항목, 대상 , maxLength-1, excludeSet)
당신이 슈퍼 컴퓨터를 사용하여 NetworkX python 네트워크 패키지로이 작업을 처리 할 수 있다고 생각하십니까? 감사합니다 – mgokhanbakal
그래프 도구 패키지는 어떻습니까? 누구든지 그것에 대한 지식이 있습니까? 위에 언급 한 것에 가장 적합한 도구입니까? 감사. – mgokhanbakal