Bellman Ford.

Slides:



Advertisements
Similar presentations
Bellman-Ford algorithm
Advertisements

Single Source Shortest Paths
* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
© DEEDS 2008Graph Algorithms1 Remarks on Dijkstra  Determining the minimum marginal element (code line: v = the vertex in M 2 with minimum v.L; ) accounts.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
Length of a Path The weight (length) of a path is the sum of the weights of its edges. adcbe Path p: Edge weights: w(a, b) = 7, w(b, c) = 2, w(c,
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
CSE 780 Algorithms Advanced Algorithms Shortest path Shortest path tree Relaxation Bellman-Ford Alg.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
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 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Shortest Paths Definitions Single Source Algorithms
1 Graph Algorithms Single source shortest paths problem Dana Shapira.
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.
CS 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis.
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
David Luebke 1 9/13/2015 CS 332: Algorithms S-S Shortest Path: Dijkstra’s Algorithm Disjoint-Set Union Amortized Analysis.
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.
CS223 Advanced Data Structures and Algorithms 1 The Bellman-Ford Shortest Path Algorithm Neil Tang 03/11/2010.
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.
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.
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.
Lecture 13 Algorithm Analysis
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
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])
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
CSC317 Shortest path algorithms
Single-Source Shortest Path
Single-Source Shortest Paths
Graphs CS Data Structures Mehmet H Gunes
Π: nil d: 0 π: nil d: ∞ 2 A B -1 π: nil d: ∞ C D E 4 π: nil d: ∞ π: nil d: ∞
Algorithms and Data Structures Lecture XIII
CS200: Algorithm Analysis
Introduction to Graphs
Ellen Walker CPSC 201 Data Structures Hiram College
Minimum Spanning Trees
SINGLE-SOURCE SHORTEST PATHS
CSCE 411 Design and Analysis of Algorithms
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
CS 583 Analysis of Algorithms
Lecture 11 Topics Application of BFS Shortest Path
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Making Change Coins: 2 and
CSC 413/513: Intro to Algorithms
Lecture 13 Algorithm Analysis
CSCE 411 Design and Analysis of Algorithms
Lecture 13 Algorithm Analysis
Honors Track: Competitive Programming & Problem Solving Avoiding negative edges Steven Ge.
Shortest Path Problems
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
The Bellman-Ford Shortest Path Algorithm Neil Tang 03/27/2008
Algorithms Searching in a Graph.
Chapter 24: Single-Source Shortest Paths
Advanced Algorithms Analysis and Design
Graph Algorithms: Shortest Path
Chapter 24: Single-Source Shortest Paths
Negative-Weight edges:
CS 3013: DS & Algorithms Shortest Paths.
Single-Source Shortest Path & Minimum Spanning Trees
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Advanced Algorithms Analysis and Design
Presentation transcript:

Bellman Ford

Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible destination. Similar to BFS. We will assume either no negative-weight edges, or no reachable negative-weight cycle that is reachable from source. Algorithm will compute a shortest-path tree. Similar to BFS tree. If there is a negative weight cycle, the algorithm indicates that no solution exists. If there is no such cycle, the algorithm produces the shortest paths and their weights.

Negative Edge A negative edge is simply an edge having a negative weight. For example, the edge X-V in the graph is a negative edge. For a negative weight you could simply perform the calculation as you would have done for positive weight edges. u v 5 –2 6 –3 8 z 7 –4 2 7 9 x y

Negative Weight Cycle.. What is the shortest path between A and E? You might at first feel as if its ABCE costing 6 ( 2+1+3 ). But actually, taking a deeper look, you would observe a negative cycle, which is BCD. The weight of BCD is 1+(-4)+2 = (-1). While traversing from A to E, i could keep cycling around inside BCD to reduce my cost by 1 each time. Like, the path A(BCD)BCE costs 5 (2+(-1)+1+3). Now repeating the cycle infinite times would keep reducing the cost by 1 each time. I could achieve a negative infinite shortest path between A and E. The problem is evident for any negative cycle in a graph. Hence, whenever a negative cycle is present, the minimum weight is not defined or is negative infinity.

Bellman-Ford returns a compact representation of the set of shortest paths from s to all other vertices in the graph reachable from s. This is contained in the predecessor subgraph. If Bellman-Ford has not converged after V(G) - 1 iterations, then there cannot be a shortest path tree, so there must be a negative weight cycle.

Can have negative-weight edges. Will “detect” reachable negative-weight cycles.

Example u v 5   –2 6 –3 8 z 7 –4 2 7   9 x y Order of Edges (U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X) u v 5   –2 6 Vertex D[v] ∏[v] Z Nil U ∞ V X Y –3 8 z 7 –4 2 7   9 x y

Example u v 5 6  –2 6 –3 8 z 7 –4 2 7 7  9 x y Order of Edges (U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X) u v 5 6  –2 6 Vertex D[v] ∏[v] Z Nil U 6 z V ∞ X Y –3 8 z 7 –4 2 7 7  9 x y Pass 1

Example u v 5 6  –2 6 –3 8 z 7 –4 2 7 7  9 x y Order of Edges (U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X) u v 5 6  –2 6 Vertex D[v] ∏[v] Z Nil U 6 z V ∞ X 7 Y –3 8 z 7 –4 2 7 7  9 x y Pass 1

Example u v 5 6 11 –2 6 –3 8 z 7 –4 2 7 7  9 x y Order of Edges (U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X) u v 5 6 11 –2 6 Vertex D[v] ∏[v] Z Nil U 6 z V 11 X 7 Y ∞ –3 8 z 7 –4 2 7 7  9 x y Pass 2

