Understanding Dijkstra's Shortest Path Algorithm Step-by-Step
Dijkstra's Shortest Path Algorithm finds the shortest path in a weighted graph from a source node to all other nodes. It starts with the initial node, marking its distance to itself as zero, while assigning infinity to the others. The algorithm iteratively selects the closest unvisited node and updates the distances to its neighbors. This process continues until all nodes are visited or the target node is reached. This description provides a thorough explanation of the algorithm's workings, initialization, updates, and termination conditions.
Understanding Dijkstra's Shortest Path Algorithm Step-by-Step
E N D
Presentation Transcript
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 iN 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. 2 24 3 9 s 18 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44
Dijkstra's Shortest Path Algorithm S = { } PQ = { s, 2, 3, 4, 5, 6, 7, t } 2 24 3 0 9 s 18 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 distance label
Dijkstra's Shortest Path Algorithm S = { } PQ = { s, 2, 3, 4, 5, 6, 7, t } delmin 2 24 3 0 9 s 18 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 distance label
Dijkstra's Shortest Path Algorithm S = { s } PQ = { 2, 3, 4, 5, 6, 7, t } decrease key 9 X 2 24 3 0 9 s 18 14 X 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 distance label 15 X
Dijkstra's Shortest Path Algorithm S = { s } PQ = { 2, 3, 4, 5, 6, 7, t } delmin 9 X 2 24 3 0 9 s 18 14 X 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 distance label 15 X
Dijkstra's Shortest Path Algorithm S = { s, 2 } PQ = { 3, 4, 5, 6, 7, t } 9 X 2 24 3 0 9 s 18 14 X 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 15 X
Dijkstra's Shortest Path Algorithm S = { s, 2 } PQ = { 3, 4, 5, 6, 7, t } decrease key 33 X 9 X 2 24 3 0 9 s 18 14 X 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 15 X
Dijkstra's Shortest Path Algorithm S = { s, 2 } PQ = { 3, 4, 5, 6, 7, t } 33 X 9 X 2 24 3 0 9 delmin s 18 14 X 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 15 X
Dijkstra's Shortest Path Algorithm S = { s, 2, 6 } PQ = { 3, 4, 5, 7, t } 32 33 X X 9 X 2 24 3 0 9 s 18 14 X 14 6 2 6 44 4 X 19 30 11 5 15 5 6 16 20 t 7 44 15 X
Dijkstra's Shortest Path Algorithm S = { s, 2, 6 } PQ = { 3, 4, 5, 7, t } 32 33 X X 9 X 2 24 3 0 9 s 18 14 X 14 6 2 6 44 4 X 19 30 11 5 15 5 6 16 20 t 7 44 delmin 15 X
Dijkstra's Shortest Path Algorithm S = { s, 2, 6, 7 } PQ = { 3, 4, 5, t } 32 33 X X 9 X 2 24 3 0 9 s 18 14 X 14 6 2 6 35 44 X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 59 X 15 X
Dijkstra's Shortest Path Algorithm S = { s, 2, 6, 7 } PQ = { 3, 4, 5, t } delmin 32 33 X X 9 X 2 24 3 0 9 s 18 14 X 14 6 2 6 35 44 X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 59 X 15 X
Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 6, 7 } PQ = { 4, 5, t } 32 33 X X 9 X 2 24 3 0 9 s 18 14 X 14 6 2 6 35 34 44 X X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 51 59 X X 15 X
Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 6, 7 } PQ = { 4, 5, t } 32 33 X X 9 X 2 24 3 0 9 s 18 14 X 14 6 2 6 35 34 44 X X 4 X 19 30 11 5 15 5 6 16 delmin 20 t 7 44 51 59 X X 15 X
Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 5, 6, 7 } PQ = { 4, t } 32 33 X X 9 X 2 24 3 0 9 s 18 14 X 14 6 2 6 45 35 34 X 44 X X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 50 51 59 X X X 15 X
Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 5, 6, 7 } PQ = { 4, t } 32 33 X X 9 X 2 24 3 0 9 s 18 14 X 14 6 2 6 45 35 34 X 44 X X 4 X 19 30 11 5 delmin 15 5 6 16 20 t 7 44 50 51 59 X X X 15 X
Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7 } PQ = { t } 32 33 X X 9 X 2 24 3 0 9 s 18 14 X 14 6 2 6 45 35 34 X 44 X X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 50 51 59 X X X 15 X
Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7 } PQ = { t } 32 33 X X 9 X 2 24 3 0 9 s 18 14 X 14 6 2 6 45 35 34 X 44 X X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 delmin 50 51 59 X X X 15 X
Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7, t } PQ = { } 32 33 X X 9 X 2 24 3 0 9 s 18 14 X 14 6 2 6 45 35 34 X 44 X X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 50 51 59 X X X 15 X
Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7, t } PQ = { } 32 33 X X 9 X 2 24 3 0 9 s 18 14 X 14 6 2 6 45 35 34 X 44 X X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 50 51 59 X X X 15 X
Modified Dijkstra’salgorithm • 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 jV’, j s, distances of directly-connected neighbors • Step A: (Find next closest node i) • Find iN 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 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
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-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
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 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