1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

CSCE 411H Design and Analysis of Algorithms Set 8: Greedy Algorithms Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 8 1 * Slides adapted.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSE 101- Winter ‘15 Discussion Section January 26th 2015.
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?
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
Introduction to Algorithms 6.046J/18.401J/SMA5503
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
CPSC 411, Fall 2008: Set 9 1 CPSC 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Fall 2008.
Minimum Spanning Tree Algorithms
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.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Shortest Path Algorithms
Shortest Paths Definitions Single Source Algorithms
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
CSE Algorithms Minimum Spanning Trees Union-Find Algorithm
CPSC 311, Fall CPSC 311 Analysis of Algorithms More Graph Algorithms Prof. Jennifer Welch Fall 2009.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
CS 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis.
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.
Topological Sorting and Least-cost Path Algorithms.
David Luebke 1 9/10/2015 CS 332: Algorithms Single-Source Shortest Path.
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.
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 8 Greedy Graph.
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
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.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
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.
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
1 Introduction to Algorithms L ECTURE 17 (Chap. 24) Shortest paths I 24.2 Single-source shortest paths 24.3 Dijkstra’s algorithm Edsger Wybe Dijkstra
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
Lecture 13 Algorithm Analysis
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
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,
David Luebke 1 3/1/2016 CS 332: Algorithms Dijkstra’s Algorithm Disjoint-Set Union.
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.
CS38 Introduction to Algorithms Lecture 3 April 8, 2014.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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.
CSCE 411 Design and Analysis of Algorithms
CSCE 411 Design and Analysis of Algorithms
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Minimum Spanning Tree Algorithms
CSCE 411 Design and Analysis of Algorithms
Lecture 13 Algorithm Analysis
Single-Source Shortest Path & Minimum Spanning Trees
Presentation transcript:

1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]

2 Single Source Shortest Path Problem Given: a directed or undirected graph G = (V,E), a source node s in V, and a weight function w: E -> R. Goal: For each vertex t in V, find a path from s to t in G with minimum weight Warning! Negative weight cycles are a problem: s t 4 55

3 Constant Weight Functions Suppose that the weights of all edges are the same. How can you solve the single- source shortest path problem? Breadth-first search can be used to solve the single-source shortest path problem. Indeed, the tree rooted at s in the BFS forest is the solution.

Intermezzo: Priority Queues 4

Priority Queues A min-priority queue is a data structure for maintaining a set S of elements, each with an associated value called key. This data structure supports the operations: insert(S,x) which realizes S := S  {x} minimum(S) which returns the element with the smallest key. extract-min(S) which removes and returns the element with the smallest key from S. decrease-key(S,x,k) which decreases the value of x’s key to the lower value k, where k < key[x]. 5

Simple Array Implementation Suppose that the elements are numbered from 1 to n, and that the keys are stored in an array key[1..n]. insert and decrease-key take O(1) time. extract-min takes O(n) time, as the whole array must be searched for the minimum. 6

Binary min-heap Implementation Suppose that we realize the priority queue of a set with n element with a binary min-heap. extract-min takes O(log n) time. decrease-key takes O(log n) time. insert takes O(log n) time. Building the heap takes O(n) time. 7

Fibonacci-Heap Implementation Suppose that we realize the priority queue of a set with n elements with a Fibonacci heap. Then extract-min takes O(log n) amortized time. decrease-key takes O(1) amortized time. insert takes O(1) time. [One can realize priority queues with worst case times as above] 8

9 Dijkstra’s Single Source Shortest Path Algorithm

10 Dijkstra's SSSP Algorithm Assumes all edge weights are nonnegative Similar to Prim's MST algorithm Start with source node s and iteratively construct a tree rooted at s Each node keeps track of tree node that provides cheapest path from s (not just cheapest path from any tree node) At each iteration, include the node whose cheapest path from s is the overall cheapest

11 Prim's vs. Dijkstra's s Prim's MST s Dijkstra's SSSP

12 Implementing Dijkstra's Alg. How can each node u keep track of its best path from s? Keep an estimate, d[u], of shortest path distance from s to u Use d as a key in a priority queue When u is added to the tree, check each of u's neighbors v to see if u provides v with a cheaper path from s: compare d[v] to d[u] + w(u,v)

13 Dijkstra's Algorithm input: G = (V,E,w) and source node s // initialization d[s] := 0 d[v] := infinity for all other nodes v initialize priority queue Q to contain all nodes using d values as keys

14 Dijkstra's Algorithm while Q is not empty do u := extract-min(Q) for each neighbor v of u do if d[u] + w(u,v) < d[v] then // relax d[v] := d[u] + w(u,v) decrease-key(Q,v,d[v]) parent(v) := u

15 Dijkstra's Algorithm Example ab de c a is source node QabcdebcdecdedededØ d[a] d[b] ∞ d[c] ∞ 1210 d[d] ∞∞∞ 1613 d[e] ∞∞ 11 iteration

