Single-Source All-Destinations Shortest Paths With Negative Costs

Slides:



Advertisements
Similar presentations
Bellman-Ford algorithm
Advertisements

1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 19 Prof. Erik Demaine.
Single Source Shortest Paths
1 Appendix B: Solving TSP by Dynamic Programming Course: Algorithm Design and Analysis.
CS138A Single Source Shortest Paths Peter Schröder.
 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
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
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.
Single-Source All-Destinations Shortest Paths With General Weights Directed weighted graph. Edges may have negative cost. No cycle whose cost is < 0.
Shortest Paths Definitions Single Source Algorithms
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.
All-Pairs Shortest Paths Given an n-vertex directed weighted graph, find a shortest path from vertex i to vertex j for each of the n 2 vertex pairs (i,j).
Discrete Math for CS Chapter 8: Directed Graphs. Discrete Math for CS digraph: A digraph is a graph G = (V,E) where V is a finite set of vertices and.
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
Using Dijkstra’s Algorithm to Find a Shortest Path from a to z 1.
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.
Introduction to Algorithms Jiafen Liu Sept
1 Chapter 6 : Graph – Part 2 교수 : 이상환 강의실 : 113,118 호, 324 호 연구실 : 과학관 204 호 Home :
Shortest Paths.
Lecture 13 Shortest Path.
CSC317 Shortest path algorithms
Algorithm Analysis Fall 2017 CS 4306/03
Introduction to Graphs
Parallel Graph Algorithms
Shortest Path Problems
Shortest Path Problems
All-Pairs Shortest Paths (26.0/25)
Single-Source All-Destinations Shortest Paths With Negative Costs
Greedy Technique.
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.
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
CS223 Advanced Data Structures and Algorithms
Single-Source All-Destinations Shortest Paths With Negative Costs
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Autumn 2015 Lecture 10 Minimum Spanning Trees
Shortest Path Problems
Dynamic Programming Characterize the structure (problem state) of optimal solution Recursively define the value of optimal solution Compute the value of.
Shortest Path Algorithms
Floyd’s Algorithm (shortest-path problem)
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
All pairs shortest path problem
Algorithms (2IL15) – Lecture 7
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Chapter 24: Single-Source Shortest Paths
CS 584 Project Write up Poster session for final Due on day of final
Shortest Path Problems
Autumn 2016 Lecture 10 Minimum Spanning Trees
Graphs G = (V, E) V are the vertices; E are the edges.
Dijkstra’s Shortest Path Algorithm
Shortest Path Problems
Shortest Paths.
Chapter 24: Single-Source Shortest Paths
Parallel Graph Algorithms
Winter 2019 Lecture 10 Minimum Spanning Trees
Chapter 24: Single-Source Shortest-Path
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Single-Source All-Destinations Shortest Paths With Negative Costs
All-Pairs Shortest Paths
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Single-Source All-Destinations Shortest Paths With Negative Costs Directed weighted graph. Edges may have negative cost. No cycle whose cost is < 0. Find a shortest path from a given source vertex s to each of the n vertices of the digraph.

Single-Source All-Destinations Shortest Paths With Negative Costs Dijkstra’s O(n2) single-source greedy algorithm doesn’t work when there are negative-cost edges. Floyd’s Theta(n3) all-pairs dynamic-programming algorithm does work in this case.

Bellman-Ford Algorithm Single-source all-destinations shortest paths in digraphs with negative-cost edges. Uses dynamic programming. Runs in O(n3) time when adjacency matrices are used. Runs in O(ne) time when adjacency lists are used.

Decision Sequence s w v To construct a shortest path from the source to vertex v, decide on the max number of edges on the path and on the vertex that comes just before v. Since the digraph has no cycle whose length is < 0, we may limit ourselves to the discovery of cycle-free (acyclic) shortest paths. A path that has no cycle has at most n-1 edges.

Problem State s w v Problem state is given by (u,k), where u is the destination vertex and k is the max number of edges. (v,n-1) is the state in which we want the shortest path to v that has at most n-1 edges.

