Lecture 2: Divide and conquer and Dynamic programming

Similar documents
Dynamic Programming( Weighted Interval Scheduling)

Dynamic Programming. Cormen et. al. IV 15

CSE 202 Dynamic Programming II

Dynamic Programming: Interval Scheduling and Knapsack

Copyright 2000, Kevin Wayne 1

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

CS 580: Algorithm Design and Analysis

6. DYNAMIC PROGRAMMING I

Dynamic Programming 1

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

Dynamic Programming. Weighted Interval Scheduling. Algorithmic Paradigms. Dynamic Programming

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

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

6. DYNAMIC PROGRAMMING I

Chapter 6. Weighted Interval Scheduling. Dynamic Programming. Algorithmic Paradigms. Dynamic Programming Applications

Chapter 6. Dynamic Programming. CS 350: Winter 2018

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

CS173 Running Time and Big-O. Tandy Warnow

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

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

Divide and Conquer Algorithms

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

CSE 421 Dynamic Programming

CS Lunch. Dynamic Programming Recipe. 2 Midterm 2! Slides17 - Segmented Least Squares.key - November 16, 2016

Copyright 2000, Kevin Wayne 1

Algorithms Exam TIN093 /DIT602

Algorithm Design and Analysis

CSE 421 Introduction to Algorithms Final Exam Winter 2005

Divide and Conquer Algorithms

Algorithms and Theory of Computation. Lecture 9: Dynamic Programming

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

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

Areas. ! Bioinformatics. ! Control theory. ! Information theory. ! Operations research. ! Computer science: theory, graphics, AI, systems,.

1 Assembly Line Scheduling in Manufacturing Sector

CSE 421 Weighted Interval Scheduling, Knapsack, RNA Secondary Structure

CS483 Design and Analysis of Algorithms

Asymptotic Analysis and Recurrences

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Algorithm Design and Analysis

Divide and Conquer. Recurrence Relations

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

This means that we can assume each list ) is

Divide and Conquer. Andreas Klappenecker. [based on slides by Prof. Welch]

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

CS483 Design and Analysis of Algorithms

Selection and Adversary Arguments. COMP 215 Lecture 19

CPSC 320 Sample Final Examination December 2013

Lecture 13: Dynamic Programming Part 2 10:00 AM, Feb 23, 2018

CS Analysis of Recursive Algorithms and Brute Force

Intractable Problems Part Two

6. DYNAMIC PROGRAMMING I

FINAL EXAM PRACTICE PROBLEMS CMSC 451 (Spring 2016)

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

data structures and algorithms lecture 2

Algorithms: Lecture 12. Chalmers University of Technology

DAA Unit- II Greedy and Dynamic Programming. By Mrs. B.A. Khivsara Asst. Professor Department of Computer Engineering SNJB s KBJ COE, Chandwad

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

CSE 421 Algorithms. T(n) = at(n/b) + n c. Closest Pair Problem. Divide and Conquer Algorithms. What you really need to know about recurrences

CPS 616 DIVIDE-AND-CONQUER 6-1

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

CSE 421 Greedy Algorithms / Interval Scheduling

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

CS60007 Algorithm Design and Analysis 2018 Assignment 1

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

Chapter 5 Divide and Conquer

1 Divide and Conquer (September 3)

COMP 355 Advanced Algorithms

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

Weighted Activity Selection

Chapter 8 Dynamic Programming

6. DYNAMIC PROGRAMMING II

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Outline DP paradigm Discrete optimisation Viterbi algorithm DP: 0 1 Knapsack. Dynamic Programming. Georgy Gimel farb

CS 561, Lecture: Greedy Algorithms. Jared Saia University of New Mexico

Matching Residents to Hospitals

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

COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background

Advanced Analysis of Algorithms - Midterm (Solutions)

Today s Outline. CS 561, Lecture 15. Greedy Algorithms. Activity Selection. Greedy Algorithm Intro Activity Selection Knapsack

CS 580: Algorithm Design and Analysis

Recurrence Relations

Midterm Exam 2 Solutions

The Knapsack Problem. 28. April /44

CS 6901 (Applied Algorithms) Lecture 3

Greedy Algorithms My T. UF

Divide and Conquer. Maximum/minimum. Median finding. CS125 Lecture 4 Fall 2016

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

CSE 202 Homework 4 Matthias Springer, A

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

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

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

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

Algorithm Design and Analysis

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17

Divide and Conquer. Andreas Klappenecker

Recurrences COMP 215

O Notation (Big Oh) We want to give an upper bound on the amount of time it takes to solve a problem.

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

General Methods for Algorithm Design

Section Summary. Sequences. Recurrence Relations. Summations Special Integer Sequences (optional)

