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

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

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

Design and Analysis of Algorithms

CS 4407 Algorithms Lecture: Shortest Path Algorithms

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

Single Source Shortest Paths

Single Source Shortest Paths

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

Shortest Path Algorithms

Introduction to Algorithms

Introduction to Algorithms

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

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

Design and Analysis of Algorithms

Introduction to Algorithms

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

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

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

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

Algorithms Booklet. 1 Graph Searching. 1.1 Breadth First Search

Introduction to Algorithms

Dynamic Programming: Shortest Paths and DFA to Reg Exps

BFS Dijkstra. Oct abhi shelat

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Examination paper for TDT4120 Algorithms and Data Structures

Query Processing in Spatial Network Databases

Algorithm Design and Analysis

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

Myriad of applications

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

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

Data Structures and and Algorithm Xiaoqing Zheng

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

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

Shortest paths with negative lengths

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

CS 241 Analysis of Algorithms

University of Toronto Department of Electrical and Computer Engineering. Final Examination. ECE 345 Algorithms and Data Structures Fall 2016

Discrete Optimization 2010 Lecture 3 Maximum Flows

CSE 4502/5717 Big Data Analytics Spring 2018; Homework 1 Solutions

Introduction to Algorithms

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

Lecture Notes for Chapter 25: All-Pairs Shortest Paths

A Simple Implementation Technique for Priority Search Queues

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

7.4 DO (uniqueness of minimum-cost spanning tree) Prove: if all edge weights are distinct then the minimum-cost spanning tree is unique.

Algorithms: Lecture 12. Chalmers University of Technology

NP-complete problems. CSE 101: Design and Analysis of Algorithms Lecture 20

Shortest paths: label setting

Partha Sarathi Mandal

Algorithm Design and Analysis

CMPSCI 611: Advanced Algorithms

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

Problem set 1. (c) Is the Ford-Fulkerson algorithm guaranteed to produce an acyclic maximum flow?

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

FINAL EXAM PRACTICE PROBLEMS CMSC 451 (Spring 2016)

25. Minimum Spanning Trees

Trees. A tree is a graph which is. (a) Connected and. (b) has no cycles (acyclic).

25. Minimum Spanning Trees

Fundamental Algorithms 11

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

Flows. Chapter Circulations

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

Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.

Mon Tue Wed Thurs Fri

Decision Diagrams and Dynamic Programming

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

COMP251: Bipartite graphs

CS 580: Algorithm Design and Analysis

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

Running Time. Assumption. All capacities are integers between 1 and C.

Yet another bidirectional algorithm for shortest paths

Generic ǫ-removal and Input ǫ-normalization Algorithms for Weighted Transducers

MA015: Graph Algorithms

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

CSE 591 Foundations of Algorithms Homework 4 Sample Solution Outlines. Problem 1

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

Matching Residents to Hospitals

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

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

SI545 VLSI CAD: Logic to Layout. Algorithms

CSI 4105 MIDTERM SOLUTION

Reductions. Reduction. Linear Time Reduction: Examples. Linear Time Reductions

A new bidirectional algorithm for shortest paths

ICS 252 Introduction to Computer Design

Maximum flow problem (part I)

Fibonacci (Min-)Heap. (I draw dashed lines in place of of circular lists.) 1 / 17

CSE 202 Homework 4 Matthias Springer, A

Algorithm Design and Analysis (NTU CSIE, Fall 2017) Homework #3. Homework #3. Due Time: 2017/12/14 (Thu.) 17:20 Contact TAs:

CMU Lecture 4: Informed Search. Teacher: Gianni A. Di Caro

L feb abhi shelat

9.1 HW (3+3+8 points) (Knapsack decision problem NP-complete)

Correctness of Dijkstra s algorithm

Chapter 6. Dynamic Programming. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 11. Approximation Algorithms. Slides by Kevin Wayne Pearson-Addison Wesley. All rights reserved.

All-Pairs Shortest Paths

CS 161: Design and Analysis of Algorithms

CS781 Lecture 3 January 27, 2011

6. DYNAMIC PROGRAMMING II

GRAPH ALGORITHMS Week 3 (16-21 October 2017)

Transcription:

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

