Introduction to Algorithms

Similar documents
Introduction to Algorithms

Introduction to Algorithms

Introduction to Algorithms

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CMPS 2200 Fall Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk. 10/8/12 CMPS 2200 Intro.

Single Source Shortest Paths

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

Single Source Shortest Paths

CS 4407 Algorithms Lecture: Shortest Path Algorithms

Design and Analysis of Algorithms

Lecture 11. Single-Source Shortest Paths All-Pairs Shortest Paths

Lecture Notes for Chapter 25: All-Pairs Shortest Paths

Introduction to Algorithms

Analysis of Algorithms. Outline. Single Source Shortest Path. Andres Mendez-Vazquez. November 9, Notes. Notes

CS 253: Algorithms. Chapter 24. Shortest Paths. Credit: Dr. George Bebis

Shortest Path Algorithms

Discrete Wiskunde II. Lecture 5: Shortest Paths & Spanning Trees

Data Structures and and Algorithm Xiaoqing Zheng

Algorithm Design and Analysis

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

Design and Analysis of Algorithms

Shortest Paths. CS 320, Fall Dr. Geri Georg, Instructor 320 ShortestPaths 3

Maximum Flow. Reading: CLRS Chapter 26. CSE 6331 Algorithms Steve Lai

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Discrete Optimization 2010 Lecture 3 Maximum Flows

Myriad of applications

Algorithm Design and Analysis

Partha Sarathi Mandal

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

Analysis of Algorithms I: All-Pairs Shortest Paths

Slides credited from Hsueh-I Lu & Hsu-Chun Hsiao

BFS Dijkstra. Oct abhi shelat

COMP251: Bipartite graphs

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Algorithms: Lecture 12. Chalmers University of Technology

All-Pairs Shortest Paths

Algorithms Booklet. 1 Graph Searching. 1.1 Breadth First Search

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Lecture 7: Shortest Paths in Graphs with Negative Arc Lengths. Reading: AM&O Chapter 5

A faster algorithm for the single source shortest path problem with few distinct positive lengths

COMP251: Bipartite graphs

Contents Lecture 4. Greedy graph algorithms Dijkstra s algorithm Prim s algorithm Kruskal s algorithm Union-find data structure with path compression

16.1 Min-Cut as an LP

CS 6301 PROGRAMMING AND DATA STRUCTURE II Dept of CSE/IT UNIT V GRAPHS

CS 241 Analysis of Algorithms

Query Processing in Spatial Network Databases

CMPSCI 611: Advanced Algorithms

CS781 Lecture 3 January 27, 2011

Introduction to Algorithms

iretilp : An efficient incremental algorithm for min-period retiming under general delay model

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Breadth-First Search of Graphs

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

ICS 252 Introduction to Computer Design

Lecture Notes Discrete Optimization

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Supplementary material for Continuous-action planning for discounted infinite-horizon nonlinear optimal control with Lipschitz values

CS 410/584, Algorithm Design & Analysis, Lecture Notes 4

Shortest paths with negative lengths

CS 580: Algorithm Design and Analysis

directed weighted graphs as flow networks the Ford-Fulkerson algorithm termination and running time

Impossibility of Distributed Consensus with One Faulty Process

Algorithm Design and Analysis

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

ne a priority queue for the nodes of G; ile (! PQ.is empty( )) select u PQ with d(u) minimal and remove it; I Informatik 1 Kurt Mehlhorn

CSE 431/531: Analysis of Algorithms. Dynamic Programming. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

L feb abhi shelat

6.889 Lecture 4: Single-Source Shortest Paths

Scribes: Po-Hsuan Wei, William Kuzmaul Editor: Kevin Wu Date: October 18, 2016

Algorithm Design and Analysis

Graph fundamentals. Matrices associated with a graph

from notes written mostly by Dr. Matt Stallmann: All Rights Reserved

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

MA015: Graph Algorithms

Introduction to Algorithms

Matching Residents to Hospitals

