Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 16 All shortest paths algorithms Properties of all shortest paths Simple algorithm:

Slides:



Advertisements
Similar presentations
1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 19 Prof. Erik Demaine.
Advertisements

Great Theoretical Ideas in Computer Science
NP-Hard Nattee Niparnan.
Approximation Algorithms
22C:19 Discrete Math Graphs Fall 2010 Sukumar Ghosh.
22C:19 Discrete Math Graphs Fall 2014 Sukumar Ghosh.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Advanced Algorithm Design and Analysis (Lecture 7) SW5 fall 2004 Simonas Šaltenis E1-215b
CS138A Single Source Shortest Paths Peter Schröder.
 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation.
1 Chapter 26 All-Pairs Shortest Paths Problem definition Shortest paths and matrix multiplication The Floyd-Warshall algorithm.
Lecture 17 Path Algebra Matrix multiplication of adjacency matrices of directed graphs give important information about the graphs. Manipulating these.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 25: All-Pairs Shortest-Paths
Management Science 461 Lecture 2b – Shortest Paths September 16, 2008.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
 2004 SDU Lecture11- All-pairs shortest paths. Dynamic programming Comparing to divide-and-conquer 1.Both partition the problem into sub-problems 2.Divide-and-conquer.
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lectures 3 Tuesday, 9/25/01 Graph Algorithms: Part 1 Shortest.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Dynamic Programming is a general algorithm design technique Dynamic Programming is a.
Lecture 22: Matrix Operations and All-pair Shortest Paths II Shang-Hua Teng.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
CSC401 – Analysis of Algorithms Lecture Notes 12 Dynamic Programming
1 Advanced Algorithms All-pairs SPs DP algorithm Floyd-Warshall alg.
Algorithms All pairs shortest path
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
1 Dynamic programming algorithms for all-pairs shortest path and longest common subsequences We will study a new technique—dynamic programming algorithms.
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
22C:19 Discrete Math Graphs Spring 2014 Sukumar Ghosh.
Programming & Data Structures
MCS312: NP-completeness and Approximation Algorithms
Nattee Niparnan. Easy & Hard Problem What is “difficulty” of problem? Difficult for computer scientist to derive algorithm for the problem? Difficult.
Chapter 5 Dynamic Programming 2001 년 5 월 24 일 충북대학교 알고리즘연구실.
Chapter 2 Graph Algorithms.
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
1 ELEC692 Fall 2004 Lecture 1b ELEC692 Lecture 1a Introduction to graph theory and algorithm.
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 9 (Part 2): Graphs  Graph Terminology (9.2)
Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
All-Pairs Shortest Paths & Essential Subgraph 01/25/2005 Jinil Han.
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.
1 The Floyd-Warshall Algorithm Andreas Klappenecker.
All-pairs Shortest Paths. p2. The structure of a shortest path: All subpaths of a shortest path are shortest paths. p : a shortest path from vertex i.
Parallel Programming: All-Pairs Shortest Path CS599 David Monismith Based upon notes from multiple sources.
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
The all-pairs shortest path problem (APSP) input: a directed graph G = (V, E) with edge weights goal: find a minimum weight (shortest) path between every.
Data Structures & Algorithms Graphs
All-Pairs Shortest Paths
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
Chapter 9: Graphs.
Introduction to Graph Theory By: Arun Kumar (Asst. Professor) (Asst. Professor)
Introduction to NP Instructor: Neelima Gupta 1.
Chapter 9 (Part 2): Graphs
Minimum Spanning Tree 8/7/2018 4:26 AM
Parameterised Complexity
Data Structures – LECTURE 13 Minumum spanning trees
Discrete Math II Howon Kim
Floyd-Warshall Algorithm
3. Brute Force Selection sort Brute-Force string matching
Advanced Algorithms Analysis and Design
All pairs shortest path problem
3. Brute Force Selection sort Brute-Force string matching
Text Book: Introduction to algorithms By C L R S
Negative-Weight edges:
Applied Discrete Mathematics Week 13: Graphs
Agenda Review Lecture Content: Shortest Path Algorithm
COSC 3101A - Design and Analysis of Algorithms 12
3. Brute Force Selection sort Brute-Force string matching
Presentation transcript:

Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 16 All shortest paths algorithms Properties of all shortest paths Simple algorithm: O(|V| 4 ) time Better algorithm: O(|V| 3 lg |V|) time Floyd-Warshall algorithm: O(|V| 3 ) time Chapter 25 in the textbook (pp 620–635).

Data Structures, Spring 2004 © L. Joskowicz 2 All shortest paths Generalization of the single source shortest-path problem. Simple solution: run the shortest path algorithm for each vertex  complexity is O(|E|.|V|.|V|) = O(|V| 4 ) for Bellman-Ford and O(|E|.lg |V|.|V|) = O(|V| 3 lg |V|) for Dijsktra. Can we do better? Intuitively it would seem so, since there is a lot of repeated work  exploit the optimal sub-path property. We indeed can do better: O(|V| 3 ).

Data Structures, Spring 2004 © L. Joskowicz 3 All-shortest paths: example (1)