Homework 3 released Due on 12/13 (Thur) 14:20 (one week only) Mini-HW 9 released Due on 12/13 (Thur) 14:20 Homework 4 released Due on 1/3 (Thur) 14:20 (four weeks later) Frequently check the website for the updated information! 2

3

Single-Source Shortest Paths Bellman-Ford Algorithm Lawler Algorithm (SSSP in DAG) Dijkstra Algorithm 4

5 Textbook Chapter 24 Single-Source Shortest Paths

Input: a weighted, directed graph G = V, E Weights can be arbitrary numbers, not necessarily distance Weight function needs not satisfy triangle inequality Output: a minimal-cost path from s to t s.t. δ s, t is the minimum weight from s to t Problem Variants Single-source shortest-path problem Single-destination shortest-path problem Single-pair shortest-path problem All-pair shortest path problem 6

Can a shortest path contain a negative-weight edge? Yes. Can a shortest path contain a negative-weight cycle? Doesn t make sense. Can a shortest path contain a cycle? No. 7

Input: a weighted, directed graph G = V, E and a source vertex s Output: a minimal-cost path from s to t, where t V 8

Let G = V, E be a weighted, directed graph with no negative-weight cycles reachable from s A shortest path tree G = V, E of s is a subgraph of G s.t. 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 simple path from s to v in G is a shortest path from s to v in G 9

Input: a weighted, directed graph G = V, E and a vertex s Output: a tree T rooted at s s.t. the path from s to u of T is a shortest path from s to u in G 10

The shortest path tree problem is equivalent to finding the minimal cost δ s, u from s to each node u in G The minimal cost from s to u in G is the length of any shortest path from s to u in G equivalence : a solution to either problem can be obtained from a solution to the other problem in linear time Shortest Path Tree Problem = Single-Source Shortest Path Problem 11

12 Textbook Chapter 24.1 The Bellman-Ford algorithm

Richard Bellman, 1920~1984 Norbert Wiener Prize in Applied Mathematics, 1970 Dickson Prize, Carnegie-Mellon University, 1970 John von Neumann Theory Award, 1976. IEEE Medal of Honor, 1979, Fellow of the American Academy of Arts and Sciences, 1975. Membership in the National Academy of Engineering, 1977 Lester R. Ford, Jr. 1927~2017 Proved the algorithm before Bellman An important contributor to the theory of network flow. 13

Idea: estimate the value of d u to approximate δ s, u Initialization Let d u = for u G Let d s = 0 Repeat the following step for sufficient number of phases For each edge u, v E, relax edge u, v Relaxing: If d v > d u + w u, v, let d v = d u + w u, v improve the estimation of d u 14

0 3 2 1 6 4 2 7 5 5 6 3 15

0 3 6 2 1 6 4 2 7 5 5 6 3 16

0 3 63 2 1 6 4 2 7 5 5 6 3 17

Observation: let P be a shortest path from s to r For any vertex u in P, the subpath of P from s to u has to be a shortest path from s to u optimal substructure For any edge u, v in P, if d u = δ s, u, then d v = δ s, v also holds after relaxing edge u, v If G contains no negative cycles, then each node u has a shortest path from s to u that has at most n 1 edges From observation, after the first i phases of improvement via relaxation, the estimation of d u for the first i + 1 nodes u in the path is precise (= δ s, u ) s u r n 1 phases 18

s u 19

BELLMAN-FORD(G, w, s) INITIALIZATION(G, s) for i = 1 to G.V - 1 for (u, v) in G.E RELAX(u, v, w) Time complexity: INITIALIZATION(G, s) for v in G.V v.d = v.π = NIL s.d = 0 RELAX(u, v, w) if v.d > u.d + w(u, v) // DECREASE-KEY v.d = u.d + w(u, v) v.π = u How to do if there is a negative cycle in the graph? 20

Q: How do we know G has negative cycles? A: Using another phase of improvement via relaxation Run another phase of improving the estimation of d u for each vertex u V via relaxing all edges E If in the n-th phase, there are still some d u being modified, we know that G has negative cycles 21

negative If there exists a negative cycle in G, in the n-th phase, there are still some d u being modified. Proof by contradiction Let C be a negative cycle of k nodes v 1, v 2,, v k (v k+1 = v 1 ) Assume d v i for all 1 i k are not changed in a phase of improvement, then for 1 i k Summing all k inequalities, the sum of edge weights of C is nonnegative 22

