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

Slides:



Advertisements
Similar presentations
Fibonacci Numbers F n = F n-1 + F n-2 F 0 =0, F 1 =1 – 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 … Straightforward recursive procedure is slow! Why? How slow? Lets.
Advertisements

Advanced Algorithm Design and Analysis Jiaheng Lu Renmin University of China
Single Source Shortest Paths
November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
Introduction To Algorithms CS 445 Discussion Session 6 Instructor: Dr Alon Efrat TA : Pooja Vaswani 03/21/2005.
Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=
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)
Introduction to Algorithms 6.046J/18.401J/SMA5503
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.
CSE 780 Algorithms Advanced Algorithms Shortest path Shortest path tree Relaxation Bellman-Ford Alg.
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 Path Problems
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1.1 Data Structure and Algorithm Lecture 11 Application of BFS  Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
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.
Analysis of Algorithms CS 477/677 Shortest Paths Instructor: George Bebis Chapter 24.
1 Graph Algorithms Single source shortest paths problem Dana Shapira.
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.
SINGLE-SOURCE SHORTEST PATHS
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.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
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.
David Luebke 1 9/13/2015 CS 332: Algorithms S-S Shortest Path: Dijkstra’s Algorithm Disjoint-Set Union Amortized Analysis.
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
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.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
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.
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
1 Weighted Graphs. 2 Outline (single-source) shortest path –Dijkstra (Section 4.4) –Bellman-Ford (Section 4.6) (all-pairs) shortest path –Floyd-Warshall.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
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.
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])
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Algorithms and Data Structures Lecture XIII
Minimum Spanning Tree Shortest Paths
CS200: Algorithm Analysis
Algorithms and Data Structures Lecture XII
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
Graph Theory Dijkstra's Algorithm.
CS 3013: DS & Algorithms Shortest Paths.
Presentation transcript:

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

October 31, This Lecture Single-source shortest paths in weighted graphs Shortest-Path Problems Properties of Shortest Paths, Relaxation Dijkstra’s Algorithm Bellman-Ford Algorithm Shortest-Paths in DAG’s

October 31, Shortest Path Generalize distance to weighted setting Digraph G = (V,E) with weight function W: E  R (assigning real values to edges) Weight of path p = v 1  v 2  …  v k is Shortest path = a path of the minimum weight Applications static/dynamic network routing robot motion planning map/route generation in traffic

October 31, Shortest-Path Problems Shortest-Path problems Single-source (single-destination). Find a shortest path from a given source (vertex s) to each of the vertices. The topic of this lecture. Single-pair. Given two vertices, find a shortest path between them. Solution to single-source problem solves this problem efficiently, too. All-pairs. Find shortest-paths for every pair of vertices. Dynamic programming algorithm. Unweighted shortest-paths – BFS.

October 31, Optimal Substructure Theorem: subpaths of shortest paths are shortest paths Proof (cut and paste) if some subpath were not the shortest path, one could substitute the shorter subpath and create a shorter total path

October 31, Triangle Inequality Definition  (u,v)  weight of a shortest path from u to v Theorem  (u,v)   (u,x) +  (x,v) for any x Proof shortest path u Î v is no longer than any other path u Î v – in particular, the path concatenating the shortest path u Î x with the shortest path x Î v

October 31, Negative Weights and Cycles? Negative edges are OK, as long as there are no negative weight cycles (otherwise paths with arbitrary small “lengths” would be possible) Shortest-paths can have no cycles (otherwise we could improve them by removing cycles) Any shortest-path in graph G can be no longer than n – 1 edges, where n is the number of vertices

October 31, Relaxation For each vertex in the graph, we maintain d[v], the estimate of the shortest path from s, initialized to  at start Relaxing an edge (u,v) means testing whether we can improve the shortest path to v found so far by going through u  uv vu 2 2   Relax(u,v)  uv vu 2 2   Relax (u,v,w) if d[v] > d[u]+w(u,v)then d[v]  d[u]+w(u,v)  [v]  u

October 31, Dijkstra's Algorithm Non-negative edge weights Greedy, similar to Prim's algorithm for MST Like breadth-first search (if all weights = 1, one can simply use BFS) Use Q, priority queue keyed by d[v] (BFS used FIFO queue, here we use a PQ, which is re- organized whenever some d decreases) Basic idea maintain a set S of solved vertices at each step select "closest" vertex u, add it to S, and relax all edges from u

