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

Similar documents
Shortest path problems

Shortest Path Problem

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

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

22 Elementary Graph Algorithms. There are two standard ways to represent a

Algorithm Design (8) Graph Algorithms 1/2

Single Source Shortest Path (SSSP) Problem

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

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Lecture 11: Analysis of Algorithms (CS ) 1

Directed Graphs. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 1

Chapter 9 Graph Algorithms

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Shortest Path Algorithms

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

CSC 8301 Design & Analysis of Algorithms: Warshall s, Floyd s, and Prim s algorithms

END-TERM EXAMINATION

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.

Binary Heaps in Dynamic Arrays

CMPSC 250 Analysis of Algorithms Spring 2018 Dr. Aravind Mohan Shortest Paths April 16, 2018

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

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

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

Minimum Spanning Trees

Priority Queues and Binary Heaps

Algebraic method for Shortest Paths problems

22 Elementary Graph Algorithms. There are two standard ways to represent a

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

Dijkstra s Shortest Path Algorithm

Shortest Paths. Nishant Mehta Lectures 10 and 11

CS521 \ Notes for the Final Exam

Algorithm Design and Analysis

Graph Algorithms (part 3 of CSC 282),

Minimum Spanning Tree

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

Shortest Paths. Nishant Mehta Lectures 10 and 11

Single Source Shortest Path

Design and Analysis of Algorithms - - Assessment

Chapter 9 Graph Algorithms

Exam 3 Practice Problems

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

COMP 251 Winter 2017 Online quizzes with answers

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

CS350: Data Structures Dijkstra s Shortest Path Alg.

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

Dr. Amotz Bar-Noy s Compendium of Algorithms Problems. Problems, Hints, and Solutions

CS711008Z Algorithm Design and Analysis

CS711008Z Algorithm Design and Analysis

Chapter 9 Graph Algorithms

CSE 417 Dynamic Programming (pt 4) Sub-problems on Trees

Introduction to Algorithms. Lecture 11

Lecture 4: Graph Algorithms

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Lecture Summary CSC 263H. August 5, 2016

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

Greedy Algorithms CSE 6331

Last week: Breadth-First Search

Artificial Intelligence

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

CSE 5311 Notes 4a: Priority Queues

Algorithms and Data Structures

Weighted Graph Algorithms Presented by Jason Yuan

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

CS 5321: Advanced Algorithms Minimum Spanning Trees. Acknowledgement. Minimum Spanning Trees

Graphs. There is no restriction on the relation E. Directed graphs also known as digraphs

l Heaps very popular abstract data structure, where each object has a key value (the priority), and the operations are:

Lecture I: Shortest Path Algorithms

MA 252: Data Structures and Algorithms Lecture 36. Partha Sarathi Mandal. Dept. of Mathematics, IIT Guwahati

Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest. Introduction to Algorithms

1 Single Source Shortest Path Algorithms

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

Chapter 25: All-Pairs Shortest-Paths

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

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

Algorithms. All-Pairs Shortest Paths. Dong Kyue Kim Hanyang University

Distributed Algorithms 6.046J, Spring, Nancy Lynch

l So unlike the search trees, there are neither arbitrary find operations nor arbitrary delete operations possible.

Outline. Computer Science 331. Definitions: Paths and Their Costs. Computation of Minimum Cost Paths

Analysis of Algorithms

looking ahead to see the optimum

Parallel Graph Algorithms

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

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

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

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

1 Dijkstra s Algorithm

Total Points: 60. Duration: 1hr

DESIGN AND ANALYSIS OF ALGORITHMS

CS4800: Algorithms & Data Jonathan Ullman

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

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

Today. Types of graphs. Complete Graphs. Trees. Hypercubes.

CSCE 411 Design and Analysis of Algorithms

Solutions to relevant spring 2000 exam problems

Transcription:

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 Floyd-Warshall algorithm in O(n 3 ) time. Neither of these algorithms were able to take advantage of a sparse input graph a situation where the number of edges e might be o(n 2 ). Today we ll see a dynamic programming algorithm due to Dijkstra that solves the single-source shortest path problem in O(e + n log n) time. (Actually, we ll only discuss an implementation that takes slightly longer, O(e log n) time.) If e = o(n 2 ), we can solve the all-pairs problem in o(n 3 ) time by solving the single-source problem for each of the n possible sources. 1