Solutions to Exercises

CS675: Convex and Combinatorial Optimization Fall 2014 Combinatorial Problems as Linear Programs. Instructor: Shaddin Dughmi

Discrete Optimization Lecture 5. M. Pawan Kumar

Problem Set. Problems on Unordered Summation. Math 5323, Fall Februray 15, 2001 ANSWERS

IS 709/809: Computational Methods in IS Research Fall Exam Review

Maximum Flow. Jie Wang. University of Massachusetts Lowell Department of Computer Science. J. Wang (UMass Lowell) Maximum Flow 1 / 27

Shortest paths: label setting

Definition: Let f(x) be a function of one variable with continuous derivatives of all orders at a the point x 0, then the series.

Discrete Mathematics, Spring 2004 Homework 9 Sample Solutions

Tropical Arithmetic and Shortest Paths

Mathematical Notation Math Calculus & Analytic Geometry III

Figure 1: Bayesian network for problem 1. P (A = t) = 0.3 (1) P (C = t) = 0.6 (2) Table 1: CPTs for problem 1. (c) P (E B) C P (D = t) f 0.9 t 0.

CHOW S LEMMA. Matthew Emerton

Branch-and-Bound for the Travelling Salesman Problem

CS675: Convex and Combinatorial Optimization Fall 2016 Combinatorial Problems as Linear and Convex Programs. Instructor: Shaddin Dughmi

6. DYNAMIC PROGRAMMING II. sequence alignment Hirschberg's algorithm Bellman-Ford distance vector protocols negative cycles in a digraph

The Matching Polytope: General graphs

Complexity Theory of Polynomial-Time Problems

Mathematical Notation Math Calculus & Analytic Geometry III

Part IA Algorithms Notes

Ngày 20 tháng 7 năm Discrete Optimization Graphs

Lecture XIV MINIMUM COST PATHS

Transcription:

Introduction to Algorithms, Lecture 5 // Introduction to Algorithms 6.46J/.4J LECTURE Shortest Paths I Properties o shortest paths Dijkstra s Correctness Analysis Breadth-irst Pro. Manolis Kellis March, L. Deinitions: Paths in graphs Consider a digraph G = (V, E) with edge-weight unction w : E. The weight o path p = v v L v k is deined to be: Example: k i= w ( p) = w( v i, v i + ). v 4 v 5 v 5 v v 4 w(p) = March, L. Deinitions: Shortest paths A shortest path rom u to v is a path o minimum weight rom u to v. The shortest-path weight rom u to v is deined as δ(u, v) = min{w(p) : p is a path rom u to v}. Note: δ(u, v) = i no path rom u to v exists. March, L. Shortest path properties:. Well-deinedness I a graph G contains a negative-weight cycle, then some shortest paths do not exist. Example: < vv <: Shortest path does not exist >: Cycle can be removed =: Choose no cycle, same cost, ewer edges, wlog Only nd to consider paths o V - edges! March, L.4 Shortest path properties:. Triangle inequality Shortest path properties:. Optimal Substructure Theorem. For all s, u, v V, we have δ(s, v) δ(s, u) + w(u, v). Theorem. Any subpath i~j o a shortest path s~k is a shortest path. Proo. ss δ(s, v) vv Proo. Cut and paste: δ(s, u) w(u, v) s i j k I optimal path p uses (u,v), then δ(s, v) = δ(s, u)+w(u, v). Else: proo by contradiction. I w p (s~>v)>δ(s, u)+w(u, v), then can make shorter path p by going through u. I not, make shorter path by s~i~j~k March, L.5 March, L.6 Leiserson, Demaine, Kellis

