Unweighted Shortest Paths
Unweighted Shortest Paths
In some shortest path problems, all edges have the same length. For
example, we may be trying to find the shortest path out of a maze. Each
cell in the maze is a node, and an edge connects two nodes if we can
move between them in a single step. In this problem, we simply want to
minimize the number of edges in a path to an exit. We therefore say that
the edges are unweighted — they contain no explicit length
information, and the length of each edge is considered to be
We could of course apply Dijkstra’s
algorithm to this
problem, using
The optimization revolves around the use of the min-priority queue. Note
that Dijkstra’s algorithm first adds all outgoing edges from the start
node u to the min-priority queue, using their lengths as their
priorities. For unweighted edges, each of these priorities will be
We claim that this behavior causes the priorities in the min-priority
queue to differ by no more than
Based on the above claim, we can now claim that whenever an edge is
added, its priority is the largest of any in the min-priority queue.
This is certainly true when we add the outgoing edges from u, as all
these edges have the same priority. Furthermore, whenever we remove an
edge with priority
As a result of this behavior, we can replace the min-priority queue with an ordinary FIFO queue, ignoring any priorities. For a graph with unweighted edges, the behavior of the algorithm will be the same. Because accessing a FIFO queue is more efficient than accessing a min-priority queue, the resulting algorithm, known as breadth-first search, is also more efficient.