Single Source Shortest Path: The Bellman-Ford Algorithm. Slides based on Kevin Wayne / Pearson-Addison Wesley

Similar documents
Dijkstra s Algorithm. Dijkstra s algorithm is a natural, greedy approach towards

CMSC351 - Fall 2014, Homework #6

Ma/CS 6a Class 27: Shortest Paths

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24

Graphs. Part II: SP and MST. Laura Toma Algorithms (csci2200), Bowdoin College

The Shortest Path Problem. The Shortest Path Problem. Mathematical Model. Integer Programming Formulation

Solving problems on graph algorithms

Shortest Path Problem

Theory of Computing. Lecture 7 MAS 714 Hartmut Klauck

Shortest Path Algorithms

Shortest path problems

1 Dijkstra s Algorithm

Single Source Shortest Path

Shortest Paths. Nishant Mehta Lectures 10 and 11

Shortest Paths. Nishant Mehta Lectures 10 and 11

Minimum Spanning Tree

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456

Lecture 11: Analysis of Algorithms (CS ) 1

Detecting negative cycles with Tarjan s breadth-first scanning algorithm

from notes written mostly by Dr. Carla Savage: All Rights Reserved

Artificial Intelligence

Single Source Shortest Path (SSSP) Problem

Chapter 7. Network Flow. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

CS599: Convex and Combinatorial Optimization Fall 2013 Lecture 14: Combinatorial Problems as Linear Programs I. Instructor: Shaddin Dughmi

CHAPTER 13 GRAPH ALGORITHMS

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

Lecture #7. 1 Introduction. 2 Dijkstra s Correctness. COMPSCI 330: Design and Analysis of Algorithms 9/16/2014

Dijkstra s Algorithm Last time we saw two methods to solve the all-pairs shortest path problem: Min-plus matrix powering in O(n 3 log n) time and the

Dijkstra s algorithm for shortest paths when no edges have negative weight.

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Routing Protocols and the IP Layer

Outline. (single-source) shortest path. (all-pairs) shortest path. minimum spanning tree. Dijkstra (Section 4.4) Bellman-Ford (Section 4.

Lecture 18 Solving Shortest Path Problem: Dijkstra s Algorithm. October 23, 2009

Minimum Spanning Trees

All Shortest Paths. Questions from exercises and exams

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Shortest Paths D E. Shortest Paths 1

Algorithms for Data Science

CS350: Data Structures Dijkstra s Shortest Path Alg.

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

NP and computational intractability. Kleinberg and Tardos, chapter 8

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Introduction. I Given a weighted, directed graph G =(V, E) with weight function

Weighted Graph Algorithms Presented by Jason Yuan

1 i n (p i + r n i ) (Note that by allowing i to be n, we handle the case where the rod is not cut at all.)

Introduction. I Given a weighted, directed graph G =(V, E) with weight function

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

CS 561, Lecture 10. Jared Saia University of New Mexico

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Graph Algorithms. Andreas Klappenecker

Problem 1. Which of the following is true of functions =100 +log and = + log? Problem 2. Which of the following is true of functions = 2 and =3?

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Dynamic Programming II

Lecture I: Shortest Path Algorithms

CSE 502 Class 23. Jeremy Buhler Steve Cole. April BFS solves unweighted shortest path problem. Every edge traversed adds one to path length

Dynamic Programming. CSE 101: Design and Analysis of Algorithms Lecture 19

Lecture 6: Shortest distance, Eulerian cycles, Maxflow

Graphs II - Shortest paths

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs).

Introduction Single-source shortest paths All-pairs shortest paths. Shortest paths in graphs

Single Source Shortest Paths

What is a minimal spanning tree (MST) and how to find one

The Shortest Path Problem

Graph Algorithms shortest paths, minimum spanning trees, etc.

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

Graph Algorithms. A Brief Introduction. 高晓沨 (Xiaofeng Gao) Department of Computer Science Shanghai Jiao Tong Univ.

Review of course COMP-251B winter 2010

Lecture 11. Weighted Graphs: Dijkstra and Bellman-Ford

Lecture 6 Basic Graph Algorithms

Chapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Algorithms (IX) Yijia Chen Shanghai Jiaotong University

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

Algorithms on Graphs: Part III. Shortest Path Problems. .. Cal Poly CSC 349: Design and Analyis of Algorithms Alexander Dekhtyar..

Scribe from 2014/2015: Jessica Su, Hieu Pham Date: October 6, 2016 Editor: Jimmy Wu

Dynamic Programming: 1D Optimization. Dynamic Programming: 2D Optimization. Fibonacci Sequence. Crazy 8 s. Edit Distance

Chapter 6. Dynamic Programming

Fundamental Algorithms CSCI-GA /Summer Solution to Homework 8

Analysis of Algorithms, I

1 Variations of the Traveling Salesman Problem

Graph Algorithms shortest paths, minimum spanning trees, etc.

1 Dynamic Programming

Graph Algorithms (part 3 of CSC 282),

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 20. Example. Shortest Paths Definitions

Design and Analysis of Algorithms

Algorithms and Data Structures

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

SUGGESTION : read all the questions and their values before you start.

COMP251: Single source shortest paths

More Graph Algorithms: Topological Sort and Shortest Distance

Communication Networks I December 4, 2001 Agenda Graph theory notation Trees Shortest path algorithms Distributed, asynchronous algorithms Page 1

DO NOT RE-DISTRIBUTE THIS SOLUTION FILE

Data Structure Lecture#24: Graphs 2 (Chapter 11) U Kang Seoul National University

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

Arbitrage. CS 320, Fall Dr. Geri Georg, Instructor 320 Arbitrage 1