Introduction to Algorithms, Lecture 5 // Today Thursday Shortest paths: Problem settings Setting All pairs Weights Grdy/DP Algorithm = Grdy BFS General General Grdy Dyn. Prog. Dyn. Prog. Dijkstra Bellman-Ford MX-mult, Floyd- Warshall, Johnson March, L. S Grdy choice property (only i all weights are ) ss ww 4 9 vv Maintain set S o nodes whose shortest path distances are minimal. At each step, include the vertex v whose current distance estimate rom S is minimum, through edge (u,v). Then edge (u,v) must be part o a shortest path, since any other indirect path to v must go through a longer-distance intermediate (because path costs never decrease, all w ). March, L. zz Note: Full proo also shows that our estimates are correct Weighted graphs (non-negative) Dijkstra s March, L.9 Single-source shortest paths (non-negative edge weights) Problem. Assume that w(u, v) or all (u, v) E. (Hence, all shortest-path weights must exist.) From a given source vertex s V, ind the shortest-path weights δ(s, v) or all v V. Algorithm: Grdy choice.. Maintain a set S o vertices whose shortestpath distances rom s are known.. At each step, a to S the vertex v V S whose distance estimate rom s is minimum.. Update the distance estimates o vertices adjacent to v. March, L. Dijkstra s d[s] or each v V {s} do d[v] S Q V Q is a priority queue maintaining V S, keyed on d[v] while Q do u EXTRACT-MIN(Q) S S {u} or each v Adj[u] do i d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) relaxation step Implicit DECREASE-KEY March, L. Example o Dijkstra s Graph with nonnegative edge weights: March, L. 4 9 EE Leiserson, Demaine, Kellis

Introduction to Algorithms, Lecture 5 // Initialize: Example o Dijkstra s S: {} 4 9 EE Example o Dijkstra s A EXTRACT-MIN(Q): S: { A } 4 9 EE March, L. March, L.4 Example o Dijkstra s Relax all edges leaving A: S: { A } 4 9 EE Example o Dijkstra s C EXTRACT-MIN(Q): 4 9 S: { A, C } EE March, L.5 March, L.6 Example o Dijkstra s Relax all edges leaving C: 5 4 9 S: { A, C } EE 5 Example o Dijkstra s E EXTRACT-MIN(Q): 5 4 9 EE 5 S: { A, C, E } March, L. March, L. Leiserson, Demaine, Kellis

Introduction to Algorithms, Lecture 5 // Example o Dijkstra s Relax all edges leaving E: 5 4 9 EE 5 S: { A, C, E } Example o Dijkstra s B EXTRACT-MIN(Q): 5 4 9 EE 5 S: { A, C, E, B } March, L.9 March, L. Example o Dijkstra s Relax all edges leaving B: 5 9 9 4 9 EE 5 S: { A, C, E, B } Example o Dijkstra s D EXTRACT-MIN(Q): 5 9 9 4 9 EE 5 S: { A, C, E, B, D } March, L. March, L. Dijkstra's: Proo o correctness March, L. Correctness proo: Part : Upper Bound (Th. 4.) Lemma. Initializing d[s] and d[v] or all v V {s} establishes d[v] δ(s, v) or all v V, and this invariant is maintained over any sequence o relaxation steps. (Katherine s Lemma) Proo. Prove invariant d[v] δ(s, v) or all vertices by induction over number o relaxation steps. Base case upon d[s]= and d[v]= otherwise. Inductive step upon relaxation o edge (u,v): by inductive hypothesis d[x] δ(s,x) or all x V. The only d value changing is d[v], and it becomes: d[v] = d[u] + w(u,v) δ(s,u)+w(u,v) by inductive hypothesis δ(s,v) by the triangle inequality. March, L.4 Leiserson, Demaine, Kellis 4

