CS330 Discussion 6.

Slides:



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

Graph Algorithms - 3 Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 13Feb 12, 2014Carnegie Mellon University.
Coloring Warm-Up. A graph is 2-colorable iff it has no odd length cycles 1: If G has an odd-length cycle then G is not 2- colorable Proof: Let v 0, …,
Graph Theory Arnold Mesa. Basic Concepts n A graph G = (V,E) is defined by a set of vertices and edges v3 v1 v2Vertex (v1) Edge (e1) A Graph with 3 vertices.
* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
The Greedy Approach Chapter 8. The Greedy Approach It’s a design technique for solving optimization problems Based on finding optimal local solutions.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
 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.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
1 Spanning Trees Lecture 20 CS2110 – Spring
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
Floyd’s Algorithm (shortest-path problem) Section 8.2.
Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Dynamic Programming is a general algorithm design technique Dynamic Programming is a.
Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the path begins is the source vertex.
1 Advanced Algorithms All-pairs SPs DP algorithm Floyd-Warshall alg.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Algorithms All pairs shortest path
More Dynamic Programming Floyd-Warshall Algorithm.
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
Prims’ spanning tree algorithm Given: connected graph (V, E) (sets of vertices and edges) V1= {an arbitrary node of V}; E1= {}; //inv: (V1, E1) is a tree,
1 The Floyd-Warshall Algorithm Andreas Klappenecker.
Introduction to Algorithms Jiafen Liu Sept
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.
All-Pairs Shortest Paths
All-pairs Shortest paths Transitive Closure
Shortest Paths.
CSC317 Shortest path algorithms
All-Pairs SPs on DG Run Dijkstra;s algorithm for each vertex or
Parallel Graph Algorithms
Shortest Path Problems
Shortest Path Problems
Shortest Path Graph represents highway system Edges have weights
Data Structures and Algorithms
Dynamic Programming Characterize the structure (problem state) of optimal solution Recursively define the value of optimal solution Compute the value of.
Unit-5 Dynamic Programming
Shortest Paths.
All-pairs Shortest Path
All-pairs shortest path
CS200: Algorithm Analysis
Analysis and design of algorithm
Unit 4: Dynamic Programming
CSE 373: Data Structures and Algorithms
Chapter 13 Graph Algorithms
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Shortest Path Problems
Dynamic Programming Characterize the structure (problem state) of optimal solution Recursively define the value of optimal solution Compute the value of.
Floyd-Warshall Algorithm
Shortest Path Algorithms
Analysis of Algorithms
Floyd’s Algorithm (shortest-path problem)
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Advanced Algorithms Analysis and Design
Lecture 13 Algorithm Analysis
All pairs shortest path problem
Dynamic Programming.
Algorithms (2IL15) – Lecture 7
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Shortest Path Problems
CSE 373 Data Structures and Algorithms
Shortest Path Problems
Shortest Paths.
Parallel Graph Algorithms
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Prims’ spanning tree algorithm
Analysis of Algorithms
All-Pairs Shortest Paths
ADVANCED COMPUTATIONAL MODELS AND ALGORITHMS
COSC 3101A - Design and Analysis of Algorithms 12
Data Structures and Algorithms
Presentation transcript:

CS330 Discussion 6

Shortest path In lecture, the following problem was discussed: Input: A graph 𝐺(𝑉,𝐸), with edge weights 𝑐 𝑒 , and 𝑠, 𝑡∈𝑉 Output: The shortest path (length) from 𝑠 to 𝑡 in 𝐺 and Dijkstra’s algorithm was found to be the optimal algorithm. If instead we want to find the shortest path from 𝑠 to all vertices, we can still use Dijkstra’s. However, if we want to find the shortest path from all vertices to all vertices, the problem becomes much more difficult.

All pairs’ shortest path (APSP) The problem is defined as follows: Input: A graph 𝐺(𝑉,𝐸), with edge weights 𝑐 𝑒 Output: For all 𝑠, 𝑡 pairs, the shortest path (length) from 𝑠 to 𝑡 in 𝐺 To simplify the algorithm, we will also require that no negative cycles exist in the graph. To solve this, we’ll use dynamic programming.

Breaking down the problem Suppose we know the shortest paths from all 𝑖∈𝑉 to all 𝑗∈𝑉, which don’t use 𝑘 as an intermediate vertex. That is, if the shortest path from 𝑖 to 𝑗 uses 𝑘 we don’t know that path, but if the second shortest path doesn’t use 𝑘, we know that path. We also know the shortest path from any vertex to 𝑘 and from 𝑘 to any vertex, since in neither case is 𝑘 is an intermediate vertex.

