Finding all the shortest paths between two nodes in unweighted undirected graph

As a caveat, remember that there can be exponentially many shortest paths between two nodes in a graph. Any algorithm for this will potentially take exponential time. That said, there are a few relatively straightforward algorithms that can find all the paths. Here’s two. BFS + Reverse DFS When running a breadth-first search over a … Read more

How to trace the path in a Breadth-First Search?

You should have look at http://en.wikipedia.org/wiki/Breadth-first_search first. Below is a quick implementation, in which I used a list of list to represent the queue of paths. # graph is in adjacent list representation graph = { ‘1’: [‘2’, ‘3’, ‘4’], ‘2’: [‘5’, ‘6’], ‘5’: [‘9′, ’10’], ‘4’: [‘7’, ‘8’], ‘7’: [’11’, ’12’] } def bfs(graph, … Read more