p 3 p 2 p 4 q 2 q 7 q 1 q 3 q 6 q 5

Similar documents
Flow Network. The following figure shows an example of a flow network:

CMPSCI 611: Advanced Algorithms

CSE 202 Homework 4 Matthias Springer, A

1 Review for Lecture 2 MaxFlow

Lecture 2: Network Flows 1

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

Preliminaries. Graphs. E : set of edges (arcs) (Undirected) Graph : (i, j) = (j, i) (edges) V = {1, 2, 3, 4, 5}, E = {(1, 3), (3, 2), (2, 4)}

Algorithms: COMP3121/3821/9101/9801

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

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

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

Lecture 8 Network Optimization Algorithms

Algorithms: Lecture 12. Chalmers University of Technology

CSE 326: Data Structures Network Flow. James Fogarty Autumn 2007

arxiv: v1 [cs.ds] 9 Apr 2018

10 Max-Flow Min-Cut Flows and Capacitated Graphs 10 MAX-FLOW MIN-CUT

CS 170 DISCUSSION 10 MAXIMUM FLOW. Raymond Chan raychan3.github.io/cs170/fa17.html UC Berkeley Fall 17

Standard Diraphs the (unique) digraph with no vertices or edges. (modulo n) for every 1 i n A digraph whose underlying graph is a complete graph.

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

CSC 1700 Analysis of Algorithms: Warshall s and Floyd s algorithms

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

1. Problem I calculated these out by hand. It was tedious, but kind of fun, in a a computer should really be doing this sort of way.

Algorithm Design CS 515 Fall 2015 Sample Final Exam Solutions

7.5 Bipartite Matching

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

Optimal Tree-decomposition Balancing and Reachability on Low Treewidth Graphs

The max flow problem. Ford-Fulkerson method. A cut. Lemma Corollary Max Flow Min Cut Theorem. Max Flow Min Cut Theorem

Max Flow: Algorithms and Applications

Quiz 1 Solutions. (a) f 1 (n) = 8 n, f 2 (n) = , f 3 (n) = ( 3) lg n. f 2 (n), f 1 (n), f 3 (n) Solution: (b)

6.046 Recitation 11 Handout

A fast algorithm to generate necklaces with xed content

Algorithms and Theory of Computation. Lecture 11: Network Flow

Discrete Optimization 2010 Lecture 3 Maximum Flows

Maximum Flow Problem (Ford and Fulkerson, 1956)

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

22 Max-Flow Algorithms

Agenda. Soviet Rail Network, We ve done Greedy Method Divide and Conquer Dynamic Programming

Review Questions, Final Exam

Algorithm Design and Analysis

2. A vertex in G is central if its greatest distance from any other vertex is as small as possible. This distance is the radius of G.

Lecture 18 April 26, 2012

Lecture 2: Divide and conquer and Dynamic programming

ACO Comprehensive Exam March 20 and 21, Computability, Complexity and Algorithms

1 Matchings in Non-Bipartite Graphs

The maximum flow problem

The min cost flow problem Course notes for Optimization Spring 2007

The min cost flow problem Course notes for Search and Optimization Spring 2004

CSC 373: Algorithm Design and Analysis Lecture 12

Algorithm Design and Analysis

CS60007 Algorithm Design and Analysis 2018 Assignment 1

Written Qualifying Exam. Spring, Friday, May 22, This is nominally a three hour examination, however you will be

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

Types of Networks. Internet Telephone Cell Highways Rail Electrical Power Water Sewer Gas

Network Flow Problems Luis Goddyn, Math 408

EE376A: Homework #3 Due by 11:59pm Saturday, February 10th, 2018

Maximum flow problem

CMPSCI 611 Advanced Algorithms Midterm Exam Fall 2015

We say that a flow is feasible for G (or just feasible if the graph and capacity function in question are obvious) if

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

Energy minimization via graph-cuts

Friday, September 21, Flows

ALG 5.4. Lexicographical Search: Applied to Scheduling Theory. Professor John Reif

On the Average Path Length of Complete m-ary Trees

Two Applications of Maximum Flow

Lecture 13. More dynamic programming! Longest Common Subsequences, Knapsack, and (if time) independent sets in trees.

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Internet Routing Example

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

