Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

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
CS138A Single Source Shortest Paths Peter Schröder.
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.
Single-Source Shortest- paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
Introduction to Algorithms 6.046J/18.401J/SMA5503
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.
CPSC 411, Fall 2008: Set 9 1 CPSC 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Fall 2008.
CS420 lecture twelve Shortest Paths wim bohm cs csu.
CSE 780 Algorithms Advanced Algorithms Shortest path Shortest path tree Relaxation Bellman-Ford Alg.
Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
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 Path Algorithms
Shortest Paths Definitions Single Source Algorithms
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 12 Graphs and basic search algorithms Motivation Definitions and properties Representation.
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.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Topological Sorting and Least-cost Path Algorithms.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
Graph, Spring 2004 © L. Joskowicz 1 Graphs and basic search algorithms Motivation Definitions and properties Representation Breadth-First Search Depth-First.
David Luebke 1 9/13/2015 CS 332: Algorithms S-S Shortest Path: Dijkstra’s Algorithm Disjoint-Set Union Amortized Analysis.
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 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.
Graphs, BFS, DFS and More…
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
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
1 Weighted Graphs. 2 Outline (single-source) shortest path –Dijkstra (Section 4.4) –Bellman-Ford (Section 4.6) (all-pairs) shortest path –Floyd-Warshall.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
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.
Shortest paths Given: A single source vertex (given s) in a weighted, directed graph. Want to compute a shortest path for each possible destination. (Single.
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])
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Algorithms and Data Structures Lecture XIII
CS200: Algorithm Analysis
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Minimum Spanning Tree Algorithms
CSC 413/513: Intro to Algorithms
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Shortest Path Problems
Chapter 24: Single-Source Shortest Paths
Chapter 24: Single-Source Shortest Paths
CS 3013: DS & Algorithms Shortest Paths.
Presentation transcript:

Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm Dijsktra’s algorithm Chapter 24 in the textbook (pp 580–599).

Data Structures, Spring 2004 © L. Joskowicz 2 Weighted graphs -- reminder A weighted graph is graph in which edges have weights (costs) w(v i, v j ) > 0. A graph is a weighted graph in which all costs are 1. Two vertices with no edge (path) between them can be thought of having an edge (path) with weight ∞ The cost of a path is the sum of the costs of its edges:

Data Structures, Spring 2004 © L. Joskowicz 3 Example: weighted graph s a b c d

Data Structures, Spring 2004 © L. Joskowicz 4 Two basic properties of shortest paths Triangle inequality Let G=(V,E) be a weighted directed graph, w: E  R a weight function and s  V be a source vertex. Then, for all edges e=(u,v)  E: δ(s,v) ≤ δ(s,u) + w(u,v) Optimal substructure of a shortest path Let p = be the shortest path between v 1 and v k. The sub-path between v i and v j, where 1 ≤ i,j ≤ k, p ij = is a shortest path.

Data Structures, Spring 2004 © L. Joskowicz 5 Negative-weight edges Shortest paths are well-defined as long as there are no negative-weight cycles. In such cycles, the longer the path, the lower the value  the shortest path has an infinite number of edges! Allow negative-weight edges, but disallow (or detect) negative-weight cycles! a b c 10 c

Data Structures, Spring 2004 © L. Joskowicz 6 Shortest paths and cycles The shortest path between any two vertices has no positive-weight cycles. The representation for shortest paths between a vertex and all other vertices is the same as the one used in the unweigthed BFS: breath-first tree: G π = (V π,E π ) such that V π = {v  V: π[v] ≠ null }  {s} and E π = {(π[v],v), v  V –{s}} We will prove that a breath-first tree is a shortest-path tree for its root s in which vertices reachable from s are in it and the unique simple path from s to v is shortest.

Data Structures, Spring 2004 © L. Joskowicz 7 Example: shortest-path tree (1) s a b c d

Data Structures, Spring 2004 © L. Joskowicz 8 Example: shortest-path tree (2) s a b c d

Data Structures, Spring 2004 © L. Joskowicz 9 Estimated distance from source As for BFS on unweighted graphs, we keep a label which is the current best estimate of the shortest distance between s and v. Initially, dist[s] = 0 and dist[v] = ∞ for all v ≠ s, and π[v] = null. At all times during the algorithm, dist[v] ≥ δ(s,v). At the end, dist[v] = δ(s,v) and (π[v],v)  E π

Data Structures, Spring 2004 © L. Joskowicz 10 Edge relaxation The process of relaxing an edge (u,v) consists of testing whether it can improve the shortest path from s to v so far by going through u. Relax(u,v) if dist[v] > dist[u] + w(u,v) then dist[v]  dist[u] + w(u,v) π[v]  u

