12 Shortest Path Flashcards
Shortest Path Problem
Needs weighted graph that can be directed or undirected.
Weights can be:
1. Uniform
2. Non-Negative, Negative, Zero, or Positive.
Single-source shortest paths problem:
Find a shortest path from a source (start) vertex ‘s’ to every other vertex ‘v’
Single-pair shortest path problem:
Find a shortest path for a source (start) vertex ‘s’ to a goal vertex ‘g’.
All pairs shortest-path problem:
Find a shortest path between all pairs of vertices.
Breadth-First Search
Discovers all vertices at distance k from s before discovering any vertices at distance k+1.
Total Running Time of BFS
O(V+E)
If the graph is unweighted (or has uniform weights), the breadth-first tree is also:
A shortest path tree
Dijkstra’s Algorithm
Solves the single-source shortest path problem in a weighted graph where all the weights are non-negative
Total Time of Dijkstra’s Algorithm If Priority Queue is Vector
O(V^2 + E) = O(V^2)
Total Time of Dijkstra’s Algorithm If Priority Queue is Binary Heap
O(V + E log V)
Connected Graph: O(E log V) since V = O(E)
Sparse graph: O(V log V) since E = O(V)
Single Pair vs Single Source
Pair is solved more efficiently (on average) but its complexity analysis is somewhat different.