Title. Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis. Nesrine Damak. Fakultät für Informatik TU München. 20.

Similar documents
Classical Shortest-Path Algorithms

Shortest path problems

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

Chapter 24. Shortest path problems. Chapter 24. Shortest path problems. 24. Various shortest path problems. Chapter 24. Shortest path problems

Shortest Paths. Nishant Mehta Lectures 10 and 11

Chapter 25: All-Pairs Shortest-Paths

Shortest Paths. Nishant Mehta Lectures 10 and 11

Lecture 11: Analysis of Algorithms (CS ) 1

Shortest Path Problem

Advanced Algorithm Design and Analysis (Lecture 5) SW5 fall 2007 Simonas Šaltenis

Minimum Spanning Trees

Algorithms on Graphs: Part III. Shortest Path Problems. .. Cal Poly CSC 349: Design and Analyis of Algorithms Alexander Dekhtyar..

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

Introduction to Algorithms. Lecture 11

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

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

Design and Analysis of Algorithms 演算法設計與分析. Lecture 13 December 18, 2013 洪國寶

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24

COMP251: Single source shortest paths

Breadth-First Search, 1. Slides for CIS 675 DPV Chapter 4. Breadth-First Search, 3. Breadth-First Search, 2

Single Source Shortest Path

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

Addis Ababa University, Amist Kilo July 20, 2011 Algorithms and Programming for High Schoolers. Lecture 12

Shortest Path Algorithms

CS 561, Lecture 10. Jared Saia University of New Mexico

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

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

Minimum Spanning Tree

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

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

COT 6405 Introduction to Theory of Algorithms

Algorithms for Data Science

Single Source Shortest Path (SSSP) Problem

All Shortest Paths. Questions from exercises and exams

Analysis of Algorithms, I

CS 561, Lecture 10. Jared Saia University of New Mexico

CS 4349 Lecture October 23rd, 2017

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

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

Dijkstra s Shortest Path Algorithm

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

Dynamic Programming: 1D Optimization. Dynamic Programming: 2D Optimization. Fibonacci Sequence. Crazy 8 s. Edit Distance

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Single Source Shortest Paths

Graphs. Part II: SP and MST. Laura Toma Algorithms (csci2200), Bowdoin College

Outlines: Graphs Part-2

The Shortest Path Problem

Data Structures and Algorithms. Werner Nutt

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

Theory of Computing. Lecture 7 MAS 714 Hartmut Klauck

Dijkstra s Algorithm and Priority Queue Implementations. CSE 101: Design and Analysis of Algorithms Lecture 5

Elementary Graph Algorithms

CS161 - Minimum Spanning Trees and Single Source Shortest Paths

Elementary Graph Algorithms: Summary. Algorithms. CmSc250 Intro to Algorithms

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

Three Graph Algorithms

Graphs: Finding shortest paths. Tecniche di Programmazione A.A. 2016/2017

Three Graph Algorithms

Graphs II - Shortest paths

CS350: Data Structures Dijkstra s Shortest Path Alg.

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.

Algorithms for Data Science

Shortest Path Routing Communications networks as graphs Graph terminology Breadth-first search in a graph Properties of breadth-first search

Minimum Spanning Trees My T. UF

Sample Solutions to Homework #4

Description of The Algorithm

CS 125 Section #5 Graph Traversal and Linear Programs October 6, 2016

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Unit 2: Algorithmic Graph Theory

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Dr. Alexander Souza. Winter term 11/12

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

Last week: Breadth-First Search

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

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

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 20. Example. Shortest Paths Definitions

Data Structures and Algorithms

Minimum Spanning Trees Ch 23 Traversing graphs

Depth-first Search (DFS)

Design and Analysis of Algorithms

Algorithm Design and Analysis

Representations of Graphs

Fundamental Algorithms CSCI-GA /Summer Solution to Homework 8

Design and Analysis of Algorithms

In this chapter, we consider some of the interesting problems for which the greedy technique works, but we start with few simple examples.

1 Single Source Shortest Path Algorithms

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

Design and Analysis of Algorithms

TIE Graph algorithms

Dist(Vertex u, Vertex v, Graph preprocessed) return u.dist v.dist

Shortest Paths: Basics. Algorithms and Networks 2016/2017 Johan M. M. van Rooij Hans L. Bodlaender

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

Solution. violating the triangle inequality. - Initialize U = {s} - Add vertex v to U whenever DIST[v] decreases.

Shortest Paths. CSE 373 Data Structures Lecture 21

COL 702 : Assignment 1 solutions

