Dynamic Programming: Shortest Paths and DFA to Reg Exps

Similar documents
Dynamic Programming: Shortest Paths and DFA to Reg Exps

Single Source Shortest Paths

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

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

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

CS 4407 Algorithms Lecture: Shortest Path Algorithms

Design and Analysis of Algorithms

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

More Dynamic Programming

Introduction to Algorithms

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

6. DYNAMIC PROGRAMMING II

Algorithms and Theory of Computation. Lecture 9: Dynamic Programming

DFA to Regular Expressions

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

Optimal Tree-decomposition Balancing and Reachability on Low Treewidth Graphs

Discrete Optimization 2010 Lecture 3 Maximum Flows

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

Deterministic Finite Automata (DFAs)

Aside: Golden Ratio. Golden Ratio: A universal law. Golden ratio φ = lim n = 1+ b n = a n 1. a n+1 = a n + b n, a n+b n a n

CS60007 Algorithm Design and Analysis 2018 Assignment 1

More Dynamic Programming

CPSC 320 (Intermediate Algorithm Design and Analysis). Summer Instructor: Dr. Lior Malka Final Examination, July 24th, 2009

Design and Analysis of Algorithms

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

1 More finite deterministic automata

Notes on State Minimization

Single Source Shortest Paths

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

Lecture 7: Dynamic Programming I: Optimal BSTs

CS 374: Algorithms & Models of Computation, Spring 2017 Greedy Algorithms Lecture 19 April 4, 2017 Chandra Chekuri (UIUC) CS374 1 Spring / 1

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

NP and NP Completeness

Deterministic Finite Automata (DFAs)

Lecture 2: Regular Expression

Introduction to Algorithms

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

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

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

CS483 Design and Analysis of Algorithms

Even More on Dynamic Programming

1 Matchings in Non-Bipartite Graphs

All-Pairs Shortest Paths

CS 161: Design and Analysis of Algorithms

Finite Automata and Formal Languages

Lecture 2: Divide and conquer and Dynamic programming

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

Partha Sarathi Mandal

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

Deterministic Finite Automata (DFAs)

Algorithms: Lecture 12. Chalmers University of Technology

CS367 Lecture 3 (old lectures 5-6) APSP variants: Node Weights, Earliest Arrivals, Bottlenecks Scribe: Vaggos Chatziafratis Date: October 09, 2015

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

CS 241 Analysis of Algorithms

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

Algorithms and Data Structures (COMP 251) Midterm Solutions

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

Chapter 8 Dynamic Programming

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

Non-context-Free Languages. CS215, Lecture 5 c

More Approximation Algorithms

10-704: Information Processing and Learning Fall Lecture 10: Oct 3

CSCE 551 Final Exam, Spring 2004 Answer Key

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

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

Algorithm Design and Analysis

Chapter 8 Dynamic Programming

Nondeterministic finite automata

SAT, NP, NP-Completeness

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

Flows. Chapter Circulations

Duality of LPs and Applications

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

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

NATIONAL UNIVERSITY OF SINGAPORE CS3230 DESIGN AND ANALYSIS OF ALGORITHMS SEMESTER II: Time Allowed 2 Hours

6.889 Lecture 4: Single-Source Shortest Paths

Analysis of Algorithms I: All-Pairs Shortest Paths

FINAL EXAM PRACTICE PROBLEMS CMSC 451 (Spring 2016)

Lecture Notes for Chapter 25: All-Pairs Shortest Paths

CSCE 551 Final Exam, April 28, 2016 Answer Key

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

Lecture 1 : Data Compression and Entropy

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

Algorithms Exam TIN093 /DIT602

Question Paper Code :

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Dynamic Programming II Date: 10/12/17

NP Completeness. CS 374: Algorithms & Models of Computation, Spring Lecture 23. November 19, 2015

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

September 11, Second Part of Regular Expressions Equivalence with Finite Aut

Lecture 8: Complete Problems for Other Complexity Classes

Regular Expressions. Definitions Equivalence to Finite Automata

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

Please give details of your answer. A direct answer without explanation is not counted.

Matching Residents to Hospitals

Midterm Exam. CS 3110: Design and Analysis of Algorithms. June 20, Group 1 Group 2 Group 3

Myriad of applications

General Methods for Algorithm Design

arxiv: v1 [cs.ds] 9 Apr 2018

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

Min/Max-Poly Weighting Schemes and the NL vs UL Problem

Transcription:

CS 374: Algorithms & Models of Computation, Spring 207 Dynamic Programming: Shortest Paths and DFA to Reg Exps Lecture 8 March 28, 207 Chandra Chekuri (UIUC) CS374 Spring 207 / 56

Part I Shortest Paths with Negative Length Edges Chandra Chekuri (UIUC) CS374 2 Spring 207 2 / 56

Single-Source Shortest Paths with Negative Edge Lengths Single-Source Shortest Path Problems Input: A directed graph G = (V, E) with arbitrary (including negative) edge lengths. For edge e = (u, v), l(e) = l(u, v) is its length. Given nodes s, t find shortest path from s to t. 2 Given node s find shortest path from s to all other nodes. s 0 9 2 3 6 8 6-6 6 30 4 5 5-8 20 9 6 6 44 7 t Chandra Chekuri (UIUC) CS374 3 Spring 207 3 / 56

Single-Source Shortest Paths with Negative Edge Lengths Single-Source Shortest Path Problems Input: A directed graph G = (V, E) with arbitrary (including negative) edge lengths. For edge e = (u, v), l(e) = l(u, v) is its length. Given nodes s, t find shortest path from s to t. 2 Given node s find shortest path from s to all other nodes. s 0 9 2 3 6 8 6-6 6 30 4 5 5-8 20 9 6 6 44 7 t Chandra Chekuri (UIUC) CS374 3 Spring 207 3 / 56

