【问题标题】:Find all paths between to vertices in a graph查找图中顶点之间的所有路径
【发布时间】:2020-01-07 20:53:37
【问题描述】:

我一直在 python 中使用https://www.geeksforgeeks.org/find-paths-given-source-destination/ 此处发布的解决方案,它适用于多个输入图,但对于这个特定的输入,我无法生成路径。代码有问题还是我的输入图形表示有问题?请帮忙。

# Python program to print all paths from a source to destination. 

from collections import defaultdict 

#This class represents a directed graph 
# using adjacency list representation 
class Graph: 

    def __init__(self,vertices): 
        #No. of vertices 
        self.V= vertices 

        # default dictionary to store graph 
        self.graph = defaultdict(list) 

    # function to add an edge to graph 
    def addEdge(self,u,v): 
        self.graph[u].append(v) 

    '''A recursive function to print all paths from 'u' to 'd'. 
    visited[] keeps track of vertices in current path. 
    path[] stores actual vertices and path_index is current 
    index in path[]'''
    def printAllPathsUtil(self, u, d, visited, path): 

        # Mark the current node as visited and store in path 
        visited[u]= True
        path.append(u) 

        # If current vertex is same as destination, then print 
        # current path[] 
        if u ==d: 
            print path 
        else: 
            # If current vertex is not destination 
            #Recur for all the vertices adjacent to this vertex 
            for i in self.graph[u]: 
                if visited[i]==False: 
                    self.printAllPathsUtil(i, d, visited, path) 

        # Remove current vertex from path[] and mark it as unvisited 
        path.pop() 
        visited[u]= False


    # Prints all paths from 's' to 'd' 
    def printAllPaths(self,s, d): 

        # Mark all the vertices as not visited 
        visited =[False]*(self.V) 

        # Create an array to store paths 
        path = [] 

        # Call the recursive helper function to print all paths 
        self.printAllPathsUtil(s, d,visited, path) 



# Create a graph given in the above diagram 
# g = Graph(4) 
# g.addEdge(0, 1) 
# g.addEdge(0, 2) 
# g.addEdge(0, 3) 
# g.addEdge(2, 0) 
# g.addEdge(2, 1) 
# g.addEdge(1, 3) 

g= Graph(5)
g.addEdge(1,2)
g.addEdge(1,4)
g.addEdge(1,5)
g.addEdge(2,1)
g.addEdge(2,3)
g.addEdge(3,2)
g.addEdge(3,4)
g.addEdge(4,1)
g.addEdge(4,3)
g.addEdge(4,5)
g.addEdge(5,1)
g.addEdge(5,4)



s = 1 ; d = 4
print ("Following are all different paths from %d to %d :" %(s, d)) 
g.printAllPaths(s, d) 

【问题讨论】:

  • 为什么你认为它不起作用?您的预期结果是什么?您从这段代码中得到了什么?
  • 为什么它不起作用?我无法追踪为什么它会抛出错误,这就是我在这里发布寻求帮助的原因。预期结果:在我的情况下 s=1 和 d=4 是 s 和 d 之间的不同路径。因此,对于给定的未注释输入,答案本身应该是 1,2,3,4 和 1,5,4 和 1,4。代码找到 1,4 和 1,2,5,4 但不是 1,5,4 并抛出错误。
  • @sudiksha 如果它抛出错误,那么你必须这么说,并分享错误消息!
  • 另外我建议远离那个网站。我一直觉得它很差,而且那个页面上的 Python 解决方案肯定是。

标签: python graph path graph-theory breadth-first-search


【解决方案1】:

问题在于class Graph() 初始化。此类使用基于 0 的索引,但您提供基于 1 的索引。将 g = Graph(5) 更改为 g = Graph(6) 期望在 [0,1,2,3,4,5] 中有节点可以解决问题。


from collections import defaultdict 

#This class represents a directed graph 
# using adjacency list representation 
class Graph: 

    def __init__(self,vertices): 
        #No. of vertices 
        self.V= vertices 

        # default dictionary to store graph 
        self.graph = defaultdict(list) 

    # function to add an edge to graph 
    def addEdge(self,u,v): 
        self.graph[u].append(v) 

    '''A recursive function to print all paths from 'u' to 'd'. 
    visited[] keeps track of vertices in current path. 
    path[] stores actual vertices and path_index is current 
    index in path[]'''
    def printAllPathsUtil(self, u, d, visited, path): 

        # Mark the current node as visited and store in path 
        visited[u]= True
        path.append(u) 

        # If current vertex is same as destination, then print 
        # current path[] 
        if u ==d: 
            print(path)
        else: 
            # If current vertex is not destination 
            #Recur for all the vertices adjacent to this vertex 
            for i in self.graph[u]: 
                if visited[i]==False: 
                    self.printAllPathsUtil(i, d, visited, path) 

        # Remove current vertex from path[] and mark it as unvisited 
        path.pop() 
        visited[u]= False


    # Prints all paths from 's' to 'd' 
    def printAllPaths(self,s, d): 

        # Mark all the vertices as not visited 
        visited =[False]*(self.V) 

        # Create an array to store paths 
        path = [] 

        # Call the recursive helper function to print all paths 
        self.printAllPathsUtil(s, d,visited, path) 



g= Graph(6)
g.addEdge(1,2)
g.addEdge(1,4)
g.addEdge(1,5)
g.addEdge(2,1)
g.addEdge(2,3)
g.addEdge(3,2)
g.addEdge(3,4)
g.addEdge(4,1)
g.addEdge(4,3)
g.addEdge(4,5)
g.addEdge(5,1)
g.addEdge(5,4)



s = 1 ; d = 4
print ("Following are all different paths from %d to %d :" %(s, d)) 
g.printAllPaths(s, d) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    • 1970-01-01
    相关资源
    最近更新 更多