Graph Representation

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

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 6: BFS and Dijkstra s

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

Transcription:

Title Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis Nesrine Damak Fakultät für Informatik TU München 20. September 2010 Nesrine Damak: Classical Shortest-Path Algorithms 1/ 35

Outline 1 Introduction Definitions The Shortest Path Problem 2 Single-Source Shortest Paths Breadth First Search BFS Dijkstra-Algorithm Bellman-Ford 3 All Pairs Shortest Paths Floyd Warshall algorithm Johnson algorithm Nesrine Damak: Classical Shortest-Path Algorithms 2/ 35

Introduction Definitions Definition A graph G(V,E) is a set V of vertices, a set E of edges, and a real-valued weight function w : E R. Definition A path P in G is a sequence of vertices (v 1,..,v n ) such that (v i,v i+1 ) E for all 1 i < n and v V. Definition The weight of a path P in G is the sum of the weights of its edges: w(p) = i w(v i 1,v i ). Nesrine Damak: Classical Shortest-Path Algorithms 3/ 35

Introduction Definitions Definition For nodes u,v V, the shortest path weight from u to v is defined to be: δ(u,v) = min(w(p)) if such a P exists or infinity otherweise. Definition For nodes u,v V a shortest path from u to v is a path with w(p) = δ(u,v) Nesrine Damak: Classical Shortest-Path Algorithms 4/ 35

Introduction The Shortest Path Problem Applications of the Shortest Path Problem find the best route to drive between Berlin and Munich or figure how to direct packets to a destination across a network image segmentation speech recognition find the center point of a graph: the vertex that minimizes the maximum distance to any other vertex in the graph. Nesrine Damak: Classical Shortest-Path Algorithms 5/ 35

Single-Source Shortest Paths Input: A graph G =(V,E), an edge weight function w and a node s V. Output: A shortest path P from s to all other vertices v V {s}. Algorithmen: unweighted case : BFS no negativ edge-weights : Dijkstra-Algorithm general case : Bellman-Ford Algorithm Nesrine Damak: Classical Shortest-Path Algorithms 6/ 35

Single-Source Shortest Paths Breadth First Search BFS Notation dist(x): distance from the initial node to x, the shortest path found by the algorithm so far Q: FIFO queue Nesrine Damak: Classical Shortest-Path Algorithms 7/ 35

Single-Source Shortest Paths Breadth First Search BFS Suppose that w(u,v) = 1 for all uv E We use a simple FIFO queue Analysis Time: O( V + E ) BFS(G) while Q empty do u DEQUEUE(Q) ; for each v adj(u) do if dist(v) = then dist(v) = dist(u)+1 ; ENQUEUE(Q,v) ; Nesrine Damak: Classical Shortest-Path Algorithms 8/ 35

Single-Source Shortest Paths Breadth First Search BFS BFS is used to solve following problems: Testing whether graph is connected. Computing a spanning forest of graph. Computing, for every vertex in graph, a path with the minimum number of edges between start vertex and current vertex or reporting that no such path exists. Computing a cycle in graph or reporting that no such cycle exists. Nesrine Damak: Classical Shortest-Path Algorithms 9/ 35

Single-Source Shortest Paths Dijkstra-Algorithm Notation S:a set of vertices, whose shortest path distances from s are known ( the solved set ). dist(x): distance from the initial node to x Q: priority queue maintaining V-S pred(v): the predecessor of the vertex v Nesrine Damak: Classical Shortest-Path Algorithms 10/ 35

Single-Source Shortest Paths Dijkstra-Algorithm Dijkstra(G,w,s) dist(s) 0; for v V {s} do dist(v) ; S empty ; Q V ; while Q empty do u Extract Min(Q) ; S S {u} ; for v adj(u) do if dist(v) > dist(u)+w(u,v) then dist(v) dist(u)+w(u,v) ; Nesrine Damak: Classical Shortest-Path Algorithms 11/ 35

Single-Source Shortest Paths Dijkstra-Algorithm Nesrine Damak: Classical Shortest-Path Algorithms 12/ 35

Single-Source Shortest Paths Dijkstra-Algorithm Proof of correctness Theorem Dijkstras Algorithm terminates with dist(v) = δ(s,v) for all v V. Proof. We show: dist(v) = δ(s,v) for every v V when v is added to S. Supposition: v is the first vertex added to S, so that dist(v) > δ(s,v) y is the first vertex in V S along a shortest path from s to v x = pred(y) S then: dist(x) = δ(s,x) xy is relaxed: dist(y) = δ(s,y) δ(s,v) < dist(v) CONTRADICTION because dist(v) dist(y)! Nesrine Damak: Classical Shortest-Path Algorithms 13/ 35

