Graph Algorithms - 3 Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 13Feb 12, 2014Carnegie Mellon University.

Slides:



Advertisements
Similar presentations
Bellman-Ford algorithm
Advertisements

and 6.855J Cycle Canceling Algorithm. 2 A minimum cost flow problem , $4 20, $1 20, $2 25, $2 25, $5 20, $6 30, $
1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 19 Prof. Erik Demaine.
Introduction to Algorithms 6.046J/18.401J/SMA5503
Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University.
Problem solving with graph search
Graphs - II Algorithms G. Miller V. Adamchik CS Spring 2014 Carnegie Mellon University.
Graph Algorithms - 4 Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 14Feb 14, 2014Carnegie Mellon University.
Epp, section 10.? CS 202 Aaron Bloomfield
 Theorem 5.9: Let G be a simple graph with n vertices, where n>2. G has a Hamilton circuit if for any two vertices u and v of G that are not adjacent,
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 13 Instructor: Paul Beame.
1 Chapter 6 Dynamic Programming Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Single Source Shortest Paths
all-pairs shortest paths in undirected graphs
More on Dynamic Programming Bellman-FordFloyd-Warshall Wednesday, July 30 th 1.
CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
CSE 101- Winter ‘15 Discussion Section January 26th 2015.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 8a-ShortestPathsMore Shortest Paths in a Graph (cont’d) Fundamental Algorithms.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Shortest Paths Definitions Single Source Algorithms
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
All-Pairs Shortest Paths
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
The Shortest Path Problem
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Dijkstra's algorithm.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Data Structures Week 9 Towards Weighted BFS So, far we have measured d s (v) in terms of number of edges in the path from s to v. Equivalent to assuming.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Shortest Paths.
Lecture 13 Shortest Path.
CSC317 Shortest path algorithms
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
All-Pairs Shortest Paths (26.0/25)
Shortest Paths.
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
CSE 373: Data Structures and Algorithms
Chapter 13 Graph Algorithms
Shortest Path Algorithms
Slide Courtesy: Uwash, UT
Honors Track: Competitive Programming & Problem Solving Avoiding negative edges Steven Ge.
CSE 373: Data Structures and Algorithms
Algorithms (2IL15) – Lecture 7
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Algorithms: Design and Analysis
Chapter 24: Single-Source Shortest Paths
CSE332: Data Abstractions Lecture 17: Shortest Paths
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
Shortest Paths.
Chapter 24: Single-Source Shortest Paths
CSE 417: Algorithms and Computational Complexity
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Directed Graphs (Part II)
Data Structures and Algorithm Analysis Lecture 8
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Graph Algorithms - 3 Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 13Feb 12, 2014Carnegie Mellon University

The Shortest Path Problem

S Given a positively weighted graph G with a source vertex s, find the shortest path from s to all other vertices in the graph.

Greedy approach When algorithm proceeds all vertices are divided into two groups - vertices whose shortest path from the source is known - vertices whose shortest path from the source is NOT known Move vertices (shortest distance) one at a time from the unknown set to the known set.

The Shortest Path Problem s  Maintain a PQ of distances from the source to a vertex

Complexity Let D(v) denote a length from the source s to vertex v. We store distances D(v) in a PQ. INIT: D(s) = 0; D(v)=  LOOP: Delete a node v from PQ using deleteMin() Update D(w) for all w in adj(v) using decreaseKey() D(w) = min[D(w), D(v) + c(v, w)] O(V) O(log V) PQ has V vertices We do O(E) updates O (V Log V + E log V)

Assume that a unsorted array is used instead of a priority queue. What would the algorithm's running time in this case?

PQ is a linear array findMin takes O(V) - for one vertex findMin takes O(V 2 ) - for all vertices Updating takes O(1) - for one edge total edge adjustment O(E) the algorithm running time O(E + V 2 )

Why Dijkstra’s algorithm does not work on graphs with negative weights? 5 BX SA

The Bellman-Ford algorithm (1958) repeat V - 1 times: for all e in E: update(e)

The Bellman-Ford Algorithm for (k = 0; k < V; k++) dist[k] = INFINITY; Queue q = new Queue(); dist[s] = 0; q.enqueue(s); while (!q.isEmpty()) { v = q.dequeue(); for each w in adj(v) do if (dist[w] > dist[v] + weight[v,w]) { dist[w] = dist[v] + weight[v,w]; if (!q.isInQueue(w)) q.enqueue(w); }}

What is the worst-case complexity of the Bellman-Ford algorithm? for (k = 0; k < V; k++) dist[k] = INFINITY; Queue q = new Queue(); dist[s] = 0; q.enqueue(s); while (!q.isEmpty()) { v = q.dequeue(); for each w in adj(v) do if (dist[w] > dist[v] + weight[v,w]) { dist[w] = dist[v] + weight[v,w]; if (!q.isInQueue(w)) q.enqueue(w); }} V E

Graph with a negative cycle? 4 BX SA How would you apply the Bellman- Ford algorithm to find out if a graph has a negative cycle?

Do not stop after V-1 iteration, perform one more round. If there is such a cycle, then some distance will be reduced…

Bellman-Ford Dynamic programming approach We will be counting the number of edges in the shortest path

