Single Source Shortest Path
Summarized notes on Introduction to Algorithms, Chapter 24
- shortest-path problem - given an weighted, directed graph
→ goal is to solve it efficiently - weight of path is is the sum of weights of its constituent edges
- shortest-path weight
- minimum with path from to - if no path exists
- shortest path is any path
with weight
Single-source shortest path find shortest path from source vertex
Variants of SSSP:
- single-destination shortest path - (reverse of sssp)
- single-pair shortest-path - (subproblem of sssp)
- all-pairs shortest path - (N
sssp)
Graphs may contain negative weight edges
- when no negative weight cycle is reachable from
, weights remain well defined - if graph contains negative weight cycle reachable from
, shortest-path weights are not well defined - If there is a negative weight cycle
Shortest path has no cycles (negative or positive weight), i.e. it is a simple path
→ such path has at most
Representing shortest paths:
- similar to breath-first trees: maintain predecessor attribute,
- shortest-paths tree is directed subgraph
vertices reachable from rooted tree with being the root- path from
to in is shortest path from to in
Relaxation
- each vertex maintains attribute
shortest-path estimate → upper bound on the weight of shortest path from to - when relaxing an edge, test if shortest path found so far can be improved, and if yes update
and - relaxation is the only means by which shortest-path estimates and predecessors change
Pseud code implementation
These helper functions are needed to implement SSSP algorithms
1 2 3 4 5 |
|
1 2 3 4 |
|
Properties of shortest paths and relaxation
- Triangle inequality
- Upper-bound property
always, and once achieves it never changes - No-path property when no path from
to exists - Convergence property If
is a shortest path in for some , and if at any time prior to relaxing edge , then at all times afterward - Path-relaxation property consider path from
to then relax edges in order then - Predecessor subgraph property once
for all , the predecessor subgraph is a shortest-paths tree rooted at
How to solve SSSP & Complexity
Running time | Algo | Graph type |
---|---|---|
BFS | unweighted graph | |
Dijkstra | nonnegative weights | |
Bellman-Ford | general case; no -w cycles | |
Topological sort; 1 |
Acyclic |