Example u v 5 6 11 –2 6 –3 8 z 7 –4 2 7 7 2 9 x y Order of Edges (U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X) u v 5 6 11 –2 6 Vertex D[v] ∏[v] Z Nil U 6 z V 11 X 7 Y 2 –3 8 z 7 –4 2 7 7 2 9 x y Pass 2

Example u v 5 6 4 –2 6 –3 8 z 7 –4 2 7 7 2 9 x y Order of Edges (U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X) u v 5 6 4 –2 6 Vertex D[v] ∏[v] Z Nil U 6 z V 4 X 7 Y 2 –3 8 z 7 –4 2 7 7 2 9 x y Pass 2

Example u v 5 2 4 –2 6 –3 8 z 7 –4 2 7 7 -2 9 x y Order of Edges (U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X) u v 5 2 4 –2 6 Vertex D[v] ∏[v] Z Nil U 2 v V 4 X 7 z Y -2 –3 8 z 7 –4 2 7 7 -2 9 x y Pass 3

Example u v 5 2 4 –2 6 –3 8 z 7 –4 2 7 7 -2 9 x y Order of Edges (U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X) u v 5 2 4 –2 6 Vertex D[v] ∏[v] Z Nil U 2 v V 4 X 7 z Y -2 –3 8 z 7 –4 2 7 7 -2 9 x y Pass 4

Example u v 5 2 4 –2 6 –3 8 z 7 –4 2 7 7 -2 9 x y Order of Edges (U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X) u v “detect” reachable negative-weight cycles. 5 2 4 –2 6 Vertex D[v] ∏[v] Z Nil U 2 v V 4 X 7 z Y -2 –3 8 z 7 –4 2 7 7 -2 9 x y

Example u v 5 2 4 –2 6 –3 8 z 7 –4 2 7 7 -2 9 x y Order of Edges (U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X) u v “detect” reachable negative-weight cycles. 5 2 4 –2 6 Vertex D[v] ∏[v] Z Nil U 2 v V 4 X 7 z Y -2 –3 8 z 7 –4 2 7 7 -2 9 x y

Example u v 5 2 4 –2 6 –3 8 z 7 –4 2 7 7 -2 9 x y Order of Edges (U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X) u v “detect” reachable negative-weight cycles. 5 2 4 –2 6 Vertex D[v] ∏[v] Z Nil U 2 v V 4 X 7 z Y -2 –3 8 z 7 –4 2 7 7 -2 9 x y

Example u v 5 2 4 –2 6 –3 8 z 7 –4 2 7 7 -2 9 x y Order of Edges (U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X) u v “detect” reachable negative-weight cycles. 5 2 4 –2 6 Vertex D[v] ∏[v] Z Nil U 2 v V 4 X 7 z Y -2 –3 8 z 7 –4 2 7 7 -2 9 x y

Example u v 5 2 4 –2 6 –3 8 z 7 –4 2 7 7 -2 9 x y Order of Edges (U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X) u v “detect” reachable negative-weight cycles. 5 2 4 –2 6 Vertex D[v] ∏[v] Z Nil U 2 v V 4 X 7 z Y -2 –3 8 z 7 –4 2 7 7 -2 9 x y

Example u v 5 2 4 –2 6 –3 8 z 7 –4 2 7 7 -2 9 x y Order of Edges (U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X) u v “detect” reachable negative-weight cycles. 5 2 4 –2 6 Vertex D[v] ∏[v] Z Nil U 2 v V 4 X 7 z Y -2 –3 8 z 7 –4 2 7 7 -2 9 x y

Example u v 5 2 4 –2 6 –3 8 z 7 –4 2 7 7 -2 9 x y Order of Edges (U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X) u v “detect” reachable negative-weight cycles. 5 2 4 –2 6 Vertex D[v] ∏[v] Z Nil U 2 v V 4 X 7 z Y -2 –3 8 z 7 –4 2 7 7 -2 9 x y

Example u v 5 2 4 –2 6 –3 8 z 7 –4 2 7 7 -2 9 x y Order of Edges (U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X) u v “detect” reachable negative-weight cycles. 5 2 4 –2 6 Vertex D[v] ∏[v] Z Nil U 2 v V 4 X 7 z Y -2 –3 8 z 7 –4 2 7 7 -2 9 x y

Example u v 5 2 4 –2 6 –3 8 z 7 –4 2 7 7 -2 9 x y Order of Edges (U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X) u v “detect” reachable negative-weight cycles. 5 2 4 –2 6 Vertex D[v] ∏[v] Z Nil U 2 v V 4 X 7 z Y -2 –3 8 z 7 –4 2 7 7 -2 9 x y

Example u v 5 2 4 –2 6 –3 8 z 7 –4 2 7 7 -2 9 x y Order of Edges (U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X) u v “detect” reachable negative-weight cycles. 5 2 4 –2 6 Vertex D[v] ∏[v] Z Nil U 2 v V 4 X 7 z Y -2 –3 8 z 7 –4 2 7 7 -2 9 x y

Example u v 5 2 4 –2 6 –3 8 z 7 –4 2 7 7 -2 9 x y Order of Edges (U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X) u v No reachable negative-weight cycles. 5 2 4 –2 6 Vertex D[v] ∏[v] Z Nil U 2 v V 4 X 7 z Y -2 –3 8 z 7 –4 2 7 7 -2 9 x y

Bellman Ford Algorithm Analysis O(V) O(E) O(VE) O(1) O(E)

Repeat V-1 times: relax all E edges. Initialize

Pass 1

Pass 1

Pass 1

Pass 1

Pass 2

Pass 2

Pass 2

Pass 3