October 31, Dijkstra’s Pseudo Code Graph G, weight function w, root s relaxing edges

October 31, Dijkstra’s Example    s uv yx    s uv yx uv    s yx    s uv yx

October 31, Observe relaxation step (lines 10-11) setting d[v] updates Q (needs Decrease-Key) similar to Prim's MST algorithm Dijkstra’s Example (2)    uv yx    uv yx

October 31, Dijkstra’s Correctness We will prove that whenever u is added to S, d[u] =  (s,u), i.e., that d is minimum, and that equality is maintained thereafter Proof Note that  v, d[v]   (s,v) Let u be the first vertex picked such that there is a shorter path than d[u], i.e., that  d[u]   (s,u) We will show that this assumption leads to a contradiction

October 31, Dijkstra Correctness (2) Let y be the first vertex  V – S on the actual shortest path from s to u, then it must be that d[y] =  (s,y) because d[x] is set correctly for y's predecessor x  S on the shortest path (by choice of u as the first vertex for which d is set incorrectly) when the algorithm inserted x into S, it relaxed the edge (x,y), assigning d[y] the correct value

October 31, But d[u] > d[y]  algorithm would have chosen y (from the PQ) to process next, not u  Contradiction Thus d[u] =  (s,u) at time of insertion of u into S, and Dijkstra's algorithm is correct Dijkstra Correctness (3)

October 31, Dijkstra’s Running Time Extract-Min executed |V| time Decrease-Key executed |E| time Time = |V| T Extract-Min + |E| T Decrease-Key T depends on different Q implementations QT(Extract -Min) T(Decrease- Key) Total array (V)(V)  (1)  (V 2 ) binary heap  (lg V)  (E lg V) Fibonacci heap  (lg V)  (1) (amort.)  (V lgV + E)

October 31, Bellman-Ford Algorithm Dijkstra’s doesn’t work when there are negative edges: Intuition – we can not be greedy any more on the assumption that the lengths of paths will only increase in the future Bellman-Ford algorithm detects negative cycles (returns false) or returns the shortest path-tree

October 31, Bellman-Ford Algorithm Bellman-Ford(G,w,s) 01 for each v  V[G] 02 d[v]   03 d[s]  0 04  [s]  NIL 05 for i  1 to |V[G]|-1 do 06 for each edge (u,v)  E[G] do 07 Relax (u,v,w) 08 for each edge (u,v)  E[G] do 09 if d[v] > d[u] + w(u,v) then return false 10 return true

October 31, Bellman-Ford Example 5    s zy x t -4    s zy x t -4    s zy x t -4    s zy x t

October 31, Bellman-Ford Example    s zy x t -4 Bellman-Ford running time: (|V|-1)|E| + |E| =  (VE) 5

October 31, Correctness of Bellman-Ford Let  i (s,u) denote the length of path from s to u, that is shortest among all paths, that contain at most i edges Prove by induction that d[u]=  i (s,u) after the i- th iteration of Bellman-Ford Base case (i=0) trivial Inductive step (say d[u] =  i-1 (s,u)): Either  i (s,u) =  i-1 (s,u) Or  i (s,u) =  i-1 (s,z) + w(z,u) In an iteration we try to relax each edge ((z,u) also), so we will catch both cases, thus d[u] =  i (s,u)

October 31, Correctness of Bellman-Ford After n-1 iterations, d[u] =  n-1 (s,u), for each vertex u. If there is still some edge to relax in the graph, then there is a vertex u, such that  n (s,u) <  n-1 (s,u). But there are only n vertices in G – we have a cycle, and it must be negative. Otherwise, d[u]=  n-1 (s,u) =  (s,u), for all u, since any shortest path will have at most n-1 edges

October 31, Shortest-Path in DAG’s Finding shortest paths in DAG’s is much easier, because it is easy to find an order in which to do relaxations – Topological sorting! DAG-Shortest-Paths(G,w,s) 01 for each v  V[G] 02 d[v]   03 d[s]  0 04 topologically sort V[G] 05 for each vertex u, taken in topolog. order do 06 for each vertex v  Adj[u] do 07 Relax(u,v,w)

October 31, Shortest-Paths in DAG’s (2) Running time:  (V+E) – only one relaxation for each edge, V times faster than Bellman-Ford

October 31, Next Lecture Introduction to Computational Geometry