TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.

Slides:



Advertisements
Similar presentations
Advanced Algorithm Design and Analysis Jiaheng Lu Renmin University of China
Advertisements

Single Source Shortest Paths
Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.
Minimum Spanning Tree CSE 331 Section 2 James Daly.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=
 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation.
CS38 Introduction to Algorithms Lecture 5 April 15, 2014.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
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.
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.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Shortest Path Algorithms
Shortest Paths Definitions Single Source Algorithms
Minimal Spanning Trees. Spanning Tree Assume you have an undirected graph G = (V,E) Spanning tree of graph G is tree T = (V,E T E, R) –Tree has same set.
DAST 2005 Tirgul 12 (and more) sample questions. DAST 2005 Q.We’ve seen that solving the shortest paths problem requires O(VE) time using the Belman-Ford.
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
Analysis of Algorithms CS 477/677 Shortest Paths Instructor: George Bebis Chapter 24.
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.
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.
Tirgul 13 Today we’ll solve two questions from last year’s exams.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Dijkstra's algorithm.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
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.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
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.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
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.
Introduction to Algorithms Jiafen Liu Sept
Graphs David Kauchak cs302 Spring DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch.
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
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
CS38 Introduction to Algorithms Lecture 3 April 8, 2014.
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])
Shortest Paths and Minimum Spanning Trees
Lecture 13 Shortest Path.
Algorithms and Data Structures Lecture XIII
CS200: Algorithm Analysis
Minimal Spanning Trees
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
CS 583 Analysis of Algorithms
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Shortest Paths and Minimum Spanning Trees
CSC 413/513: Intro to Algorithms
Lecture 13 Algorithm Analysis
Slide Courtesy: Uwash, UT
Lecture 13 Algorithm Analysis
Fundamental Data Structures and Algorithms
CSE332: Data Abstractions Lecture 17: Shortest Paths
Slide Courtesy: Uwash, UT
Lecture 12 Shortest Path.
CS 3013: DS & Algorithms Shortest Paths.
Presentation transcript:

TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1

Weighted distance Until now we have only considered unweighted graphs. Today we will focus on weighted graphs. Reminder: a weighted graph is G=(V,E,W), where W:VxV  ℝ. For now, we will assume that for all w ∈ W, w>0 That is, every edge gets a positive number representing it’s weight.

Weighted paths Definition: the weight of a path v 1  v 2  …  v k is equal to the sum of weights: W(v 1  v 2 )+W(v 2  v 3 )+…+W(v k-1  v k ) Definition: the weighted distance between two nodes in a graph is the minimal weight of all paths between them. This is a natural extension of the notion of distance in unweighted graphs. Today we will see how to compute the weighted distance of all nodes from a given node s in a weighted graph G. Notations: (v) – v’s real weighted distance from s [v] – the algorithm’s answer regarding v’s distance from s.

Finding the minimal weighted distance In unweighted graphs we used BFS to compute the distance between a node v and all other nodes. Q: Can we use it on weighted graphs by adding up weights as we go? A: no! Example: s a b =4 = 2 =3

Insights Why does this fail? Visiting a first fails since we marked it as ‘visited’. Insight #1: a given distance cannot be fixed - we need to be able update it’s distance once we find shorter path s a b

Insights Let’s expand our example and run BFS with updates: The a  c update did not help since a’s distance was wrong at the time of update. Notice that if we first expand b and then a, it does work. s a b c

Insights Let’s expand our example and run BFS with updates: The a  c update did not help since a’s distance was wrong at the time of update. Notice that if we first expand b and then a, it does work. s a b c

Insights Insight #2: allowing distance updates is not enough – we must visit nodes in a certain order. Q: Is it true that there always exists an ordering such that BFS returns the correct weighted distances? A: YES! s a b c

Insights But what is a good order? We saw that updates can be ‘missed’ – when does this happen? Insight #3: if we went through the nodes in order of their actual distance, we would never miss an update. But finding the distance is the problem itself… s a b c

Locality and greediness Global order looks hard to find – what about local order? Insight #4: After expanding s: b is the node with the smallest current [ ] value b‘s [ ] value is equal to it’s real distance b is also the next yet to be expanded node in the ordering by If we choose b next, we won’t miss any updates! Is this always true? s a b c

Proposed ‘greedy’ solution At every step we will have: A - Nodes we’ve expanded B - Nodes we’ve visited C - Nodes we haven’t visited yet At each step we will: Expand the node v ∈ B with the current smallest [ ] Update the [ ] value of it’s neighbors from B Visit it’s neighbors from C and update their [ ] values Move v to A We’ll need to show that when a node is added to A, d=. Greedy!

Algorithm: AlmostDijkstra(G,s) For all v ≠ s [v]= ∞ [s] = 0 A =, B = {s}, C = V\S While B ≠ Choose v ∈ B with minimal [ ] For all neighbors u ∈ B,C of v [u] = min([u], [v]+W(v,u)) if u ∈ C, move u into B Move v from B to A

Proof of correctness Lemma 1: a sub-path of a shortest path is also a shortest path Proof: take p=x  …  y  …  z, p’=x  …  y. by contradiction there exists a path q from x to y with len(q)<len(p’), then the path q with y  …  z is shorter than p. This contradicts p being a shortest path. Lemma 2: for all v ∈ V, in every step, (v)<=d(v) Proof: at the begging, [s]=0=(s), and for all v ≠ s, [v] = ∞ ≥ (v). Since values are changed by updates, assume by contradiction v is the first node updated such that [v]<(v). Since v was updated (assume by u): [u]+w(u,v) = [v] < (v) ≤ (u)+w(u,v) ⇒ [u] < (u) in contradiction to v being the first with <.