The basic strategy is a bit of a hybrid of ideas we ve seen already. As in the Prim algorithm discussed in HW#2, we expand a set of vertices from just the source to the entire set, making a locally optimal choice at each step and ensuring that we have a globally optimal solution inside the set. As in the Floyd-Warshall algorithm, at intermediate stages we will consider the shortest paths among those that use only a given set of intermediate vertices. But in contrast to Floyd-Warshall, these sets of intermediate vertices will emerge from the algorithm rather than being predetermined. Let G be our graph and s our designated source. We let X be our growing set of vertices, originally just {s} and eventually all of V. Our key data structure will be an array d that will give a distance d[v] for each vertex v. At any time during the run of the algorithm, d[v] will be the distance from s to v using only intermediate vertices in X. Thus initially d[v] is: 0, if v = s the length of the edge from s to v, if any, if there is no such edge 2

The basic step of the algorithm will be to choose another vertex to add to X, and update the array d accordingly. We make a greedy choice of vertex, letting u be the vertex in V \ X such that d[u] is smallest, and setting X to be X {u}. The update step is to take each vertex v in V \ X and consider whether adding u to X requires us to change d[v]. It suffices to look at d[u] plus the length of the edge from u to v, if any, and replace d[v] by this if it is an improvement. Why couldn t some other path through X from s to v be better than this? To improve on the path that gave rise to the old value of d[v], it would have to visit u, and could visit u only once (since we are assuming non-negative weights). The best way to u through X takes distance d[u]. If we went from s to u and then to some other vertex w in X before going to v, we would have been better off going directly from s to w, since: Lemma: As X grows, d[v] never increases. It reaches its final value when v enters X, and at that time every vertex in X has distance at most d[v] and every vertex outside X has distance at least d[v]. 3

Correctness of Dijkstra s Algorithm If we believe that d[v] always represents the shortest distance from s by paths that only use intermediate vertices in X, then by the time that X = V we must have d[v] as the shortest distance overall. But just to be sure, let s look at an arbitrary v and make sure that d[v] gets set correctly. First note that d[v] can never be set too small, since the array d is only set on the evidence of an actual path from s. We must make sure that any other path from s to v must have length at least the final value of d[v]. If there are any vertices where d[v] is set incorrectly, let v be one whose d[v] is smallest. Then we can assume that all u with d[u] < d[v] have their distances correct. At the time that v is added to X, all such u must already be in X, since v is the d-minimum vertex not in X. Now consider any path from s to v. Let (x, y) be the edge on which is first leaves X (we may have s = x or y = v). If y v, the length of the path is at least d[y], and d[y] d[v]. Otherwise the length of the path is at least d[x] + e(x, v). When x entered X, d[v] was compared against this very value. So again we know that this other path has length at least d[v]. 4

Implementation of Dijkstra s Algorithm While we maintain the array d, we also have to keep the set V \ X and carry out two operations on it: Find and remove the item u of V \ X such that d[u] is minimal, and Find an item v in V \X and decrease d[v] if necessary. These are the two characteristic operations of a priority queue. If we have a set of tasks, for example, each with a priority, we may have to find and perform the task of highest priority, or increase the priority of some task. (We could also consider decreasing a priority, but we don t need that for Dijkstra and it adds complications.) In the Dijkstra algorithm we will set up a priority queue of at most n 1 items. We will need to carry out n 1 REMOVE-MIN operations, and e DECREASE-KEY operations. (Why e? Because we only need to update d[v] if there is an edge to v from the vertex u currently being added to X.) 5

