Docsity

Single Source Shortest Path - Object-Oriented Programming II - Quiz 19 | CMSC 132, Quizzes of Computer Science

Partial preview of the text

Download Single Source Shortest Path - Object-Oriented Programming II - Quiz 19 | CMSC 132 and more Quizzes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Single Source Shortest Path

Algorithm

Department of Computer Science

University of Maryland, College Park

Single Source Shortest Path

Common graph problem

  1. Find path from X to Y with lowest edge weight
  2. Find path from X to any Y with lowest edge weight

Useful for many applications

Shortest route in map Lowest cost trip Most efficient internet route

Dijkstra’s algorithm solves problem 2

Can also be used to solve problem 1 Would use different algorithm if only interested in a single destination

Shortest Path – Intuition for Dijkstra’s

At each step in

the algorithm

Shortest paths are known for nodes in S Store in C[K] length of shortest path to node K (for all paths through nodes in { S } ) Add to { S } next closest node

S

Shortest Path – Intuition for Djikstra’s

Update distance to J

after adding node K

Previous shortest path to K already in C[ K ] Possibly shorter path to J by going through node K Compare C[ J ] with C[ K ] + weight of (K,J), update C[ J ] if needed

Dijkstra’s Shortest Path Example

Initial state

S = 

C P

1 0 none 2  none 3  none 4  none 5  none

Dijkstra’s Shortest Path Example

Find shortest paths starting from node 1

S = 1

C P

1 0 none 2  none 3  none 4  none 5  none

Djikstra’s Shortest Path Example

Find node K with smallest C[K] and add to S

S = { 1, 2 }

C P

1 0 none 2 5 1 3 8 1 4  none 5  none

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) = 6

C[4] = min ( , C[2] + (2,4) ) = min ( , 5 + 10) = 15

C P

1 0 none 2 5 1 3 6 2 4 15 2 5  none

Dijkstra’s Shortest Path Example

Update C[K] for all neighbors of 3 not in S

{ S } = 1, 2, 3

C[4] = min (15 , C[3] + (3,4) ) = min (15 , 6 + 3) = 9

C P

1 0 none 2 5 1 3 6 2 4 9 3 5  none

Dijkstra’s Shortest Path Example

Find node K with smallest C[K] and add to S

{ S } = 1, 2, 3, 4

C P

1 0 none 2 5 1 3 6 2 4 9 3 5  none

Dijkstra’s Shortest Path Example

Find node K with smallest C[K] and add to S

S = { 1, 2, 3, 4, 5 }

C P

1 0 none 2 5 1 3 6 2 4 9 3 5 18 4

Dijkstra’s Shortest Path Example

All nodes in S, algorithm is finished

S = { 1, 2, 3, 4, 5 }

C P

1 0 none 2 5 1 3 6 2 4 9 3 5 18 4

Typical Problem for Exam/Quiz

CS History Moment: Edsger Wybe Dijkstra

Dutch computer scientist Contributions Shortest Path Algorithm Reverse Polish Notation Semaphore construct A. M. Turing Award Recipient Turing Awards - http://awards.acm.org/homepage.cfm?srt=all&awd= Info and picture from Wikipedia http://en.wikipedia.org/wiki/Edsger_Dijkstra