Data Structures, Spring 2004 © L. Joskowicz 11 Properties of shortest paths and relaxation Triangle inequality ∀ e = (u,v)  E: δ(s,v) ≤ δ(s,u) + w(u,v) Upper-boundary property ∀ v  V: dist[v] ≥ δ(s,v) at all times, and it keeps decreasing. No-path property if there is no path from s to v, then dist[v]= δ(s,v) = ∞

Data Structures, Spring 2004 © L. Joskowicz 12 Properties of shortest paths and relaxation Convergence property if s  u  v is a shortest path in G for some u and v, and dist[u]= δ(s,u) at any time prior to relaxing edge (u,v), then dist[v]= δ(s,v) at all times afterwards. Path-relaxation property Let p = be the shortest path between v 0 and v k. When the edges are relaxed in the order (v 0, v 1 ), (v 1, v 2 ), … (v k-1, v k ), then dist[v k ]= δ(s,v k ). Predecessor sub-graph property once dist[v]= δ(s,v) ∀ v  V, the predecessor subgraph is a shortest-paths tree rooted at s.

Data Structures, Spring 2004 © L. Joskowicz 13 Two shortest-path algorithms 1.Bellmann-Ford algorithm 2.Dijkstra’s algorithm – Generalization of BFS

Data Structures, Spring 2004 © L. Joskowicz 14 Bellman-Ford’s algorithm: overview Allows negative weights. If there is a negative cycle, returns “a negative cycle exists”. The idea: –There is a shortest path from s to any other vertex that does not contain a non-negative cycle (can be eliminated to produce a shorter path). –The maximal number of edges in such a path with no cycles is |V| – 1, because it can have at most |V| nodes on the path if there is no cycle. –  it is enough to check paths of up to |V| – 1 edges.

Data Structures, Spring 2004 © L. Joskowicz 15 Bellman-Ford’s algorithm

Data Structures, Spring 2004 © L. Joskowicz 16 Example: Bellman-Ford’s algorithm (1) s a b c d 0 ∞ ∞ ∞∞ Edge order (a,b) (a,c) (a,d) (b,a) (c,b) (c,d) (d,s) (d,b) (s,a) (s,b)

Data Structures, Spring 2004 © L. Joskowicz 17 Example: Bellman-Ford’s algorithm (2) s a b c d 0 6 ∞ 7 ∞ Edge order (a,b) (a,c) (a,d) (b,a) (c,b) (c,d) (d,s) (d,b) (s,a) (s,c) ∞ ∞

Data Structures, Spring 2004 © L. Joskowicz 18 Example: Bellman-Ford’s algorithm (3) s a b c d Edge order (a,b) (a,c) (a,d) (b,a) (c,b) (c,d) (d,s) (d,b) (s,a) (s,c) 11 ∞ ∞

Data Structures, Spring 2004 © L. Joskowicz 19 Example: Bellman-Ford’s algorithm (4) s a b c d Edge order (a,b) (a,c) (a,d) (b,a) (c,b) (c,d) (d,s) (d,b) (s,a) (s,b) 6

Data Structures, Spring 2004 © L. Joskowicz 20 Example: Bellman-Ford’s algorithm (5) s a b c d Edge order (a,b) (a,c) (a,d) (b,a) (c,b) (c,d) (d,s) (d,b) (s,a) (s,b) 2

Data Structures, Spring 2004 © L. Joskowicz 21 Bellman-Ford’s algorithm: properties The first pass over the edges – only neighbors of s are affected (1-edge paths). All shortest paths with one edge are found. The second pass – shortest paths with edges are found. After |V|-1 passes, all possible paths are checked. Claim: we need to update any vertex in the last pass if and only if there is a negative cycle reachable from s in G.

Data Structures, Spring 2004 © L. Joskowicz 22 Bellman Ford algorithm: proof (1) One direction we already know: if we need to update an edge in the last iteration then there is a negative cycle, because we proved before that if there are no negative cycles, and the shortest paths are well defined, we find them in the |V|–1 iteration. We claim that if there is a negative cycle, we will discover a problem in the last iteration. Because, suppose there is a negative cycle But the algorithm does not find any problem in the last iteration, which means that for all edges, we have that for all edges in the cycle.

Data Structures, Spring 2004 © L. Joskowicz 23 Proof by contradiction: for all edges in the cycle After summing up over all edges in the cycle, we discover that the term on the left is equal to the first term on the right (just a different order of summation). We can subtract them, and we get that the cycle is actually positive, which is a contradiction. Bellman Ford algorithm: proof (2)

Data Structures, Spring 2004 © L. Joskowicz 24 Bellman-Ford’s algorithm: complexity Visits |V|–1 vertices  O(|V|) Performs vertex relaxation on all edges  O(|E|) Overall, O(|V|.|E|) time and O(|V|+|E|) space.

