0% found this document useful (0 votes)
3 views

DemoDijkstra algorithm

The document presents a detailed demonstration of Dijkstra's algorithm, focusing on its initialization, basic steps, and efficient implementation for finding the shortest path in a graph. It outlines the process of selecting unexplored nodes and updating path lengths and predecessors. The final section emphasizes the termination conditions, where the shortest path lengths and predecessors are established for each node.

Uploaded by

r95425153
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DemoDijkstra algorithm

The document presents a detailed demonstration of Dijkstra's algorithm, focusing on its initialization, basic steps, and efficient implementation for finding the shortest path in a graph. It outlines the process of selecting unexplored nodes and updating path lengths and predecessors. The final section emphasizes the termination conditions, where the shortest path lengths and predecessors are established for each node.

Uploaded by

r95425153
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

4.

G REEDY A LGORITHMS II

‣ Dijkstra’s algorithm demo


‣ Dijkstra’s algorithm demo
(efficient implementation)

Lecture slides by Kevin Wayne


Copyright © 2005 Pearson-Addison Wesley
http://www.cs.princeton.edu/~wayne/kleinberg-tardos

Last updated on 11/15/17 10:10 AM


4. G REEDY A LGORITHMS II

‣ Dijkstra’s algorithm demo


‣ Dijkstra’s algorithm demo
(efficient implementation)
Dijkstra’s algorithm demo

Initialize S ← { s } and d[s] ← 0.


Repeatedly choose unexplored node v ∉ S which minimizes

(v) = min d[u] + e


e = (u,v) : u S the length of a shortest path from s
to some node u in explored part S,
add v to S; set d[v] ← π(v) and pred[v] ← argmin. followed by a single edge e = (u, v)

d[s] 0

S s 16 2

4 8 7 5 6

3 1

3
Dijkstra’s algorithm demo

Initialize S ← { s } and d[s] ← 0.


Repeatedly choose unexplored node v ∉ S which minimizes

(v) = min d[u] + e


e = (u,v) : u S the length of a shortest path from s
to some node u in explored part S,
add v to S; set d[v] ← π(v) and pred[v] ← argmin. followed by a single edge e = (u, v)

d[s] 0 0 + 16 = 16

S s 16

4 8

0+4=4 0+8=8
4
Dijkstra’s algorithm demo

Initialize S ← { s } and d[s] ← 0.


Repeatedly choose unexplored node v ∉ S which minimizes

(v) = min d[u] + e


e = (u,v) : u S the length of a shortest path from s
to some node u in explored part S,
add v to S; set d[v] ← π(v) and pred[v] ← argmin. followed by a single edge e = (u, v)

0 0 + 16 = 16

s 16

S 8
pred[v]

v 3

4 4+3=7
0+8=8 5
Dijkstra’s algorithm demo

Initialize S ← { s } and d[s] ← 0.


Repeatedly choose unexplored node v ∉ S which minimizes

(v) = min d[u] + e


e = (u,v) : u S the length of a shortest path from s
to some node u in explored part S,
add v to S; set d[v] ← π(v) and pred[v] ← argmin. followed by a single edge e = (u, v)

0 + 16 = 16
0 7 + 7 = 14

s 16

S 7

4 7 7+1=8
6
Dijkstra’s algorithm demo

Initialize S ← { s } and d[s] ← 0.


Repeatedly choose unexplored node v ∉ S which minimizes

(v) = min d[u] + e


e = (u,v) : u S the length of a shortest path from s
to some node u in explored part S,
add v to S; set d[v] ← π(v) and pred[v] ← argmin. followed by a single edge e = (u, v)

0 + 16 = 16
7 + 7 = 14
0 8 + 5 = 13 8 + 6 = 14

s 16

S 7 5 6

4 7 8
7
Dijkstra’s algorithm demo

Initialize S ← { s } and d[s] ← 0.


Repeatedly choose unexplored node v ∉ S which minimizes

(v) = min d[u] + e


e = (u,v) : u S the length of a shortest path from s
to some node u in explored part S,
add v to S; set d[v] ← π(v) and pred[v] ← argmin. followed by a single edge e = (u, v)

13 + 2 = 15
0 13 8 + 6 = 14

s 2

S 6

4 7 8
8
Dijkstra’s algorithm demo

Initialize S ← { s } and d[s] ← 0.


Repeatedly choose unexplored node v ∉ S which minimizes

(v) = min d[u] + e


e = (u,v) : u S

add v to S; set d[v] ← π(v) and pred[v] ← argmin.

0 13 14

4 7 8
9
Dijkstra’s algorithm demo

Initialize S ← { s } and d[s] ← 0.


Repeatedly choose unexplored node v ∉ S which minimizes