Proof of correctness Induction on n=|A|. Claim: All nodes in A have a correct [ ] value Basis: n=1, A={s}, and [s] = 0 = (s). Step: we will assume for all k<n, and prove for n. Let v be the n’th node inserted into A. Let p be a true shortest weighted path between s and v. Case 1 - p ⊆ A: Before inserting v into A it’s size was less than n, so by the induction assumption for all w ∈ A, [w] = (w). Let u be the node before v in p. Since u ∈ A, by the above [u] = (u). Since v is a neighbor of u, when u was inserted into A, v’s [ ] value was updated. Since (u,v) ∈ p and p is a shortest path, after the update [v] ≤ (v). From lemma 1 [v] ≥ (v), therefore [v] = (v).

Proof of correctness Case 2 - p ⊈ A: By contradiction, assume v is inserted into A with a wrong [ ]. Let y be the first node in p that is not in A (y ≠ v, otherwise we’d be in case 1). s ∈ A, so A is not empty. Therefore, let x be y’s predecessor in p, so (x,y) ∈ E, x ∈ A. Since x ∈ A, and since x was inserted into A when it’s size was less than n, by the induction assumption it’s [x] = (x). Consider (y) and [y]. From lemma 2, (y) ≤ [y]. From lemma 1, the sub-path p’=s  …  x  y ⊈ p is a shortest path from s to x. Since all of x’s neighbors (including y) were updated when x was inserted into A, and at that time [x]=(x), after the update [y]=(y) (From lemma 1 again, any other update could not have resulted in [y]<(y) ).

Proof of correctness Since the length of p’ is less than p, since they are both shortest paths, and since w>0, we get that (y) < (v). Since from lemma 1 (v) ≤ [v], we get that: [y] = (y) < (v) ≤ [v] So [y]< [v],in contradiction to the algorithm choosing the node with the smallest [ ] value. Therefore, (v) = [v].

Choosing wisely In each step we need to choose v in B with the minimal [ ] value. Nodes are moved from C to B [ ] values of nodes are updated all the time. Q: What data structure gives these functions at a good running time? A: Priority queue! Using a binary min-heap we get: ExtractMin – O(logn) Insert – O(logn) DecreaseKey – O(logn) Notice that our algorithm is actually BFS with a priority queue (instead of a regular queue).

19 Dijkstra’s Algorithm Input: graph G=(V,E,W), directed or undirected Starting node s W consists of positive edges Output: for each node v ∈ V: It’s weighted distance from s A shortest weighted path from s

Dijkstra(G,s) for all u  V : [u] = ∞ [s] = 0 H = buildPriorityQueue(V) // using [ ]-values as keys while H ≠ : u = extractMin(H) for all edges (u,v)  E: if [v] > [u] + w(u,v): [v] = [u] + w(u,v) decreaseKey(H, v, [v]) 20

Dijkstra-Running Example 21 C A 2 4 BD E 3 A:0 B:∞ C:∞ D:∞ E: ∞ A:0 B:4 C:2 D:8 E: 7 A:0 B:4 C:2 D:10 E: 7 A:0 B:4 C:2 D:∞ E: ∞

Dijkstra-Complexity Dijkstra's algorithm is structurally identical to BFS. However, it is slower because the priority queue is computationally more demanding than the constant-time pop and push of BFS. Runtime analysis using a binary heap: BuildPriorityQueue takes O(|V|). ExtractMin and Insert are executed |V| times each – once for each node, thus it takes O(|V|log|V|). In the worst case, DecreaseKey can be executed for every edge, thus |E| times, giving O(|E|log|V|). Overall we get O((|V|+|E|)log|V|) 22

Negative Weights Q: Can Dijkstra work with negative weights? A: No! for example: 23 s And this is wrong! 7 3 4

Bellman-Ford Algorithm Input: Directed graph G = (V,E) edge lengths {w e :e  E} with no negative cycles vertex s  V Output: For all vertices u reachable from s, [u] is set to the distance from s to u. 24

Bellman-Ford Algorithm Idea At a given time we have a “guess” of the shortest paths from s to other vertices. We go over edges and see if they can improve our guess This must be repeated many times s v w x b c d a

Bellman-Ford(G,w,s) for all u  V : [u] = ∞ [s] = 0 repeat |V|-1 times for all (u,v)  E: [v] = min{[v], [u] + w(u,v)} 26

Running Example sbacde 0  0263  s b c e d a

Bellman Ford & Negative cycles When there is a negative cycle in a graph shortest path are not (well) defined. How can the algorithm detect negative cycles ? Such a cycle would allow us to endlessly apply rounds of update operations, reducing [ ] estimates every time. So instead of stopping after |V|-1 iterations, perform one extra round. There is a negative cycle if and only if some [ ] value is reduced during this final round. 28 s

Bellman Ford-Complexity for all u  V : [u] = ∞ [s] = 0 repeat |V|-1 times for all (u,v)  E: [v] = min{[v], [u] + w(u,v)} 29 O)|E||V|)