Dijkstra

Dijkstra’s algorithm
N: set of nodes for which shortest path already found
Initialization: (Start with source node s)


N = {s}, Ds = 0, “s is distance zero from itself”
Dj=Csj for all j  s, distances of directly-connected
neighbors
Step A: (Find next closest node i)




Find i  N such that
Di = min Dj
for j  N
Add i to N
If N contains all the nodes, stop
Step B: (update minimum costs)



For each node j  N
Dj = min (Dj, Di+Cij)
Go to Step A
Minimum distance from s
to j through node i in N
Dijkstra's Shortest Path Algorithm
Find shortest path from s to t.
24
2
9
s
3
18
14
6
30
15
11
5
5
16
20
7
6
2
44
4
19
6
t
2
Dijkstra's Shortest Path Algorithm
S={ }
PQ = { s, 2, 3, 4, 5, 6, 7, t }


0
s
24
2
9
14
18


30

11
5
5
16
20
7
distance label

6
2
6
15
3
44
4
19
6
t

3
Dijkstra's Shortest Path Algorithm
S={ }
PQ = { s, 2, 3, 4, 5, 6, 7, t }
delmin
0
s


24
2
9
14
18


30

11
5
5
16
20
7
distance label

6
2
6
15
3
44
4
19
6
t

4
Dijkstra's Shortest Path Algorithm
S={s}
PQ = { 2, 3, 4, 5, 6, 7, t }
decrease key

X
 9
0
s
24
2
9
18
X
 14
14

30

11
5
5
16
20
7
distance label
X
 15
6
2
6
15
3
44
4
19
6
t

5
Dijkstra's Shortest Path Algorithm
S={s}
PQ = { 2, 3, 4, 5, 6, 7, t }
delmin

X 9

0
s
24
2
9
18
X 14

14

30

11
5
5
16
20
7
distance label
X
 15
6
2
6
15
3
44
4
19
6
t

6
Dijkstra's Shortest Path Algorithm
S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }

X 9

0
s
24
2
9
18
X 14

14

30

11
5
5
16
20
7
X
 15
6
2
6
15
3
44
4
19
6
t

7
Dijkstra's Shortest Path Algorithm
S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }
decrease key
X
 33
X 9

0
s
24
2
9
18
X 14

14

30

11
5
5
16
20
7
X
 15
6
2
6
15
3
44
4
19
6
t

8
Dijkstra's Shortest Path Algorithm
S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }
X
 33
X 9

0
s
24
2
9
3
delmin
18
X 14

14
6

30
15

11
5
5
16
20
7
X
 15
6
2
44
4
19
6
t

9
Dijkstra's Shortest Path Algorithm
S = { s, 2, 6 }
PQ = { 3, 4, 5, 7, t }
32
X
 33
X
X 9

0
s
24
2
9
18
X 14

14
30

44
X

11
5
5
16
20
7
X
 15
6
2
6
15
3
44
4
19
6
t

10
Dijkstra's Shortest Path Algorithm
S = { s, 2, 6 }
PQ = { 3, 4, 5, 7, t }
32
X
 33
X
X 9

0
s
24
2
9
18
X 14

14
30

44
X

11
5
5
16
20
7
X
 15
6
2
6
15
3
44
delmin
4
19
6
t

11
Dijkstra's Shortest Path Algorithm
S = { s, 2, 6, 7 }
PQ = { 3, 4, 5, t }
32
X
 33
X
X 9

0
s
24
2
9
18
X 14

14
30
44
X 35
X

5
5
X
 15

11
16
20
7
6
2
6
15
3
44
4
19
6
t

59 X
12
Dijkstra's Shortest Path Algorithm
S = { s, 2, 6, 7 }
PQ = { 3, 4, 5, t }
delmin
32
X
 33
X
X 9

0
s
24
2
9
18
X 14

14
30
44
X 35
X

5
5
X
 15

11
16
20
7
6
2
6
15
3
44
4
19
6
t

59 X
13
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 6, 7 }
PQ = { 4, 5, t }
32
X
 33
X
X 9

0
s
24
2
9
18
X 14

14
30
44
X 34
X 35
X

5
5
X
 15

11
16
20
7
6
2
6
15
3
44
4
19
6
t
51 59

X X
14
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 6, 7 }
PQ = { 4, 5, t }
32
X
 33
X
X 9

0
s
24
2
9
18
X 14

14
30
20
7
X
 15
44
X 34
X 35
X

