Download presentation
Presentation is loading. Please wait.
Published byPatience Paul Modified over 9 years ago
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 zero from itself” n D j =C sj for all j s, distances of directly-connected neighbors Step A: (Find next closest node i) n Find i N such that n D i = min Dj for j N n Add i to N n If N contains all the nodes, stop Step B: (update minimum costs) n For each node j N n D j = min (D j, D i +C ij ) n Go to Step A Minimum distance from s to j through node i in N
2
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
3
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 }
4
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
5
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
6
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
7
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
8
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
9
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
10
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 44 X X 32
11
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 44 X delmin X 33 X 32
12
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 44 X 35 X 59 X 24 X 33 X 32
13
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 44 X 35 X 59 X delmin X 33 X 32
14
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 44 X 35 X 59 XX 51 X 34 X 33 X 32
15
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 44 X 35 X 59 XX 51 X 34 delmin X 33 X 32 24
16
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 44 X 35 X 59 XX 51 X 34 24 X 50 X 45 X 33 X 32
17
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 44 X 35 X 59 XX 51 X 34 24 X 50 X 45 delmin X 33 X 32
18
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 44 X 35 X 59 XX 51 X 34 24 X 50 X 45 X 33 X 32
19
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 44 X 35 X 59 XX 51 X 34 X 50 X 45 delmin X 33 X 32 24
20
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 44 X 35 X 59 XX 51 X 34 X 50 X 45 X 33 X 32
21
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 44 X 35 X 59 XX 51 X 34 X 50 X 45 X 33 X 32
22
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 n V’ = V – {sub-path} //search over nodes not already in sub-path n N = {s}, D s = 0 for s sub-path, “s is distance zero from itself” n D j =C sj for all j V’, j s, distances of directly-connected neighbors Step A: (Find next closest node i) n Find i N such that n D i = min Dj for j N n Add i to N n If N contains j=target-node, – return N, C sj – Else return //no path to target-node Step B: (update minimum costs) n For each node j N n D j = min (D j, D i +C ij ) n Go to Step A
23
Modified Dijkstra’s k-Path algorithm Dijkstra-recurse (G, target-node, Path, count) n 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, count) // graph with edge deleted to prevent finding same path Else New-Path=Dijkstra-aux (G(V,E), target-node, pop-Path, count) n End while n Return Path-set
24
Modified Dijkstra’s k-Path algorithm Dijkstra (G, target-node) Initialization: Start with node s= source node n V’ = V – {s} //search over all nodes n Path-set = //set of min-cost paths n count=0 //path counter n Path = {s} n 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 ) n End while n Return Path-set
25
25
26
Modified Dijkstra’s k-Path algorithm Dijkstra-recurse (G, target-node, Path, count) n 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 Else New-Path=Dijkstra-aux (G(V,E), target-node, pop-Path, count) n End while n Return Path-set
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.