16 Correctness of Dijkstra's Alg. Let T i be the tree constructed after i-th iteration of the while loop: The nodes in T i are not in Q The edges in T i are indicated by parent variables Show by induction on i that the path in T i from s to u is a shortest path and has distance d[u], for all u in T i. Basis: i = 1. s is the only node in T 1 and d[s] = 0.

17 Correctness of Dijkstra's Alg. Induction: Assume T i is a correct shortest path tree. We need to show that T i+1 is a correct shortest path tree as well. Let u be the node added in iteration i. Let x = parent(u). s x TiTi u T i+1 Need to show path in T i+1 from s to u is a shortest path, and has distance d[u]

18 Correctness of Dijkstra's Alg s x TiTi u T i+1 P, path in T i+1 from s to u (a,b) is first edge in P' that leaves T i a b P', another path from s to u

19 Correctness of Dijkstra's Alg s x TiTi u T i+1 a b P' P Let P1 be part of P' before (a,b). Let P2 be part of P' after (a,b). w(P') = w(P1) + w(a,b) + w(P2) ≥ w(P1) + w(a,b) (nonneg wts) ≥ wt of path in T i from s to a + w(a,b) (inductive hypothesis) ≥ w(s->x path in T i ) + w(x,u) (alg chose u in iteration i and d-values are accurate, by inductive hypothesis = w(P). So P is a shortest path, and d[u] is accurate after iteration i+1.

20 Running Time of Dijstra's Alg. initialization: insert each node once O(V T ins ) O(V) iterations of while loop one extract-min per iteration => O(V T ex ) for loop inside while loop has variable number of iterations… For loop has O(E) iterations total one decrease-key per iteration => O(E T dec ) Total is O(V (T ins + T ex ) + E T dec )

21 Running Time using Binary Heaps and Fibonacci Heaps O(V(T ins + T ex ) + E  T dec ) If priority queue is implemented with a binary heap, then T ins = T ex = T dec = O(log V) total time is O(E log V) There are fancier implementations of the priority queue, such as Fibonacci heap: T ins = O(1), T ex = O(log V), T dec = O(1) (amortized) total time is O(V log V + E)

22 Using Simpler Heap Implementations O(V(T ins + T ex ) + E  T dec ) If graph is dense, so that |E| =  (V 2 ), then it doesn't help to make T ins and T ex to be at most O(V). Instead, focus on making T dec be small, say constant. Implement priority queue with an unsorted array: T ins = O(1), T ex = O(V), T dec = O(1) total is O(V 2 )

23 The Bellman-Ford Algorithm

24 What About Negative Edge Weights? Dijkstra's SSSP algorithm requires all edge weights to be nonnegative. This is too restrictive, since it suffices to outlaw negative weight cycles. Bellman-Ford SSSP algorithm can handle negative edge weights. [It even can detect negative weight cycles if they exist.]

25 Bellman-Ford: The Basic Idea Consider each edge (u,v) and see if u offers v a cheaper path from s compare d[v] to d[u] + w(u,v) Repeat this process |V| - 1 times to ensure that accurate information propgates from s, no matter what order the edges are considered in

26 Bellman-Ford SSSP Algorithm input: directed or undirected graph G = (V,E,w) //initialization initialize d[v] to infinity and parent[v] to nil for all v in V other than the source initialize d[s] to 0 and parent[s] to s // main body for i := 1 to |V| - 1 do for each (u,v) in E do // consider in arbitrary order if d[u] + w(u,v) < d[v] then d[v] := d[u] + w(u,v) parent[v] := u

27 Bellman-Ford SSSP Algorithm // check for negative weight cycles for each (u,v) in E do if d[u] + w(u,v) < d[v] then output "negative weight cycle exists"

28 Running Time of Bellman-Ford O(V) iterations of outer for loop O(E) iterations of inner for loop O(VE) time total

29 Correctness of Bellman-Ford Assume no negative-weight cycles. Lemma: d[v] is never an underestimate of the actual shortest path distance from s to v. Lemma: If there is a shortest s-to-v path containing at most i edges, then after iteration i of the outer for loop, d[v] is at most the actual shortest path distance from s to v. Theorem: Bellman-Ford is correct. This follows from the two lemmas and the fact that every shortest path has at most |V| - 1 edges.

30 Bellman-Ford Example s c a b 3 — process edges in order (c,b) (a,b) (c,a) (s,a) (s,c) Exercise!

31 Correctness of Bellman-Ford Suppose there is a negative weight cycle. Then the distance will decrease even after iteration |V| - 1 shortest path distance is negative infinity This is what the last part of the code checks for.

32 The Boost Graph Library The BGL contains generic implementations of all the graph algorithms that we have discussed: Breadth-First-Search Depth-First-Search Kruskal’s MST algorithm Prim’s MST algorithm Strongly Connected Components Dijkstra’s SSSP algorithm Bellman-Ford SSSP algorithm I recommend that you gain experience with this useful library. Recommended reading: The Boost Graph Library by J.G. Siek, L.-Q. Lee, and A. Lumsdaine, Addison-Wesley, 2002.