Introduction to Algorithms, Lecture 5 // Correctness proo: Part : Convergence (Th. 4.6) Lemma. Let u be v s predecessor on a shortest path rom s to v. Then, i d[u] = δ(s, u) and edge (u, v) is relaxed, we have d[v] =δ(s, v) ater the relaxation. Proo. Observe that δ(s, v) = δ(s, u) + w(u, v). Suppose that d[v] > δ(s, v) beore the relaxation. (Otherwise, we re done.) Then, the test d[v] > d[u] + w(u, v) succds, because d[v] > δ(s, v) = δ(s, u) + w(u, v) = d[u] + w(u, v), and the sets d[v] = d[u] + w(u, v) = δ(s, v). Correctness proo (Th. 4.6): Part : Correctness o Dijkstra Theorem. Dijkstra s terminates with d[v] = δ(s, v) or all v V. Proo. It suices to show that d[v] = δ(s, v) or every v V when v is aed to S. Suppose u is the irst vertex aed to S or which d[u] >δ(s, u). Let y be the irst vertex in V S along a shortest path rom s to u, and let x be its predecessor: S, just beore aing u. ss xx yy March, L.5 March, L.6 Correctness proo: Part : Correctness o Dijkstra ss S xx yy Dijkstra's Runtime analysis Since u is the irst vertex violating the claimed invariant, we have d[x] = δ(s, x). When x was aed to S, the edge (x, y) was relaxed, which implies that d[y]=δ(s, y) δ(s, u) < d[u]. But, d[u] d[y] by our choice o u. Contradiction. March, L. March, L. V times Analysis o Dijkstra degr(u) times while Q do u EXTRACT-MIN(Q) S S {u} or each v Adj[u] do i d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) Handshaking Lemma Θ(E) implicit DECREASE-KEY s. Time = Θ(V T EXTRACT-MIN + E T DECREASE-KEY ) Note: Same ormula as in the analysis o Prim s minimum spanning tr. March, L.9 Analysis o Dijkstra (continued) Time = Θ(V) T EXTRACT-MIN + Θ(E) T DECREASE-KEY Q T EXTRACT-MIN T DECREASE-KEY Total array O(V) O() O(V ) binary heap O(lg V) O(lg V) O(E lg V) Fibonacci heap O(lg V) amortized O() amortized O(E + V lg V) worst case March, L. Leiserson, Demaine, Kellis 5

Introduction to Algorithms, Lecture 5 // Unweighted graphs: BFS (all edges have positive, unit weights) w(u,v)= or all (u,v) March, L. Unweighted graphs Suppose that w(u, v) = or all (u, v) E. Can Dijkstra s be improved? Use a simple FIFO queue instead o a priority queue. Breadth-irst while Q do u DEQUEUE(Q) or each v Adj[u] do i d[v] = then d[v] d[u] + ENQUEUE(Q, v) Analysis: Time = O(V + E). March, L. Example o breadth-irst Example o breadth-irst cc cc a March, L. March, L.4 Example o breadth-irst Example o breadth-irst cc a b d cc a b d c e March, L.5 March, L.6 Leiserson, Demaine, Kellis 6

Introduction to Algorithms, Lecture 5 // Example o breadth-irst Example o breadth-irst cc a b d c e cc a b d c e March, L. March, L. Example o breadth-irst cc a b d c e g i Example o breadth-irst 4 cc 4 a b d c e g i March, L.9 March, L.4 Example o breadth-irst cc a b d c e g i h Example o breadth-irst cc 4 a b d c e g i h March, L.4 March, L.4 Leiserson, Demaine, Kellis

Introduction to Algorithms, Lecture 5 // Example o breadth-irst cc a b d c e g i h Example o breadth-irst cc a b d c e g i h March, L.4 March, L.44 Correctness o BFS while Q do u DEQUEUE(Q) or each v Adj[u] do i d[v] = then d[v] d[u] + ENQUEUE(Q, v) Key idea: The FIFO Q in breadth-irst mimics the priority queue Q in Dijkstra. Invariant: v comes ater u in Q implies that d[v] = d[u] or d[v] = d[u] +. Today Thursday Shortest paths: Summary Setting All pairs Weights Gr./DP Algorithm = Grdy BFS: O(V+E) General General Grdy DP DP Dijkstra: O(E+VlgV) Bellman-Ford MX-mult, Floyd- Warshall, Johnson March, L.45 March, L.46 Leiserson, Demaine, Kellis