Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.

Slides:



Advertisements
Similar presentations
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Advertisements

November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
CS138A Single Source Shortest Paths Peter Schröder.
Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=
 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation.
Data Structures and Algorithms Graphs Single-Source Shortest Paths (Dijkstra’s Algorithm) PLSD210(ii)
chapter Single-Source Shortest Paths Problem Definition Shortest paths and Relaxation Dijkstra’s algorithm (can be viewed as a greedy algorithm)
Theory of Computing Lecture 8 MAS 714 Hartmut Klauck.
Introduction to Algorithms 6.046J/18.401J/SMA5503
CS38 Introduction to Algorithms Lecture 5 April 15, 2014.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
CS420 lecture twelve Shortest Paths wim bohm cs csu.
Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
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.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Shortest Paths Definitions Single Source Algorithms
DAST 2005 Tirgul 12 (and more) sample questions. DAST 2005 Q.We’ve seen that solving the shortest paths problem requires O(VE) time using the Belman-Ford.
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
1 Graph Algorithms Single source shortest paths problem Dana Shapira.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
CS 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis.
SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the.
Dijkstra's algorithm.
Topological Sorting and Least-cost Path Algorithms.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
David Luebke 1 9/10/2015 CS 332: Algorithms Single-Source Shortest Path.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
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.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
David Luebke 1 9/13/2015 CS 332: Algorithms S-S Shortest Path: Dijkstra’s Algorithm Disjoint-Set Union Amortized Analysis.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
1 Shortest Path Problems How can we find the shortest route between two points on a road map? Model the problem as a graph problem: –Road map is a weighted.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
Introduction to Algorithms Jiafen Liu Sept
The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
1 Introduction to Algorithms L ECTURE 17 (Chap. 24) Shortest paths I 24.2 Single-source shortest paths 24.3 Dijkstra’s algorithm Edsger Wybe Dijkstra
Lecture 13 Algorithm Analysis
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
Length of a Path The weight (length) of a path is the sum of the weights of its edges. adcbe Path p: Edge weights: w(a, b) = 7, w(b, c) = 2, w(c,
David Luebke 1 3/1/2016 CS 332: Algorithms Dijkstra’s Algorithm Disjoint-Set Union.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
Shortest paths Given: A single source vertex (given s) in a weighted, directed graph. Want to compute a shortest path for each possible destination. (Single.
CS38 Introduction to Algorithms Lecture 3 April 8, 2014.
GRAPH THEORY Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS )
Single-Source Shortest Paths (25/24) HW: 25-2 and 25-3 p. 546/24-2 and 24-3 p.615 Given a graph G=(V,E) and w: E   weight of is w(p) =  w(v[i],v[i+1])
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Data Structures and Algorithms
Algorithms and Data Structures Lecture XIII
Minimum Spanning Tree Shortest Paths
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Page 620 Single-Source Shortest Paths
2018/12/27 chapter25.
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
CSC 413/513: Intro to Algorithms
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 12 Shortest Path.
CS 3013: DS & Algorithms Shortest Paths.
Data Structures and Algorithm Analysis Lecture 8
Presentation transcript:

Theory of Computing Lecture 7 MAS 714 Hartmut Klauck

Shortest paths in weighted graphs We are given a graph G (adjacency list with weights W(u,v)) No edge means W(u,v)= 1 We look for shortest paths from start vertex s to all other vertices Length of a path is the sum of edge weights Distance ± (u,v) is the minimum path length on any path u to v

Variants Single-Source Shortest-Path (SSSP): Shortest paths from s to all v All-Pairs Shortest-Path (APSP): Shortest paths between all u,v We will now consider SSSP Solved in unweighted graphs by BFS Convention: we don‘t allow negative weight cycles

Note on storing paths Again we store predecessors  (v), starting at NIL In the end they will form a shortest path tree

Setup We will use estimates d(v), starting from d(s)=0 and d(v)= 1 for all other v (Pessimism!) Improve estimates until tight

Relaxing an edge Basic Operation: – Relax(u,v,W) if d(v)>d(u)+W(u,v) then d(v):=d(u)+W(u,v);  (v):=u I.e., if we find a better estimate we go for it

Properties of Relaxing Every sequence of relax operations satisfies: 1.d(v) ¸  (s,v) at all time 2.Vertices with  (s,v)= 1 always have d(v)= 1 3.If s  u  v is a shortest path and (u,v) an edge and d(u)=  (s,u). Relaxing (u,v) gives d(v)=  (s,v) d(v) · d(u)+W(u,v) after relaxing =  (s,u)+W(u,v) =  (s,v) by the minimality of partial shortest paths

Observation Consider a shortest path p from v 1 to v k – All subpaths p‘ of p are also shortest! – E.g. p‘=v 4 ...  v k-34 is a shortest path Rough Idea: We should find shortest paths with fewer edges earlier – Starting with 1 edge (shortest edge from v) – Caution: cannot find shortest paths strictly in order of number of edges, just try to find all subpaths of a shortest path p before p Reason: – Never need to reconsider our decisions – `Greedy Algorithm‘

Dijkstra‘s Algorithm Solves SSSP Condition: W(u,v) ¸ 0 for all edges Idea: store vertices so that we can choose a vertex with minimal distance estimate Choose v with minimal d(v), relax all edges Until all v are processed v that has been processed will never be processed again

Data Structure: Priority Queue Store n vertices and their distance estimate d(v) Operations: – ExtractMin: Get the vertex with minimum d(v) – DecreaseKey(v,x): replace d(v) with a smaller value – Initialize – Insert(v) – Test for empty

Dijkstra‘s Algorithm Initialize  (v)=NIL for all v and d(s)=0, d(v)= 1 otherwise S= ; set of vertices processed Insert all vertices into Q (priority queue) While Q  ; – u:=ExtractMin(Q) – S:=S [ {u} – For all neighbors v of u: relax(u,v,W) relax uses DecreaseKey

Dijkstras Algorithmus

Things to do: 1.Prove correctness 2.Implement Priority Queues 3.Analyze the running time

3) Running Time n ExtractMin Operations and m DecreaseKey Operations – n vertices, m edges Their time depends on the implementation of the Priority Queue

2) There is a Priority Queue with the following running times: – Amortized time of ExtractMin is O(log n), i.e. the total time for (any) n operations is O(n log n) – Amortized time of DecreaseKey is O(1), i.e. total time for m operations O(m) With this the running time of Dijkstra is O(m + n log n)

Simple Implementation Store d(v) in an array – DecreaseKey in time O(1) – ExtractMin in time O(n) Total running time: O(n 2 ) for ExtractMin and O(m) for DecreaseKey This is good enough if G is given as adjacency matrix

1) Correctness First some observations 1.d(v) ¸  (s,v) at all times (proof by induction) 2.Hence: vertices with  (s,v)= 1 always have d(v)= 1 3.Let s  u  v be a shortest path with final edge (u,v) and let d(u)=  (s,u) (at some time). Relaxing (u,v) gives d(v)=  (s,v) 4.If at any time d(v)= 1 for all v 2 V-S, then all remaining vertices are not reachable from s

Correctness Theorem: Dijkstra‘s Algorithm terminates with d(v)=  (s,v) for all v. Proof by induction over the time a vertex is taken from the priority queue and put into S – Invariant: For all v 2 S we have d(v)=  (s,v) – In the beginning this is true since S= ;

Correctness – Have to show that the next chosen vertex has d(v) = ± (s,v) – For s this is trivially true – For v  s: Only vertices with finite d(v) are chosen, or all remaining vertices are not reachable (and have correct d(v) ) There must be a (shortest) path s to v Let p be such a path, and y the first vertex outside S on p, x its predecessor

Correctness Claim: d(y)=  (s,y), when v is chosen from Q Then: d(v) · d(y)=  (s,y) ·  (s,v). QED Proof of Claim: – d(x)=  (s,x) by induction hypothesis – edge (x,y) has been relaxed, so d(y)=  (s,y)

Correctness Hence d(v) is correct for all vertices in S In the end S=V, all distances are correct Still need to show: the predecessor tree computed is also correct