(v) = min d[u] + e


e = (u,v) : u S

add v to S; set d[v] ← π(v) and pred[v] ← argmin.

0 13 14

pred[v]

π[v] 4 7 8
10
4. G REEDY A LGORITHMS II

‣ Dijkstra’s algorithm demo


‣ Dijkstra’s algorithm demo
(efficient implementation)
Dijkstra’s algorithm demo (efficient implementation)

Initialization.
For all v ≠ s : π [v] ← ∞.
For all v ≠ s : pred [v] ← null.
S ← ∅ and π [s] ← 0.

π[s] 0

s 16 2

4 8 7 5 6

3 1

12
Dijkstra’s algorithm demo (efficient implementation)

Basic step. Choose unexplored node u ∉ S with minimum π [u].


Add u to S.
For each edge e = (u, v) leaving u, if π [v] > π [u] + e then:
π [v] ← π [u] + e

pred[v] ← e

0 0 + 16 = 16

s 16

4 8

0+4=4 0+8=8
13
Dijkstra’s algorithm demo (efficient implementation)

Basic step. Choose unexplored node u ∉ S with minimum π [u].


Add u to S.
For each edge e = (u, v) leaving u, if π [v] > π [u] + e then:
π [v] ← π [u] + e

pred[v] ← e

0 0 + 16 = 16

s 16

4 8

pred[v]

0+4=4 0+8=8
14
Dijkstra’s algorithm demo (efficient implementation)

Basic step. Choose unexplored node u ∉ S with minimum π [u].


Add u to S.
For each edge e = (u, v) leaving u, if π [v] > π [u] + e then:
π [v] ← π [u] + e

pred[v] ← e

0 16

4 8
4+3=7 15
Dijkstra’s algorithm demo (efficient implementation)

Basic step. Choose unexplored node u ∉ S with minimum π [u].


Add u to S.
For each edge e = (u, v) leaving u, if π [v] > π [u] + e then:
π [v] ← π [u] + e

pred[v] ← e

0 16

4 8
4+3=7 16
Dijkstra’s algorithm demo (efficient implementation)

Basic step. Choose unexplored node u ∉ S with minimum π [u].


Add u to S.
For each edge e = (u, v) leaving u, if π [v] > π [u] + e then:
π [v] ← π [u] + e

pred[v] ← e

7 + 7 = 14
0 16

4 7 7+1=8
17
Dijkstra’s algorithm demo (efficient implementation)

Basic step. Choose unexplored node u ∉ S with minimum π [u].


Add u to S.
For each edge e = (u, v) leaving u, if π [v] > π [u] + e then:
π [v] ← π [u] + e

pred[v] ← e

7 + 7 = 14
0 16

4 7 7+1=8
18
Dijkstra’s algorithm demo (efficient implementation)

Basic step. Choose unexplored node u ∉ S with minimum π [u].


Add u to S.
For each edge e = (u, v) leaving u, if π [v] > π [u] + e then:
π [v] ← π [u] + e

pred[v] ← e

8 + 5 = 13
0 14 8 + 6 = 14

5 6

4 7 8
19
Dijkstra’s algorithm demo (efficient implementation)

Basic step. Choose unexplored node u ∉ S with minimum π [u].


Add u to S.
For each edge e = (u, v) leaving u, if π [v] > π [u] + e then:
π [v] ← π [u] + e

pred[v] ← e

8 + 5 = 13
0 14 8 + 6 = 14

5 6

4 7 8
20
Dijkstra’s algorithm demo (efficient implementation)

Basic step. Choose unexplored node u ∉ S with minimum π [u].


Add u to S.
For each edge e = (u, v) leaving u, if π [v] > π [u] + e then:
π [v] ← π [u] + e

pred[v] ← e

13 + 2 = 15
0 13 14

s 2

4 7 8
21
Dijkstra’s algorithm demo (efficient implementation)

Basic step. Choose unexplored node u ∉ S with minimum π [u].


Add u to S.
For each edge e = (u, v) leaving u, if π [v] > π [u] + e then:
π [v] ← π [u] + e

pred[v] ← e

0 13 14

4 7 8
22
Dijkstra’s algorithm demo (efficient implementation)

Basic step. Choose unexplored node u ∉ S with minimum π [u].


Add u to S.
For each edge e = (u, v) leaving u, if π [v] > π [u] + e then:
π [v] ← π [u] + e

pred[v] ← e

0 13 14

4 7 8
23
Dijkstra’s algorithm demo (efficient implementation)

Termination.
π [v] = length of a shortest s↝v path.
pred[v] = last edge on a shortest s↝v path.

0 13 14

pred[v]

π[v] 4 7 8
24

You might also like