Cost Function s w v Let d(v,k) be the length of a shortest path from the source vertex to vertex v under the constraint that the path has at most k edges. d(v,n-1) is the length of a shortest unconstrained path from the source vertex to vertex v. We want to determine d(v,n-1) for every vertex v.

Value Of d(*,0) d(v,0) is the length of a shortest path from the source vertex to vertex v under the constraint that the path has at most 0 edges. s d(s,0) = 0. d(v,0) = infinity for v != s.

Recurrence For d(*,k), k > 0 d(v,k) is the length of a shortest path from the source vertex to vertex v under the constraint that the path has at most k edges. If this constrained shortest path goes through no edge, then d(v,k) = d(v,0).

Recurrence For d(*,k), k > 0 If this constrained shortest path goes through at least one edge, then let w be the vertex just before v on this shortest path (note that w may be s). s w v We see that the path from the source to w must be a shortest path from the source vertex to vertex w under the constraint that this path has at most k-1 edges. d(v,k) = d(w,k-1) + length of edge (w,v).

Recurrence For d(*,k), k > 0 d(v,k) = d(w,k-1) + length of edge (w,v). s w v We do not know what w is. We can assert d(v,k) = min{d(w,k-1) + length of edge (w,v)}, where the min is taken over all w such that (w,v) is an edge of the digraph. Combining the two cases considered yields: d(v,k) = min{d(v,0), min{d(w,k-1) + length of edge (w,v)}}

Pseudocode To Compute d(*,*) // initialize d(*,0) d(s,0) = 0; d(v,0) = infinity, v != s; // compute d(*,k), 0 < k < n for (int k = 1; k < n; k++) { d(v,k) = d(v,0), 1 <= v <= n; for (each edge (u,v)) d(v,k) = min{d(v,k), d(u,k-1) + cost(u,v)} } Note d(v,0) is infinity except when v = s.

p(*,*) Let p(v,k) be the vertex just before vertex v on the shortest path for d(v,k). p(v,0) is undefined. Used to construct shortest paths.

Example 1 2 4 3 6 5 7 -6 9 1 Source vertex is 1.

Example v - - - - - - - - - - - k - - - - - - - 3 7 1 1 - 3 7 7 16 8 1 2 4 3 6 5 7 -6 9 1 1 2 3 4 5 6 v - - - - - - - - - - - k - - - - - - - 1 3 7 1 1 - 3 7 7 16 8 1 2 1 4 4 2 - 3 2 7 7 10 8 6 2 1 3 4 - 4 2 6 7 10 8 6 2 1 3 4 d(v,k) p(v.k)

Example v - 2 6 7 10 8 6 2 1 3 4 k - 2 6 7 9 8 6 2 1 3 4 d(v,k) p(v.k) 5 7 -6 9 1 1 2 3 4 5 6 v 4 - 2 6 7 10 8 6 2 1 3 4 k - 5 2 6 7 9 8 6 2 1 3 4 d(v,k) p(v.k)

Shortest Path From 1 To 5 - 2 6 7 9 8 6 2 1 3 4 p(v,5) d(v,5) 1 -6 3 4 2 6 7 9 8 6 2 1 3 4 p(v,5) d(v,5)

Observations d(v,k) = min{d(v,0), min{d(w,k-1) + length of edge (w,v)}} d(s,k) = 0 for all k. If d(v,k) = d(v,k-1) for all v, then d(v,j) = d(v,k-1), for all j >= k-1 and all v. If we stop computing as soon as we have a d(*,k) that is identical to d(*,k-1) the run time becomes O(n3) when adjacency matrix is used. O(ne) when adjacency lists are used.

Observations The computation may be done in-place. d(v) = min{d(v), min{d(w) + length of edge (w,v)}} instead of d(v,k) = min{d(v,0), min{d(w,k-1) + length of edge (w,v)}} Following iteration k, d(v,k+1) <= d(v) <= d(v,k) On termination d(v) = d(v,n-1). Space requirement becomes O(n) for d(*) and p(*).