What you're trying to do is essentially to find a path between two vertices in a (directed?) graph check out Dijkstra's algorithm if you need shortest path or write a simple recursive function if you need whatever paths exist.
Breadth-first search traverses a graph and in fact finds all paths from a starting node. Usually, BFS doesn't keep all paths, however. Instead, it updates a prededecessor function π to save the shortest path. You can easily modify the algorithm so that π(n)
doesn't only store one predecessor but a list of possible predecessors.
Then all possible paths are encoded in this function, and by traversing π recursively you get all possible path combinations.
One good pseudocode which uses this notation can be found in Introduction to Algorithms by Cormen et al. and has subsequently been used in many University scripts on the subject. A Google search for “BFS pseudocode predecessor π
” uproots this first hit.
If you want all the paths, use recursion.
Using an adjacency list, preferably, create a function f() that attempts to fill in a current list of visited vertices. Like so:
void allPaths(vector<int> previous, int current, int destination)
{
previous.push_back(current);
if (current == destination)
//output all elements of previous, and return
for (int i = 0; i < neighbors[current].size(); i++)
allPaths(previous, neighbors[current][i], destination);
}
int main()
{
//...input
allPaths(vector<int>(), start, end);
}
Due to the fact that the vector is passed by value (and thus any changes made further down in the recursive procedure aren't permanent), all possible combinations are enumerated.
You can gain a bit of efficiency by passing the previous vector by reference (and thus not needing to copy the vector over and over again) but you'll have to make sure that things get popped_back() manually.
One more thing: if the graph has cycles, this won't work. (I assume in this case you'll want to find all simple paths, then) Before adding something into the previous vector, first check if it's already in there.
If you want all shortest paths, use Konrad's suggestion with this algorithm.
Dijkstra's algorithm applies more to weighted paths and it sounds like the poster was wanting to find all paths, not just the shortest.
For this application, I'd build a graph (your application sounds like it wouldn't need to be directed) and use your favorite search method. It sounds like you want all paths, not just a guess at the shortest one, so use a simple recursive algorithm of your choice.
The only problem with this is if the graph can be cyclic.
With the connections:
- 1, 2
- 1, 3
- 2, 3
- 2, 4
While looking for a path from 1->4, you could have a cycle of 1 -> 2 -> 3 -> 1.
In that case, then I'd keep a stack as traversing the nodes. Here's a list with the steps for that graph and the resulting stack (sorry for the formatting - no table option):
current node (possible next nodes minus where we came from) [stack]
- 1 (2, 3) [1]
- 2 (3, 4) [1, 2]
- 3 (1) [1, 2, 3]
- 1 (2, 3) [1, 2, 3, 1] //error - duplicate number on the stack - cycle detected
- 3 () [1, 2, 3] // back-stepped to node three and popped 1 off the stack. No more nodes to explore from here
- 2 (4) [1, 2] // back-stepped to node 2 and popped 1 off the stack.
- 4 () [1, 2, 4] // Target node found - record stack for a path. No more nodes to explore from here
- 2 () [1, 2] //back-stepped to node 2 and popped 4 off the stack. No more nodes to explore from here
- 1 (3) [1] //back-stepped to node 1 and popped 2 off the stack.
- 3 (2) [1, 3]
- 2 (1, 4) [1, 3, 2]
- 1 (2, 3) [1, 3, 2, 1] //error - duplicate number on the stack - cycle detected
- 2 (4) [1, 3, 2] //back-stepped to node 2 and popped 1 off the stack
- 4 () [1, 3, 2, 4] Target node found - record stack for a path. No more nodes to explore from here
- 2 () [1, 3, 2] //back-stepped to node 2 and popped 4 off the stack. No more nodes
- 3 () [1, 3] // back-stepped to node 3 and popped 2 off the stack. No more nodes
- 1 () [1] // back-stepped to node 1 and popped 3 off the stack. No more nodes
- Done with 2 recorded paths of [1, 2, 4] and [1, 3, 2, 4]
The original code is a bit cumbersome and you might want to use the collections.deque instead if you want to use BFS to find if a path exists between 2 points on the graph. Here is a quick solution I hacked up:
Note: this method might continue infinitely if there exists no path between the two nodes. I haven't tested all cases, YMMV.
from collections import deque
# a sample graph
graph = {'A': ['B', 'C','E'],
'B': ['A','C', 'D'],
'C': ['D'],
'D': ['C'],
'E': ['F','D'],
'F': ['C']}
def BFS(start, end):
""" Method to determine if a pair of vertices are connected using BFS
Args:
start, end: vertices for the traversal.
Returns:
[start, v1, v2, ... end]
"""
path = []
q = deque()
q.append(start)
while len(q):
tmp_vertex = q.popleft()
if tmp_vertex not in path:
path.append(tmp_vertex)
if tmp_vertex == end:
return path
for vertex in graph[tmp_vertex]:
if vertex not in path:
q.append(vertex)