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

CS 4407 Algorithms Lecture: Shortest Path Algorithms

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

Design and Analysis of Algorithms

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

Introduction to Algorithms

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

Algorithms and Theory of Computation. Lecture 9: Dynamic Programming

6. DYNAMIC PROGRAMMING II

Discrete Optimization 2010 Lecture 3 Maximum Flows

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

DFA to Regular Expressions

Optimal Tree-decomposition Balancing and Reachability on Low Treewidth Graphs

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

More Dynamic Programming

Design and Analysis of Algorithms

CS60007 Algorithm Design and Analysis 2018 Assignment 1

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

More Dynamic Programming

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

All-Pairs Shortest Paths

Single Source Shortest Paths

Lecture 2: Regular Expression

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

1 More finite deterministic automata

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

Deterministic Finite Automata (DFAs)

Lecture 7: Dynamic Programming I: Optimal BSTs

CS483 Design and Analysis of Algorithms

Notes on State Minimization

Even More on Dynamic Programming

Introduction to Algorithms

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

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

Finite Automata and Formal Languages

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

Partha Sarathi Mandal

Algorithms: Lecture 12. Chalmers University of Technology

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

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

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

Deterministic Finite Automata (DFAs)

Algorithm Design and Analysis

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

CS 241 Analysis of Algorithms

Analysis of Algorithms I: All-Pairs Shortest Paths

CS 161: Design and Analysis of Algorithms

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

Lecture Notes for Chapter 25: All-Pairs Shortest Paths

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

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

Nondeterministic finite automata

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

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

Find: a multiset M { 1,..., n } so that. i M w i W and. i M v i is maximized. Find: a set S { 1,..., n } so that. i S w i W and. i S v i is maximized.

Flows. Chapter Circulations

Lecture 2: Divide and conquer and Dynamic programming

Regular Expressions. Definitions Equivalence to Finite Automata

Duality of LPs and Applications

Myriad of applications

1 Matchings in Non-Bipartite Graphs

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

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

Algorithms Exam TIN093 /DIT602

Algorithms and Data Structures (COMP 251) Midterm Solutions

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Shortest paths: label setting

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

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

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

Question Paper Code :

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

Shortest Path Algorithms

MAKING A BINARY HEAP

Chapter 8 Dynamic Programming

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

Discrete Optimization Lecture 5. M. Pawan Kumar

Maximum sum contiguous subsequence Longest common subsequence Matrix chain multiplication All pair shortest path Kna. Dynamic Programming

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.

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

General Methods for Algorithm Design

Chapter 8 Dynamic Programming

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

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

arxiv: v1 [cs.ds] 9 Apr 2018

Lecture 8: Complete Problems for Other Complexity Classes

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

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

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

CSCE 551 Final Exam, April 28, 2016 Answer Key

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

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

Matching Residents to Hospitals

6.889 Lecture 4: Single-Source Shortest Paths

Transcription:

CS 374: Algorithms & Models of Computation, Fall 205 Dynamic Programming: Shortest Paths and DFA to Reg Exps Lecture 7 October 22, 205 Chandra & Manoj (UIUC) CS374 Fall 205 / 54

Part I Shortest Paths with Negative Length Edges Chandra & Manoj (UIUC) CS374 2 Fall 205 2 / 54

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 & Manoj (UIUC) CS374 3 Fall 205 3 / 54

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 & Manoj (UIUC) CS374 3 Fall 205 3 / 54

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 & Manoj (UIUC) CS374 4 Fall 205 4 / 54