What are the distances computed by Dijkstra s algorithm? s y 5 x 5 z w The distance as computed by Dijkstra algorithm starting from s: (A) s = 0, x = 5, y =, z = 0. (B) s = 0, x =, y = 2, z = 5. (C) s = 0, x = 5, y =, z = 2. (D) IDK. Chandra Chekuri (UIUC) CS374 4 Spring 207 4 / 56

Dijkstra s Algorithm and Negative Lengths With negative length edges, Dijkstra s algorithm can fail s 5 x 5 y z w Chandra Chekuri (UIUC) CS374 5 Spring 207 5 / 56

Dijkstra s Algorithm and Negative Lengths With negative length edges, Dijkstra s algorithm can fail s 5 x 5 s 5 x 5 y z w y z w Chandra Chekuri (UIUC) CS374 5 Spring 207 5 / 56

Dijkstra s Algorithm and Negative Lengths With negative length edges, Dijkstra s algorithm can fail s 5 x 5 s 5 x 5 y z w y z 2 w Chandra Chekuri (UIUC) CS374 5 Spring 207 5 / 56

Dijkstra s Algorithm and Negative Lengths With negative length edges, Dijkstra s algorithm can fail s 5 x 5 s 5 x 5 y z w y z 2 w 3 Chandra Chekuri (UIUC) CS374 5 Spring 207 5 / 56

Dijkstra s Algorithm and Negative Lengths With negative length edges, Dijkstra s algorithm can fail s 5 x 5 s 5 5 x 5 y z w y z 2 w 3 Chandra Chekuri (UIUC) CS374 5 Spring 207 5 / 56

Dijkstra s Algorithm and Negative Lengths With negative length edges, Dijkstra s algorithm can fail s y 5 x 5 z w s 5 y 5 x 5 z 2 0 w 3 Chandra Chekuri (UIUC) CS374 5 Spring 207 5 / 56

Dijkstra s Algorithm and Negative Lengths With negative length edges, Dijkstra s algorithm can fail s y 5 x 5 z w s 5 y 5 x 5 z 0 2 Shortest path w 3 Chandra Chekuri (UIUC) CS374 5 Spring 207 5 / 56

Dijkstra s Algorithm and Negative Lengths With negative length edges, Dijkstra s algorithm can fail s y 5 x 5 z w s 5 y 5 x 5 z 0 2 Shortest path w 3 False assumption: Dijkstra s algorithm is based on the assumption that if s = v 0 v v 2... v k is a shortest path from s to v k then dist(s, v i ) dist(s, v i + ) for 0 i < k. Holds true only for non-negative edge lengths. Chandra Chekuri (UIUC) CS374 5 Spring 207 5 / 56

Negative Length Cycles Definition A cycle C is a negative length cycle if the sum of the edge lengths of C is negative. s 9 6 5 g b 0 8 f 30-8 20 e 44 c -6 6 3 d 9 t 6 Chandra Chekuri (UIUC) CS374 6 Spring 207 6 / 56

Negative Length Cycles Definition A cycle C is a negative length cycle if the sum of the edge lengths of C is negative. s 9 6 5 g b 0 8 f 30-8 20 e 44 c -6 6 3 d 9 t 6 Chandra Chekuri (UIUC) CS374 6 Spring 207 6 / 56

Shortest Paths and Negative Cycles Given G = (V, E) with edge lengths and s, t. Suppose G has a negative length cycle C, and 2 s can reach C and C can reach t. Question: What is the shortest distance from s to t? Possible answers: Define shortest distance to be: undefined, that is, OR 2 the length of a shortest simple path from s to t. Chandra Chekuri (UIUC) CS374 7 Spring 207 7 / 56

Shortest Paths and Negative Cycles Given G = (V, E) with edge lengths and s, t. Suppose G has a negative length cycle C, and 2 s can reach C and C can reach t. Question: What is the shortest distance from s to t? Possible answers: Define shortest distance to be: undefined, that is, OR 2 the length of a shortest simple path from s to t. Lemma If there is an efficient algorithm to find a shortest simple s t path in a graph with negative edge lengths, then there is an efficient algorithm to find the longest simple s t path in a graph with positive edge lengths. Finding the s t longest path is difficult. NP-Hard! Chandra Chekuri (UIUC) CS374 7 Spring 207 7 / 56

Alterantively: Finding Shortest Walks Given a graph G = (V, E): A path is a sequence of distinct vertices v, v 2,..., v k such that (v i, v i + ) E for i k. 2 A walk is a sequence of vertices v, v 2,..., v k such that (v i, v i + ) E for i k. Vertices are allowed to repeat. Define dist(u, v) to be the length of a shortest walk from u to v. If there is a walk from u to v that contains negative length cycle then dist(u, v) = 2 Else there is a path with at most n edges whose length is equal to the length of a shortest walk and dist(u, v) is finite Helpful to think about walks Chandra Chekuri (UIUC) CS374 8 Spring 207 8 / 56

Shortest Paths with Negative Edge Lengths Problems Algorithmic Problems Input: A directed graph G = (V, E) with edge lengths (could be negative). For edge e = (u, v), l(e) = l(u, v) is its length. Questions: Given nodes s, t, either find a negative length cycle C that s can reach or find a shortest path from s to t. 2 Given node s, either find a negative length cycle C that s can reach or find shortest path distances from s to all reachable nodes. 3 Check if G has a negative length cycle or not. Chandra Chekuri (UIUC) CS374 9 Spring 207 9 / 56

Shortest Paths with Negative Edge Lengths In Undirected Graphs Note: With negative lengths, shortest path problems and negative cycle detection in undirected graphs cannot be reduced to directed graphs by bi-directing each undirected edge. Why? Problem can be solved efficiently in undirected graphs but algorithms are different and more involved than those for directed graphs. Beyond the scope of this class. If interested, ask instructor for references. Chandra Chekuri (UIUC) CS374 0 Spring 207 0 / 56

