Shortest Path Algorithm Review and the k-shortest path Algorithm
Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u...
Transcript of Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u...
![Page 1: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/1.jpg)
1
Shortest Paths 1
![Page 2: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/2.jpg)
Introduction to Algorithms November 1, 2004 L14.2© 2001–4 by Charles E. Leiserson
Paths in graphsConsider a digraph G = (V, E) with edge-weight function w : E → R. The weight of path p = v1 →v2 →L→ vk is defined to be
∑−
=+=
1
11),()(
k
iii vvwpw .
![Page 3: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/3.jpg)
Introduction to Algorithms November 1, 2004 L14.3© 2001–4 by Charles E. Leiserson
Paths in graphsConsider a digraph G = (V, E) with edge-weight function w : E → R. The weight of path p = v1 →v2 →L→ vk is defined to be
∑−
=+=
1
11),()(
k
iii vvwpw .
v1v1
v2v2
v3v3
v4v4
v5v54 –2 –5 1
Example:
w(p) = –2
![Page 4: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/4.jpg)
Introduction to Algorithms November 1, 2004 L14.4© 2001–4 by Charles E. Leiserson
Shortest paths
A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined asδ(u, v) = min{w(p) : p is a path from u to v}.
Note: δ(u, v) = ∞ if no path from u to v exists.
![Page 5: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/5.jpg)
Introduction to Algorithms November 1, 2004 L14.5© 2001–4 by Charles E. Leiserson
Optimal substructure
Theorem. A subpath of a shortest path is a shortest path.
![Page 6: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/6.jpg)
Introduction to Algorithms November 1, 2004 L14.6© 2001–4 by Charles E. Leiserson
Optimal substructure
Theorem. A subpath of a shortest path is a shortest path.
Proof. Cut and paste:
![Page 7: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/7.jpg)
Introduction to Algorithms November 1, 2004 L14.7© 2001–4 by Charles E. Leiserson
Optimal substructure
Theorem. A subpath of a shortest path is a shortest path.
Proof. Cut and paste:
![Page 8: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/8.jpg)
Introduction to Algorithms November 1, 2004 L14.8© 2001–4 by Charles E. Leiserson
Triangle inequality
Theorem. For all u, v, x ∈ V, we haveδ(u, v) ≤ δ(u, x) + δ(x, v).
![Page 9: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/9.jpg)
Introduction to Algorithms November 1, 2004 L14.9© 2001–4 by Charles E. Leiserson
Triangle inequality
Theorem. For all u, v, x ∈ V, we haveδ(u, v) ≤ δ(u, x) + δ(x, v).
uu
Proof.
xx
vvδ(u, v)
δ(u, x) δ(x, v)
![Page 10: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/10.jpg)
Introduction to Algorithms November 1, 2004 L14.10© 2001–4 by Charles E. Leiserson
Well-definedness of shortest paths
If a graph G contains a negative-weight cycle, then some shortest paths may not exist.
![Page 11: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/11.jpg)
Introduction to Algorithms November 1, 2004 L14.11© 2001–4 by Charles E. Leiserson
Well-definedness of shortest paths
If a graph G contains a negative-weight cycle, then some shortest paths may not exist.
Example:
uu vv
…
< 0
![Page 12: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/12.jpg)
Introduction to Algorithms November 1, 2004 L14.12© 2001–4 by Charles E. Leiserson
Single-source shortest pathsProblem. From a given source vertex s ∈ V, find the shortest-path weights δ(s, v) for all v ∈ V.If all edge weights w(u, v) are nonnegative, all shortest-path weights must exist. IDEA: Greedy.1. Maintain a set S of vertices whose shortest-
path distances from s are known.2. At each step add to S the vertex v ∈ V – S
whose distance estimate from s is minimal.3. Update the distance estimates of vertices
adjacent to v.
![Page 13: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/13.jpg)
Introduction to Algorithms November 1, 2004 L14.13© 2001–4 by Charles E. Leiserson
Dijkstra’s algorithmd[s] ← 0for each v ∈ V – {s}
do d[v] ←∞S ←∅Q ← V ⊳ Q is a priority queue maintaining V – S
![Page 14: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/14.jpg)
Introduction to Algorithms November 1, 2004 L14.14© 2001–4 by Charles E. Leiserson
Dijkstra’s algorithmd[s] ← 0for each v ∈ V – {s}
do d[v] ←∞S ←∅Q ← V ⊳ Q is a priority queue maintaining V – Swhile Q ≠ ∅
do u ← EXTRACT-MIN(Q)S ← S ∪ {u}for each v ∈ Adj[u]
do if d[v] > d[u] + w(u, v)then d[v] ← d[u] + w(u, v)
![Page 15: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/15.jpg)
Introduction to Algorithms November 1, 2004 L14.15© 2001–4 by Charles E. Leiserson
Dijkstra’s algorithmd[s] ← 0for each v ∈ V – {s}
do d[v] ←∞S ←∅Q ← V ⊳ Q is a priority queue maintaining V – Swhile Q ≠ ∅
do u ← EXTRACT-MIN(Q)S ← S ∪ {u}for each v ∈ Adj[u]
do if d[v] > d[u] + w(u, v)then d[v] ← d[u] + w(u, v)
relaxation step
Implicit DECREASE-KEY
![Page 16: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/16.jpg)
Introduction to Algorithms November 1, 2004 L14.16© 2001–4 by Charles E. Leiserson
Example of Dijkstra’s algorithm
AA
BB DD
CC EE
10
3
1 4 7 98
2
2
Graph with nonnegative edge weights:
![Page 17: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/17.jpg)
Introduction to Algorithms November 1, 2004 L14.17© 2001–4 by Charles E. Leiserson
Example of Dijkstra’s algorithm
AA
BB DD
CC EE
10
3
1 4 7 98
2
2
Initialize:
A B C D EQ:0 ∞ ∞ ∞ ∞
S: {}
0
∞
∞ ∞
∞
![Page 18: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/18.jpg)
Introduction to Algorithms November 1, 2004 L14.18© 2001–4 by Charles E. Leiserson
Example of Dijkstra’s algorithm
AA
BB DD
CC EE
10
3
1 4 7 98
2
2A B C D EQ:0 ∞ ∞ ∞ ∞
S: { A }
0
∞
∞ ∞
∞“A” ← EXTRACT-MIN(Q):
![Page 19: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/19.jpg)
Introduction to Algorithms November 1, 2004 L14.19© 2001–4 by Charles E. Leiserson
Example of Dijkstra’s algorithm
AA
BB DD
CC EE
10
3
1 4 7 98
2
2A B C D EQ:0 ∞ ∞ ∞ ∞
S: { A }
0
10
3 ∞
∞
10 3
Relax all edges leaving A:
∞ ∞
![Page 20: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/20.jpg)
Introduction to Algorithms November 1, 2004 L14.20© 2001–4 by Charles E. Leiserson
Example of Dijkstra’s algorithm
AA
BB DD
CC EE
10
3
1 4 7 98
2
2A B C D EQ:0 ∞ ∞ ∞ ∞
S: { A, C }
0
10
3 ∞
∞
10 3
“C” ← EXTRACT-MIN(Q):
∞ ∞
![Page 21: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/21.jpg)
Introduction to Algorithms November 1, 2004 L14.21© 2001–4 by Charles E. Leiserson
Example of Dijkstra’s algorithm
AA
BB DD
CC EE
10
3
1 4 7 98
2
2A B C D EQ:0 ∞ ∞ ∞ ∞
S: { A, C }
0
7
3 5
11
10 37 11 5
Relax all edges leaving C:
∞ ∞
![Page 22: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/22.jpg)
Introduction to Algorithms November 1, 2004 L14.22© 2001–4 by Charles E. Leiserson
Example of Dijkstra’s algorithm
AA
BB DD
CC EE
10
3
1 4 7 98
2
2A B C D EQ:0 ∞ ∞ ∞ ∞
S: { A, C, E }
0
7
3 5
11
10 37 11 5
“E” ← EXTRACT-MIN(Q):
∞ ∞
![Page 23: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/23.jpg)
Introduction to Algorithms November 1, 2004 L14.23© 2001–4 by Charles E. Leiserson
Example of Dijkstra’s algorithm
AA
BB DD
CC EE
10
3
1 4 7 98
2
2A B C D EQ:0 ∞ ∞ ∞ ∞
S: { A, C, E }
0
7
3 5
11
10 3 ∞ ∞7 11 57 11
Relax all edges leaving E:
![Page 24: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/24.jpg)
Introduction to Algorithms November 1, 2004 L14.24© 2001–4 by Charles E. Leiserson
Example of Dijkstra’s algorithm
AA
BB DD
CC EE
10
3
1 4 7 98
2
2A B C D EQ:0 ∞ ∞ ∞ ∞
S: { A, C, E, B }
0
7
3 5
11
10 3 ∞ ∞7 11 57 11
“B” ← EXTRACT-MIN(Q):
![Page 25: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/25.jpg)
Introduction to Algorithms November 1, 2004 L14.25© 2001–4 by Charles E. Leiserson
Example of Dijkstra’s algorithm
AA
BB DD
CC EE
10
3
1 4 7 98
2
2A B C D EQ:0 ∞ ∞ ∞ ∞
S: { A, C, E, B }
0
7
3 5
9
10 3 ∞ ∞7 11 57 11
Relax all edges leaving B:
9
![Page 26: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/26.jpg)
Introduction to Algorithms November 1, 2004 L14.26© 2001–4 by Charles E. Leiserson
Example of Dijkstra’s algorithm
AA
BB DD
CC EE
10
3
1 4 7 98
2
2A B C D EQ:0 ∞ ∞ ∞ ∞
S: { A, C, E, B, D }
0
7
3 5
9
10 3 ∞ ∞7 11 57 11
9
“D” ← EXTRACT-MIN(Q):
![Page 27: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/27.jpg)
3
Correctness
![Page 28: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/28.jpg)
4
Correctness
![Page 29: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/29.jpg)
5
Correctness
![Page 30: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/30.jpg)
Introduction to Algorithms November 1, 2004 L14.34© 2001–4 by Charles E. Leiserson
Analysis of Dijkstrawhile Q ≠ ∅
do u ← EXTRACT-MIN(Q)S ← S ∪ {u}for each v ∈ Adj[u]
do if d[v] > d[u] + w(u, v)then d[v] ← d[u] + w(u, v)
![Page 31: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/31.jpg)
Introduction to Algorithms November 1, 2004 L14.35© 2001–4 by Charles E. Leiserson
Analysis of Dijkstra
|V |times
while Q ≠ ∅do u ← EXTRACT-MIN(Q)
S ← S ∪ {u}for each v ∈ Adj[u]
do if d[v] > d[u] + w(u, v)then d[v] ← d[u] + w(u, v)
![Page 32: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/32.jpg)
Introduction to Algorithms November 1, 2004 L14.36© 2001–4 by Charles E. Leiserson
Analysis of Dijkstra
degree(u)times
|V |times
while Q ≠ ∅do u ← EXTRACT-MIN(Q)
S ← S ∪ {u}for each v ∈ Adj[u]
do if d[v] > d[u] + w(u, v)then d[v] ← d[u] + w(u, v)
![Page 33: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/33.jpg)
Introduction to Algorithms November 1, 2004 L14.37© 2001–4 by Charles E. Leiserson
Analysis of Dijkstra
degree(u)times
|V |times
Handshaking Lemma ⇒Θ(E) implicit DECREASE-KEY’s.
while Q ≠ ∅do u ← EXTRACT-MIN(Q)
S ← S ∪ {u}for each v ∈ Adj[u]
do if d[v] > d[u] + w(u, v)then d[v] ← d[u] + w(u, v)
![Page 34: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/34.jpg)
Introduction to Algorithms November 1, 2004 L14.38© 2001–4 by Charles E. Leiserson
Analysis of Dijkstra
degree(u)times
|V |times
Handshaking Lemma ⇒Θ(E) implicit DECREASE-KEY’s.Time = Θ(V·TEXTRACT-MIN + E·TDECREASE-KEY)
Note: Same formula as in the analysis of Prim’s minimum spanning tree algorithm.
while Q ≠ ∅do u ← EXTRACT-MIN(Q)
S ← S ∪ {u}for each v ∈ Adj[u]
do if d[v] > d[u] + w(u, v)then d[v] ← d[u] + w(u, v)
![Page 35: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/35.jpg)
Introduction to Algorithms November 1, 2004 L14.39© 2001–4 by Charles E. Leiserson
Analysis of Dijkstra (continued)
Time = Θ(V)·TEXTRACT-MIN + Θ(E)·TDECREASE-KEY
Q TEXTRACT-MIN TDECREASE-KEY Total
![Page 36: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/36.jpg)
Introduction to Algorithms November 1, 2004 L14.40© 2001–4 by Charles E. Leiserson
Analysis of Dijkstra (continued)
Time = Θ(V)·TEXTRACT-MIN + Θ(E)·TDECREASE-KEY
Q TEXTRACT-MIN TDECREASE-KEY Total
array O(V) O(1) O(V2)
![Page 37: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/37.jpg)
Introduction to Algorithms November 1, 2004 L14.41© 2001–4 by Charles E. Leiserson
Analysis of Dijkstra (continued)
Time = Θ(V)·TEXTRACT-MIN + Θ(E)·TDECREASE-KEY
Q TEXTRACT-MIN TDECREASE-KEY Total
array O(V) O(1) O(V2)binary heap O(lg V) O(lg V) O(E lg V)
![Page 38: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/38.jpg)
Introduction to Algorithms November 1, 2004 L14.43© 2001–4 by Charles E. Leiserson
Unweighted graphsSuppose that w(u, v) = 1 for all (u, v) ∈ E. Can Dijkstra’s algorithm be improved?
![Page 39: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/39.jpg)
Introduction to Algorithms November 1, 2004 L14.44© 2001–4 by Charles E. Leiserson
Unweighted graphs
• Use a simple FIFO queue instead of a priority queue.
Suppose that w(u, v) = 1 for all (u, v) ∈ E. Can Dijkstra’s algorithm be improved?
![Page 40: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/40.jpg)
Introduction to Algorithms November 1, 2004 L14.45© 2001–4 by Charles E. Leiserson
Unweighted graphs
while Q ≠ ∅do u ← DEQUEUE(Q)
for each v ∈ Adj[u]do if d[v] = ∞
then d[v] ← d[u] + 1ENQUEUE(Q, v)
• Use a simple FIFO queue instead of a priority queue.
Breadth-first search
Suppose that w(u, v) = 1 for all (u, v) ∈ E. Can Dijkstra’s algorithm be improved?
![Page 41: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/41.jpg)
Introduction to Algorithms November 1, 2004 L14.46© 2001–4 by Charles E. Leiserson
Unweighted graphs
while Q ≠ ∅do u ← DEQUEUE(Q)
for each v ∈ Adj[u]do if d[v] = ∞
then d[v] ← d[u] + 1ENQUEUE(Q, v)
• Use a simple FIFO queue instead of a priority queue.
Analysis: Time = O(V + E).
Breadth-first search
Suppose that w(u, v) = 1 for all (u, v) ∈ E. Can Dijkstra’s algorithm be improved?
![Page 42: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/42.jpg)
Introduction to Algorithms November 1, 2004 L14.47© 2001–4 by Charles E. Leiserson
Example of breadth-first search
aa
bb
cc
dd
eegg
ii
ff hh
Q:
![Page 43: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/43.jpg)
Introduction to Algorithms November 1, 2004 L14.48© 2001–4 by Charles E. Leiserson
Example of breadth-first search
aa
bb
cc
dd
eegg
ii
ff hh
Q: a
0
0
![Page 44: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/44.jpg)
Introduction to Algorithms November 1, 2004 L14.49© 2001–4 by Charles E. Leiserson
Example of breadth-first search
aa
bb
cc
dd
eegg
ii
ff hh
Q: a b d
0
1
1
1 1
![Page 45: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/45.jpg)
Introduction to Algorithms November 1, 2004 L14.50© 2001–4 by Charles E. Leiserson
Example of breadth-first search
aa
bb
cc
dd
eegg
ii
ff hh
Q: a b d c e
0
1
1
2 2
1 2 2
![Page 46: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/46.jpg)
Introduction to Algorithms November 1, 2004 L14.51© 2001–4 by Charles E. Leiserson
Example of breadth-first search
aa
bb
cc
dd
eegg
ii
ff hh
Q: a b d c e
0
1
1
2 2
2 2
![Page 47: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/47.jpg)
Introduction to Algorithms November 1, 2004 L14.52© 2001–4 by Charles E. Leiserson
Example of breadth-first search
aa
bb
cc
dd
eegg
ii
ff hh
Q: a b d c e
0
1
1
2 2
2
![Page 48: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/48.jpg)
Introduction to Algorithms November 1, 2004 L14.53© 2001–4 by Charles E. Leiserson
Example of breadth-first search
aa
bb
cc
dd
eegg
ii
ff hh
Q: a b d c e g i
0
1
1
2 2
3
3
3 3
![Page 49: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/49.jpg)
Introduction to Algorithms November 1, 2004 L14.54© 2001–4 by Charles E. Leiserson
Example of breadth-first search
aa
bb
cc
dd
eegg
ii
ff hh
Q: a b d c e g i f
0
1
1
2 2
3
3
4
3 4
![Page 50: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/50.jpg)
Introduction to Algorithms November 1, 2004 L14.55© 2001–4 by Charles E. Leiserson
Example of breadth-first search
aa
bb
cc
dd
eegg
ii
ff hh
Q: a b d c e g i f h
0
1
1
2 2
3
3
4 4
4 4
![Page 51: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/51.jpg)
Introduction to Algorithms November 1, 2004 L14.56© 2001–4 by Charles E. Leiserson
Example of breadth-first search
aa
bb
cc
dd
eegg
ii
ff hh
Q: a b d c e g i f h
0
1
1
2 2
3
3
4 4
4
![Page 52: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/52.jpg)
Introduction to Algorithms November 1, 2004 L14.57© 2001–4 by Charles E. Leiserson
Example of breadth-first search
aa
bb
cc
dd
eegg
ii
ff hh
Q: a b d c e g i f h
0
1
1
2 2
3
3
4 4
![Page 53: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/53.jpg)
Introduction to Algorithms November 1, 2004 L14.58© 2001–4 by Charles E. Leiserson
Example of breadth-first search
aa
bb
cc
dd
eegg
ii
ff hh
Q: a b d c e g i f h
0
1
1
2 2
3
3
4 4
![Page 54: Shortest Paths 1 · Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as δ(u, v) = min{w(p) : p](https://reader036.fdocuments.us/reader036/viewer/2022071217/604b12f4cbc8c2505d747ba9/html5/thumbnails/54.jpg)
Introduction to Algorithms November 1, 2004 L14.59© 2001–4 by Charles E. Leiserson
Correctness of BFS
Key idea:The FIFO Q in breadth-first search mimics the priority queue Q in Dijkstra.• Invariant: v comes after u in Q implies that
d[v] = d[u] or d[v] = d[u] + 1.
while Q ≠ ∅do u ← DEQUEUE(Q)
for each v ∈ Adj[u]do if d[v] = ∞
then d[v] ← d[u] + 1ENQUEUE(Q, v)