Chapter 24: Single-Source Shortest-Path

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
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 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 Paths Definitions Single Source Algorithms
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
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.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
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.
1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very.
Lecture 13 Algorithm Analysis
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
Shortest Paths and Minimum Spanning Trees
Lecture 13 Shortest Path.
Shortest Paths C B A E D F Shortest Paths 1
CSE 2331/5331 Topic 9: Basic Graph Alg.
Algorithms and Data Structures Lecture XIII
CS200: Algorithm Analysis
Introduction to Graphs
Shortest Path Problems
Minimum Spanning Trees
Data Structures and Algorithms CMPSC 465
Advanced Algorithms Analysis and Design
SINGLE-SOURCE SHORTEST PATHS
Shortest Paths C B A E D F Shortest Paths
CSCE 411 Design and Analysis of Algorithms
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Page 620 Single-Source Shortest Paths
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Chapter 22: Elementary Graph Algorithms I
Lectures on Graph Algorithms: searching, testing and sorting
Basic Graph Algorithms
2018/12/27 chapter25.
Advanced Algorithms Analysis and Design
Lecture 11 Topics Application of BFS Shortest Path
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Shortest Path Algorithms
Minimum Spanning Tree Algorithms
Shortest Paths and Minimum Spanning Trees
CSC 413/513: Intro to Algorithms
Lecture 13 Algorithm Analysis
CSCE 411 Design and Analysis of Algorithms
Lecture 13 Algorithm Analysis
Fundamental Data Structures and Algorithms
Shortest Path Problems
Algorithms (2IL15) – Lecture 7
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Algorithms Searching in a Graph.
Chapter 24: Single-Source Shortest Paths
Fundamental Data Structures and Algorithms
CSE332: Data Abstractions Lecture 17: Shortest Paths
Advanced Algorithms Analysis and Design
Shortest Path Problems
Chapter 24: Single-Source Shortest Paths
Negative-Weight edges:
CS 3013: DS & Algorithms Shortest Paths.
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Topological Sorting Minimum Spanning Trees Shortest Path
Advanced Algorithms Analysis and Design
Premaster Course Algorithms 1 Chapter 6: Shortest Paths
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

Chapter 24: Single-Source Shortest-Path

About this lecture What is the problem about ? Dijkstra’s Algorithm [1959] ~ Prim’s Algorithm [1957] Folklore Algorithm for DAG [???] Bellman-Ford Algorithm Discovered by Bellman [1958], Ford [1962] Allowing negative edge weights

Single-Source Shortest Path Let G = (V,E) be a weighted graph the edges in G have weights can be directed/undirected can be connected/disconnected Let s be a special vertex, called source Target: For each vertex v, compute the length of shortest path from s to v

Single-Source Shortest Path E.g., 4 8 11 7 9 10 14 2 1 6 s 4 8 11 7 9 10 14 2 1 6 s 12 19 21

If d(w) > d(v) + w(v, w) Relax A common operation that is used in the algorithms is called Relax : when a vertex v can be reached from the source with a certain distance, we examine an outgoing edge, say (v,w), and check if we can improve w E.g., Can we improve this? Can we improve these? 4 8 11 1 2 6 7 s ? v If d(w) > d(v) + w(v, w) d(w) = d(v) + w(v, w)

Dijkstra’s Algorithm Dijkstra(G, s) For each vertex v, Mark v as unvisited, and set d(v) = ∞ ; Set d(s) = 0 ; while (there is unvisited vertex) { v = unvisited vertex with smallest d ; Visit v, and Relax all its outgoing edges; } return d ;

Example s Relax s 8 7 4 ∞ ∞ ∞ 9 2 11 ∞ 4 14 ∞ 7 6 8 ∞ 10 ∞ ∞ 1 2 4 8 ∞ 4 14 ∞ 7 6 8 ∞ 10 ∞ ∞ 1 2 4 8 11 7 9 10 14 2 1 6 ∞ Relax s

Example 4 8 11 7 9 10 14 2 1 6 s ∞ 4 8 11 7 9 10 14 2 1 6 ∞ Relax 12 s

Example 4 8 11 7 9 10 14 2 1 6 s ∞ 12 4 8 11 7 9 10 14 2 1 6 ∞ Relax 12 15 s

Example 4 8 11 7 9 10 14 2 1 6 s ∞ 12 15 4 8 11 7 9 10 14 2 1 6 ∞ Relax 12 15 s