Why Negative Lengths? Several Applications Shortest path problems useful in modeling many situations in some negative lenths are natural 2 Negative length cycle can be used to find arbitrage opportunities in currency trading 3 Important sub-routine in algorithms for more general problem: minimum-cost flow Chandra Chekuri (UIUC) CS374 Spring 207 / 56

Negative cycles Application to Currency Trading Currency Trading Input: n currencies and for each ordered pair (a, b) the exchange rate for converting one unit of a into one unit of b. Questions: Is there an arbitrage opportunity? 2 Given currencies s, t what is the best way to convert s to t (perhaps via other intermediate currencies)? Concrete example: Chinese Yuan = 0.6 Euro 2 Euro =.367 US dollar 3 US Dollar = 7. Chinese Yuan. Thus, if exchanging $ Yuan Euro $, we get: 0.6.367 7. =.07896$. Chandra Chekuri (UIUC) CS374 2 Spring 207 2 / 56

Reducing Currency Trading to Shortest Paths Observation: If we convert currency i to j via intermediate currencies k, k 2,..., k h then one unit of i yields exch(i, k ) exch(k, k 2 )... exch(k h, j) units of j. Chandra Chekuri (UIUC) CS374 3 Spring 207 3 / 56

Reducing Currency Trading to Shortest Paths Observation: If we convert currency i to j via intermediate currencies k, k 2,..., k h then one unit of i yields exch(i, k ) exch(k, k 2 )... exch(k h, j) units of j. Create currency trading directed graph G = (V, E): For each currency i there is a node v i V 2 E = V V : an edge for each pair of currencies 3 edge length l(v i, v j ) = Chandra Chekuri (UIUC) CS374 3 Spring 207 3 / 56

Reducing Currency Trading to Shortest Paths Observation: If we convert currency i to j via intermediate currencies k, k 2,..., k h then one unit of i yields exch(i, k ) exch(k, k 2 )... exch(k h, j) units of j. Create currency trading directed graph G = (V, E): For each currency i there is a node v i V 2 E = V V : an edge for each pair of currencies 3 edge length l(v i, v j ) = log(exch(i, j)) can be negative Chandra Chekuri (UIUC) CS374 3 Spring 207 3 / 56

Reducing Currency Trading to Shortest Paths Observation: If we convert currency i to j via intermediate currencies k, k 2,..., k h then one unit of i yields exch(i, k ) exch(k, k 2 )... exch(k h, j) units of j. Create currency trading directed graph G = (V, E): For each currency i there is a node v i V 2 E = V V : an edge for each pair of currencies 3 edge length l(v i, v j ) = log(exch(i, j)) can be negative Exercise: Verify that There is an arbitrage opportunity if and only if G has a negative length cycle. 2 The best way to convert currency i to currency j is via a shortest path in G from i to j. If d is the distance from i to j then one unit of i can be converted into 2 d units of j. Chandra Chekuri (UIUC) CS374 3 Spring 207 3 / 56

Reducing Currency Trading to Shortest Paths Math recall - relevant information log(α α 2 α k ) = log α + log α 2 + + log α k. 2 log x > 0 if and only if x >. Chandra Chekuri (UIUC) CS374 4 Spring 207 4 / 56

Shortest Paths with Negative Lengths Lemma Let G be a directed graph with arbitrary edge lengths. If s = v 0 v v 2... v k is a shortest path from s to v k then for i < k: s = v 0 v v 2... v i is a shortest path from s to v i Chandra Chekuri (UIUC) CS374 5 Spring 207 5 / 56

Shortest Paths with Negative Lengths Lemma Let G be a directed graph with arbitrary edge lengths. If s = v 0 v v 2... v k is a shortest path from s to v k then for i < k: s = v 0 v v 2... v i is a shortest path from s to v i 2 False: dist(s, v i ) dist(s, v k ) for i < k. Holds true only for non-negative edge lengths. Chandra Chekuri (UIUC) CS374 5 Spring 207 5 / 56

Shortest Paths with Negative Lengths Lemma Let G be a directed graph with arbitrary edge lengths. If s = v 0 v v 2... v k is a shortest path from s to v k then for i < k: s = v 0 v v 2... v i is a shortest path from s to v i 2 False: dist(s, v i ) dist(s, v k ) for i < k. Holds true only for non-negative edge lengths. Cannot explore nodes in increasing order of distance! We need other strategies. Chandra Chekuri (UIUC) CS374 5 Spring 207 5 / 56

Shortest Paths and Recursion Compute the shortest path distance from s to t recursively? 2 What are the smaller sub-problems? Chandra Chekuri (UIUC) CS374 6 Spring 207 6 / 56

Shortest Paths and Recursion Compute the shortest path distance from s to t recursively? 2 What are the smaller sub-problems? Lemma Let G be a directed graph with arbitrary edge lengths. If s = v 0 v v 2... v k is a shortest path from s to v k then for i < k: s = v 0 v v 2... v i is a shortest path from s to v i Chandra Chekuri (UIUC) CS374 6 Spring 207 6 / 56

Shortest Paths and Recursion Compute the shortest path distance from s to t recursively? 2 What are the smaller sub-problems? Lemma Let G be a directed graph with arbitrary edge lengths. If s = v 0 v v 2... v k is a shortest path from s to v k then for i < k: s = v 0 v v 2... v i is a shortest path from s to v i Sub-problem idea: paths of fewer hops/edges Chandra Chekuri (UIUC) CS374 6 Spring 207 6 / 56