5
5
6
2
6
15
3
delmin
44

11
16
4
19
6
t
51 59

X X
15
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 5, 6, 7 }
PQ = { 4, t }
32
X
 33
X
X 9

0
s
24
2
9
18
X 14

14
30
44
X 34
X 35
X

5
5
X
 15
11
16
20
7
6
2
6
15
3
44
45 X

4
19
6
t
50 51

X 59
X X
16
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 5, 6, 7 }
PQ = { 4, t }
32
X
 33
X
X 9

0
s
24
2
9
18
X 14

14
30
44
X 34
X 35
X

5
5
X
 15
11
16
20
7
6
2
6
15
3
44
45 X

4
19
delmin
6
t
50 51

X 59
X X
17
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7 }
PQ = { t }
32
X
 33
X
X 9

0
s
24
2
9
18
X 14

14
30
44
X 34
X 35
X

5
5
X
 15
11
16
20
7
6
2
6
15
3
44
45 X

4
19
6
t
50 51

X 59
X X
18
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7 }
PQ = { t }
32
X
 33
X
X 9

0
s
24
2
9
18
X 14

14
30
44
X 34
X 35
X

5
5
X
 15
11
16
20
7
6
2
6
15
3
44
45 X

4
19
6
t
delmin
50 51

X 59
X X
19
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7, t }
PQ = { }
32
X
 33
X
X 9

0
s
24
2
9
18
X 14

14
30
44
X 34
X 35
X

5
5
X
 15
11
16
20
7
6
2
6
15
3
44
45 X

4
19
6
t
50 51

X 59
X X
20
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7, t }
PQ = { }
32
X
 33
X
X 9

0
s
24
2
9
18
X 14

14
30
44
X 34
X 35
X

5
5
X
 15
11
16
20
7
6
2
6
15
3
44
45 X

4
19
6
t
50 51

X 59
X X
21
Modified Dijkstra’s algorithm
Dijkstra-aux (G, target-node,sub-path)
N: set of nodes for which shortest path already found
Initialization: Start with node s= (pop sub-path)//last node on sub-path



V’ = V – {sub-path} //search over nodes not already in sub-path
N = {s}, Ds = 0 for s  sub-path, “s is distance zero from itself”
Dj=Csj for all jV’, j  s, distances of directly-connected neighbors
Step A: (Find next closest node i)




Find i  N such that
Di = min Dj
for j  N
Add i to N
If N contains j=target-node,
–
–
return N, Csj
Else return  //no path to target-node
Step B: (update minimum costs)



For each node j  N
Dj = min (Dj, Di+Cij)
Go to Step A
Modified Dijkstra’s k-Path algorithm
Dijkstra-recurse (G, target-node, Path, count)

Do while count< k and Path  
–


New-Path = Dijkstra-aux (G, target-node, Path)// min-cost path to targetnode
 If New-Path   //another min-cost path
– count=count+1; Path-set=Path-setNew-Path
– E’ = E – {(pop-Path, target-node)//remove edge from graph
– New-Path=Dijkstra-aux (G(V,E’), target-node, pop-Path, count)
// graph with edge deleted to prevent finding same path
 Else New-Path=Dijkstra-aux (G(V,E), target-node, pop-Path, count)
End while
Return Path-set
Modified Dijkstra’s k-Path algorithm
Dijkstra (G, target-node)
Initialization: Start with node s= source node





V’ = V – {s} //search over all nodes
Path-set =  //set of min-cost paths
count=0 //path counter
Path = {s}
Do while count< k and Path  
–


New-Path = Dijkstra-aux (G, target-node, Path)// min-cost path to
target-node
 If New-Path   //another min-cost path
– count=count+1; Path-set=Path-setNew-Path
– E’ = E – {(pop-Path, target-node)//remove edge from graph
– New-Path=Dijkstra-aux (G(V,E’), target-node, pop-Path )//
min-cost path to target-node
 Else New-Path=Dijkstra-aux (G(V,E), target-node, pop-Path )
End while
Return Path-set
25
Modified Dijkstra’s k-Path algorithm
Dijkstra-recurse (G, target-node, Path, count)

Do while count< k and Path  
–


New-Path = Dijkstra-aux (G, target-node, Path)// min-cost path to targetnode
 If New-Path   //another min-cost path
– count=count+1; Path-set=Path-setNew-Path
 Else New-Path=Dijkstra-aux (G(V,E), target-node, pop-Path, count)
End while
Return Path-set