Dijkstra s Algorithm and Negative Lengths With negative length edges, Dijkstra s algorithm can fail s 5 x 5 y z w Chandra & Manoj (UIUC) CS374 5 Fall 205 5 / 54

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 & Manoj (UIUC) CS374 5 Fall 205 5 / 54

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 & Manoj (UIUC) CS374 5 Fall 205 5 / 54

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 & Manoj (UIUC) CS374 5 Fall 205 5 / 54

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 & Manoj (UIUC) CS374 5 Fall 205 5 / 54

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 & Manoj (UIUC) CS374 5 Fall 205 5 / 54

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 & Manoj (UIUC) CS374 5 Fall 205 5 / 54

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 & Manoj (UIUC) CS374 5 Fall 205 5 / 54

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 & Manoj (UIUC) CS374 6 Fall 205 6 / 54

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 & Manoj (UIUC) CS374 6 Fall 205 6 / 54

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 & Manoj (UIUC) CS374 7 Fall 205 7 / 54

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 & Manoj (UIUC) CS374 7 Fall 205 7 / 54

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 & Manoj (UIUC) CS374 8 Fall 205 8 / 54

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 & Manoj (UIUC) CS374 9 Fall 205 9 / 54

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 & Manoj (UIUC) CS374 0 Fall 205 0 / 54

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 & Manoj (UIUC) CS374 Fall 205 / 54

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 & Manoj (UIUC) CS374 2 Fall 205 2 / 54

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 & Manoj (UIUC) CS374 3 Fall 205 3 / 54

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 & Manoj (UIUC) CS374 3 Fall 205 3 / 54

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 & Manoj (UIUC) CS374 3 Fall 205 3 / 54

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 & Manoj (UIUC) CS374 3 Fall 205 3 / 54

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 & Manoj (UIUC) CS374 4 Fall 205 4 / 54

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 & Manoj (UIUC) CS374 5 Fall 205 5 / 54

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 & Manoj (UIUC) CS374 5 Fall 205 5 / 54

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 & Manoj (UIUC) CS374 5 Fall 205 5 / 54

Shortest Paths and Recursion Compute the shortest path distance from s to t recursively? 2 What are the smaller sub-problems? Chandra & Manoj (UIUC) CS374 6 Fall 205 6 / 54

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 & Manoj (UIUC) CS374 6 Fall 205 6 / 54

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 & Manoj (UIUC) CS374 6 Fall 205 6 / 54

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 & Manoj (UIUC) CS374 7 Fall 205 7 / 54

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 & Manoj (UIUC) CS374 7 Fall 205 7 / 54

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 & Manoj (UIUC) CS374 7 Fall 205 7 / 54

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 & Manoj (UIUC) CS374 7 Fall 205 7 / 54

Example e 8 2 d 0 5 3 b 8 a 4 6 3 s f 3 c Chandra & Manoj (UIUC) CS374 8 Fall 205 8 / 54

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 & Manoj (UIUC) CS374 9 Fall 205 9 / 54

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 & Manoj (UIUC) CS374 9 Fall 205 9 / 54

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 & Manoj (UIUC) CS374 9 Fall 205 9 / 54

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 & Manoj (UIUC) CS374 9 Fall 205 9 / 54

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 & Manoj (UIUC) CS374 9 Fall 205 9 / 54

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 & Manoj (UIUC) CS374 9 Fall 205 9 / 54

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 & Manoj (UIUC) CS374 20 Fall 205 20 / 54

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 & Manoj (UIUC) CS374 2 Fall 205 2 / 54

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 & Manoj (UIUC) CS374 22 Fall 205 22 / 54

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 & Manoj (UIUC) CS374 22 Fall 205 22 / 54

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 & Manoj (UIUC) CS374 23 Fall 205 23 / 54

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 & Manoj (UIUC) CS374 24 Fall 205 24 / 54

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 & Manoj (UIUC) CS374 24 Fall 205 24 / 54

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 & Manoj (UIUC) CS374 25 Fall 205 25 / 54

Negative Cycle Detection Negative Cycle Detection Given directed graph G with arbitrary edge lengths, does it have a negative length cycle? Chandra & Manoj (UIUC) CS374 26 Fall 205 26 / 54

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 & Manoj (UIUC) CS374 26 Fall 205 26 / 54

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 & Manoj (UIUC) CS374 27 Fall 205 27 / 54

Part II Shortest Paths in DAGs Chandra & Manoj (UIUC) CS374 28 Fall 205 28 / 54

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 & Manoj (UIUC) CS374 29 Fall 205 29 / 54

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 & Manoj (UIUC) CS374 29 Fall 205 29 / 54

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 & Manoj (UIUC) CS374 30 Fall 205 30 / 54

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 & Manoj (UIUC) CS374 30 Fall 205 30 / 54

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 & Manoj (UIUC) CS374 3 Fall 205 3 / 54

