0

I have the implemetation of BFS but it stores only one path. How can I modify this code to store all paths from a starting to node to the end. Any ideas?

    def BFS(G, user1, user2):
        path = []
        for v in G:
            v.setDistance(0)
            v.setPred(None)
        vertQueue = Queue()
        vertQueue.enqueue(G.getVertex(user1))
        while vertQueue.size() > 0:
            currentVert = vertQueue.dequeue()
            for nbr in currentVert.getConnections():
                if nbr.getColor() == 'white':
                    nbr.setColor('gray')
                    nbr.setDistance(currentVert.getDistance() + 1)
                    nbr.setPred(currentVert)
                    vertQueue.enqueue(nbr)
                currentVert.setColor('black')
        prev = G.getVertex(user2)
        while prev.getPred():
            path.append(prev.getPred().getId())
            prev = prev.getPred()
        path = path[::-1]
        path.append(user2)
        return ' -> '.join(path)
  • What is ```G``` here? Add any details that might help us understand the question better – Abhinav Mathur Nov 03 '20 at 07:16
  • G is the graph. getPred() gives the previous node connected to the current node. getVertex() is to get the node in the graph since user1 and user2 are passed as strings. If there's still anything that confuses you please let me know – PlayerUnknown007 Nov 03 '20 at 15:37

0 Answers0