Breaking down the problem Suppose we know the shortest paths from all 𝑖∈𝑉 to all 𝑗∈𝑉, which don’t use 𝑘 as an intermediate vertex. There’s two cases to consider for each 𝑖, 𝑗 pair: 𝑘 is on the shortest path from 𝑖 to 𝑗. What is the shortest 𝑖, 𝑗 path length here? 𝑘 isn’t on the shortest path from 𝑖 to 𝑗. What is the shortest 𝑖, 𝑗 path length here? Once we know these two path lengths, which do we pick?

Generalization Let’s generalize the previous problem – suppose we know all shortest paths for all 𝑖, 𝑗 pairs using only intermediate vertices in 𝑆, a subset of 𝑉, and we want to know all the shortest paths using only intermediate vertices in 𝑆∪{𝑘}. Again, there are two cases: The shortest path using only 𝑆∪ 𝑘 contains 𝑘. What is the shortest 𝑖, 𝑗 path length using only 𝑆∪ 𝑘 in this case? The shortest path using only 𝑆∪{𝑘} doesn’t contain 𝑘. What is the shortest 𝑖, 𝑗 path length using only 𝑆∪ 𝑘 in this case?

Recursive formulation Let us define an arbitrary order on the vertices, i.e. let 𝑉= {1, 2, 3, …𝑛}. Let 𝑓(𝑖, 𝑗, 𝑘) be the shortest path length from 𝑖 to 𝑗 using only intermediate vertices {1, 2, 3, …𝑘}. Then: If 𝑘 is on the shortest path, 𝑓 𝑖, 𝑗, 𝑘 =𝑓 𝑖, 𝑘, 𝑘−1 +𝑓(𝑘, 𝑗, 𝑘−1) Else, 𝑓 𝑖, 𝑗, 𝑘 =𝑓(𝑖, 𝑗, 𝑘−1) Equivalently, 𝑓 𝑖, 𝑗, 𝑘 = min 𝑓 𝑖,𝑗,𝑘−1 , 𝑓 𝑖, 𝑘, 𝑘−1 +𝑓 𝑘, 𝑗, 𝑘−1 In what order should we solve the subproblems? (i.e. how should we iterate over 𝑖,𝑗,𝑘?

Base cases For the base cases – if we can’t use any intermediate vertices, then what is the shortest path from 𝑖 to 𝑗? What’s the shortest path from a vertex to itself?

First version def APSP(G): SP = 3-D array indexed 1 to n, 1 to n, 0 to n For each vertex i: SP[i][i][0] = 0 For each edge (i, j): SP[i][j][0] = e(i,j) For k from 1 to n: For i from 1 to n: For j from 1 to n: SP[i][j][k] = min(SP[i][j][k-1], SP[i][k][k-1]+SP[k][j][k-1]) Return SP[1:n][1:n][n]

Analysis Our DP table is clearly size 𝑂( 𝑛 3 ), and each update takes 𝑂(1) time, so the runtime is simply 𝑂( 𝑛 3 ). This runtime is pretty good – there are various improvements, but they come with nuances. However, we can very easily improve on the spatial complexity.

Improving the algorithm One nice property of this algorithm: If we compute in increasing 𝑘 order, once we compute 𝑓(𝑖, 𝑗,𝑘), we don’t need to store 𝑓(𝑖, 𝑗, 𝑘− 1). We won’t use 𝑓(𝑖, 𝑗, 𝑘−1) to compute any 𝑓 𝑢, 𝑣, 𝑘+1 , 𝑓 𝑢, 𝑣, 𝑘+2 … because the difference in the third argument is 2 or greater. We won’t use 𝑓(𝑖, 𝑗, 𝑘−1) to compute any 𝑓(𝑢, 𝑣, 𝑘) unless 𝑖=𝑘 or 𝑗=𝑘, in which case 𝑓(𝑖,𝑗,𝑘) and 𝑓(𝑖, 𝑗, 𝑘−1) are the same. Thus, at any stage in the algorithm, we only need to store 𝑓(𝑖, 𝑗, 𝑘) for the most recent 𝑘 value.

Second version def APSP(G): SP = 2-D array indexed 1 to n, 1 to n For each vertex i: SP[i][i] = 0 For each edge (i, j): SP[i][j] = e(i,j) For k from 1 to n: For i from 1 to n: For j from 1 to n: SP[i][j]= min(SP[i][j], SP[i][k]+SP[k][j]) Return SP

Improvements Now, we only require 𝑂( 𝑛 2 ) space at no cost to correctness or runtime. This is the Floyd-Warshall algorithm. The algorithm was developed in 1962, and until 2014, all improvements on it required the graph to hold specific properties.