我正在使用这里的功能:
def find_all_paths(graph, start, end, mode = 'OUT', maxlen = None):
def find_all_paths_aux(adjlist, start, end, path, maxlen = None):
path = path + [start]
if start == end:
return [path]
paths = []
if maxlen is None or len(path) <= maxlen:
for node in adjlist[start] - set(path):
paths.extend(find_all_paths_aux(adjlist, node, end, path, maxlen))
return paths
adjlist = [set(graph.neighbors(node, mode = mode)) \
for node in xrange(graph.vcount())]
all_paths = []
start = start if type(start) is list else [start]
end = end if type(end) is list else [end]
for s in start:
for e in end:
all_paths.extend(find_all_paths_aux(adjlist, s, e, [], maxlen))
return all_paths
找到两个节点之间的所有路径。
另一种功能是这里的功能:
def find_all_paths(graph, start, end):
path = []
paths = []
queue = [(start, end, path)]
while queue:
start, end, path = queue.pop()
print 'PATH', path
path = path + [start]
if start == end:
paths.append(path)
for node in set(graph[start]).difference(path):
queue.append((node, end, path))
return paths
我想扩展其中一个函数以获取另一个参数,即“via_nodes”列表。
如果路径在其结束节点和开始节点之间具有这些 via_nodes 之一,则不应返回它。
首先使用该函数计算所有路径很容易,然后排除满足上述条件的路径,但为了提高性能,我希望它在遇到 via_node 时停止路径搜索早期阶段。
有任何想法吗?