4.4 Shortest Paths. Dijkstra's algorithm implementation acyclic networks negative weights. Reference: Algorithms in Java, 4 th edition, Section 4.

Breadth-First Search, 1. Slides for CIS 675 DPV Chapter 4. Breadth-First Search, 3. Breadth-First Search, 2

Copyright 2000, Kevin Wayne 1

Graphs and Algorithms 2016

Module 27: Chained Matrix Multiplication and Bellman-Ford Shortest Path Algorithm

Design and Analysis of Algorithms

Transcription:

Single Source Shortest Path: The Bellman-Ford Algorithm Slides based on Kevin Wayne / Pearson-Addison Wesley

Single Source Shortest Path Problem Shortest path network. Directed graph G = (V, E, w). Weight w e = weight of edge e. Shortest path problem: given a starting vertex s, find shortest directed path from s to all other vertices Option #1: if all edge weights are non-negative, use Dijkstra Greedy algorithm works in O(n 2 ) or O(m log n) Option #2: if some edge weights are negative, use Bellman-Ford Runs in O(mn) time Also used to determine if there exists a negative-weight cycle Slides based on Kevin Wayne / Pearson-Addison Wesley 2

Applications Involving Shortest Paths with Negative Edge Weights Applications: Distance-vector routing protocols in networking Arbitrage scenarios in finance Situations in which it is possible to make guaranteed profit E.g. currency exchange in which you start with one currency and end up after a sequence of conversions with more money than you started with A negative cycle implies an arbitrage scenario Slides based on Kevin Wayne / Pearson-Addison Wesley 3

Bellman-Ford Algorithm Converting to Positive Weights. Couldn t we just add a sufficiently large constant to each edge weight so that they all become positive? And then back out this extra weight from each path when we re done? No the problem is that the actual shortest paths might change to the wrong answer 2 2 S T 3 3-3 Slides based on Kevin Wayne / Pearson-Addison Wesley 4

Bellman-Ford Algorithm Key Observation: If there are no negative cycles, then the shortest path from s to t is simple (no repeat nodes) and has at most n-1 edges (we will use the same observation in a different way in the Floyd- Warshall algorithm coming up next) Why? Any path with more than n-1 edges must visit at least one node twice, but the corresponding cycle can t help the shortest path (because all cycles have non-negative weight), so it could be removed and the total distance could only get better. Slides based on Kevin Wayne / Pearson-Addison Wesley 5

Bellman-Ford Algorithm Basic Idea: Iterate over the number of vertices Keep track of current shortest path (distance and parent) for each vertex For each iteration, relax all edges Consider whether this edge can be used to improve the current shortest path of the vertex at its endpoint After k iterations, the first k steps of any shortest path are correct and will never change from that point (we ll prove shortly) Because every shortest path is simple, we know that after at most n-1 iterations, we ll have every shortest path determined Slides based on Kevin Wayne / Pearson-Addison Wesley 6

Bellman-Ford Algorithm Bellman-Ford algorithm. Complexity: O(mn) Iterate over the vertices Consider every edge during each iteration Bellman-Ford ( G=(V,E,w), s ) 1. For every vertex v 2. d[v] = 3. d[s]=0 4. For i=1 to V -1 do 5. For every edge (u,v) in E do 6. If d[v]>d[u]+w(u,v) then 7. d[v]=d[u]+w(u,v), parent[v] = u 8. Return d[], parent[] Slides based on Kevin Wayne / Pearson-Addison Wesley

Bellman-Ford Correctness Correctness Argument assuming no negative weight cycles: Proof by induction over the iterations of the algorithm Claim: Consider any vertex t and a shortest path P from s to t. Let P be defined as v 0 v 1 v 2 v k, where v 0 = s and v k = t. After n k iterations of Bellman-Ford, all vertices along the path v 0 v 1...v n have had their shortest path computed. Base case: n = 0. Shortest path for s is 0, and can t be improved by any cycles. Inductive case: Suppose true for j iterations. Then the shortest path from s to v j has been calculated, and in iteration j+1, all edges are relaxed again, in particular edge e = (v j, v j+1 ), such that the shortest path is correctly computed for v j+1. Note that by definition of the Bellman-Ford algorithm, the shortest path to a vertex can never increase from one iteration to the next, and it can never get lower than the true shortest path. Slides based on Kevin Wayne / Pearson-Addison Wesley

Bellman-Ford: Negative Cycles Negative Cycles Assuming there are no negative cycles, every shortest path is simple and contains at most n-1 edges Therefore the Bellman-Ford algorithm correctly identifies all shortest paths from source vertex s in at most n-1 iterations. As soon as you have an iteration in which nothing changes (no vertices receive improved shortest paths) the algorithm is finished nothing can change from that point. So to check for cycles, after completing n-1 iterations of Bellman- Ford, simply scan all edges one more time to see if there is a vertex that could still be improved. If so, that implies a path longer than n-1 edges to achieve the shortest path, which implies a negative cycle. Slides based on Kevin Wayne / Pearson-Addison Wesley

Bellman-Ford Algorithm Bellman-Ford algorithm. Complexity: O(mn) Iterate over the vertices Consider every edge during each iteration Bellman-Ford ( G=(V,E,w), s ) 1. For every vertex v 2. d[v] = 3. d[s]=0 4. For i=1 to V -1 do 5. For every edge (u,v) in E do 6. If d[v]>d[u]+w(u,v) then 7. d[v]=d[u]+w(u,v), parent[v] = u 8. For every edge (u,v) in E do 9. If d[v]>d[u]+w(u,v) then 10. Return NEGATIVE CYCLE 11.Return d[], parent[] Slides based on Kevin Wayne / Pearson-Addison Wesley