Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start...
-
Upload
patience-paul -
Category
Documents
-
view
215 -
download
3
Transcript of Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start...
![Page 1: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/1.jpg)
Dijkstra’s algorithmN: set of nodes for which shortest path already foundInitialization: (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
![Page 2: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/2.jpg)
2
Dijkstra's Shortest Path Algorithm
Find shortest path from s to t.
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
![Page 3: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/3.jpg)
3
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
0
distance label
S = { }PQ = { s, 2, 3, 4, 5, 6, 7, t }
![Page 4: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/4.jpg)
4
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
0
distance label
S = { }PQ = { s, 2, 3, 4, 5, 6, 7, t }
delmin
![Page 5: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/5.jpg)
5
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
distance label
S = { s }PQ = { 2, 3, 4, 5, 6, 7, t }
decrease key
X
X
X
![Page 6: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/6.jpg)
6
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
distance label
S = { s }PQ = { 2, 3, 4, 5, 6, 7, t }
X
X
X
delmin
![Page 7: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/7.jpg)
7
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2 }PQ = { 3, 4, 5, 6, 7, t }
X
X
X
![Page 8: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/8.jpg)
8
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2 }PQ = { 3, 4, 5, 6, 7, t }
X
X
X
decrease key
X 33
![Page 9: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/9.jpg)
9
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2 }PQ = { 3, 4, 5, 6, 7, t }
X
X
X
X 33
delmin
![Page 10: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/10.jpg)
10
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2, 6 }PQ = { 3, 4, 5, 7, t }
X
X
X
X 33
44X
X
32
![Page 11: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/11.jpg)
11
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2, 6 }PQ = { 3, 4, 5, 7, t }
X
X
X
44X
delmin
X 33X
32
![Page 12: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/12.jpg)
12
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2, 6, 7 }PQ = { 3, 4, 5, t }
X
X
X
44X
35X
59 X
24
X 33X
32
![Page 13: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/13.jpg)
13
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2, 6, 7 }PQ = { 3, 4, 5, t }
X
X
X
44X
35X
59 X
delmin
X 33X
32
![Page 14: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/14.jpg)
14
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2, 3, 6, 7 }PQ = { 4, 5, t }
X
X
X
44X
35X
59 XX 51
X 34
X 33X
32
![Page 15: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/15.jpg)
15
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2, 3, 6, 7 }PQ = { 4, 5, t }
X
X
X
44X
35X
59 XX 51
X 34
delmin
X 33X
32
24
![Page 16: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/16.jpg)
16
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2, 3, 5, 6, 7 }PQ = { 4, t }
X
X
X
44X
35X
59 XX 51
X 34
24
X 50
X 45
X 33X
32
![Page 17: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/17.jpg)
17
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2, 3, 5, 6, 7 }PQ = { 4, t }
X
X
X
44X
35X
59 XX 51
X 34
24
X 50
X 45
delmin
X 33X
32
![Page 18: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/18.jpg)
18
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2, 3, 4, 5, 6, 7 }PQ = { t }
X
X
X
44X
35X
59 XX 51
X 34
24
X 50
X 45
X 33X
32
![Page 19: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/19.jpg)
19
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2, 3, 4, 5, 6, 7 }PQ = { t }
X
X
X
44X
35X
59 XX 51
X 34
X 50
X 45
delmin
X 33X
32
24
![Page 20: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/20.jpg)
20
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2, 3, 4, 5, 6, 7, t }PQ = { }
X
X
X
44X
35X
59 XX 51
X 34
X 50
X 45
X 33X
32
![Page 21: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/21.jpg)
21
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
S = { s, 2, 3, 4, 5, 6, 7, t }PQ = { }
X
X
X
44X
35X
59 XX 51
X 34
X 50
X 45
X 33X
32
![Page 22: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/22.jpg)
Modified Dijkstra’s algorithmDijkstra-aux (G, target-node,sub-path)N: set of nodes for which shortest path already foundInitialization: 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 jV’, 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
![Page 23: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/23.jpg)
Modified Dijkstra’s k-Path algorithmDijkstra-recurse (G, target-node, Path, count)
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-setNew-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
![Page 24: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/24.jpg)
Modified Dijkstra’s k-Path algorithmDijkstra (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-setNew-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
![Page 25: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/25.jpg)
25
![Page 26: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.](https://reader035.fdocuments.us/reader035/viewer/2022070407/56649e445503460f94b381f6/html5/thumbnails/26.jpg)
Modified Dijkstra’s k-Path algorithmDijkstra-recurse (G, target-node, Path, count)
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-setNew-Path Else New-Path=Dijkstra-aux (G(V,E), target-node, pop-Path,
count) End while Return Path-set