Example 4 8 11 7 9 10 14 2 1 6 s ∞ 12 15 4 8 11 7 9 10 14 2 1 6 Relax 12 15 25 21 s

Example 4 8 11 7 9 10 14 2 1 6 s 12 15 25 21 4 8 11 7 9 10 14 2 1 6 Relax 12 19 21 s

Example 4 8 11 7 9 10 14 2 1 6 s 12 19 21 Relax 4 8 11 7 9 10 14 2 1 6 s 12 19 21

Example 4 8 11 7 9 10 14 2 1 6 s 12 19 21 Relax 4 8 11 7 9 10 14 2 1 6 s 12 19 21

Example 4 8 11 7 9 10 14 2 1 6 s 12 19 21 Relax 4 8 11 7 9 10 14 2 1 6 s 12 19 21

Correctness Theorem: The kth vertex closest to the source s is selected at the kth step inside the while loop of Dijkstra’s algorithm Also, by the time a vertex v is selected, d(v) will store the length of the shortest path from s to v How to prove ? (By induction)

Proof Both statements are true for k = 1 ; Let vj = jth closest vertex from s Now, suppose both statements are true for k = 1, 2, …, r-1 Consider the rth closest vertex vr If there is no path from s to vr  d(vr) = ∞ is never changed Else, there must be a shortest path from s to vr ; Let vt be the vertex immediately before vr in this path

Proof (cont) Then, we have t  r-1 (why??)  d(vr) is set correctly once vt is selected, and the edge (vt,vr) is relaxed (why??)  After that, d(vr) is fixed (why??)  d(vr) is correct when vr is selected ; also, vr must be selected at the rth step, because no unvisited nodes can have a smaller d value at that time Thus, the proof of inductive case completes t<= r -1 because any node y not in S (Fig. 24.7) has delta (s, y) >= delta (s, vi) for i<= r-1 It is because delta (s, vt) + w(vt, vr) <= delta(s, vi) + w(vi, vr) for i <= r -1 and i <> t

Performance Dijkstra’s algorithm is similar to Prim’s By using Fibonacci Heap (Chapter 19), Relax  Decrease-Key Pick vertex  Extract-Min Running Time: the amortized cost of each |V| operation Extract-Min is O(log V) At most O(E) Decrease-Key  Total Time: O(E + V log V)

Finding Shortest Path in DAG We have a faster algorithm for DAG : DAG-Shortest-Path(G, s) Topological Sort G ; For each v, set d(v) =  ; Set d(s) = 0 ; for (k = 1 to |V|) { v = kth vertex in topological order ; Relax all outgoing edges of v ; } return d ;

Example 4 8 11 3 2 6 5 s Topological Sort 8 s 4 3 2 6 5 11

Example 8 s 4 3 2 6 5 11  Process this node 8 s 4 3 2 6 5 11 Relax 

Example 8 s 4 3 2 6 5 11  Process this node 8 s 4 3 2 6 5 11 Relax 

Example 8 s 4 3 2 6 5 11  Process this node 8 s 4 3 2 6 5 11 Relax 

Example s Relax s Process this node 8 4 3 2 6 5 11  8 4 3 2 6 5 11  Process this node 8 s 4 3 2 6 5 11 Relax  10

Example s Relax s Process this node 8 4 3 2 6 5 11  8 4 3 2 6 5 11  10 Process this node 8 s 4 3 2 6 5 11 Relax  10

Example s Relax s Process this node 8 4 3 2 6 5 11  8 4 3 2 6 5 11  10 Process this node 8 s 4 3 2 6 5 11 Relax  10

Correctness Theorem: By the time a vertex v is selected, d(v) will store the length of the shortest path from s to v How to prove ? (By induction)

Proof Let vj = jth vertex in the topological order We will show that d(vk) is set correctly when vk is selected, for k = 1,2, …, |V| When k = 1, vk = v1 = leftmost vertex If it is the source, d(vk) = 0 If it is not the source, d(vk) =  In both cases, d(vk) is correct (why?) Base case is correct

Proof (cont) Now, suppose the statement is true for k = 1, 2, …, r-1 Consider the vertex vr If there is no path from s to vr  d(vr) =  is never changed Else, we shall use similar arguments as proving the correctness of Dijkstra’s algorithm …

Proof (cont) First, let vt be the vertex immediately before vr in the shortest path from s to vr  t  r-1  d(vr) is set correctly once vt is selected, and the edge (vt,vr) is relaxed  After that, d(vr) is fixed  d(vr) is correct when vr is selected Thus, the proof of inductive case completes

