Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cinda Heeren / Geoffrey Tien

Similar presentations


Presentation on theme: "Cinda Heeren / Geoffrey Tien"— Presentation transcript:

1 Cinda Heeren / Geoffrey Tien
Shortest Paths Dijkstra's Algorithm April 06, 2018 Cinda Heeren / Geoffrey Tien

2 Cinda Heeren / Geoffrey Tien
Announcements PA4 due Apr.09 Geoff's office hours next week Monday & Wednesday, 10:00 – 14:00 Final exam Thursday, April 12 15:30 – 18:00 (150 minutes) SRC A, B, C Assigned seating by name, please be seated in your correct spot! April 06, 2018 Cinda Heeren / Geoffrey Tien

3 Single-source shortest path
Given a graph 𝐺= 𝑉,𝐸 and a vertex 𝑠∈𝑉, find the shortest path from 𝑠 to every vertex in 𝑉 Variations Weighted vs unweighted Cyclic vs acyclic Positive weights only vs negative weights allowed Multiple weight types to optimize April 06, 2018 Cinda Heeren / Geoffrey Tien

4 Single-Source Shortest Path
Un/directed, weighted graphs, no negative cycles What is the least cost path from one vertex to another? For weighted graphs, this is the path that has the smallest sum of its edge weights A 1 4 B C 5 3 2 E 1 5 D 8 1 F 2 G The shortest path between B and G is: B-D-E-F-G (7) and not B-G (8) April 06, 2018 Cinda Heeren / Geoffrey Tien

5 Cinda Heeren / Geoffrey Tien
Dijkstra's Algorithm Classic algorithm for solving shortest path in weighted graphs without negative weights A greedy algorithm Best local choice is made at each step, without considering future consequences Intuition: Shortest path from source vertex to itself is 0 Cost of going to adjacent nodes is at most edge weights Cheapest of these must be shortest path to that node Update paths for new node and continue picking shortest path April 06, 2018 Cinda Heeren / Geoffrey Tien

6 Cinda Heeren / Geoffrey Tien
Dijkstra's Algorithm Initialize the cost of reaching each vertex to ∞ Initialize the cost of the source to 0 While there are unvisited vertices left in the graph Select the unvisited vertex with the lowest cost: 𝑢 Mark 𝑢 as visited, and note the vertex 𝑣 which was used to reach 𝑢 For each vertex 𝑤 which is adjacent to 𝑢 𝑤's cost = min(𝑤's old cost, 𝑢's cost + cost of (𝑢,𝑤)) And note the "predecessor" vertex which can be used to reach 𝑤 with the lowest cost April 06, 2018 Cinda Heeren / Geoffrey Tien

7 Dijkstra's Algorithm Example: directed graph A B C D E F G H 2 4 7 9 1
8 10 3 Source node: C Priority queue: Vertex Cost Predecessor A B C D E F G H Visited Cost Predecessor Note that we need access to arbitrary elements in this prQ in order to update April 06, 2018 Cinda Heeren / Geoffrey Tien

8 Cinda Heeren / Geoffrey Tien
Dijkstra's Algorithm Undirected graph 2 2 B A F 3 H Source node: C 1 9 2 10 1 4 C G Priority queue: 2 8 Vertex Cost Predecessor A B C D E F G H 1 D E 7 Visited Cost Predecessor April 06, 2018 Cinda Heeren / Geoffrey Tien

9 Dijkstra's algorithm C, 0, C B, 1, C D, 2, C A, 3, B F, 3, B G, 5, F
Retrieving the shortest path vertex, cost, predecessor Once the results array is complete paths from the start vertex can be retrieved Done by looking up the end vertex (the vertex to which one is trying to find a path) and backtracking through parent vertices to the start For example to find a path to vertex E backtrack through: E, G, F, B, C Note: there should be some efficient way to search the results array for a vertex and this path is in reverse order! C, 0, C B, 1, C D, 2, C A, 3, B F, 3, B G, 5, F H, 6, F E, 6, G April 06, 2018 Cinda Heeren / Geoffrey Tien

10 Correctness VISITED VERTICES The cloud proof, informally 𝑤 𝑦 𝑢 𝑠 𝑣 𝑥
Vertices in the cloud have a known shortest path, and 𝑤 is the next vertex to add according to the priority queue Proof by induction, note that negative edge weights wreck this proof! April 06, 2018 Cinda Heeren / Geoffrey Tien

11 Data structures for Dijkstra's algorithm
Selecting unvisited node with the minimum cost Priority queue / min-heap! Building initial heap is 𝑂 𝑉 RemoveMin executed 𝑉 times, total 𝑂 𝑉 log 𝑉 Each of 𝐸 edges has to be processed once Looking up (and changing) the current cost of a vertex in a heap takes 𝑂 𝑉 for an unindexed heap (𝑂 1 if the heap is indexed) The heap property needs to be preserved after a change for an additional cost of 𝑂 log 𝑉 The total cost is 𝑉 + 𝑉 log 𝑉 + 𝐸 𝑉 + log 𝑉 Or, 𝑂 𝑉 log 𝑉 + 𝐸 𝑉 If the heap is indexed the cost is 𝑂 𝑉 + 𝐸 log 𝑉 April 06, 2018 Cinda Heeren / Geoffrey Tien

12 Cinda Heeren / Geoffrey Tien
Exercise Given the following undirected, weighted graph, use Dijkstra's algorithm to determine the cost and vertex sequence of a shortest path from A to I. A 12 B 8 9 9 C 10 8 5 5 D 6 E F 14 G 3 4 H 12 7 4 9 I 7 J 8 K April 06, 2018 Cinda Heeren / Geoffrey Tien

13 Readings for this lesson
Carrano & Henry Chapter (Shortest path) Congratulations, you're done! April 06, 2018 Cinda Heeren / Geoffrey Tien


Download ppt "Cinda Heeren / Geoffrey Tien"

Similar presentations


Ads by Google