Data Structures, Spring 2004 © L. Joskowicz 25 Bellman-Ford on DAGs For Directed Acyclic Graphs (DAG), O(|V|+|E|) relaxations are sufficient when the vertices are visited in topologically sorted order: DAG-Shortest-Path(G) 1.Topologically sort the vertices in G 2.Initialize G (dist[v] and π(v)) with s as source. 3.for each vertex u in topologically sorted order do 4. for each vertex v incident to u do 5. Relax(u,v)

Data Structures, Spring 2004 © L. Joskowicz 26 Example: Bellman-Ford on a DAG (1) 5 a s 0 ∞ 2 b ∞ 7 c ∞ d ∞ -2 e ∞ Vertices sorted from left to right

Data Structures, Spring 2004 © L. Joskowicz 27 Example: Bellman-Ford on a DAG (2) 5 a s 0 ∞ 2 b ∞ 7 c ∞ d ∞ -2 e ∞

Data Structures, Spring 2004 © L. Joskowicz 28 Example: Bellman-Ford on a DAG (3) 5 a s 0 ∞ 2 b 2 7 c 6 d ∞ -2 e ∞

Data Structures, Spring 2004 © L. Joskowicz 29 Example: Bellman-Ford on a DAG (4) 5 a s 0 ∞ 2 b 2 7 c 6 d 6 -2 e

Data Structures, Spring 2004 © L. Joskowicz 30 Example: Bellman-Ford on a DAG (5) 5 a s 0 ∞ 2 b 2 7 c 6 d 5 -2 e

Data Structures, Spring 2004 © L. Joskowicz 31 Example: Bellman-Ford on a DAG (6) 5 a s 0 ∞ 2 b 2 7 c 6 d 5 -2 e

Data Structures, Spring 2004 © L. Joskowicz 32 Example: Bellman-Ford on a DAG (7) 5 a s 0 ∞ 2 b 2 7 c 6 d 5 -2 e

Data Structures, Spring 2004 © L. Joskowicz 33 Bellman-Ford on DAGs: correctness Path-relaxation property Let p = be the shortest path between v 0 and v k. When the edges are relaxed in the order (v 0, v 1 ), (v 1, v 2 ), … (v k-1, v k ), then dist[v k ]= δ(s,v k ). In a DAG, we have the correct ordering! Therefore, the complexity is O(|V|+|E|).

Data Structures, Spring 2004 © L. Joskowicz 34 Dijkstra’s algorithm: overview Idea: Do the same as BFS for unweighted graphs, with two differences: –use the cost as the distance function –use a minimum priority queue instead of a simple queue.

Data Structures, Spring 2004 © L. Joskowicz 35 The BFS algorithm BFS(G, s) label[s]  current; dist[s] = 0; π[s] = null for all vertices u in V – {s} do label[u]  not_visited; dist[u] = ∞; π[u] = null EnQueue(Q,s) while Q is not empty do u  DeQueue(Q) for each v that is a neighbor of u do if label[v] = not_visited then label[v]  current dist[v]  dist[u] + 1; π[v]  u EnQueue(Q,v) label[u]  visited

Data Structures, Spring 2004 © L. Joskowicz 36 Example: BFS algorithm s a b c d

Data Structures, Spring 2004 © L. Joskowicz 37 Example: Dijkstra’s algorithm s a b c d

Data Structures, Spring 2004 © L. Joskowicz 38 Dijkstra’s algorithm Dijkstra(G, s) label[s]  current; dist[s] = 0; π[u] = null for all vertices u in V – {s} do label[u]  not_visited; dist[u] = ∞; π[u] = null Q  s while Q is not empty do u  Extract-Min(Q) for each v that is a neighbor of u do if label[v] = not_visited then label[v]  current if d[v] > d[u] + w(u,v) then d[v]  d[u] + w(u,v); π[v] = u Insert-Queue(Q,v) label[u] = visited

Data Structures, Spring 2004 © L. Joskowicz 39 Example: Dijkstra’s algorithm (1) s a b c d 0 ∞ ∞ ∞∞

Data Structures, Spring 2004 © L. Joskowicz 40 Example: Dijkstra’s algorithm (2) s a b c d 0 ∞ 5∞

Data Structures, Spring 2004 © L. Joskowicz 41 Example: Dijkstra’s algorithm (3) s a b c d

Data Structures, Spring 2004 © L. Joskowicz 42 Example: Dijkstra’s algorithm (4) s a b c d

Data Structures, Spring 2004 © L. Joskowicz 43 Example: Dijkstra’s algorithm (5) s a b c d

Data Structures, Spring 2004 © L. Joskowicz 44 Example: Dijkstra’s algorithm (6) s a b c d