Data Structures, Spring 2004 © L. Joskowicz 4 All-shortest paths: example (2) dist pred

Data Structures, Spring 2004 © L. Joskowicz 5 All-shortest paths: example (3) dist pred

Data Structures, Spring 2004 © L. Joskowicz 6 All shortest-paths: representation We will use matrices to represent the graph, the shortest path lengths, and the predecessor sub-graphs. Edge matrix: entry (i,j) in adjacency matrix W is the weight of the edge between vertex i and vertex j. Shortest-path lengths matrix: entry (i,j) in L is the shortest path length between vertex i and vertex j. Predecessor matrix: entry (i,j) in Π is the predecessor of j on some shortest path from i (null when i = j or when there is no path).

Data Structures, Spring 2004 © L. Joskowicz 7 All-shortest paths: definitions Edge matrix: entry (i,j) in adjacency matrix W is the weight of the edge between vertex i and vertex j. Shortest-paths graph: the graphs G π,i = (V π,i, E π,i ) are the shortest-path graphs rooted at vertex I, where:

Data Structures, Spring 2004 © L. Joskowicz 8 Example: edge matrix

Data Structures, Spring 2004 © L. Joskowicz 9 Example: shortest-paths matrix

Data Structures, Spring 2004 © L. Joskowicz 10 Example: predecessor matrix

Data Structures, Spring 2004 © L. Joskowicz 11 The structure of a shortest path 1.All sub-paths of a shortest path are shortest paths. Let p = be the shortest path from v 1 to v k. The sub-path between v i and v j, where 1 ≤ i,j ≤ k, p ij = is a shortest path. 2.The shortest path from vertex i to vertex j with at most m edges is either: –the shortest path with at most (m-1) edges (no improvement) –the shortest path consisting of a shortest path within the (m-1) vertices + the weight of the edge from a vertex within the (m-1) vertices to an extra vertex m.

Data Structures, Spring 2004 © L. Joskowicz 12 Recursive solution to all-shortest paths Let l (m) ij be the minimum weight of any path from vertex i to vertex j that has at most m edges. When m=0: For m ≥ 1, l (m) ij is the minimum of l (m–1) ij and the shortest path going through the vertices neighbors:

Data Structures, Spring 2004 © L. Joskowicz 13 All-shortest-paths: solution Let W=(w ij ) be the edge weight matrix and L=(l ij ) the all-shortest shortest path matrix computed so far, both n×n. Compute a series of matrices L (1), L (2), …, L (n–1) where for m = 1,…,n–1, L (m) = (l (m) ij ) is the matrix with the all-shortest-path lengths with at most m edges. Initially, L (1) = W, and L (n–1) containts the actual shortest-paths. Basic step: compute L (m) from L (m–1) and W.

Data Structures, Spring 2004 © L. Joskowicz 14 Algorithm for extending all-shortest paths by one edge: from L (m-1) to L (m) Extend-Shortest-Paths(L=(l ij ),W) n  rows[L] Let L’ =(l’ ij ) be an n×n matrix. for i  1 to n do for j  1 to n do l’ ij  ∞ for k  1 to n do l’ ij  min(l’ ij, l ik + w kj ) return L’ Complexity: Θ(|V| 3 )

Data Structures, Spring 2004 © L. Joskowicz 15 This is exactly as matrix multiplication! Matrix-Multiply(A,B) n  rows[A] Let C =(c ij ) be an n×n matrix. for i  1 to n do for j  1 to n do c ij  0 (l’ ij  ∞) for k  1 to n do c ij  c ij + a ik.b kj (l’ ij  min(l’ ij, l ij + w kj )) return L’

Data Structures, Spring 2004 © L. Joskowicz 16 Paths with at most two edges

Data Structures, Spring 2004 © L. Joskowicz 17 Paths with at most three edges

Data Structures, Spring 2004 © L. Joskowicz 18 Paths with at most four edges

Data Structures, Spring 2004 © L. Joskowicz 19 Paths with at most five edges

Data Structures, Spring 2004 © L. Joskowicz 20 All-shortest paths algorithm All-Shortest-Paths(W) n  rows[W] L (1)  W for m  2 to n–1 do L (m)  Extend-Shortest-Paths(L (m–1),W) return L (m) Complexity: Θ(|V| 4 )

Data Structures, Spring 2004 © L. Joskowicz 21 Improved all-shortest paths algorithm The goal is to compute the final L (n–1), not all the L (m) We can avoid computing most L (m) as follows: L (1) = W L (2) = W.W L (4) = W 4 = W 2.W 2 … Since the final product is equal to L (n–1) only |lg(n–1)| iterations are necessary! repeated squaring

Data Structures, Spring 2004 © L. Joskowicz 22 Faster-all-shortest paths algorithm Faster-All-Shortest-Paths(W) n  rows[W] L (1)  W while m < n–1 do L (2m)  Extend-Shortest-Paths(L (m),L (m) ) m  2m return L (m) Complexity: Θ(|V| 3 lg (|V|))