0 1 d 010. h 0111 i g 01100

Flows and Cuts. 1 Concepts. CS 787: Advanced Algorithms. Instructor: Dieter van Melkebeek

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

1 Reductions and Expressiveness

Chapter 7. Network Flow. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

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

CSE 421 Introduction to Algorithms Final Exam Winter 2005

Markov chains (week 6) Solutions

Even More on Dynamic Programming

Lexicographic Flow. Dexter Kozen Department of Computer Science Cornell University Ithaca, New York , USA. June 25, 2009

Midterm Exam 2 Solutions

CS 1501 Recitation. Xiang Xiao

Lecture 1 : Data Compression and Entropy

(tree searching technique) (Boolean formulas) satisfying assignment: (X 1, X 2 )

More Dynamic Programming

Slides for CIS 675. Huffman Encoding, 1. Huffman Encoding, 2. Huffman Encoding, 3. Encoding 1. DPV Chapter 5, Part 2. Encoding 2

Introduction to Discrete Optimization

(c) Give a proof of or a counterexample to the following statement: (3n 2)= n(3n 1) 2

CSC Design and Analysis of Algorithms. LP Shader Electronics Example

MIDTERM Fundamental Algorithms, Spring 2008, Professor Yap March 10, 2008

13 Dynamic Programming (3) Optimal Binary Search Trees Subset Sums & Knapsacks

Nearest Neighbor Search with Keywords

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

CS1800: Mathematical Induction. Professor Kevin Gold

More Dynamic Programming

Advanced Analysis of Algorithms - Midterm (Solutions)

Lecture 13. More dynamic programming! Longest Common Subsequences, Knapsack, and (if time) independent sets in trees.

1. (12 points) Give a table-driven parse table for the following grammar: 1) X ax 2) X P 3) P DT 4) T P 5) T ε 7) D 1

CS 580: Algorithm Design and Analysis. Jeremiah Blocki Purdue University Spring 2018

Econ 172A, Fall 2012: Final Examination Solutions (II) 1. The entries in the table below describe the costs associated with an assignment

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Methods for solving recurrences

Transcription:

Discrete Fréchet distance Consider Professor Bille going for a walk with his personal dog. The professor follows a path of points p 1,..., p n and the dog follows a path of points q 1,..., q m. We assume that the walk is partitioned into a number of small steps, where the professor and the dog in each step either both move from p i tp p i+1 and from q j to q j+1, respectively, or only one of them moves and the other one stays. The goal is to find the smallest possible length L of the leash, such that the professor and the dog can move from p 1 and q 1, resp., to p n and q n. They cannot move backwards, and we only consider the distance between points. The distance L is also known as the discrete Fréchet distance. We let L(i, j) denote the smallest possible length of the leash, such that the professor and the dog can move from p 1 and q 1 to p i and q j, resp. For two points p and q, let d(p, q) denote the distance between the them. In the example below the dotted lines denote where Professor Bille (black nodes) and the dog (white nodes) are at time 1 to 8. The minimum leash length is L = d(p 1, q 4 ). p 1 p 2 p 3 p 4 p 5 q 7 q 1 q 2 q 3 q 4 q 5 q 6 Q1: Recurrence Give a recursive formula for L(i, j). d(p 1, q 1 ) for i = 1 j = 1 (1) max(d(p 1, q j ), L(1, j 1)) for i = 1 (2) L(i, j) = max(d(p i, q 1 ), L(i 1, j)) for j = 1 (3) max(d(p i, q j ), min{l(i 1, j), L(i, j 1), L(i 1, j 1)}) otherwise (4) The three first cases correspond to the following three base cases: (1) both Professor Bille and the dog are on the first nodes in their respective paths (p 1 /q 1 ) (2) Professor Bille stands on the first node (p 1 ) while only the dog is moving (3) the dog stands on the first node (q 1 ) and Professor Bille is the only who is moving. In the general case (4), we must use a leash of length at least the distance between p i and q j since they could not otherwise stand on p i and q i respectively. Furthermore the leash must be long enough for the steps made so far. We consider the following three cases for the previous step and take the best of these (ie. the smallest): (a) Professor Bille moves (b) the dog moves (c) they both move. Q2: Algorithm Give an algorithm that computes the length of the shortest possible leash. Analyze space and time usage of your solution. COMPUTE-LENGTH() L := n m table for i = 1 to n do for j = 1 to m do 1

