Data Structures and Algorithm Analysis Lecture 8

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Data Structures and Algorithms Graphs Single-Source Shortest Paths (Dijkstra’s Algorithm) PLSD210(ii)
CS38 Introduction to Algorithms Lecture 5 April 15, 2014.
CSE 421 Algorithms Richard Anderson Dijkstra’s algorithm.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Shortest Path Algorithms
Shortest Paths Definitions Single Source Algorithms
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
Dijkstra’s Shortest Paths CS 312 Lecture 4. Announcements Project 1 comes out Friday –Min spanning trees and scheduling –due 2 weeks from Friday (1 week.
© 2004 Goodrich, Tamassia Shortest Paths1 Shortest Paths (§ 13.6) Given a weighted graph and two vertices u and v, we want to find a path of minimum total.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Greed: Shortest Path s 3 t
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Dijkstra's algorithm.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Weighted Graphs In a weighted graph, each edge has an associated numerical value, called the weight of the edge Edge weights may represent, distances,
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 8 Greedy Graph.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 7 Greedy Graph.
Lecture 16. Shortest Path Algorithms
CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
Lecture 13 Algorithm Analysis
1 Chapter 5-2 Greedy Algorithms Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
1 Sections 2.5 and 4.4 Priority Queues and Dijkstra’s Algorithm For Shortest Paths Some Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley.
CSE 421 Algorithms Richard Anderson Lecture 8 Optimal Caching Dijkstra’s algorithm.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 10.
CS38 Introduction to Algorithms Lecture 3 April 8, 2014.
Data Structures and Algorithm Analysis Lecture 5
Chapter 4 Greedy Algorithms
Shortest Path 6/18/2018 4:22 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Lecture 13 Shortest Path.
Graph Algorithms BFS, DFS, Dijkstra’s.
Algorithms and Data Structures Lecture XIII
Chapter 4 Greedy Algorithms
EMIS 8374 Dijkstra’s Algorithm Updated 18 February 2008
Data Structures and Algorithms CMPSC 465
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
Shortest Paths C B A E D F Shortest Paths
Autumn 2016 Lecture 9 Dijkstra’s algorithm
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
4.4 Shortest Paths in a Graph
Autumn 2015 Lecture 9 Dijkstra’s algorithm
Richard Anderson Lecture 9 Dijkstra’s algorithm
Richard Anderson Lecture 9 Dijkstra’s algorithm
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Slide Courtesy: Uwash, UT
Lecture 13 Algorithm Analysis
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Chapter 24: Single-Source Shortest Paths
Shortest Paths.
Slide Courtesy: Uwash, UT
Winter 2019 Lecture 9 Dijkstra’s algorithm
Lecture 26 CSE 331 Nov 1, 2010.
Chapter 24: Single-Source Shortest Paths
CSE 417: Algorithms and Computational Complexity
Lecture 12 Shortest Path.
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Directed Graphs (Part II)
Autumn 2019 Lecture 9 Dijkstra’s algorithm
Presentation transcript:

Data Structures and Algorithm Analysis Lecture 8 CSCI 256 Data Structures and Algorithm Analysis Lecture 8 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some by Iker Gondra

Recall Greedy Analysis Strategies Greedy algorithm stays ahead: Show that after each step of the greedy algorithm, its solution is at least as good as any other algorithm's Exchange argument: Gradually transform any solution to the one found by the greedy algorithm without hurting its quality

Shortest Path Problem Directed graph G = (V, E) Source s, destination t Length e = length of edge e Shortest path problem: find shortest directed path from s to t cost of path = sum of edge costs in path 2 23 3 9 s Cost of path s-2-3-5-t = 9 + 23 + 2 + 16 = 48. 18 14 2 6 6 30 4 19 5 11 15 5 6 20 16 t 7 44

Dijkstra's Algorithm choose Dijkstra's algorithm: determines the length of the shortest path from source node s to each other node in the graph: Maintain a set of explored nodes S for which we have determined the shortest path distance d(u) from s to u Initialize S = { s }, d(s) = 0 while for each unexplored node v find shortest path that can be constructed by travelling along a path through S to some u in S followed by the edge(u,v) e v d(u) u S s

Letting d(u) be the length of the shortest path from s to u, for u in S (i.e., u is explored node) Then for each v’ find (v’) = min (d(u) + le ) (u in S for which exists e= (u,v’)) Find the v with the minimum (v); add v to S and define d(v) = (v)

Simulate Dijkstra’s algorithm (starting from s) on the graph Vertex Added Round s a b c d 1 1 2 3 4 5 c a 1 3 2 1 s 4 4 6 1 b d 3

Dijkstra’s Algorithm as a greedy algorithm Heuristic: Elements committed to the solution by order of minimum distance from s

Correctness Proof Elements in S have the correct label Key to proof: when v is added to S, it has the correct distance label y x s u v

Warmup If P is a shortest path from s to v, and if t is on the path P, the segment from s to t is a shortest path between s and t WHY? v t s

Induction statement: For any set S of size n in an arbitrary graph G, if s is in S then for any u in S, d(u), found by Dijkstra’s algorithm, is the length of the shortest path from s to u.

Dijkstra's Algorithm: Proof of Correctness For each node v  S, d(v) is the length of the shortest s-v path Pf. (by induction on |S|) Base case: |S| = 1 is trivial Inductive hypothesis: Assume true for |S| = k  1 Let v be next node added to S, using the edge u-v By Dijkstra, d(v) (= (v) ) is the length of the s-v path found, is shorter than (y) for any path with one edge out of S to y for any y. Consider any other s-v path P (the blue path). We'll see that it's no shorter than (v) Let x-y be the first edge in P that leaves S, and let P' be the subpath to x We can see that  (P)  (v) P  (P)   (P') +  (x,y)  d(x) +  (x, y)  (y)  (v) P' x y nonnegative weights inductive hypothesis defn of (y) Dijkstra chose v instead of y s S u v

Implementation: There are n-1 iterations of the while loop, each adding a new node to S. One might then consider each v not in S and go through all the edges between u in S and v to determine the min (d(u) + le). With m edges, computing these can take O(m) time so running time is O(mn). We can do better

Dijkstra's Algorithm: Implementation For each unexplored node, explicitly maintain Efficient implementation: Maintain a priority queue of unexplored nodes, prioritized (key value) by (v) Next node to explore = node v with minimum (v) When exploring v, for each incident edge e = (v,w), update Full discussion with tips on how to manage the priority queue is found in the text.

Using a priority queue, Dijkstra’s Algorithm can be implemented in a graph with n nodes and m edges to run in O(m) time plus the time for n ExtractMin and m ChangeKey Operations. Since each priority queue operation can be made to run in O(log n), the overall time is O(m log n) (assuming m > n).