A simple implementation of a priority queue uses a heap. A heap is a binary tree of items, where each item has a value that is smaller than or equal to that of either of its children. (That s the case for a min-heap in a maxheap the parent s value would be greater than or equal to the childrens.) We also insist that the tree be as close as possible to balanced, with the bottom level of leaves left-justified. Clearly the item with the smallest value must be at the root. We can easily remove this item, but we then have to restore the heap property for the smaller remaining tree. What we do is to move the rightmost leaf to the root, then promote items over their parents as necessary to restore the heap property. To begin, if either of the root s children is smaller than the root then we swap the root with the smaller of the two. Then we look at the old root s new children and swap the old root with the smaller of them if needed. In this way the root item (the original rightmost leaf) moves down until it is in an acceptable position. This might take O(log n) operations. 6

When an item has its value decreased, the shape of the heap has not changed but that item might need to move up. We can have it do so by swapping it with its parent until the heap property is restored. We can check that doing this gets rid of the single violation of the heap property without creating new ones. With a heap implementation, connected by pointers to a single array of d values, the Dijkstra algorithm has O(n) phases and O(e) total updates of d values, taking O(e log n) time total. 7

In principle, but not generally in practice, the performance of the Dijkstra algorithm can be improved if we implement the priority queue using Fibonacci heaps. Such a priority queue keeps a number of heaps, linked by pointers, instead of just one. Individual operations on the queue may take more than constant time, but it turns out that the amortized time for a sequence of operations is O(1) per operation. That is, any sequence of s REMOVE- MIN and DECREASE-KEY operations on the priority queue takes O(s) total time. The proof of this involves a potential function, like the one we discussed for the treecompression algorithm for the union-find problem. With this improvement, the running time of Dijkstra s algorithm drops to O(e) on a weighted graph with n vertices and e edges. In practice, the overhead of Fibonacci heaps makes them impractical, and ordinary heaps are usually used. 8

The Bellman-Ford Algorithm Here is another dynamic programming algorithm for the all-pairs shortest-path problem, due to Bellman and Ford. It is slightly slower than Dijkstra s algorithm but is more general because it can handle negative weights as long as there are no negative cycles. Why would we care about shortest paths where distances could be negative? Consider a graph where the nodes represent various different assets and the edges represent possible transactions trading one asset for another. The edge weight could represent the potential profit or loss from a transaction, and these weights could change rapidly with the market. A shortest path from i to j would represent the most economical way to convert asset i to asset j. A negative cycle would represent an arbitrage opportunity, which we would expect not to persist for very long if the traders are paying attention. 9

CLRS and Kleinberg Tardos give different presentations of the Bellman-Ford algorithm, and I will follow KT here. (This algorithm isn t in the Adler notes.) We ll solve the all-pairs problem by solving the n single-destination problems separately. Fix a destination vertex t. For any vertex v and number i, define Opt(v, i) to be the shortest distance from v to t using a path of at most i edges. Thus Opt(v, 1) is the length of the edge from v to t if there is one, and Opt(v, n 1) is the actual shortest-path distance. (If there are no negative cycles, the shortest path must be a simple path, never revisiting a vertex.) Note that we also compute Opt(t, i) for each i if one of these is ever negative we have found a negative cycle and the calculation fails. 10

All we need to do is to compute Opt(v, i + 1) for each v, given that we have already found Opt(v, i) for each v. This is easy. One candidate for Opt(v, i + 1) is just Opt(v, i) it may be that no path with i + 1 edges is shorter than the best path with i edges. For every edge (v, w) out of v, we have another candidate path, whose length is the edge weight e(v, w) plus Opt(w, i). We find the smallest path weight among these candidates and set Opt(v + 1) to that. Correctness is obvious by induction on i we prove that each value Opt(v, i) is indeed the least weight of any path of i or fewer edges from v to t. How long does this take? We have a phase of computation for each i, and in each phase we must check one candidate for each vertex and one for each edge, requiring O(n + e) time (O(e) for a connected graph). Thus our total time for the single-destination problem is O(ne), and for the all-pairs problem O(n 2 e). By contrast, Dijkstra took O(e log n) for a single source and O(en log n) for all pairs, but could not handle negative weights. 11

Another advantage of Bellman-Ford is its local nature. Each node has its own estimate of the distance to t, which it can maintain by getting information from its neighbors. Algorithms similar to this are used for routing in computer networks, particular when the membership of the network and the relative distances change frequently. 12