Transcription:

Chapter 2 Lecture 2: Divide and conquer and Dynamic programming 2.1 Divide and Conquer Idea: - divide the problem into subproblems in linear time - solve subproblems recursively - combine the results in linear time, so that the result remains correct. Well known example: Mergesort, sorting n numbers in n log n time: Divide the input into two Sort the two halves by recursive MergeSort Merge the two sorted list in linear time. Often Divide and Conquer works for problems were brute force algorithms are polynomial time. In the basic case we divide the problem into two subproblems, but here we start with the general case immediately. How can we derive D-C result? i) We can guess the emerging running time, and prove it by induction, or ii) we can unroll the recurrence, by analyzing the first levels, identifying the patters, and summing over all levels. Unrolling the recurrence, general approach: (see figure 4.2 (5.2)) Divide the problem of q subproblems of n/2 size, and combine the results in cn time. Note, that the challenge is the division to subproblems, and the combination of the results, such that the result after merging is correct. For running time T (n) we have T (n) qt (n/2) + cn, T (2) c. 1. Analysing the first few levels: First level: cn + all subsequent recursive calls Second and third level: qc(n/2) + all subsequent calls, q 2 c(n/2 2 ) + all subsequent calls 2. Identifying the pattern: For level i: (q/2) i cn, that is, the algorithm has more and more to do in each layer, if q > 2. 3. Summing up over all levels, where we have log 2 n levels T (n) log s n 1 i=0 (q/2) i cn = (q/2)log 2 n 1 c cn q/2 1 q/2 1 n(q/2)log 2 n (2.1) = c 2 nn log2q 1 = c 2 n log2q. (2.2) 11

12 CHAPTER 2. LECTURE 2: DIVIDE AND CONQUER AND DYNAMIC PROGRAMMING where we have used that a log b = b log a. We can then state the following theorem. Theorem 6. Any T (n) satisfying T (n) qt (n/2) + cn for q > 2 is bounded by O(n log 2 q ). For example, if you need to divide the problem in q = 3 subproblems, the running time will be O(n log 2 3 ) = O(n 1.59 ). Note, if you have a quadratic brute force approach, then dividing the problem into four subproblems does not help. Problem. Integer multiplication For n digit numbers, following elementary school methods, the running time is O(n 2 ). (We multiply one number by one digit at a time, for all n digits.) For simplicity, we consider numbers of same number of digits. Let us try an alternative method, that leads to Divide and Conquer. We write the numbers as x 1 2 n/2 + x 2, where x 1 is the higher order n/2 bits, x 2 is the lower order n/2 bits. xy = (x 1 2 n/2 + x 2 )(y 1 2 n/2 + y 2 ) = x 1 y 1 2 n + (x 1 y 0 + x 0 y 1 )2 n/2 + x 0 y 0. Note, that this leads to a q = 4 case, which is unfortunately leads to quadratic running time. We can do it with q = 3: calculate x 1 y 1, x 0 y 0 calculate (x 1 + x 0 )(y 1 + y 0 ) = x 1 y 1 + x 1 y 0 + x 0 y 1 + x 0 y 0. get the required x 1 y 0 + x 0 y 1 from these. Final algorithm is given in the book, with complexity O(n log 2 3 ). Reading: closest points for the difficulties of linear combinations of results. Other D&C problems: counting inversions, Fast Fourier. Further generalization: similar results when the merging is not in linear time. 2.2 Dynamic programming The name comes from Bellman, it emerged before the wide spread of computers. Dynamic stays changing it time, and programming stays for planning. Algorithmic paradigms: Greedy. Build up a solution incrementally, by step wise optimization according to some local criterion. Divide-and-conquer. Break up a problem into independent subproblems, solve each subproblem, and combine solutions. Dynamic programming. Break up a problem into a series of overlapping subproblems, and build up solutions to larger and larger subproblems. 2.2.1 From recursion to iteration - weighted interval scheduling Recursive solution Problem. Weighted interval scheduling. We have a set R of requests i, given by starting and finishing times (s i, f i ), and a weight v i. R = n. A subsets of requests is compatible, if no two of them overlap in time. The goal is to find a maximum weight compatible subset. We do some preprocessing: order requests according to finishing time, and record the last compatible request as p(i). Recursive thinking: let us consider the optimal solution O, and the last interval. There are two options. 1. n O: then O includes an (the) optimal solution for request {1,...p(n)}.