Single-Source Shortest Paths Dijkstra-Algorithm s x y v Nesrine Damak: Classical Shortest-Path Algorithms 14/ 35

Single-Source Shortest Paths Dijkstra-Algorithm Time complexity Supposition: Fibonacci-heaps Initialization O( V ) Extract-Min V O(log V ) DecreaseKey E O( V ) so is the time complexity of Dijkstra :O( E + V log V ) Nesrine Damak: Classical Shortest-Path Algorithms 15/ 35

Single-Source Shortest Paths Bellman-Ford The main idea is that if the edge uv is the last edge of the shortest path to v, the cost of the shortest path to v is the cost of the shortest path to u plus the weight of w(u,v). Bellman-Ford algorithm finds all shortest-path lengths from a source s V to all v V or determines that a negative-weight cycle exists. Nesrine Damak: Classical Shortest-Path Algorithms 16/ 35

Single-Source Shortest Paths Bellman-Ford Bellman-Ford(G,w,s) dist(s) 0; for v V {s} do dist(v) ; pred(v) := null; Nesrine Damak: Classical Shortest-Path Algorithms 17/ 35

Single-Source Shortest Paths Bellman-Ford for i from 1 to V 1 do for each edge uv E do if dist(u)+w(u,v) < dist(v) then dist(v) := dist(u)+w(u,v) ; pred(v) := u ; for each edge uv E do if dist(u)+w(u,v) < dist(v) then error Graph contains a negative-weight cycle ; Nesrine Damak: Classical Shortest-Path Algorithms 18/ 35

Single-Source Shortest Paths Bellman-Ford Nesrine Damak: Classical Shortest-Path Algorithms 19/ 35

Single-Source Shortest Paths Bellman-Ford Proof of correctness Theorem if G = (V,E) contains no negative-weight cycles, then after the Bellman-Ford algorithm executes, dist(v) = δ(s,v) for all v V Proof. let v V be any vertex, and consider a shortest path P from s to v with the minimum number of edges. we have: δ(s,v i ) = δ(s,v i 1 )+w(v i 1,v i ). Initially: dist(v 0 ) = 0 = δ(s,v 0 ) After 1 pass through E : dist(v 1 ) = δ(s,v 1 ).. After k passes through E : dist(v k ) = δ(s,v k ) Longest simple path has V 1 edges. Nesrine Damak: Classical Shortest-Path Algorithms 20/ 35

Single-Source Shortest Paths Bellman-Ford Time complexity The Bellman-Ford algorithm simply relaxes all the edges, and does this V 1 times: It runs in O( V E ) time: the best time bound for the sssp problem. Nesrine Damak: Classical Shortest-Path Algorithms 21/ 35

All Pairs Shortest Paths Input: A connected graph G=(V,E) and an edge weight function w. Output: For all pairs u,v V of nodes a shortest path from u to v a V V -matrix. Possible algorithms: Naive implementation: Use standard single-source algorithms V times Dijkstra : running a O(E +VlogV) process V times Bellman Ford algorithm on a dense graph will take about O(V 2 E) Floyd Warshall algorithm Johnson algorithm Nesrine Damak: Classical Shortest-Path Algorithms 22/ 35

All Pairs Shortest Paths Floyd Warshall algorithm Notation dist (k) (i,j) is the distance from i to j with intermediate vertices belonging to the set {1,2,...,k} Nesrine Damak: Classical Shortest-Path Algorithms 23/ 35

All Pairs Shortest Paths Floyd Warshall algorithm Floyd-Warshall(G,w) for i = 1 to V do for j = 1 to V do if there is an edge from i to j then dist (0) (i,j) = the length of the edge from i to j ; dist (0) (i,j) = ; for k = 1 to V do for i = 1 to V do for j = 1 to V do dist (k) (i,j) = min(dist (k 1) (i,j),dist (k 1) (i,k)+dist (k 1) (k,j)) ; Nesrine Damak: Classical Shortest-Path Algorithms 24/ 35

All Pairs Shortest Paths Floyd Warshall algorithm The algorithms running time is clearly O( V 3 ) DIST (k) is the V V Matrix [dist (k) (i,j)]. dist (0) (i,j) = w(i,j) (no intermediate vertex = the edge from i to j ) Claim: dist ( V ) (i,j) is the length of the shortest path from i to j. So our aim is to compute DIST ( V ). Subproblems: compute DIST (k) for k = 0,..., V (dynamic!) Nesrine Damak: Classical Shortest-Path Algorithms 25/ 35