Seidel s Algorithm When we used min-plus matrix powering to solve the allpairs shortest path problem in O(n 3 log n) time, we noted that we were forced to use ordinary matrix multiplication rather than subcubic methods such as Strassen s. Could we, at least in principle, use subcubic matrix multiplication to improve the running time for all-pairs shortest path? What about just the problem of determining the transitive closure of a graph? Warshall s algorithm takes O(n 3 ) time, and boolean matrix powering takes O(n 3 log n) using ordinary matrix multiplication. And the boolean semiring, like the min-plus semiring, does not support subtraction, so we cannot use the Strassen method directly. 13

But there is a way to use integer matrix multiplication to simulate the boolean version. If A and B are boolean matrices, and we compute C = AB using integer matrix multiplication, then c ij tells us the number of paths from i to j. All we need to do is change any nonzero entries of C to ones, and we have the correct boolean result. We have to be a bit careful in our implementation with regard to how we represent integers as finite bit strings. For example, suppose we used integer matrix powering to compute the transitive closure of a graph, representing the integers as Java long variables, and the graph happened to have exactly 2 64 paths from i to j? But we know that the integer product of two boolean matrices can have no entry larger than n, so we could carry out the computation modulo n + 1 or any larger number. We could implement powering by repeated squaring, converting each integer matrix result to a boolean matrix before proceeding. Theorem: Let µ(n) be the time complexity of integer matrix multiplication. Then two n by n boolean matrices may be multiplied in O(µ(n)) time, and we can compute the reflexive transitive closure of an n-vertex graph in O(µ(n) log n) time. 14

Today we will present an algorithm due to Seidel that uses subcubic matrix multiplication to attack the all-pairs shortest path problem. But this algorithm only works on a special case of the problem, when the graph is connected, undirected, and unweighted. The matrices we multiply, two at a time, will be either boolean or integer. The integer matrices will have relatively small entries, at most n 2. Remember that Strassen s method, which we saw, shows µ(n) to be O(n 2.81... ), and the best known method (due to Coppersmith and Winograd) shows that µ(n) = O(n 2.376 ). The best lower bound is only Ω(n 2 ), and the true asymptotic value of µ(n) is unknown. 15

What will a single matrix multiplication do for us? If A is the matrix of G, then the boolean A 2 tells us about paths of length exactly two. This suggests a recursive attack on the problem. Let G 2 be the graph that consists of the edges of G together with an edge short-cutting every twostep path (except for those from vertices to themselves). The adjacency matrix M[G 2 ] of G 2 is easy to calculate in O(µ(n)) time we take A 2, component-wise OR it with A, and zero out the main diagonal. This takes only O(n 2 ) on top of the one boolean matrix multiplication. Now we ll recursively apply our algorithm to find the shortest-path distance matrix of G 2. The distance from i to j in G 2 is about half the distance from i to j in G more precisely, it is the ceiling of half the distance. This is because a path of length 2d in G corresponds to a path of d shortcut edges in G 2, and a path of length 2d + 1 in G corresponds to a path of d shortcut edges and one ordinary edge in G 2. 16

In order to compute the exact distances in G, then, we will need only one bit of additional information for each i and j whether the shortest-path distance from i to j is odd or even. We define a matrix P [G] to be D[G] reduced modulo 2. So P [G] will have a 1 in the (i, j) entry exactly when the shortest-path distance from i to j is odd. For example, the diagonal entries of P [G] will all be 0, since the distance from a vertex to itself is 0, and the entries where A has ones will also have ones. The resulting algorithm to compute D[G] is a bit bizarre: Compute M[G 2 ] in O(µ(n)) time. If M[G 2 ](i, j) = 1 whenever i j, return the answer. Otherwise compute D [G] = D[G 2 ] recursively. Compute P [G] from D [G] in time O(µ(n)) by a method to be given later. Return D[G] = 2D [G] P [G]. This leaves us with the questions: Why is this correct? How long does it take? How do we get P [G] from D [G] in O(µ(n)) time? 17