Hop-based Recursion: Bellman-Ford Algorithm Single-source problem: fix source s. Assume that all nodes can be reached by s in G Assume G has no negative-length cycle (for now). d(v, k): shortest walk length from s to v using at most k edges. Chandra Chekuri (UIUC) CS374 7 Spring 207 7 / 56

Hop-based Recursion: Bellman-Ford Algorithm Single-source problem: fix source s. Assume that all nodes can be reached by s in G Assume G has no negative-length cycle (for now). d(v, k): shortest walk length from s to v using at most k edges. Note: dist(s, v) = d(v, n ). Chandra Chekuri (UIUC) CS374 7 Spring 207 7 / 56

Hop-based Recursion: Bellman-Ford Algorithm Single-source problem: fix source s. Assume that all nodes can be reached by s in G Assume G has no negative-length cycle (for now). d(v, k): shortest walk length from s to v using at most k edges. Note: dist(s, v) = d(v, n ). Recursion for d(v, k): Chandra Chekuri (UIUC) CS374 7 Spring 207 7 / 56

Hop-based Recursion: Bellman-Ford Algorithm Single-source problem: fix source s. Assume that all nodes can be reached by s in G Assume G has no negative-length cycle (for now). d(v, k): shortest walk length from s to v using at most k edges. Note: dist(s, v) = d(v, n ). Recursion for d(v, k): { min u V (d(u, k ) + l(u, v)). d(v, k) = min d(v, k ) Base case: d(s, 0) = 0 and d(v, 0) = for all v s. Chandra Chekuri (UIUC) CS374 7 Spring 207 7 / 56

Example e 8 2 d 0 5 3 b 8 a 4 6 3 s f 3 c Chandra Chekuri (UIUC) CS374 8 Spring 207 8 / 56

Bellman-Ford Algorithm for each u V do d(u, 0) d(s, 0) 0 for k = to n do for each v V do d(v, k) d(v, k ) for each edge (u, v) In(v) do d(v, k) = min{d(v, k), d(u, k ) + l(u, v)} for each v V do dist(s, v) d(v, n ) Chandra Chekuri (UIUC) CS374 9 Spring 207 9 / 56

Bellman-Ford Algorithm for each u V do d(u, 0) d(s, 0) 0 for k = to n do for each v V do d(v, k) d(v, k ) for each edge (u, v) In(v) do d(v, k) = min{d(v, k), d(u, k ) + l(u, v)} for each v V do dist(s, v) d(v, n ) Running time: Chandra Chekuri (UIUC) CS374 9 Spring 207 9 / 56

Bellman-Ford Algorithm for each u V do d(u, 0) d(s, 0) 0 for k = to n do for each v V do d(v, k) d(v, k ) for each edge (u, v) In(v) do d(v, k) = min{d(v, k), d(u, k ) + l(u, v)} for each v V do dist(s, v) d(v, n ) Running time: O(mn) Chandra Chekuri (UIUC) CS374 9 Spring 207 9 / 56

Bellman-Ford Algorithm for each u V do d(u, 0) d(s, 0) 0 for k = to n do for each v V do d(v, k) d(v, k ) for each edge (u, v) In(v) do d(v, k) = min{d(v, k), d(u, k ) + l(u, v)} for each v V do dist(s, v) d(v, n ) Running time: O(mn) Space: Chandra Chekuri (UIUC) CS374 9 Spring 207 9 / 56

Bellman-Ford Algorithm for each u V do d(u, 0) d(s, 0) 0 for k = to n do for each v V do d(v, k) d(v, k ) for each edge (u, v) In(v) do d(v, k) = min{d(v, k), d(u, k ) + l(u, v)} for each v V do dist(s, v) d(v, n ) Running time: O(mn) Space: O(m + n 2 ) Chandra Chekuri (UIUC) CS374 9 Spring 207 9 / 56

Bellman-Ford Algorithm for each u V do d(u, 0) d(s, 0) 0 for k = to n do for each v V do d(v, k) d(v, k ) for each edge (u, v) In(v) do d(v, k) = min{d(v, k), d(u, k ) + l(u, v)} for each v V do dist(s, v) d(v, n ) Running time: O(mn) Space: O(m + n 2 ) Space can be reduced to O(m + n). Chandra Chekuri (UIUC) CS374 9 Spring 207 9 / 56

Bellman-Ford Algorithm for each u V do d(u) d(s) 0 for k = to n do for each v V do for each edge (u, v) In(v) do d(v) = min{d(v), d(u) + l(u, v)} for each v V do dist(s, v) d(v) Running time: O(mn) Space: O(m + n) Exercise: Argue that this achieves same results as algorithm on previous slide. Chandra Chekuri (UIUC) CS374 20 Spring 207 20 / 56

Bellman-Ford: Negative Cycle Detection Check if distances change in iteration n. for each u V do d(u) d(s) 0 for k = to n do for each v V do for each edge (u, v) In(v) do d(v) = min{d(v), d(u) + l(u, v)} (* One more iteration to check if distances change *) for each v V do for each edge (u, v) In(v) do if (d(v) > d(u) + l(u, v)) Output Negative Cycle for each v V do dist(s, v) d(v) Chandra Chekuri (UIUC) CS374 2 Spring 207 2 / 56

Correctness of the Bellman-Ford Algorithm Via induction: For each v, d(v, k) is the length of a shortest walk from s to v with at most k hops. Chandra Chekuri (UIUC) CS374 22 Spring 207 22 / 56

Correctness of the Bellman-Ford Algorithm Via induction: For each v, d(v, k) is the length of a shortest walk from s to v with at most k hops. Lemma Suppose G does not have a negative length cycle reachable from s. Then for all v, dist(s, v) = d(v, n ). Moreover, d(v, n ) = d(v, n). Proof. Exercise. Corollary Bellman-Ford correctly outputs the shortest path distances if G has no negative length cycle reachable from s. Chandra Chekuri (UIUC) CS374 22 Spring 207 22 / 56

Correctness: detecting negative length cycle Lemma G has a negative length cycle reachable from s if and only if there is some node v such that d(v, n) < d(v, n ). Lemma proves correctness of negative cycle detection by Bellman-Ford algorithm. The only if direction follows from Lemma on previous slide. We prove the if direction in the next slide. Chandra Chekuri (UIUC) CS374 23 Spring 207 23 / 56

Correctness: detecting negative length cycle Lemma Suppose G has a negative cycle C reachable from s. Then there is some node v C such that d(v, n) < d(v, n ). Chandra Chekuri (UIUC) CS374 24 Spring 207 24 / 56

Correctness: detecting negative length cycle Lemma Suppose G has a negative cycle C reachable from s. Then there is some node v C such that d(v, n) < d(v, n ). Proof. Suppose not. Let C = v v 2... v h v be negative length cycle reachable from s. d(v i, n ) is finite for i h since C is reachable from s. By assumption d(v, n) d(v, n ) for all v C; implies no change in n th iteration; d(v i, n ) = d(v i, n) for i h. This means d(v i, n ) d(v i, n ) + l(v i, v i ) for 2 i h and d(v, n ) d(v n, n ) + l(v n, v ). Adding up all these inequalities results in the inequality 0 l(c) which contradicts the assumption that l(c) < 0. Chandra Chekuri (UIUC) CS374 24 Spring 207 24 / 56

Finding the Paths and a Shortest Path Tree How do we find a shortest path tree in addition to distances? For each v the d(v) can only get smaller as algorithm proceeds. If d(v) becomes smaller it is because we found a vertex u such that d(v) > d(u) + l(u, v) and we update d(v) = d(u) + l(u, v). That is, we found a shorter path to v through u. For each v have a prev(v) pointer and update it to point to u if v finds a shorter path via u. At end of algorithm prev(v) pointers give a shortest path tree oriented towards the source s. Chandra Chekuri (UIUC) CS374 25 Spring 207 25 / 56

Negative Cycle Detection Negative Cycle Detection Given directed graph G with arbitrary edge lengths, does it have a negative length cycle? Chandra Chekuri (UIUC) CS374 26 Spring 207 26 / 56

Negative Cycle Detection Negative Cycle Detection Given directed graph G with arbitrary edge lengths, does it have a negative length cycle? Bellman-Ford checks whether there is a negative cycle C that is reachable from a specific vertex s. There may negative cycles not reachable from s. 2 Run Bellman-Ford V times, once from each node u? Chandra Chekuri (UIUC) CS374 26 Spring 207 26 / 56

Negative Cycle Detection Add a new node s and connect it to all nodes of G with zero length edges. Bellman-Ford from s will fill find a negative length cycle if there is one. Exercise: why does this work? 2 Negative cycle detection can be done with one Bellman-Ford invocation. Chandra Chekuri (UIUC) CS374 27 Spring 207 27 / 56

Part II Shortest Paths in DAGs Chandra Chekuri (UIUC) CS374 28 Spring 207 28 / 56

Shortest Paths in a DAG Single-Source Shortest Path Problems Input A directed acyclic graph G = (V, E) with arbitrary (including negative) edge lengths. For edge e = (u, v), l(e) = l(u, v) is its length. Given nodes s, t find shortest path from s to t. 2 Given node s find shortest path from s to all other nodes. Chandra Chekuri (UIUC) CS374 29 Spring 207 29 / 56

Shortest Paths in a DAG Single-Source Shortest Path Problems Input A directed acyclic graph G = (V, E) with arbitrary (including negative) edge lengths. For edge e = (u, v), l(e) = l(u, v) is its length. Given nodes s, t find shortest path from s to t. 2 Given node s find shortest path from s to all other nodes. Simplification of algorithms for DAGs No cycles and hence no negative length cycles! Hence can find shortest paths even for negative length edges 2 Can order nodes using topological sort Chandra Chekuri (UIUC) CS374 29 Spring 207 29 / 56

Algorithm for DAGs Want to find shortest paths from s. Ignore nodes not reachable from s. 2 Let s = v, v 2, v i +,..., v n be a topological sort of G Chandra Chekuri (UIUC) CS374 30 Spring 207 30 / 56

Algorithm for DAGs Want to find shortest paths from s. Ignore nodes not reachable from s. 2 Let s = v, v 2, v i +,..., v n be a topological sort of G Observation: shortest path from s to v i cannot use any node from v i +,..., v n 2 can find shortest paths in topological sort order. Chandra Chekuri (UIUC) CS374 30 Spring 207 30 / 56

Algorithm for DAGs for i = to n do d(s, v i ) = d(s, s) = 0 for i = to n do for each edge (v i, v j ) in Adj(v i ) do d(s, v j ) = min{d(s, v j ), d(s, v i ) + l(v i, v j )} return d(s, ) values computed Correctness: induction on i and observation in previous slide. Running time: O(m + n) time algorithm! Works for negative edge lengths and hence can find longest paths in a DAG. Chandra Chekuri (UIUC) CS374 3 Spring 207 3 / 56

Bellman-Ford and DAGs Bellman-Ford is based on the following principles: The shortest walk length from s to v with at most k hops can be computed via dynamic programming G has a negative length cycle reachable from s iff there is a node v such that shortest walk length reduces after n hops. We can find hop-constrained shortest paths via graph reduction. Given G = (V, E) with edge lengths l(e) and integer k construction new layered graph G = (V, E ) as follows. V = V {0,, 2,..., k}. E = {((u, i), (v, i + ) (u, v) E, 0 i < k}, l((u, i), (v, i + )) = l(u, v) Lemma Shortest path distance from (u, 0) to (v, k) in G is equal to the shortest walk from u to v in G with exactly k edges. Chandra Chekuri (UIUC) CS374 32 Spring 207 32 / 56

Layered DAG: Figure Chandra Chekuri (UIUC) CS374 33 Spring 207 33 / 56

Part III All Pairs Shortest Paths Chandra Chekuri (UIUC) CS374 34 Spring 207 34 / 56

Shortest Path Problems Shortest Path Problems Input A (undirected or directed) graph G = (V, E) with edge lengths (or costs). For edge e = (u, v), l(e) = l(u, v) is its length. Given nodes s, t find shortest path from s to t. 2 Given node s find shortest path from s to all other nodes. 3 Find shortest paths for all pairs of nodes. Chandra Chekuri (UIUC) CS374 35 Spring 207 35 / 56

Single-Source Shortest Paths Single-Source Shortest Path Problems Input A (undirected or directed) graph G = (V, E) with edge lengths. For edge e = (u, v), l(e) = l(u, v) is its length. Given nodes s, t find shortest path from s to t. 2 Given node s find shortest path from s to all other nodes. Chandra Chekuri (UIUC) CS374 36 Spring 207 36 / 56

Single-Source Shortest Paths Single-Source Shortest Path Problems Input A (undirected or directed) graph G = (V, E) with edge lengths. For edge e = (u, v), l(e) = l(u, v) is its length. Given nodes s, t find shortest path from s to t. 2 Given node s find shortest path from s to all other nodes. Dijkstra s algorithm for non-negative edge lengths. Running time: O((m + n) log n) with heaps and O(m + n log n) with advanced priority queues. Bellman-Ford algorithm for arbitrary edge lengths. Running time: O(nm). Chandra Chekuri (UIUC) CS374 36 Spring 207 36 / 56

All-Pairs Shortest Paths All-Pairs Shortest Path Problem Input A (undirected or directed) graph G = (V, E) with edge lengths. For edge e = (u, v), l(e) = l(u, v) is its length. Find shortest paths for all pairs of nodes. Chandra Chekuri (UIUC) CS374 37 Spring 207 37 / 56

All-Pairs Shortest Paths All-Pairs Shortest Path Problem Input A (undirected or directed) graph G = (V, E) with edge lengths. For edge e = (u, v), l(e) = l(u, v) is its length. Find shortest paths for all pairs of nodes. Apply single-source algorithms n times, once for each vertex. Non-negative lengths. O(nm log n) with heaps and O(nm + n 2 log n) using advanced priority queues. 2 Arbitrary edge lengths: O(n 2 m). Θ ( n 4) if m = Ω ( n 2). Chandra Chekuri (UIUC) CS374 37 Spring 207 37 / 56

All-Pairs Shortest Paths All-Pairs Shortest Path Problem Input A (undirected or directed) graph G = (V, E) with edge lengths. For edge e = (u, v), l(e) = l(u, v) is its length. Find shortest paths for all pairs of nodes. Apply single-source algorithms n times, once for each vertex. Non-negative lengths. O(nm log n) with heaps and O(nm + n 2 log n) using advanced priority queues. 2 Arbitrary edge lengths: O(n 2 m). Θ ( n 4) if m = Ω ( n 2). Can we do better? Chandra Chekuri (UIUC) CS374 37 Spring 207 37 / 56

All-Pairs: Recursion on index of intermediate nodes Number vertices arbitrarily as v, v 2,..., v n 2 dist(i, j, k): length of shortest walk from v i to v j among all walks in which the largest index of an intermediate node is at most k (could be if there is a negative length cycle). i 4 3 2 5 j 0 2 00 dist(i, j, 0) = dist(i, j, ) = dist(i, j, 2) = dist(i, j, 3) = Chandra Chekuri (UIUC) CS374 38 Spring 207 38 / 56

All-Pairs: Recursion on index of intermediate nodes Number vertices arbitrarily as v, v 2,..., v n 2 dist(i, j, k): length of shortest walk from v i to v j among all walks in which the largest index of an intermediate node is at most k (could be if there is a negative length cycle). i 4 3 2 5 j 0 2 00 dist(i, j, 0) = 00 dist(i, j, ) = dist(i, j, 2) = dist(i, j, 3) = Chandra Chekuri (UIUC) CS374 38 Spring 207 38 / 56

All-Pairs: Recursion on index of intermediate nodes Number vertices arbitrarily as v, v 2,..., v n 2 dist(i, j, k): length of shortest walk from v i to v j among all walks in which the largest index of an intermediate node is at most k (could be if there is a negative length cycle). i 4 3 2 5 j 0 2 00 dist(i, j, 0) = 00 dist(i, j, ) = 9 dist(i, j, 2) = dist(i, j, 3) = Chandra Chekuri (UIUC) CS374 38 Spring 207 38 / 56

All-Pairs: Recursion on index of intermediate nodes Number vertices arbitrarily as v, v 2,..., v n 2 dist(i, j, k): length of shortest walk from v i to v j among all walks in which the largest index of an intermediate node is at most k (could be if there is a negative length cycle). i 4 3 2 5 j 0 2 00 dist(i, j, 0) = 00 dist(i, j, ) = 9 dist(i, j, 2) = 8 dist(i, j, 3) = Chandra Chekuri (UIUC) CS374 38 Spring 207 38 / 56

All-Pairs: Recursion on index of intermediate nodes Number vertices arbitrarily as v, v 2,..., v n 2 dist(i, j, k): length of shortest walk from v i to v j among all walks in which the largest index of an intermediate node is at most k (could be if there is a negative length cycle). i 4 3 2 5 j 0 2 00 dist(i, j, 0) = 00 dist(i, j, ) = 9 dist(i, j, 2) = 8 dist(i, j, 3) = 5 Chandra Chekuri (UIUC) CS374 38 Spring 207 38 / 56

For the following graph, dist(i, j, 2) is... i 8 5 2 5 2 3 j 2 0 200 (A) 9 (B) 0 (C) (D) 2 (E) 5 Chandra Chekuri (UIUC) CS374 39 Spring 207 39 / 56

All-Pairs: Recursion on index of intermediate nodes dist(i, k, k ) k dist(k, j, k ) i j dist(i, j, k ) dist(i, j, k) = min { dist(i, j, k ) dist(i, k, k ) + dist(k, j, k ) Base case: dist(i, j, 0) = l(i, j) if (i, j) E, otherwise Correctness: If i j shortest walk goes through k then k occurs only once on the path otherwise there is a negative length cycle. Chandra Chekuri (UIUC) CS374 40 Spring 207 40 / 56

All-Pairs: Recursion on index of intermediate nodes If i can reach k and k can reach j and dist(k, k, k ) < 0 then G has a negative length cycle containing k and dist(i, j, k) =. Recursion below is valid only if dist(k, k, k ) 0. We can detect this during the algorithm or wait till the end. { dist(i, j, k ) dist(i, j, k) = min dist(i, k, k ) + dist(k, j, k ) Chandra Chekuri (UIUC) CS374 4 Spring 207 4 / 56

Floyd-Warshall Algorithm for All-Pairs Shortest Paths for i = to n do for j = to n do dist(i, j, 0) = l(i, j) (* l(i, j) = if (i, j) / E, 0 if i = j *) for k = to n do for i = to n do for j = to n do dist(i, j, k) = min { dist(i, j, k ), dist(i, k, k ) + dist(k, j, k ) for i = to n do if (dist(i, i, n) < 0) then Output that there is a negative length cycle in G Chandra Chekuri (UIUC) CS374 42 Spring 207 42 / 56

Floyd-Warshall Algorithm for All-Pairs Shortest Paths for i = to n do for j = to n do dist(i, j, 0) = l(i, j) (* l(i, j) = if (i, j) / E, 0 if i = j *) for k = to n do for i = to n do for j = to n do dist(i, j, k) = min { dist(i, j, k ), dist(i, k, k ) + dist(k, j, k ) for i = to n do if (dist(i, i, n) < 0) then Output that there is a negative length cycle in G Running Time: Chandra Chekuri (UIUC) CS374 42 Spring 207 42 / 56

Floyd-Warshall Algorithm for All-Pairs Shortest Paths for i = to n do for j = to n do dist(i, j, 0) = l(i, j) (* l(i, j) = if (i, j) / E, 0 if i = j *) for k = to n do for i = to n do for j = to n do dist(i, j, k) = min { dist(i, j, k ), dist(i, k, k ) + dist(k, j, k ) for i = to n do if (dist(i, i, n) < 0) then Output that there is a negative length cycle in G Running Time: Θ(n 3 ), Space: Θ(n 3 ). Chandra Chekuri (UIUC) CS374 42 Spring 207 42 / 56

Floyd-Warshall Algorithm for All-Pairs Shortest Paths for i = to n do for j = to n do dist(i, j, 0) = l(i, j) (* l(i, j) = if (i, j) / E, 0 if i = j *) for k = to n do for i = to n do for j = to n do dist(i, j, k) = min { dist(i, j, k ), dist(i, k, k ) + dist(k, j, k ) for i = to n do if (dist(i, i, n) < 0) then Output that there is a negative length cycle in G Running Time: Θ(n 3 ), Space: Θ(n 3 ). Correctness: via induction and recursive definition Chandra Chekuri (UIUC) CS374 42 Spring 207 42 / 56

Floyd-Warshall Algorithm: Finding the Paths Question: Can we find the paths in addition to the distances? Chandra Chekuri (UIUC) CS374 43 Spring 207 43 / 56

Floyd-Warshall Algorithm: Finding the Paths Question: Can we find the paths in addition to the distances? Create a n n array Next that stores the next vertex on shortest path for each pair of vertices 2 With array Next, for any pair of given vertices i, j can compute a shortest path in O(n) time. Chandra Chekuri (UIUC) CS374 43 Spring 207 43 / 56

Floyd-Warshall Algorithm Finding the Paths for i = to n do for j = to n do dist(i, j, 0) = l(i, j) (* l(i, j) = if (i, j) not edge, 0 if i = j *) Next(i, j) = for k = to n do for i = to n do for j = to n do if (dist(i, j, k ) > dist(i, k, k ) + dist(k, j, k )) then dist(i, j, k) = dist(i, k, k ) + dist(k, j, k ) Next(i, j) = k for i = to n do if (dist(i, i, n) < 0) then Output that there is a negative length cycle in G Exercise: Given Next array and any two vertices i, j describe an O(n) algorithm to find a i-j shortest path. Chandra Chekuri (UIUC) CS374 44 Spring 207 44 / 56

Summary of results on shortest paths Single source No negative edges Dijkstra O(n log n + m) Edge lengths can be negative Bellman Ford O(nm) All Pairs Shortest Paths No negative edges n * Dijkstra O ( n 2 log n + nm ) No negative cycles n * Bellman Ford O ( n 2 m ) = O ( n 4) No negative cycles BF + n * Dijkstra O ( nm + n 2 log n ) No negative cycles Floyd-Warshall O ( n 3) Unweighted Matrix multiplication O(n 2.38 ), O(n 2.58 ) Chandra Chekuri (UIUC) CS374 45 Spring 207 45 / 56

Part IV DFA to Regular Expression Chandra Chekuri (UIUC) CS374 46 Spring 207 46 / 56

Back to Regular Languages We saw the following two theorems previously. Theorem For every NFA N over a finite alphabet Σ there is DFA M such that L(M) = L(N). Theorem For every regular expression r over finite alphabet Σ there is a NFA N such that L(N) = L(r). Chandra Chekuri (UIUC) CS374 47 Spring 207 47 / 56

Back to Regular Languages We saw the following two theorems previously. Theorem For every NFA N over a finite alphabet Σ there is DFA M such that L(M) = L(N). Theorem For every regular expression r over finite alphabet Σ there is a NFA N such that L(N) = L(r). We claimed the following theorem which would prove equivalence of NFAs, DFAs and regular expressions. Theorem For every DFA M over a finite alphabet Σ there is a regular expression r such that L(M) = L(r). Chandra Chekuri (UIUC) CS374 47 Spring 207 47 / 56

DFA to Regular Expression Given DFA M = (Q, Σ, δ, q, F ) want to construct an equivalent regular expression r. Idea: Number states of DFA: Q = {q,..., q n } where Q = n. Define L i,j = {w δ(q i, w) = q j }. Note L i,j is regular. Why? L(M) = qi F L,i. Obtain regular expression r i,j for L i,j. Then r = q i F r,i is regular expression for L(M). Note: Using q for start state is intentional to help in the notation for the recursion. Chandra Chekuri (UIUC) CS374 48 Spring 207 48 / 56

A recursive expression for L i,j Define L k i,j be set of strings w in L i,j such that the highest index state visted by M on walk from q i to q j (not counting end points i and j) on input w is at most k. From definition L i,j = L n i,j Chandra Chekuri (UIUC) CS374 49 Spring 207 49 / 56

A recursive expression for L i,j Define L k i,j be set of strings w in L i,j such that the highest index state visted by M on walk from q i to q j (not counting end points i and j) on input w is at most k. From definition L i,j = L n i,j Claim: L 0 i,j = { {a Σ δ(q i, a) = q j } if i j {a Σ δ(q i, a) = q j } {ɛ} if i = j Chandra Chekuri (UIUC) CS374 49 Spring 207 49 / 56

A recursive expression for L i,j Define L k i,j be set of strings w in L i,j such that the highest index state visted by M on walk from q i to q j (not counting end points i and j) on input w is at most k. From definition L i,j = L n i,j Claim: { L 0 {a Σ δ(q i, a) = q j } if i j i,j = {a Σ δ(q i, a) = q j } {ɛ} if i = j ( ) L k i,j = L k i,j L k i,k (L k k,k ) L k k,j Chandra Chekuri (UIUC) CS374 49 Spring 207 49 / 56

A recursive expression for L i,j Claim: L 0 i,j = { {a Σ δ(q i, a) = q j } if i j {a Σ δ(q i, a) = q i } {ɛ} if i = j L k i,j = L k i,j ( ) L k i,k (L k k,k ) L k k,j Proof: by picture q k q i q j Chandra Chekuri (UIUC) CS374 50 Spring 207 50 / 56

A recursive expression for L i,j Claim: L i,j = L n i,j L 0 i,j = {a Σ δ(q i, a) = q j } L k i,j = L k i,j ( ) L k i,k (L k k,k ) L k k,j From claim, can easily construct regular expression r k i,j for L k i,j. This leads to a regular expression for L(M) = qi F L,i = qi F L n,i Chandra Chekuri (UIUC) CS374 5 Spring 207 5 / 56

Example b a b q q 2 a Chandra Chekuri (UIUC) CS374 52 Spring 207 52 / 56

Example b a b q q 2 a L(M) = L 2,2 r 2,2 = r,2 + r,2 (r 2,2 ) r 2,2 r,2 = r 0,2 + r 0, (r 0, ) r 0,2 r 2,2 = r 0 2,2 + r 0 2, (r 0, ) r 0,2 r 0, = r 0 2,2 = (b + ɛ) r 0,2 = r 0 2, = a Chandra Chekuri (UIUC) CS374 52 Spring 207 52 / 56

Correctness Similar to that of Floyd-Warshall algorithms for shortest paths via induction. The length of the regular expression can be exponential in the size of the original DFA. Chandra Chekuri (UIUC) CS374 53 Spring 207 53 / 56

Dynamic Programming: Postscript Dynamic Programming = Smart Recursion + Memoization Chandra Chekuri (UIUC) CS374 54 Spring 207 54 / 56

Dynamic Programming: Postscript Dynamic Programming = Smart Recursion + Memoization How to come up with the recursion? 2 How to recognize that dynamic programming may apply? Chandra Chekuri (UIUC) CS374 54 Spring 207 54 / 56

Some Tips Problems where there is a natural linear ordering: sequences, paths, intervals, DAGs etc. Recursion based on ordering (left to right or right to left or topological sort) usually works. 2 Problems involving trees: recursion based on subtrees. 3 More generally: Problem admits a natural recursive divide and conquer 2 If optimal solution for whole problem can be simply composed from optimal solution for each separate pieces then plain divide and conquer works directly 3 If optimal solution depends on all pieces then can apply dynamic programming if interface/interaction between pieces is limited. Augment recursion to not simply find an optimum solution but also an optimum solution for each possible way to interact with the other pieces. Chandra Chekuri (UIUC) CS374 55 Spring 207 55 / 56

Examples Longest Increasing Subsequence: break sequence in the middle say. What is the interaction between the two pieces in a solution? 2 Sequence Alignment: break both sequences in two pieces each. What is the interaction between the two sets of pieces? 3 Independent Set in a Tree: break tree at root into subtrees. What is the interaction between the subtrees? 4 Independent Set in an graph: break graph into two graphs. What is the interaction? Very high! 5 Knapsack: Split items into two sets of half each. What is the interaction? Chandra Chekuri (UIUC) CS374 56 Spring 207 56 / 56