All Pairs Shortest Paths Floyd Warshall algorithm How do we compute dist (k) (i,j) assuming that we have already computed the previous matrix DIST (k 1)? We dont go through k at all: Then the shortest path from i to j uses only intermediate vertices {1,...,k 1} and hence the length of the shortest path is dist (k 1) (i,j). Nesrine Damak: Classical Shortest-Path Algorithms 26/ 35

All Pairs Shortest Paths Floyd Warshall algorithm We do go through k: we can assume that we pass through k exactly once (no negative cycles). That is, we go from i to k, and then from k to j: we should take the shortest path from i to k, and the shortest path from k to j (optimality). Each of these paths uses intermediate vertices only in {1,2,..,k 1}. The length of the path is : dist (k 1) (i,k)+dist (k 1) (k,j). This suggests the following recursive rule for computing DIST (k) : dist (0) (i,j) = w(i,j) dist (k) (i,j) = min(dist (k 1) (i,j),dist (k 1) (i,k)+dist (k 1) (k,j)) After V iterations, dist V (i,j) is the shortest path between i and j. Nesrine Damak: Classical Shortest-Path Algorithms 27/ 35

All Pairs Shortest Paths Johnson algorithm Notation a shortest-path tree rooted at the source vertex s is a directed subgraph G = (V,E ) where V and E are subsets of V and E respectively, such that V is the set of vertices reachable from s in G G forms a rooted tree with root s for all v V, the unique path from s to v G is the shortest path from s to v in G Nesrine Damak: Classical Shortest-Path Algorithms 28/ 35

Idea: All Pairs Shortest Paths Johnson algorithm Add a new node s so that w(s,v) = 0 for all v V a new graph G. Use the BellmanFord algorithm to check for negative weight cycles and find h(v) = δ(s,v) in G. Reweight the edges using h(v) with the reweighting function ŵ(u,v) w(u,v)+h(u) h(v). Use Dijkstras algorithm on the transformed graph (with no negative edges) in order to find the shortest path. Pseudocode Johnson(G,w) Compute G, where V[G ] = V[G] s ; E[G ] = E[G] (s,v) : v V[G] ; for all v V[G] do w(s,v) = 0 ; Nesrine Damak: Classical Shortest-Path Algorithms 29/ 35

All Pairs Shortest Paths Johnson algorithm if BELLMAN FORD(G,w,s) = FALSE then print the input graph contains a negative weight cycle ; for each vertex v V[G ] do set h(v) to the value of δ(s,v) computed by the Bellman-Ford alg.; for each edge (u,v) E[G ] do ŵ(u,v) w(u,v)+h(u) h(v) ; for each vertex u V[G] do run DIJKSTRA(G,ŵ,u) to compute δ (u,v) for all v V[G]; for each vertex v V[G] do dist(u,v) δ (u,v)+h(v) h(u) ; Nesrine Damak: Classical Shortest-Path Algorithms 30/ 35

All Pairs Shortest Paths Johnson algorithm Nesrine Damak: Classical Shortest-Path Algorithms 31/ 35

All Pairs Shortest Paths Johnson algorithm Are all the ŵ s non-negative? YES δ(s,u)+w(u,v) δ(s,v) otherwise s u v would be shorter than the shortest path s v Nesrine Damak: Classical Shortest-Path Algorithms 32/ 35

All Pairs Shortest Paths Johnson algorithm Does the reweighting preserve the shortest path? YES ŵ(p) = ŵ(v i,v i+1 ) = w(v 1,v 2 )+δ(s,v 1 ) δ(s,v 2 )+...+w(v k 1,v k )+δ(s,v k 1 ) δ(s,v k ) = w(p)+δ(s,v 1 ) δ(s,v k ) Nesrine Damak: Classical Shortest-Path Algorithms 33/ 35

All Pairs Shortest Paths Johnson algorithm Time complexity 1 Computing G: Θ(V) 2 Bellman-Ford: Θ(VE) 3 Reweighting: Θ(E) 4 Running (Modified) Dijkstra: Θ(V 2 lgv +VE) 5 Adjusting distances: Θ(V 2 ) Total is dominated by Dijkstra: Θ(V 2 lgv +VE) Nesrine Damak: Classical Shortest-Path Algorithms 34/ 35

All Pairs Shortest Paths Johnson algorithm Thank you! Nesrine Damak: Classical Shortest-Path Algorithms 35/ 35