if i = 1 and j = 1 then L[i, j] := d(p 1, q 1 ) else if i = 1 then L[i, j] := max(d(p 1, q j ), L[1, j - 1]) else if j = 1 then L[i, j] := max(d(p i, q 1 ), L[i - 1, 1]) else L[i, j] := max(d(p i, q j ), min(l[i - 1, j], L[i, j - 1], L[i - 1, j - 1])) end if end for end for return L[n, m] Since we store the results to the subproblems in a table L of size n m we use Θ(nm) space. The outer loop always make n iterations and the inner loop always makes m iterations per iteration of the outer loop. All other operations can be done in constant time, and thus the total time of the algorithm is Θ(nm). Q3: Print solution Extend your algorithm to print out paths for the professor and the dog. The algorithm must return where the professor and the dog is at each time step. Analyze the time and space usage of your solution. First use the above algorithm to compute L and then call PRINT-ROUTE(n, m) to print the sequence of locations the Professor and the dog will visit. PRINT-ROUTE(i, j) if i > 1 and L[i - 1, j] <= L[n,m] then PRINT-ROUTE(i - 1, j) else if j > 1 and L[i, j - 1] <= L[n, m] then PRINT-ROUTE(i, j - 1) else if i > 1 and j > 1 PRINT-ROUTE(i - 1, j - 1) print (i, j) We do not need to save any additional things to reconstrunct the paths, thus the space usage is still Θ(nm). A path from (p 1, q 1 ) to (p n, q m ) can contain at most n + m nodes as at least one must move in each step and they cannot move backwards. Thus PRINT-ROUTE will call itself less than n + m times. All the operations in PRINT-ROUTE (except the recursive calls) can be done in constant time. Thus the running time of PRINT-ROUTE is O(n + m). The overall running time is therefore Θ(nm) + O(n + m) = Θ(nm). 2

Blood donors At the halloween party at a well-known academic institution north of Copenhagen not all went smooth and some students had to be taken to medical emergency treatment at Rigshospitalet. In total 150 had to get a transfusion of one bag of blood. The hospital had 155 bags in stock. The distribution of blood groups in the supply and amongst the students is shown in the table below. Blood type A B 0 AB Bags in stock 44 31 42 38 Demand 37 33 40 40 Type A patients can only receive blood of type A or type 0; type B patients can receive only type B or type 0; type 0 patients can receive only type 0; and type AB patients can receive any of the four types. Model the problem as a flow network problem. Draw the corresponding network, and interpret the meaning of the nodes, and edges (edges capacities). Describe how to check whether every student can get a transfusion, otherwise how many can get one. You do not have to solve the problem explicitly. The problem can be modelled as a flow problem. Create a graph G with a source node s, a sink node t, 4 nodes for the supply of each blood type s T (where T is the blood type), and 4 nodes for the demand of each blood type d T. Make an edge from s to s T with capacity equal to the number of bags in stock of blood type T. Make an edge from s T to d U with unlimited capacity iff a patient with blood type T can use blood of type U. Finally make edges from d T to t with capacity equal to the number of students with blood type T. Use a maximum flow algorithm like Ford-Fulkerson to find the maximum s t flow in g. If the value of the maximum flow equals the number of students then all students can get a transfusion, otherwise the maximum flow is the number of students that can. bags s A students d A s 44 31 42 38 s B s 0 d B d 0 40 40 37 33 t Correctness. ] s AB First we show a flow in G corresponds to a valid assignment of bags to students. Since all the capacities are integral, we can assume the flow on any edge is integral. Let a flow of f(s T, d U ) units from s T to d U correspond to f(s T, d U ) bags of bloodtype T is given to students with bloodtype U. Because we only have edges from s T to d U if students with blood type T can use bags of type U, we only assign bags to students whose blood type matches. Because of the capacity constraints on the edges from s to s T and flow conservation in s T we do not use more bags than we have of any bloodtype. Because of the capacity d AB 3

