Shortest Paths in Unweighted Digraphs
Explore how breadth-first search (BFS) can be used to find shortest paths in unweighted directed graphs by building a breadth-first tree rooted at the source vertex. Understand the relationship between path length and tree depth, and see how to recover exact shortest paths using parent pointers. This lesson helps you grasp a fundamental graph traversal approach used in many applications.
We'll cover the following...
There are some applications where BFS can be used as a substitute for DFS (for example, in detecting bipartite graphs or the Ford-Fulkerson algorithm covered in a later chapter). However, there are other applications that work exclusively with only one of the two search algorithms. For example, BFS cannot be used as a substitute for Tarjan’s algorithm. On the other hand, unlike DFS, BFS can be used directly for finding the shortest paths in unweighted graphs and digraphs.
In this lesson, we’d like to show that a breadth-first tree of an unweighted digraph represents a tree of all shortest paths from the root to all other vertices. Recall that the length of a path in an unweighted digraph is just the number of edges in the path.
Note: The definitions of shortest paths and the discussion in this lesson apply equally well to unweighted graphs. But for convenience, we focus our attention on unweighted digraphs.
BFS for finding shortest paths
Given an unweighted digraph , we want to show that a shortest path in corresponds to the path in a breadth-first tree of that’s rooted at . This may be rephrased more precisely as follows:
For any shortest path from a vertex to in , the length of the path is the same as the depth of in any breadth-first tree of .
There’s a simple reason for this. Let’s pause and think about it.
Suppose a shortest path from to in ...