Part III All Pairs Shortest Paths Chandra & Manoj (UIUC) CS374 32 Fall 205 32 / 54

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 & Manoj (UIUC) CS374 33 Fall 205 33 / 54

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 & Manoj (UIUC) CS374 34 Fall 205 34 / 54

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 & Manoj (UIUC) CS374 34 Fall 205 34 / 54

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 & Manoj (UIUC) CS374 35 Fall 205 35 / 54

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 & Manoj (UIUC) CS374 35 Fall 205 35 / 54

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 & Manoj (UIUC) CS374 35 Fall 205 35 / 54

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 & Manoj (UIUC) CS374 36 Fall 205 36 / 54

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 & Manoj (UIUC) CS374 36 Fall 205 36 / 54

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 & Manoj (UIUC) CS374 36 Fall 205 36 / 54

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 & Manoj (UIUC) CS374 36 Fall 205 36 / 54

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 & Manoj (UIUC) CS374 36 Fall 205 36 / 54

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 & Manoj (UIUC) CS374 37 Fall 205 37 / 54

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 & Manoj (UIUC) CS374 38 Fall 205 38 / 54

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 & Manoj (UIUC) CS374 39 Fall 205 39 / 54

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 & Manoj (UIUC) CS374 40 Fall 205 40 / 54

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 & Manoj (UIUC) CS374 40 Fall 205 40 / 54

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 & Manoj (UIUC) CS374 40 Fall 205 40 / 54

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 & Manoj (UIUC) CS374 40 Fall 205 40 / 54

Floyd-Warshall Algorithm: Finding the Paths Question: Can we find the paths in addition to the distances? Chandra & Manoj (UIUC) CS374 4 Fall 205 4 / 54

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 & Manoj (UIUC) CS374 4 Fall 205 4 / 54

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 & Manoj (UIUC) CS374 42 Fall 205 42 / 54

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 & Manoj (UIUC) CS374 43 Fall 205 43 / 54

Part IV DFA to Regular Expression Chandra & Manoj (UIUC) CS374 44 Fall 205 44 / 54

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 & Manoj (UIUC) CS374 45 Fall 205 45 / 54

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 & Manoj (UIUC) CS374 45 Fall 205 45 / 54

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 FL,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 & Manoj (UIUC) CS374 46 Fall 205 46 / 54

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 that is reached by M on walk from q i to q j on input w is at most k. From definition L i,j = L n i,j Chandra & Manoj (UIUC) CS374 47 Fall 205 47 / 54

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 that is reached by M on walk from q i to q 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 & Manoj (UIUC) CS374 47 Fall 205 47 / 54

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 that is reached by M on walk from q i to q 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 L k i,j = Lk i,j ( ) L k i,k (L k k,k ) L k k,j Chandra & Manoj (UIUC) CS374 47 Fall 205 47 / 54

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 j } {ɛ} if i = j L k i,j = Lk i,j ( ) L k i,k (L k k,k ) L k k,j Proof: by picture q k q i q j Chandra & Manoj (UIUC) CS374 48 Fall 205 48 / 54

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 = Lk 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 Lk i,j. This leads to a regular expression for L(M) = qi FL,i = qi FL n,i Chandra & Manoj (UIUC) CS374 49 Fall 205 49 / 54

Example b a b q q 2 a Chandra & Manoj (UIUC) CS374 50 Fall 205 50 / 54

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 = r0,2 + r0, (r0, ) r 0,2 r 2,2 = r0 2,2 + r0 2, (r0, ) r 0,2 r 0, = r0 2,2 = (b + ɛ) r 0,2 = r0 2, = a Chandra & Manoj (UIUC) CS374 50 Fall 205 50 / 54

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 & Manoj (UIUC) CS374 5 Fall 205 5 / 54

Dynamic Programming: Postscript Dynamic Programming = Smart Recursion + Memoization Chandra & Manoj (UIUC) CS374 52 Fall 205 52 / 54

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 & Manoj (UIUC) CS374 52 Fall 205 52 / 54

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 & Manoj (UIUC) CS374 53 Fall 205 53 / 54

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 & Manoj (UIUC) CS374 54 Fall 205 54 / 54