Data Structures, Spring 2004 © L. Joskowicz 23 Floyd-Warshall algorithm Assumes there are no negative-weight cycles. Uses a different characterization of the structure of the shortest path. It exploits the properties of the intermediate vertices of the shortest path. Runs in O(|V| 3 ).

Data Structures, Spring 2004 © L. Joskowicz 24 Structure of the shortest path (1) An intermediate vertex v i of a simple path p= is any vertex other than v 1 or v k. Let V={1,2,…,n} and let K={1,2,…,k} be a subset for k ≤ n. For any pair of vertices i,j in V, consider all paths from i to j whose intermediate vertices are drawn from K. Let p be the minimum-weight path among them. i k1k1 j k2k2 p

Data Structures, Spring 2004 © L. Joskowicz 25 Structure of the shortest path (2) 1. k is not an intermediate vertex of path p: All vertices of path p are in the set {1,2,…,k–1}  a shortest path from i to j with all intermediate vertices in {1,2,…,k–1} is also a shortest path with all intermediate vertices in {1,2,…,k}. 2. k is an intermediate vertex of path p: Break p into two pieces: p 1 from i to k and p 2 from k to j. Path p 1 is a shortest path from i to k and path p 2 is a shortest path from k to j with all intermediate vertices in {1,2,…,k–1}.

Data Structures, Spring 2004 © L. Joskowicz 26 Structure of the shortest path (3) i k j p2p2 all intermediate vertices in {1,2,…,k–1} all intermediate vertices in {1,2,…,k} p1p1 all intermediate vertices in {1,2,…,k–1}

Data Structures, Spring 2004 © L. Joskowicz 27 Recursive definition Let d (k) ij be the weight of a shortest path from vertex i to vertex j for which all intermediate vertices are in the set {1,2,…,k}. Then: The matrices D (k) = (d (k) ij ) will keep the intermediate solutions.

Data Structures, Spring 2004 © L. Joskowicz 28 Example: Floyd-Warshall algorithm (1) K={1} Improvements: (4,2) and (4,5)

Data Structures, Spring 2004 © L. Joskowicz 29 Example: Floyd-Warshall algorithm (2) K={1,2} Improvements: (1,4) (3,4),(3,5)

Data Structures, Spring 2004 © L. Joskowicz 30 Example: Floyd-Warshall algorithm (3) K={1,2,3} Improvement: (4,2)

Data Structures, Spring 2004 © L. Joskowicz 31 Example: Floyd-Warshall algorithm (4) K={1,2,3,4} Improvements: (1,3),(1,4) (2,1), (2,3), (2,5) (3,1),(3,5), (5,1),(5,2),(5,3)

Data Structures, Spring 2004 © L. Joskowicz 32 Example: Floyd-Warshall algorithm (5) K={1,2,3,4,5} Improvements: (1,2),(1,3),(1,4)

Data Structures, Spring 2004 © L. Joskowicz 33 Transitive closure (1) Given a directed graph G=(V,E) with vertices V = {1,2,…,n} determine for every pair of vertices (i,j) if there is a path between them. The transitive closure graph of G, G*=(V,E*) is such that E* = {(i,j): if there is a path i and j}. Represent E* as a binary matrix and perform logical binary operations AND (/\) and OR (\/) instead of min and + in the Floyd-Warshall algorithm.

Data Structures, Spring 2004 © L. Joskowicz 34 Transitive closure (2) The definition of the transitive closure is: The matrices T (k) indicate if there is a path with at most k edges between s and i.

Data Structures, Spring 2004 © L. Joskowicz 35 Transitive closure algorithm Transitive-Closure(W) n  rows[W] T (0)  Binarize(W) for k  1 to n do for i  1 to n do for j  1 to n do return T (n) Complexity: Θ(|V| 3 )

Data Structures, Spring 2004 © L. Joskowicz 36 Summary Adjacency matrix representation is the most convenient for representing all-shortest-paths. Computing all shortest-paths is akin to taking the transitive closure of the edge weights. Matrix multiplication algorithm runs in O(|V| 3 lg |V|). The Floyd-Warshall algorithm improves paths through intermediate vertices instead of working on individual edges. Its running time: O(|V| 3 ).

Data Structures, Spring 2004 © L. Joskowicz 37 Other graph algorithms Many more interesting problems, including network flow, graph isomorphism, coloring, partition, etc. Problems can be classified by the type of solution. Easy problems: polynomial-time solutions O(f (n)) where f (n) is a polynomial function of degree at most k. Hard problems: exponential-time solutions O(f (n)) where f (n) is an exponential function, usually 2 n.

Data Structures, Spring 2004 © L. Joskowicz 38 Easy graph problems Network flow – maximum flow problem Maximum bipartite matching Planarity testing and plane embedding.

Data Structures, Spring 2004 © L. Joskowicz 39 Hard graph problems Graph and sub-graph isomorphism. Largest clique, Independent set Vertex tour (Traveling Salesman problem) Graph partition Vertex coloring However, not all is lost! Good heuristics that perform well in most cases Polynomial-time approximation algorithms