简体   繁体   中英

Find all paths between to vertices in a graph

I have been using the solution posted here https://www.geeksforgeeks.org/find-paths-given-source-destination/ in python and it works fairly well for several input graphs but for this particular input I am not able to generate paths. Is there a problem in the code or is there a problem in my input graph representation? please help.

# 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) 

The problem is with class Graph() initialization. This class uses a 0-based indexing but you give a 1-based indexing. Changing g = Graph(5) to g = Graph(6) which expect to have nodes in [0,1,2,3,4,5] solves the issue.


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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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