2.2. DYNAMIC PROGRAMMING 13 2. n / O: then O is the same as the optimal solutions for requests {1,...n 1}. Based on this we have the following recursive equation: and j O iff OP T (j) = max(v j + OP T (p(j)), OP T (j 1)), v j + OP T (p(j) > OP T (j 1). This gives us a recursive algorithm Compute-Opt(j). See book. Theorem 7. Compute-Opt(j) correctly computes OPT(j) for each j. Proof. Proof by induction, and following the same reasoning as above. Are we done? No. Running time of the recursive Compute-Opt: grows according to the Fibonacci numbers, and is therefore exponential. Memorizing the recursion Compute-Opt becomes exponential, because it calculates the same instance of Compute-Opt(j) for several times. Memorized version, M-Compute-Opt stores results once computed. See algorithm in book. Theorem 8. The running time of M-Compute-Opt(n) is O(n), assuming that the intervals are sorted by their finish times. Proof. We look for a suitable progress measure that decreases by one each time M-Compute-Opt is called. The number of entries that are empty is such a measure. If we also need the optimal set, and not only the maximum weight, then we need additional post processing, Find-Solution(j). See algorithm in book. Theorem 9. The running time of Find-Solution(n) is O(n). Proof. This is a recursive solution which calls strictly lower values, that is, completes in O(n) time. Iterative solution The key tool for the efficient recursive solution was the maintaining of the array M of already computed values. But then nothing stops us to directly compute the entries of M, which leads to an iterative algorithm, Iterative-Compute-Opt. See book. Theorem 10. The running time of Iterative-Compute-Opt(n) is O(n). 2.2.2 Basic Steps of Dynamic Programming To find a dynamic programming based solution, we need to find a collection of subproblems derived from the original problem that satisfies the following: 1. There are a polynomial number of subproblems. Reasonable requirement to keep the running time polynomial. (We had two for the interval scheduling.) 2. The solution can be easily computed from the solutions to the subproblems. Again, to make sure the algorithm remains polynomial. (It was the maximum operation for interval scheduling.) 3. There is a natural ordering of subproblems from smallest to largest. This is needed to be able to use memorization or iteration. (It was the number of intervals considered, according to increasing finishing times.)

14 CHAPTER 2. LECTURE 2: DIVIDE AND CONQUER AND DYNAMIC PROGRAMMING 2.2.3 Subset sums and Knapsack problems Here the direct approach of defining subproblems do not work. We demonstrate the technique of adding a new variable. Problem. Subset sum problem: We are given n items with wights w i N, and a bound W. Select a subset S, such that i S w i W and i S w i is as large as possible. Solution approaches: greedy solutions with starting from the heaviest or lightest item obviously do not work. So we try with dynamic programming. Let us assume we ordered the requests. Unfortunately the recursion as in the interval scheduling does not work, if item n is part of the optimal solution, all the others may be part of it as well, only the limit W is decreased. Since we need to keep track of the remaining space, we introduce one more variable: OP T (i, w) = max S {1...i} w j w j w j S j S Then we can write up a recursive construction of the optimal set O: if n / O then OP T (n, W ) = OP T (n 1, W ), if n O then OP T (n, W ) = w n + OP T (n 1, W w n ), or if w < w i then OP T (i, w) = OP T (i 1, w), otherwise OP T (i, w) = max(op T (i 1, w), w i + OP T (i 1, w w i )). See the algorithm in the book. For the iterative implementation we need to fill in a matrix M of size n W. Note, it also requires the assumptions, that weights are integer. Theorem 11. The Subset-Sum(n,W) algorithm correctly computes the optimal value of the problem and runs in O(nW ) time. Proof. Each entry of M is filled in in O(1) time, by comparing previous entries. We call such algorithms pseudo-polynomial, as the running time depends on the largest integer involved in the problem. These algorithms get less practical as this number grows large. Theorem 12. Given the table M, the optimal set S can be backtracked in O(n) time. Problem. Integer Knapsack: We are given n items with values v i 0, wights w i N, and a bound W. Select a subset S, such that i S w i W and i S v i is as large as possible. We can use exactly the same approach as for the subset sum problem, with slightly different recursion rule, but the same matrix M: if n / O then OP T (n, W ) = OP T (n 1, W ), if n O then OP T (n, W ) = v n + OP T (n 1, W w n ), or if w < w i then OP T (i, w) = OP T (i 1, w), otherwise OP T (i, w) = max(op T (i 1, w), v i + OP T (i 1, w w i ). Theorem 13. The Knapsack(n,W) problem can be solved in O(nW ) time.

2.2. DYNAMIC PROGRAMMING 15 2.2.4 Shortest path in a graph, including negative weights Recall, the Dijskra algorithm finds shortest path in graphs with positive weights, in O(m log n) steps. Problem. Shortest path with negative weights: Given G = (V, E), and weights {c ij }. Decide if G has a negative cycle. If the graphs has no negative cycle, find a path P from s to t with minimum total cost. We can easily see some wrong approaches. Dijsktra does not work, since now we can not easily declare the set of discovered nodes. Another idea would be to add a large constant to each weights, so that each of them becomes positive. But this penalize long paths more that short paths, and the shortest path may change. Let us see a dynamic programming approach. We will end up with the Bellman-Ford Algorithm, which is one of the first applications of dynamic programming. Remember, what we need: polynomial number of subproblems easy merging of subproblems a natural sequence of items to be included. Moreover, we will use the following simple statement. Lemma 9. If G does not have negative cycles, then the shortest paths have at most n-1 edges ( V = n). We need two tricks from earlier discussions: we will use more than two subproblems, and we will have two variables: i for using at most i edges, and v as the start of a path to t Then we know the following about the optimal path P, representing OP T (i, v), that is, shortest path from v to t with maximum i edges: if P uses at most i 1 edges, then OP T (i, v) = OP T (i 1, v), if P is uses i edges, and the first edge is (v, w), then OP T (i, v) = c vw + OP T (i 1, w), or, as a recursive formula: if i > 0 then OP T (i, v) = min(op T (i 1, v), min (OP T (i 1, w) + c vw)). w The iterative shortest path algorithm fills in an M matrix of n n. To fill in an entry, it needs to compare n previous entries. See algorithm in book. Theorem 14. The Shortest-Path algorithm correctly computes the minimum cost of an s-t path in a graph with no negative cycles, and runs in O(n 3 ) time. Remark. The algorithm can be implemented in O(mn) time, where n 2 is changed to m since only w v pairs with edges need to be evaluated. Similarly, the space requirements can be decreased to O(n). This shortest-path algorithm is relevant even for graphs with only positive weights, due to the possibility of distributed implementation. In the distributed implementation each node pushes its new shortest path values to its neighbors, neighbors update their own values, etc. Algorithms with synchronous and asynchronous implementation in the book. The synchronous distributed implementation is the official Bellman-Ford algorithm. 2.2.5 Negative Cycles in Graphs Actually, up to now we solved only half of the shortest path problem. We have not discussed the detection of cycles yet. Note two subcases: the negative cycle is on a path to the sink, or the negative cycle is not on a path to the sink. If we want to detect all cycles, then we need to use an augmented graph, adding a node t and an edge from all nodes to t.

16 CHAPTER 2. LECTURE 2: DIVIDE AND CONQUER AND DYNAMIC PROGRAMMING Lemma 10. The augmented graph G has negative cycle such that there is a path from the cycle to the sink t iff G has negative cycle. Proof. Forward direction (If G has negative cycle, then G has too): Since no edge leaves t, the negative cycle can not go through t, and needs to be there in G as well. Backward direction (If G has negative cycle, then G has too): If G has a negative cycle, it will not disappear due to the augmentation. Also, a path needs to go to t, since there is an edge from all nodes to t. Algorithm. Bellman-Ford extension to detect cycles. Take B-F, and let it run for i n. Lemma 11. If a node v can reach t and it is contained in a negative cycle, then lim OP T (i, v) =. i Lemma 12. If G has no negative cycles, then OP T (i, v) = OP T (n 1, v), v, i n. The question is for how large i do we need to test? e.g., OP T (n, v) = OP T (n 1, v) may hold for a v in a negative cycle (an entire loop needs to be made, so maybe there is no path which is just one link longer). Therefore, the following result is important. Theorem 15. There is no negative cycle with path to t iff OP T (n, v) = OP T (n 1, v) holds v. Proof. If no negative path then OP T (n, v) = OP T (n 1, v) holds: see previous lemma. If OP T (n, v) = OP T (n 1, v) then no negative path: OPT values for iteration i are calculated only from the values in iteration i 1, so if they have not changed in one iteration, they will never change. This means there can not be a negative cycle, since then the lemma before the last one would hold. Corollary 2. If in a graph G with n nodes OP T (n, v) OP T (n 1, v), then there is a path from v to t containing a negative cycle. This also helps to backtrack the negative cycle in O(n) steps. 2.2.6 Other dynamic programming problems Segmented least squares: example for multi-way choices, Sequence alignment Parsing 2.3 What have we learned Divide and Conquer: Solve subproblems q, and combine results in linear time Running time O(n log 2 q ) Examples: sorting, multiplication, closest points, convolution Dynamic programming: Similarly to divide and conquer we build up solution from subproblems Design steps: recursion, recursion with memorization, iterative solution To derive complexity: see how entries in the iterative solution fill up Famous problems: weighted interval, subset sum, knapsack, shortest paths