Docsity

Dijkstra's Algorithm for Finding Shortest Paths in Graphs - Prof. Nelson Padua-Perez, Study notes of Computer Science

Partial preview of the text

Download Dijkstra's Algorithm for Finding Shortest Paths in Graphs - Prof. Nelson Padua-Perez and more Study notes Computer Science in PDF only on Docsity!

Graphs & Graph Algorithms 2

CMSC 132

Department of Computer ScienceUniversity of Maryland, College Park

Overview

Shortest path Djikstra’s algorithm

Shortest Path – Dijkstra’s Algorithm^ Maintain

Nodes with known shortest path from start

S

Cost of shortest path to node K from start

C[K]

Only for paths through nodes in S Predecessor to K on shortest path

P[K]

Updated whenever new (lower) C[K] discovered Remembers actual path with lowest cost

Shortest Path – Intuition for Dijkstra’s^ At each step inthe algorithm

Shortest pathsare known fornodes in

S

Store in

C[K]

length ofshortest path tonode K (for allpaths throughnodes in { S } ) Add to { S } nextclosest node

S

Shortest Path – Djikstra’s Algorithm^ Algorithm

Add starting node to S Repeat until all nodes in S

Find node K not in S with smallest C[ K ] Add K to S Examine C[J] for all neighbors J of K not in S

If ( C[K] + weight for edge (K,J) ) < C[J]

New shortest path by first going to K, then J Update C[J]

←←←←^

C[K] + weight for edge (K,J)

Update P[J]

←←←←^

K

Shortest Path – Dijkstra’s Algorithm S = {}

,^ P[ ] = none for all nodes

C[start] = 0, C[ ] =

∞∞∞∞^

for all other nodes

while ( not all nodes in S

find node K not in S with smallest C[K] add K to Sfor each node J not in S adjacent to K if ( C[K] + cost of (K,J) < C[J] )C[J] = C[K] + cost of (K,J)P[J] = K

Optimal

solution computed with

greedy

algorithm

Dijkstra’s Shortest Path Example Find shortest paths starting from node 1 S = 1

none ∞∞∞∞ 5

none ∞∞∞∞ 4

none ∞∞∞∞ 3

none ∞∞∞∞ 2

none 0 1

P

C

Djikstra’s Shortest Path Example Update C[K] for all neighbors of 1 not in { S } S = { 1 }

C[2] = min (

∞∞∞∞^

, C[1] + (1,2) ) = min (

∞∞∞∞^

C[3] = min (

∞∞∞∞^

, C[1] + (1,3) ) = min (

∞∞∞∞^

none ∞∞∞∞ 5

none ∞∞∞∞ 4

1 8 3

1 5 2

none 0 1

P

C

Dijkstra’s Shortest Path Example Update C[K] for all neighbors of 2 not in S S = { 1, 2 }

C[3] = min (8 , C[2] + (2,3) ) = min (8 , 5 + 1) =

C[4] = min (

∞∞∞∞^

, C[2] + (2,4) ) = min (

∞∞∞∞^

none ∞∞∞∞ 5

2 15 4

2 6 3

1 5 2

none 0 1

P

C

Dijkstra’s Shortest Path Example Find node K with smallest C[K] and add to S S = { 1, 2, 3 }

none ∞∞∞∞ 5

2 15 4

2 6 3

1 5 2

none 0 1

P

C

Dijkstra’s Shortest Path Example Find node K with smallest C[K] and add to S { S } = 1, 2, 3, 4

none ∞∞∞∞ 5

3 9 4

2 6 3

1 5 2

none 0 1

P

C

Dijkstra’s Shortest Path Example Update C[K] for all neighbors of 4 not in S S = { 1, 2, 3, 4 }

C[5] = min (

∞∞∞∞^

, C[4] + (4,5) ) = min (

∞∞∞∞^

4 18 5

3 9 4

2 6 3

1 5 2

none 0 1

P

C

Dijkstra’s Shortest Path Example All nodes in S, algorithm is finished S = { 1, 2, 3, 4, 5 }

4 18 5

3 9 4

2 6 3

1 5 2

none 0 1

P

C

Dijkstra’s Shortest Path Example Find shortest path from start to K^ Start at K^ Trace back predecessors in P[ ] Example paths (in reverse)^2

→→→→^

→→→→^

→→→→^

4 18 5

3 9 4

2 6 3

1 5 2

none 0 1

P

C