constraints on the edges from d T to t and flow conservation in d T, we do not give blood to more students than needs it. Secondly, we show there exists a valid flow in G for any valid assignment of bags to students. We construct the flow as follows: # bags of type T used for u = s v = s T f(u, v) = # bags of type T assigned to students of type D for u = s T v = u D # students with type T satisfied for u = d T v = t The capacity constraints are satisfied for edges from s to s T because an assignment of bags cannot use more bags than we have and the capacity on the edges are exactly the number of bags available of that type. A student can be assigned at most one bag of blood, and thus the capacity constraints on edges from d T to t are satisfied since the capacity is the number of students with the blood type. Flow conservation in the nodes s T is satisfied since the number of bags of type T used is equal to the number of students they are given to. There is flow conservation in the nodes d T as the number of students with blood type T who is given blood is equal to the sum of the different types of blood they are given. Combined this means a maximum flow corresponds to a maximum number of students can get a transfusion. Lexicographically smallest shift In chemical databases for circular molecules, each molecule is represented by a circular string of chemical characters. To allow faster lookup and comparisons of molecules, one wants to store each circular string by a canonical linear string. A natural choice for a canonical in ear strings the one that is lexicographically smallest. That gives the following computational problem. Assume we are given a string T = x 1... x n of length n. A shift of T by s, 0 s < n, is the string T s = x s+1 x s+2... x n x 1 x 2... x s. In this problem we want to find the lexicographically smallest shift, i.e. the shift s where T s is lexicographically smallest among T 0,..., T n 1. Eg. T 2 = T 7 = a a b a b a a b a b are the lexicographically smallest shifts of the string T = a b a a b a b a a b Q1 State all s where T s is a lexicographically smallest shift of the string T = b c a b a a b c a b a a b c a b a a T 4 = T 10 = T 16 = a a b c a b a a b c a b a a b c a b Q2 Describe an algorithm that given a string T of length n over an alphabet of size O(1) computes all s where T s is a lexicographically smallest shift of T. State the algorithms running time. Construct the suffix tree S of the string T T $ where $ is a new character lexicographically larger than all others in the alphabet. Assume all the children lists in S are lexicographically ordered from left to right. Follow the left-most path in S from the root. Stop when a node v where there is at least n + 2 characters on the path from the root to it is reached. Look at all the leafs in the subtree of v, and output the indices of the suffixes they correspond to. Correctness. First we must argue, that the algorithm always can find a node v such that the number of characters from the root to v is at least n + 2 by only looking at the left-most path. Assume this is not the case, then the left-most leaf in the tree must correspond to a suffix s$ of length less than n + 2. Because 4

we have repeated T after itself, then there must exist another suffix st $. But s$ is lexicographically larger than st $ (because of how we chose $), which contradicts with all children being lexicographically ordered from left to right. All the suffixes represented by leafs in the subtree under v share the same first n characters, so if we only look at the n first characters in each suffix they must be lexicographically the same, thus they all correspond to lexicographically smallest shifts. Analysis. Constructing the suffix tree can be done in O(2n + 1) = O(n) time since the length of the string is 2n + 1 and the alphabet size is constant. A simple path from the root to any node is at most n nodes, so following the left-most path takes O(n) time. Reporting the indices from all the leafs below v takes O(n) time since we must traverse a tree with less than n nodes. So the total running time is O(n). 5

Maximum flow and Minimum Cut Consider the below network with edge capacities. Give a maximum flow from s to t in the network (state for each edge the flow along the edge), state the value of the maximal flow, and a minimum cut where the capacity of the flow equals the maximal flow. 5 / 5 / 5 / 5 / 4 / 0 / 9 / 4 / 4 / 5 / 2 / 4 / The maximum flow is 11. A minimum s-t cut is {s, D, A, B} / {C, E, G, H, F, t} and is illustrated by the red line. (Another minimum s-t cut is {s, D, A, B, C, F } / {E, G, H, t} - answering just one of these is sufficient) The Edmonds-Karp algorithm Consider the Edmonds-Karp algorithm applied to the above graph to find a maximal flow. State the augmenting paths found by the algorithm. State for each augmenting path the nodes on the path and the value augmented with along the path. augmenting path value s t 2 s D E F t 3 s D E G H t 1 s D A B C F t 2 s D A B C F E G H t 3 6