BELLMAN-FORD(G, w, s) INITIALIZATION(G, s) for i = 1 to G.V - 1 for (u, v) in G.E RELAX(u, v, w) for (u, v) in G.E if v.d > u.d + w(u, v) return FALSE return TRUE Time complexity: negative cycle detection Finding a shortest-path tree of G: INITIALIZATION(G, s) for v in G.V v.d = v.π = NIL s.d = 0 RELAX(u, v, w) if v.d > u.d + w(u, v) // DECREASE-KEY v.d = u.d + w(u, v) v.π = u 23

24 Textbook Chapter 24.2 Single-source shortest paths in directed acyclic graphs

Input: a weighted, directed, and acyclic graph G = V, E and a source vertex s Output: a shortest-path distance from s to t, where t V No negative cycle! 25

Idea: one phase relaxation Perform a topological sort in linear time on the input DAG For i = 1 to n Let v i be the i-th node in the above order Relax each outgoing edge (v i, u) from v i Time complexity: s 26

Assume this is a shortest path from s to u If we follow the order from topological sort to relax the vertices edges, in this shortest path, the left edge must be relaxed before the right edge One phase of improvement is enough s u 27

28 Textbook Chapter 24.3 Dijkstra s algorithm

Input: a non-negative weighted, directed, graph G = V, E and a source vertex s Output: a shortest-path distance from s to t, where t V No negative cycle! 29

Idea: BFS finds shortest paths on unweighted graph by expanding the search frontier Initialization Loops for n iterations, where each iteration relax outgoing edges of an unprocessed node u with minimal d u marks u as processed 30

1 0 10 2 3 9 4 6 5 7 2 31

1 10 0 10 2 3 9 4 6 5 7 5 2 32

1 8 14 0 10 2 3 9 4 6 5 7 5 7 2 33

1 8 13 0 10 2 3 9 4 6 5 7 5 7 2 34

1 8 9 0 10 2 3 9 4 6 5 7 5 7 2 35

1 8 9 0 10 2 3 9 4 6 5 7 5 7 2 36

1 8 9 0 10 2 3 9 4 6 5 7 5 7 2 37

The vertex selected by Dijkstra s algorithm into the processed set must precise estimation of its shortest path distance. Prove by contradiction Assume u is the first vertex for being processed (minimal distance) Let a shortest path P from s to u, x is the last vertex in P from S y is the first vertex in P not from S d y = δ s, y because x, y is relaxed when putting x into S y should be processed before u, contradiction. 38 s processed nodes S The first node x y u a shortest path from s to u

DIJKSTRA(G, w, s) INITIALIZATION(G, s) S = empty Q = G.v // INSERT while Q empty u = EXTRACT-MIN(Q) S = S {u} for v in G.adj[u] RELAX(u, v, w) Min-priority queue INSERT: EXTRACT-MIN: DECREASE-KEY: Total complexity: INITIALIZATION(G, s) for v in G.V v.d = v.π = NIL s.d = 0 RELAX(u, v, w) if v.d > u.d + w(u, v) // DECREASE-KEY v.d = u.d + w(u, v) v.π = u 39

DIJKSTRA(G, w, s) INITIALIZATION(G, s) S = empty Q = G.v // INSERT while Q empty u = EXTRACT-MIN(Q) S = S {u} for v in G.adj[u] RELAX(u, v, w) Fibonacci heap (Textbook Ch. 19) BUILD-MIN-HEAP: EXTRACT-MIN: DECREASE-KEY: Total complexity: (amortized) (amortized) INITIALIZATION(G, s) for v in G.V v.d = v.π = NIL s.d = 0 RELAX(u, v, w) if v.d > u.d + w(u, v) // DECREASE-KEY v.d = u.d + w(u, v) v.π = u 40

Single-Source Shortest Paths Bellman-Ford Algorithm (general graph and weights) and detecting negative cycles Lawler Algorithm (acyclic graph) Dijkstra Algorithm (non-negative weights) with Fibonacci heap 41

42 Important announcement will be sent to @ntu.edu.tw mailbox & post to the course website Course Website: http://ada.miulab.tw Email: ada-ta@csie.ntu.edu.tw