Shortest Path Algorithms ♢ COMP2521 ♢ (23T1)
COMP2521(23T1) ♢ Shortest Path ♢ [0/10]
Path = sequence of edges in graph G
- p = (v0,v1,weight1), (v1,v2,weight2), …, (vm-1,vm,weightm)
cost(path) = sum of edge weights along path
Shortest path between vertices s and t
- a simple path p(s,t) where s = first(p), t = last(p)
- no other simple path q(s,t) has cost(q) < cost(p)
Assumptions: weighted digraph, no negative weights.
Applications: navigation, routing in data networks, …
COMP2521(23T1) ♢ Shortest Path ♢ [1/10]
Some variations on shortest path (SP) ...
Source-target SP problem
- shortest path from source vertex s to target vertex t
Single-source SP problem
- set of shortest paths from source vertex s to all other vertices
All-pairs SP problems
- set of shortest paths between all pairs of vertices s and t
COMP2521(23T1) ♢ Shortest Path ♢ [2/10]
❖ Single-source Shortest Path (SSSP) | |
Shortest paths from s to all other vertices
-
dist[]
V-indexed array of cost of shortest path from s
-
pred[]
V-indexed array of predecessor in shortest path from s
Example:
COMP2521(23T1) ♢ Shortest Path ♢ [3/10]
Assume: dist[]
and pred[]
as above
- but containing data for shortest paths discovered so far
If we have ...
-
dist[v]
is length of shortest known path from s to v
-
dist[w]
is length of shortest known path from s to w
- edge (v,w,weight)
Relaxation updates data for w if we find a shorter path from s to w :
- if
dist[v]+weight < dist[w]
then
update dist[w]←dist[v]+weight
and pred[w]←v
COMP2521(23T1) ♢ Shortest Path ♢ [4/10]
Relaxation along edge e = (v,w,3) :
COMP2521(23T1) ♢ Shortest Path ♢ [5/10]
One approach to solving single-source shortest path …
dist[]
pred[]
vSet
dijkstraSSSP(G,source):
| Input graph G, source node
|
| initialise all dist[] to ∞
| dist[source]=0
| initialise all pred[] to -1
| vSet=all vertices of G
| while vSet ≠ ∅ do
| | find v ∈ vSet with minimum dist[v]
| | for each (v,w,weight) ∈ edges(G) do
| | relax along (v,w,weight)
| | end for
| | vSet=vSet \ {v}
| end while
COMP2521(23T1) ♢ Shortest Path ♢ [6/10]
❖ Tracing Dijkstra's Algorithm | |
How Dijkstra's algorithm runs when source = 0:
COMP2521(23T1) ♢ Shortest Path ♢ [7/10]
❖ Analysis of Dijkstra's Algorithm | |
Why Dijkstra's algorithm is correct ...
Hypothesis:
(a) for visited s, dist[s] is shortest distance from source
(b) for unvisited t, dist[t] is shortest distance from source via visited nodes
Ultimately, all nodes are visited, so ...
- ∀ v, dist[v] is shortest distance from source
COMP2521(23T1) ♢ Shortest Path ♢ [8/10]
❖ ... Analysis of Dijkstra's Algorithm | |
Proof:
Base case: no visited nodes, dist[source]=0, dist[s]=∞ for all other nodes
Induction step:
- If s is unvisited node with minimum dist[s], then dist[s] is shortest distance from source to s:
- if ∃ shorter path via only visited nodes, then dist[s] would have been updated when processing the predecessor of s on this path
- if ∃ shorter path via an unvisited node u, then dist[u]<dist[s], which is impossible if s has min distance of all unvisited nodes
- This implies that (a) holds for s after processing s
- (b) still holds for all unvisited nodes t after processing s:
- if ∃ shorter path via s we would have just updated dist[t]
- if ∃ shorter path without s we would have found it previously
COMP2521(23T1) ♢ Shortest Path ♢ [9/10]
❖ ... Analysis of Dijkstra's Algorithm | |
Time complexity analysis …
Each edge needs to be considered once ⇒ O(E).
Outer loop has O(V) iterations.
Implementing "find s ∈ vSet with minimum dist[s]
"
- try all
s
∈vSet
⇒ cost = O(V) ⇒ overall cost = O(E + V2) = O(V2)
- using a PQueue to implement extracting minimum
- can improve overall cost to O(E + V·log V)
COMP2521(23T1) ♢ Shortest Path ♢ [10/10]
Produced: 26 Mar 2023