如何使用python打印多原点和多目标图形的所有路径

如何使用python打印多原点和多目标图形的所有路径,python,graph,Python,Graph,我搜索了很多链接,但仍然找不到任何示例代码来打印多原点和多目标图形的所有路径。 我尝试使用BFS方法,但不知道如何为新路径设置中断函数,有谁能给我一个简单的示例,说明如何创建多原点多目标图 或者您能告诉我如何更改下面的函数,使“last”变量不是单个目标点吗? 下面是代码,感谢您的帮助: def findpaths(g: List[List[int]], src: List[int], dst: List[int], v: int) -> None: #

我搜索了很多链接,但仍然找不到任何示例代码来打印多原点和多目标图形的所有路径。
我尝试使用BFS方法,但不知道如何为新路径设置中断函数,有谁能给我一个简单的示例,说明如何创建多原点多目标图
或者您能告诉我如何更改下面的函数,使“last”变量不是单个目标点吗?
下面是代码,感谢您的帮助:


    def findpaths(g: List[List[int]], src: List[int], dst: List[int], v: int) -> None:
      
      # Create a queue which stores the paths
      q = deque()
      
      # Path vector to store the current path
      path = []
      path.append(src)
      q.append(path.copy())
      
      while q:
        path = q.popleft()
        last = path[len(path) - 1] ###?how to make changes?
        
        # If last vertex is the desired destination then print the path
        if (last == dst):  ###I have multiple destination, but how to set the variables?
          printpath(path)
    
        # Traverse to all the nodes connected to current vertex and push new path to queue
        for i in range(len(g[last])):
          if (isNotVisited(g[last][i], path)):
            newpath = path.copy()
            newpath.append(g[last][i])
            q.append(newpath)


这是一个递归生成器,通过对图形
g
中的所有
src
dst
对执行深度优先搜索(DFS),生成所有可能的路径

from itertools import product

def find_path(g, src, dst):
    graph = {}
    res = {}
    for from_, to_ in g:
        graph.setdefault(from_, set()).add(to_)
        graph.setdefault(to_, set()).add(from_) # comment this line for directed graph

    def dfs(src, dst, parents, seen):
        if src in seen:
            return
        if src == dst:
            yield parents + [src]
        seen.add(src)
        for i in graph.get(src, []):
            yield from dfs(i, dst, parents + [src], seen)
        seen.remove(src)

    for source, destination in product(src, dst):
        res[f'{source} to {destination}'] = list(dfs(source, destination, [], set()))

    return res


g = [(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4)]
src = [1, 2, 3, 4]
dst = [1, 2, 3, 4]

print(find_path(g, src, dst))
输出

{'1 to 1': [[1]],
 '1 to 2': [[1, 2], [1, 3, 2], [1, 3, 4, 2], [1, 4, 2], [1, 4, 3, 2]],
 '1 to 3': [[1, 2, 3], [1, 2, 4, 3], [1, 3], [1, 4, 2, 3], [1, 4, 3]],
 '1 to 4': [[1, 2, 3, 4], [1, 2, 4], [1, 3, 2, 4], [1, 3, 4], [1, 4]],
 '2 to 1': [[2, 1], [2, 3, 1], [2, 3, 4, 1], [2, 4, 1], [2, 4, 3, 1]],
 '2 to 2': [[2]],
 '2 to 3': [[2, 1, 3], [2, 1, 4, 3], [2, 3], [2, 4, 1, 3], [2, 4, 3]],
 '2 to 4': [[2, 1, 3, 4], [2, 1, 4], [2, 3, 1, 4], [2, 3, 4], [2, 4]],
 '3 to 1': [[3, 1], [3, 2, 1], [3, 2, 4, 1], [3, 4, 1], [3, 4, 2, 1]],
 '3 to 2': [[3, 1, 2], [3, 1, 4, 2], [3, 2], [3, 4, 1, 2], [3, 4, 2]],
 '3 to 3': [[3]],
 '3 to 4': [[3, 1, 2, 4], [3, 1, 4], [3, 2, 1, 4], [3, 2, 4], [3, 4]],
 '4 to 1': [[4, 1], [4, 2, 1], [4, 2, 3, 1], [4, 3, 1], [4, 3, 2, 1]],
 '4 to 2': [[4, 1, 2], [4, 1, 3, 2], [4, 2], [4, 3, 1, 2], [4, 3, 2]],
 '4 to 3': [[4, 1, 2, 3], [4, 1, 3], [4, 2, 1, 3], [4, 2, 3], [4, 3]],
 '4 to 4': [[4]]}

能否显示
g
src
dst
v
的示例输入?还有预期的输出I,很抱歉回复太晚了,我以为没有人回答我,结果如下:\'g[[371123],[371000123],[113130],[123450],[17110],[123610],[123120]…]\src[371]\dst[210310350500610341350320440]。\'v'是'g','src','dst'中所有的唯一数字,正如您可能注意到的,在g和dst中有很多重复项,我还需要删除g和dst中的重复项,以获得列表中的唯一值,我是否应该使用“set()”?那么您显示的值就是输入?还可以显示输出吗?和预期输出:->1。从src 371到dst 210的路径为371 123 210 371 123 141 210 371 123 161 172 210 371 211 189 120 210 2。从src 371到dst 310的路径为。。。。。上面的输出只是为了显示格式,因为我还没有打印所有路径,所以不确定有多少路径是从371到210的它们超出了网页字符限制,因为它们来自我的数据集,具有巨大的值