Performance DAG-Shortest-Path selects vertex sequentially according to topological order no need to perform Extract-Min We can store the d values of the vertices in a single array  Relax takes O(1) time Running Time: Topological sort : O(V + E) time O(V) select, O(E) Relax : O(V + E) time  Total Time: O(V + E)

Handling Negative Weight Edges When a graph has negative weight edges, shortest path may not be well-defined v 4 8 11 -7 s What is the shortest path from s to v? E.g.,

Handling Negative Weight Edges The problem is due to the presence of a cycle C, reachable by the source, whose total weight is negative  C is called a negative-weight cycle How to handle negative-weight edges ??  if input graph is known to be a DAG, DAG-Shortest-Path is still correct  For the general case, we can use Bellman-Ford algorithm

Bellman-Ford Algorithm Bellman-Ford(G, s) // runs in O(VE) time For each v, set d(v) =  ; Set d(s) = 0 ; for (k = 1 to |V|-1) Relax all edges in G in any order ; /* check if s reaches a neg-weight cycle */ for each edge (u,v), if (d(v) > d(u) + weight(u,v)) return “something wrong !!” ; return d ;

Example 1 ∞ ∞ ∞ s s ∞ ∞ Relax all ∞ Relax all Relax all s s ∞ 8 8 4 4 -7 -7 3 10 3 10 8 ∞ ∞ Relax all 8 ∞ 8 -2 Relax all 8 8 4 Relax all 1 4 4 4 s s -7 10 -7 3 3 10 8 -2 11 8 ∞ 7 7 -2 -2

Example 1 After the 4th Relax all s 8 4 4 s -7 3 10 8 7 10 -2 After checking, we found that there is nothing wrong  distances are correct

Example 2 ∞ ∞ ∞ s s ∞ ∞ Relax all ∞ Relax all Relax all s s ∞ 8 8 4 4 -7 -7 3 1 3 1 8 ∞ ∞ Relax all 8 ∞ 8 -2 -2 Relax all 8 8 4 Relax all 1 -7 4 4 1 s s -7 -7 3 1 3 1 8 8 ∞ 2 7 -2 -2

This edge shows something must be wrong … Example 2 After the 4th Relax all This edge shows something must be wrong … 8 4 -7 -15 s -7 3 1 8 -8 -6 -2 After checking, we found that something must be wrong  distances are incorrect

Correctness (Part 1) Theorem: If the graph has no negative-weight cycle, then for any vertex v with shortest path from s consists of k edges, Bellman-Ford sets d(v) to the correct value after the kth Relax all (for any ordering of edges in each Relax all ) How to prove ? (By induction)

Corollary Corollary: If there is no negative-weight cycle, then when Bellman-Ford terminates, d(v) ≤ d(u) + weight(u,v) for all edge (u,v) Proof: By previous theorem, d(u) and d(v) are the length of shortest path from s to u and v, respectively. Thus, we must have d(v) ≤ length of any path from s to v  d(v) ≤ d(u) + weight(u,v)

“Something Wrong” Lemma Lemma: If there is a negative-weight cycle, then when Bellman-Ford terminates, d(v) > d(u) + weight(u,v) for some edge (u,v) How to prove ? (By contradiction)

d(v) ≤ d(u) + weight(u,v) Proof Firstly, we know that there is a cycle C = (v1, v2, … , vk, v1) whose total weight is negative That is, Si = 1 to k weight(vi, vi+1) ‹ 0 Now, suppose on the contrary that d(v) ≤ d(u) + weight(u,v) for all edge (u,v) at termination

weight(u,v) ≥ d(v) - d(u) Proof (cont) Can we obtain another bound for Si = 1 to k weight(vi, vi+1) ? By rearranging, for all edge (u,v) weight(u,v) ≥ d(v) - d(u)  Si = 1 to k weight(vi, vi+1) ≥ Si = 1 to k (d(vi+1) - d(vi)) = 0 (why?)  Contradiction occurs !!

d(v) > d(u) + weight(u,v) Correctness (Part 2) Combining the previous corollary and lemma, we have: Theorem: There is a negative-weight cycle in the input graph if and only if when Bellman-Ford terminates, d(v) > d(u) + weight(u,v) for some edge (u,v)

Homework Practice at home: 1-3, 2-4, 3-4, 3-6 Exercises: 3-10 (Due: Dec. 28)