Dijksatra
-
Upload
tanmay-baranwal -
Category
Education
-
view
258 -
download
0
description
Transcript of Dijksatra
![Page 1: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/1.jpg)
C#ODE STUDIO
Algorithms L17.1
Algorithms
LECTURE 17 Shortest Paths I• Properties of shortest paths• Dijkstra’s algorithm• Correctness• Analysis• Breadth-first search
![Page 2: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/2.jpg)
C#ODE STUDIO
Algorithms L17.2
Paths in graphs
Consider a digraph G = (V, E) with edge-weight function w : E R. The weight of path p = v1 v2 vk is defined to be
1
11),()(
k
iii vvwpw .
![Page 3: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/3.jpg)
C#ODE STUDIO
Algorithms L17.3
Paths in graphs
Consider a digraph G = (V, E) with edge-weight function w : E R. The weight of path p = v1 v2 vk is defined to be
1
11),()(
k
iii vvwpw .
v1v1
v2v2
v3v3
v4v4
v5v54 –2 –5 1
Example:
w(p) = –2
![Page 4: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/4.jpg)
C#ODE STUDIO
Algorithms L17.4
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: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/5.jpg)
C#ODE STUDIO
Algorithms L17.5
Optimal substructure
Theorem. A subpath of a shortest path is a shortest path.
![Page 6: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/6.jpg)
C#ODE STUDIO
Algorithms L17.6
Optimal substructure
Theorem. A subpath of a shortest path is a shortest path.
Proof. Cut and paste:
![Page 7: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/7.jpg)
C#ODE STUDIO
Algorithms L17.7
Optimal substructure
Theorem. A subpath of a shortest path is a shortest path.
Proof. Cut and paste:
![Page 8: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/8.jpg)
C#ODE STUDIO
Algorithms L17.8
Triangle inequality
Theorem. For all u, v, x V, we have(u, v) (u, x) + (x, v).
![Page 9: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/9.jpg)
C#ODE STUDIO
Algorithms L17.9
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: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/10.jpg)
C#ODE STUDIO
Algorithms L17.10
Well-definedness of shortest paths
If a graph G contains a negative-weight cycle, then some shortest paths may not exist.
![Page 11: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/11.jpg)
C#ODE STUDIO
Algorithms L17.11
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: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/12.jpg)
C#ODE STUDIO
Algorithms L17.12
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: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/13.jpg)
C#ODE STUDIO
Algorithms L17.13
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: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/14.jpg)
C#ODE STUDIO
Algorithms L17.14
Dijkstra’s algorithmd[s] 0for each v V – {s}
do d[v] S Q V ⊳ Q is a priority queue maintaining V – 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 15: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/15.jpg)
C#ODE STUDIO
Algorithms L17.15
Dijkstra’s algorithmd[s] 0for each v V – {s}
do d[v] S Q V ⊳ Q is a priority queue maintaining V – 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)
relaxation step
Implicit DECREASE-KEY
![Page 16: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/16.jpg)
C#ODE STUDIO
Algorithms L17.16
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: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/17.jpg)
C#ODE STUDIO
Algorithms L17.17
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: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/18.jpg)
C#ODE STUDIO
Algorithms L17.18
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: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/19.jpg)
C#ODE STUDIO
Algorithms L17.19
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
Relax all edges leaving A:
![Page 20: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/20.jpg)
C#ODE STUDIO
Algorithms L17.20
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
“C” EXTRACT-MIN(Q):
![Page 21: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/21.jpg)
C#ODE STUDIO
Algorithms L17.21
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 7 11 5
Relax all edges leaving C:
![Page 22: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/22.jpg)
C#ODE STUDIO
Algorithms L17.22
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 7 11 5
“E” EXTRACT-MIN(Q):
![Page 23: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/23.jpg)
C#ODE STUDIO
Algorithms L17.23
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 7 11 57 11
Relax all edges leaving E:
![Page 24: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/24.jpg)
C#ODE STUDIO
Algorithms L17.24
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 7 11 57 11
“B” EXTRACT-MIN(Q):
![Page 25: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/25.jpg)
C#ODE STUDIO
Algorithms L17.25
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 7 11 57 11
Relax all edges leaving B:
9
![Page 26: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/26.jpg)
C#ODE STUDIO
Algorithms L17.26
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 7 11 57 11
9
“D” EXTRACT-MIN(Q):
![Page 27: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/27.jpg)
C#ODE STUDIO
Algorithms L17.27
Correctness — Part ILemma. Initializing d[s] 0 and d[v] for all v V – {s} establishes d[v] (s, v) for all v V, and this invariant is maintained over any sequence of relaxation steps.
![Page 28: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/28.jpg)
C#ODE STUDIO
Algorithms L17.28
Correctness — Part ILemma. Initializing d[s] 0 and d[v] for all v V – {s} establishes d[v] (s, v) for all v V, and this invariant is maintained over any sequence of relaxation steps.Proof. Suppose not. Let v be the first vertex for which d[v] < (s, v), and let u be the vertex that caused d[v] to change: d[v] = d[u] + w(u, v). Then,
d[v] < (s, v) supposition (s, u) + (u, v) triangle inequality (s,u) + w(u, v) sh. path specific path d[u] + w(u, v) v is first violation
Contradiction.
![Page 29: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/29.jpg)
C#ODE STUDIO
Algorithms L17.29
Correctness — Part IILemma. Let u be v’s predecessor on a shortest path from s to v. Then, if d[u] = (s, u) and edge (u, v) is relaxed, we have d[v] (s, v) after the relaxation.
![Page 30: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/30.jpg)
C#ODE STUDIO
Algorithms L17.30
Correctness — Part IILemma. Let u be v’s predecessor on a shortest path from s to v. Then, if d[u] = (s, u) and edge (u, v) is relaxed, we have d[v] (s, v) after the relaxation.
Proof. Observe that (s, v) = (s, u) + w(u, v). Suppose that d[v] > (s, v) before the relaxation. (Otherwise, we’re done.) Then, the test d[v] > d[u] + w(u, v) succeeds, because d[v] > (s, v) = (s, u) + w(u, v) = d[u] + w(u, v), and the algorithm sets d[v] = d[u] + w(u, v) = (s, v).
![Page 31: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/31.jpg)
C#ODE STUDIO
Algorithms L17.31
Correctness — Part IIITheorem. Dijkstra’s algorithm terminates with d[v] = (s, v) for all v V.
![Page 32: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/32.jpg)
C#ODE STUDIO
Algorithms L17.32
Correctness — Part IIITheorem. Dijkstra’s algorithm terminates with d[v] = (s, v) for all v V.Proof. It suffices to show that d[v] = (s, v) for every v V when v is added to S. Suppose u is the first vertex added to S for which d[u] (s, u). Let y be the first vertex in V – S along a shortest path from s to u, and let x be its predecessor:
ssxx yy
uu
S, just before adding u.
![Page 33: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/33.jpg)
C#ODE STUDIO
Algorithms L17.33
Correctness — Part III (continued)
Since u is the first vertex violating the claimed invariant, we have d[x] = (s, x). When x was added to S, the edge (x, y) was relaxed, which implies that d[y] = (s, y) (s, u) d[u]. But, d[u] d[y] by our choice of u. Contradiction.
ssxx yy
uuS
![Page 34: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/34.jpg)
C#ODE STUDIO
Algorithms L17.34
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 35: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/35.jpg)
C#ODE STUDIO
Algorithms L17.35
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 36: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/36.jpg)
C#ODE STUDIO
Algorithms L17.36
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 37: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/37.jpg)
C#ODE STUDIO
Algorithms L17.37
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 38: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/38.jpg)
C#ODE STUDIO
Algorithms L17.38
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 39: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/39.jpg)
C#ODE STUDIO
Algorithms L17.39
Analysis of Dijkstra (continued)
Time = (V)·TEXTRACT-MIN + (E)·TDECREASE-KEY
Q TEXTRACT-MIN TDECREASE-KEYTotal
![Page 40: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/40.jpg)
C#ODE STUDIO
Algorithms L17.40
Analysis of Dijkstra (continued)
Time = (V)·TEXTRACT-MIN + (E)·TDECREASE-KEY
Q TEXTRACT-MIN TDECREASE-KEYTotal
array O(V) O(1) O(V2)
![Page 41: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/41.jpg)
C#ODE STUDIO
Algorithms L17.41
Analysis of Dijkstra (continued)
Time = (V)·TEXTRACT-MIN + (E)·TDECREASE-KEY
Q TEXTRACT-MIN TDECREASE-KEYTotal
array O(V) O(1) O(V2)
binary heap O(lg V) O(lg V) O(E lg V)
![Page 42: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/42.jpg)
C#ODE STUDIO
Algorithms L17.42
Analysis of Dijkstra (continued)
Time = (V)·TEXTRACT-MIN + (E)·TDECREASE-KEY
Q TEXTRACT-MIN TDECREASE-KEYTotal
array O(V) O(1) O(V2)
binary heap O(lg V) O(lg V) O(E lg V)
Fibonacci heap
O(lg V)amortized
O(1)amortized
O(E + V lg V)worst case
![Page 43: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/43.jpg)
C#ODE STUDIO
Algorithms L17.43
Unweighted graphs
Suppose that w(u, v) = 1 for all (u, v) E. Can Dijkstra’s algorithm be improved?
![Page 44: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/44.jpg)
C#ODE STUDIO
Algorithms L17.44
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 45: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/45.jpg)
C#ODE STUDIO
Algorithms L17.45
Unweighted graphs
while Q do u DEQUEUE(Q)
for each v Adj[u]do if d[v] =
then d[v] d[u] + 1 ENQUEUE(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 46: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/46.jpg)
C#ODE STUDIO
Algorithms L17.46
Unweighted graphs
while Q do u DEQUEUE(Q)
for each v Adj[u]do if d[v] =
then d[v] d[u] + 1 ENQUEUE(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 47: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/47.jpg)
C#ODE STUDIO
Algorithms L17.47
Example of breadth-first search
aa
bb
cc
dd
ee
gg
ii
ff hh
Q:
![Page 48: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/48.jpg)
C#ODE STUDIO
Algorithms L17.48
Example of breadth-first search
aa
bb
cc
dd
ee
gg
ii
ff hh
Q: a
0
0
![Page 49: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/49.jpg)
C#ODE STUDIO
Algorithms L17.49
Example of breadth-first search
aa
bb
cc
dd
ee
gg
ii
ff hh
Q: a b d
0
1
1
1 1
![Page 50: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/50.jpg)
C#ODE STUDIO
Algorithms L17.50
Example of breadth-first search
aa
bb
cc
dd
ee
gg
ii
ff hh
Q: a b d c e
0
1
1
2 2
1 2 2
![Page 51: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/51.jpg)
C#ODE STUDIO
Algorithms L17.51
Example of breadth-first search
aa
bb
cc
dd
ee
gg
ii
ff hh
Q: a b d c e
0
1
1
2 2
2 2
![Page 52: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/52.jpg)
C#ODE STUDIO
Algorithms L17.52
Example of breadth-first search
aa
bb
cc
dd
ee
gg
ii
ff hh
Q: a b d c e
0
1
1
2 2
2
![Page 53: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/53.jpg)
C#ODE STUDIO
Algorithms L17.53
Example of breadth-first search
aa
bb
cc
dd
ee
gg
ii
ff hh
Q: a b d c e g i
0
1
1
2 2
3
3
3 3
![Page 54: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/54.jpg)
C#ODE STUDIO
Algorithms L17.54
Example of breadth-first search
aa
bb
cc
dd
ee
gg
ii
ff hh
Q: a b d c e g i f
0
1
1
2 2
3
3
4
3 4
![Page 55: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/55.jpg)
C#ODE STUDIO
Algorithms L17.55
Example of breadth-first search
aa
bb
cc
dd
ee
gg
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 56: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/56.jpg)
C#ODE STUDIO
Algorithms L17.56
Example of breadth-first search
aa
bb
cc
dd
ee
gg
ii
ff hh
Q: a b d c e g i f h
0
1
1
2 2
3
3
4 4
4
![Page 57: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/57.jpg)
C#ODE STUDIO
Algorithms L17.57
Example of breadth-first search
aa
bb
cc
dd
ee
gg
ii
ff hh
Q: a b d c e g i f h
0
1
1
2 2
3
3
4 4
![Page 58: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/58.jpg)
C#ODE STUDIO
Algorithms L17.58
Example of breadth-first search
aa
bb
cc
dd
ee
gg
ii
ff hh
Q: a b d c e g i f h
0
1
1
2 2
3
3
4 4
![Page 59: Dijksatra](https://reader031.fdocuments.net/reader031/viewer/2022013011/5590d5a21a28ab60148b4790/html5/thumbnails/59.jpg)
C#ODE STUDIO
Algorithms L17.59
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] + 1 ENQUEUE(Q, v)