Data Structures, Spring 2004 © L. Joskowicz 45 Dijkstra’s algorithm: correctness (1) Theorem: Upon termination of the Dijkstra’s algorithm, for each dist[v] = δ(s,v) for each vertex v  V, Definition: a path from s to v is said to be a special path if it is the shortest path from s to v in which all vertices (except maybe for v) are inside S. Lemma: At the end of each iteration of the while loop, the following two properties hold: 1.For each w  S, dist[w] is the length of the shortest path from s to w which stays inside S. 2.For each w  V–S, dist(w) is the length of the shortest special path from s to w. The theorem follows when S = V.

Data Structures, Spring 2004 © L. Joskowicz 46 Dijkstra’s algorithm: correctness (2) Proof: by induction on the size of S. For |S|=1, it is clearly true: dist[v] = ∞ except for the neighbors of s, which contain the length of the shortest special path. Induction step: suppose that in the last iteration node v was added added to S. By the induction assumption, dist[v] is the length of the shortest special path to v. It is also the length of the general shortest path to v, since if there is a shorter path to v passing through nodes of S, and x is the first node of S in that path, then x would have been selected and not v. So the first property still holds.

Data Structures, Spring 2004 © L. Joskowicz 47 Property 2: Let x  S. Consider the shortest new special path to w If it doesn’t include v, dist[x] is the length of that path by the induction assumption from the last iteration since dist[x] did not change in the final iteration. If it does include v, then v can either be a node in the middle or the last node before x. Note that v cannot be a node in the middle since then the path would pass from s to v to y in S, but by property 1, the shortest path to y would have been inside S  v need not be included. If v is the last node before x on the path, then dist[x] contains the distance of that path, by the substitution dist[x] = dist[v] + w(v,x) in the algorithm. Dijkstra’s algorithm: correctness (3)

Data Structures, Spring 2004 © L. Joskowicz 48 Dijkstra’s algorithm: complexity The algorithm performs |V| Extract-Min operations and |E| Insert-Queue operations. When the priority queue is implemented as a heap, insert takes O(lg|V|) and Extract-Min takes O(lg(|V|). The total time is O(|V|lg|V |) + O(|E|lg|V|) = O(|E|lg|V|) When |E| = O(|V| 2 ), this is not optimal. In this case, there are many more insert than extract operations. Solution: Implement the priority queue as an array! Insert will take O(1) and Extract-Min O(|V|)  O(|V| 2 ) + O(|E|) = O(|V| 2 ) which is better than the heap as long as |E| is O(|V| 2 /lg (|V|)).

Data Structures, Spring 2004 © L. Joskowicz 49 Application: difference constraints Given a system of m difference constraints over n variables, find a solution if one exists. x i – x j ≤ b k for 1 ≤ i, j ≤ n and 1 ≤ k ≤ m Constraint graph G: each variable x i is a vertex, each constraint x i – x j ≤ b k is a directed edge from x i to x j with weight b k. When G does not have negative cycles, the minimum path distances of the vertices are the solution to the system of constraint differences.

Data Structures, Spring 2004 © L. Joskowicz 50 Example: difference constraints (1) x 1 – x 2 ≤ 0 x 1 – x 5 ≤ -1 x 2 – x 5 ≤ 1 x 3 – x 1 ≤ 5 x 4 – x 1 ≤ 4 x 4 – x 3 ≤ -1 x 5 – x 3 ≤ -3 x 5 – x 4 ≤ -3 Solution: x = (-5,-3,0,-1,-4) 0 x1 x1 x2 x2 x5 x5 x4 x4 x3 x3 s

Data Structures, Spring 2004 © L. Joskowicz 51 Example: difference constraints (2) Solution: x = (-5,-3,0,-1,-4) 0 x1 x1 x2 x2 x5 x5 x4 x4 x3 x3 s

Data Structures, Spring 2004 © L. Joskowicz 52 Why does this work? Theorem: Let Ax ≤ b be a set of m difference constraints over n variables, and G=(V,E) its corresponding constraint graph. If G has no negative weight cycles, then x = (δ(v 0,v 1 ),δ(v 0,v 2 ), …,δ(v 0,v n )) is a feasible solution for the system. If G has a negative cycle, then there is no feasible solution. Proof outline: For all edges (v i,v j ) in E: δ(v 0,v j ) ≤ δ(v 0,v i ) + w(v i,v j ) δ(v 0,v j ) – δ(v 0,v i ) ≤ w(v i,v j ) x j – x j ≤ w(v i,v j )

Data Structures, Spring 2004 © L. Joskowicz 53 Summary Solving the shortest-path problem on weighted graphs is performed by relaxation, based on the path triangle inequality: for all edges e=(u,v)  E: δ(s,v) ≤ δ(s,u) + w(u,v) Two algorithms for solving the problem: –Bellman Ford: for each vertex, relaxation on all edges. Takes O(|E|.|V|) time. Works on graphs with non- negative cycles. –Dijkstra: BFS-like, takes O(|E|lg|V|) time.