Dynamic programming approach For each node, find the length of the shortest path to t that uses at most 1 edge, or write down ∞ if there is no such path. Suppose for all v we have solved for length of the shortest path to t that uses k − 1 or fewer edges. How can we use this to solve for the shortest path that uses k or fewer edges? We go to some neighbor x of v, and then take the shortest path from x to t that uses k−1 or fewer edges.

All-Pairs Shortest Paths (APSP) Given a weighted graph, find a shortest path from any vertex to any other vertex. Note, no distinguished vertex

All-Pairs Shortest Paths One approach: run Dijkstra's algorithm using every vertex as a source. Complexity: O(V E Log V) But what about negative weights… sparse: O(V 2 Log V) dense: O(V 3 Log V)

APSP: Bellman-Ford’s Complexity : O(V 2 E) Note, for a dense graph we have O(V 4 ).

APSP : Dynamic programming approach Floyd-Warshall, O(V 3 ) We won’t discuss it…

APSP: Johnson’s algorithm Complexity: O(V E + V E log V) for a dense graph -- O(V 3 log V). for a sparse graph -- O(V 2 log V).

Johnson’s Algorithm It improves the runtime only when a graph has negative weights. A bird’s view: - Reweight the graph, so all weights are nonnegative (by running Bellman-Ford’s) - Run Dijkstra’s on all vertices Complexity: O(V E + V E log V)

Johnson’s Algorithm: intuition The way to improve the runtime is to run Dijkstra’s from each vertex. But Dijkstra’s does not work on negative edges. So what about if we change the edge weight to be nonnegative? We have to be careful on changing the edge weight… to preserve the shortest path

Wrong reweighting (adding the fix amount) 2 BX SA C -3 The actual shortest path to X is S-B-C-X Let us add 3 to all edges

Wrong reweighting (adding the fix amount) BX SA 5 6 C 5 06 Shortest path to X is S-A-X

Adding the fix amount does not work, since every shortest path has a different number of edges

Johnson’s Algorithm: reweighting Every edge (v, u) with the cost w(v, u) is replaced by w * (v, u) = w(v, u) + p(v) – p(u) where p(v) will be decided later. u w(v, u) = 2 v p(v)=-2 p(u)=1 w * (v, u) = 2 + (-2) – 1 = - 1

Johnson’s Algorithm: reweighting Theorem. All paths between the same two vertices are reweighted by the same amount. Proof. Consider path v = v 1  v 2  …  v n = u Then we have w * (v,u) = w * (v 1, v 2 ) + … + w * (v n-1, v n ) = w(v 1, v 2 ) + p(v 1 ) – p(v 2 ) + w(v 2, v 3 ) + p(v 2 ) – p(v 3 ) + … w * (v, u) = w(v, u) + p(v) – p(u) Telescoping sum

Johnson’s reweighting changes any path between u and v by the same amount and therefore preserves the shortest path unchanged w * (v, u) = w(v, u) + p(v) – p(u)

Find vertex labeling P(v) c a x b 4 -2 y z First we need to create a new vertex and connect it to all other vertices with zero weight. s Note this change in the graph won’t change the shortest distances between vertices.

Running SSSP c a x b 4 -2 y z s Next we run Bellman-Ford’s starting at vertex s. Shortest Path s-a is 0 s-b is s-c is and so on… 0 -6 Now we define p(v) as the shortest distance s-v.

Johnson’s Reweighting c a x b 4 -2 y z Here we redraw the example by using s w * (v, u) = w(v, u) + p(v) – p(u) Edge (a,b): -2+0-(-2) = Edge (b,c): -1+(-2)-(-3) = 0 0 Edge (z,x): 1+0-(-1) =

New graph c a x b y z After Johnson’s reweighting we get a new graph with non-negative weights Remember, Johnson’s reweighting preserves the shortest path. Now we can use Dijkstra’s

Johnson’s Algorithm: reweighting Theorem. After reweighting every edge has a nonnegative cost. Proof.Consider edge (v, u) p(v) is the shortest distance from s to v w * (v, u) = w(v, u) + p(v) – p(u) p(u) is the shortest distance from s to u p(u) ≤ p(v) + w(v, u) vu s since the shortest path s-u cannot be longer then p(v) + (v, u). QED

Johnson’s Algorithm 1. Add a new vertex s and connect it with all other vertices. 2. Run Bellman-Ford’s algorithm from s to compute p(v). Note that Bellman-Ford’s algorithm will correctly report if the original graph has a negative cost cycle. 3. Reweight all edges: w * (v,u) =w(v,u)+p(v)–p(u) 4. Run Dijkstra’s algorithm from all vertices 5. Compute the actual distances by subtracting p(v)–p(u)

Complexity 1. Add a new vertex s and connect it with all other vertices. 2. Run Bellman-Ford’s algorithm from s to compute p(v). 3. Reweight all edges: w * (v,u) =w(v,u)+p(v)–p(u) 4. Run Dijkstra’s algorithm from all vertices 5. Compute the actual distances by subtracting p(v)–p(u) O(E) O(V) O(V E) O(E) O(V E log V) Total: O(V E log V)

Johnson’s Algorithm It shines for sparse graphs with negative edges O(V 2 log V) Better than Floyd-Warshall, which is O(V 3 )