Weighted Activity Selection

Similar documents
CSE 421 Introduction to Algorithms Final Exam Winter 2005

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

Dynamic Programming: Interval Scheduling and Knapsack

Activity selection. Goal: Select the largest possible set of nonoverlapping (mutually compatible) activities.

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

Lecture 2: Divide and conquer and Dynamic programming

Dynamic Programming( Weighted Interval Scheduling)

CSE 421 Dynamic Programming

Analysis of Algorithms - Using Asymptotic Bounds -

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

Problem. Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26

CMPS 2200 Fall Divide-and-Conquer. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

(a) Write a greedy O(n log n) algorithm that, on input S, returns a covering subset C of minimum size.

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

Algorithm Analysis Recurrence Relation. Chung-Ang University, Jaesung Lee

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort

Recap & Interval Scheduling

COMP 355 Advanced Algorithms

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

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

Topic 17. Analysis of Algorithms

Notes for Recitation 14

COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background

CMSC 451: Lecture 7 Greedy Algorithms for Scheduling Tuesday, Sep 19, 2017

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

CS 6901 (Applied Algorithms) Lecture 3

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

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

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

CS2223 Algorithms D Term 2009 Exam 3 Solutions

Dynamic Programming. Cormen et. al. IV 15

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

Greedy Algorithms My T. UF

Online Interval Coloring and Variants

Knapsack and Scheduling Problems. The Greedy Method

Solutions. Problem 1: Suppose a polynomial in n of degree d has the form

Dynamic Programming (CLRS )

This means that we can assume each list ) is

CSE 613: Parallel Programming. Lecture 8 ( Analyzing Divide-and-Conquer Algorithms )

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 10

Introduction to Mathematical Programming IE406. Lecture 21. Dr. Ted Ralphs

CS 4104 Data and Algorithm Analysis. Recurrence Relations. Modeling Recursive Function Cost. Solving Recurrences. Clifford A. Shaffer.

Searching. Sorting. Lambdas

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Divide-and-conquer: Order Statistics. Curs: Fall 2017

CS583 Lecture 11. Many slides here are based on D. Luebke slides. Review: Dynamic Programming

Aditya Bhaskara CS 5968/6968, Lecture 1: Introduction and Review 12 January 2016

Integer Linear Programming

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

CS264: Beyond Worst-Case Analysis Lecture #11: LP Decoding

Week 5: Quicksort, Lower bound, Greedy

General Methods for Algorithm Design

Fundamental Algorithms

1 Some loose ends from last time

Algorithms Design & Analysis. Analysis of Algorithm

data structures and algorithms lecture 2

Recap: Prefix Sums. Given A: set of n integers Find B: prefix sums 1 / 86

Copyright 2000, Kevin Wayne 1

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

Design and Analysis of Algorithms

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

CS 580: Algorithm Design and Analysis

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

Divide and Conquer Strategy

Faster Algorithms for some Optimization Problems on Collinear Points

Topics in Approximation Algorithms Solution for Homework 3

CS60007 Algorithm Design and Analysis 2018 Assignment 1

3.1 Asymptotic notation

Time Analysis of Sorting and Searching Algorithms

1 Basic Definitions. 2 Proof By Contradiction. 3 Exchange Argument

Divide and Conquer. Arash Rafiey. 27 October, 2016

Solving recurrences. Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive algorithms.

Mat 3770 Bin Packing or

Data Structures and Algorithms Chapter 3

Limitations of Algorithm Power

CS 344 Design and Analysis of Algorithms. Tarek El-Gaaly Course website:

Algorithm Design and Analysis

Computer Algorithms CISC4080 CIS, Fordham Univ. Outline. Last class. Instructor: X. Zhang Lecture 2

Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2

Greedy vs Dynamic Programming Approach

Santa Claus Schedules Jobs on Unrelated Machines

CSE 613: Parallel Programming. Lectures ( Analyzing Divide-and-Conquer Algorithms )

University of California Berkeley CS170: Efficient Algorithms and Intractable Problems November 19, 2001 Professor Luca Trevisan. Midterm 2 Solutions

Algorithms Design & Analysis. Dynamic Programming

Asymptotic Algorithm Analysis & Sorting

is called an integer programming (IP) problem. model is called a mixed integer programming (MIP)

CS261: Problem Set #3

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

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

Data Structures and Algorithms CSE 465

Mergesort and Recurrences (CLRS 2.3, 4.4)

Dynamic Programming 1

CSE 202 Dynamic Programming II

Data Structures and Algorithms

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

INTEGER PROGRAMMING. In many problems the decision variables must have integer values.

On Two Class-Constrained Versions of the Multiple Knapsack Problem

Algorithms. Quicksort. Slide credit: David Luebke (Virginia)

CS6375: Machine Learning Gautam Kunapuli. Decision Trees

Transcription:

Weighted Activity Selection Problem This problem is a generalization of the activity selection problem that we solvd with a greedy algorithm. Given a set of activities A = {[l, r ], [l, r ],..., [l n, r n ]} and a positive weight function w : A R +, nd a subset S A of the activities such that s t =, for s, t S, and s S w (s) is maximized. We will solve this problem iteratively, rening our algorithm at each stage to get a more ecient solution. s t 5 u 4 Greedy algorithm for unweighted activity selection chooses {s, u}. Chosing the greatest weight rst selects {t}. The optimal solution is {u, v}. v Optimal Substructure: The key to creating dynamic programming algorithms is observing some optimal substructure: How part of an optimal solution (substructure) is an optimal solution to a subproblem. We did this with the unweighted activity selection problem. Recall our proof of correctness of our greedy activity selection: We showed that the choices made after the rst selection were an optimal solution to a subproblem the set of activities that did not conict with with the rst choice. Our identication of the optimal substructure will be dierence because we do not have a greedy strategy to choose a rst activity. We notice that if the set of activities A is non-empty, an optimal solution S A must contain some activity because the weight function w is positive. So consider some s S (we do not care which one). Then all of the activities in S \ {s} are either left of s or right of s because activities of S do not overlap. Let Then S L(s), S R(s), and {s} partition S. L (s) = {a A : a s = and a is left of s} R (s) = {a A : a s = and a is right of s} S L(s) S R(s) s This problem is similar to the unweighted variant. So we guess that S L (s) is an optimal solution the weighted activity selection problem restricted to L (s). Is this true? Yes! To see why, consider an optimal selecton T of L (s). Recall, that this means that t u =, for t, u T, and t T w (t) is maximum in particular, t T w (t) t S L(s) w (t).

To prove that S L(s) is an optimal solution to the subprobem, we substitute T for S L(s) in S. Then the optimality of S will force the above inequality to be an equality. First, we must show that this substitution is valid that is, t u =, for t, u (S \ L (s)) T. By construction, any two activities from S \ L(s) do not overlap and any two activities from T do not overlap. So the last case to consider is when u S \ L(s) and t T. When this happens, t is left of u because t is left of s and s is the leftmost element of S \ L(s). Let us look at the weight of the selections of A. w (t) = w (t) + w (t) t S = t S\L(s) w (t) + t S\L(s) t T w (t) t (S\L(s)) T t S L(s) w (t) by optimality of T But, t S w (t) t (S\L(s)) T w (t) by optimality of S. Therefore, t S w (t) = t (S\L(s)) T w (t) and t T w (t) = t S L(s) w (t). Thus, S L (s) is an optimal selection for L (s). Notice that we have just proven that we can substitute any optimal solution T to L(s) for S L (s) in S because t T w (t) = t S L(s) w (t)! This suggests a divide-and-conquer algorithm for solving this problem: Choose an s; recursively solve for L(s) and R(s); combine them with s to see if we get an optimal solution. We do not know which s to pick, so we try them all! Algorithm DCMaxSelect(A) S max for each [l, r] A do L {[p, q] A : q < l} R {[p, q] A : r < p} T DCMaxSelect(L) U DCMaxSelect(R) δ = w ([l, r]) + s T w (s) + s U w (s) if max < δ then S T U {[l, r]} max δ return S We use caching to turn this into a dynamic programming solution. The input to DCMaxSelect is a subset of A, which has n elements. Recall that there n possible subsets. So just initializing a table large enough to be indexed by all possible inputs would take exponential time. Fortunately, not all n subsets of A occur in recursive calls to DCMaxSelect.

L l r R L R l r left subproblem We can prove inductively (on the depth of recursion) that the input to every recursive call is a subset of the form {[l, r] A : α < l and r < ω} for some α and ω. In fact, α will be or l i and ω will be or r j, for some i and j. So there are only O ( n ) such inputs. But we cannot index a table (cache) with continuous α and ω. However, α and ω only take on a nite set of values. Let X = {l, l,..., l n } {r, r,..., r n } {, } = {x, x..., x k }, where k n + and x i < x j, for i < j Then we represent α and ω with an index from... k. Let max[i, j] =the maximum weight activity selection of all of the activities of A contained in the interval (x i, x j ), where i < j. 4 6 4 8 3 5 7 5 x i x j 3 4 5 6 7 8-0 0 0 0 - - 0 0 0 0 - - - 0 0 0 0 0 5 3 - - - - 0 0 0 0 0 4 4 - - - - - 0 0 0 0 4 5 - - - - - - 0 0 0 4 6 - - - - - - - 0 0 0 7 - - - - - - - - 0 0 8 - - - - - - - - - 0 - - - - - - - - - - Before calling the following subroutine, we create max from the input and initialize every element of max to. 3

Algorithm DPMaxActivitySelect(x i, x j ) if x i x j then return 0 if max[i, j] then return max[i, j] for each [x s, x t ] A do if x i < x s and x t < x j then max[i, j] max [i, j], DPMaxActivitySelect(x i, x s ) + w ([x s, x t ]) + DPMaxActivitySelect(x t, x j ) Runtime complexity: In the worst case, we ll every cell in the table (O(n ) cells), where each cell could make O(n) recursive calls (i.e. we must try every interval in the region corresponding to the cell). So the total work done is O(n 3 ). Filling the table recursively with caching is a good option because many of the table cells are unused: Certain cells are unused because α is in {, l, l,..., l n } and ω is in {r, r,... r n, }. Iterative Solution: For the subproblems that result from calculating max[i, j], we need the values of max[i, s] and max[t, j] where i < s < t < j (i.e. [x s, x t ] is an activity in the range (x i, x j )). To satisfy this ordering dependency, we could ll in the table max one column at a time starting from the leftmost column and proceding to the rightmost. Then within each column, we would ll it from top to bottom. max j max j i. k t, j i,s i,j left dependency satisfied by right ordering i. k k k Eliminating a Dimension: The greedy solution to the unweighted activity selection problem iteratively added activities to the end of the schedule, but our latest dynamic programming solution to the weighted variant inserts activities arbitrarily. By changing our dynamic programming solution to be more like our greedy algorithm, we get a better solution. Let S be an optimal solution to the weighted variant. Then it contains a rightmost activity s. As before, let L(s) be all the activities in A that are strictly to the left of s. Then S L(s) is an optimal solution to the problem restricted to L(s) by the same reasoning as before. Similarly if T is an optimal solution to L(s), then T {s} is an optimal solution to A. This leads directly to a divide-and-conquer algorithm similar to our previous version, except that it makes one recursive call inside its main loop instead of two. A major improvement of this approach is that all of the subproblems are of the form{[l, r] A : r < ω}, where ω = r i or. So there are only O(n) of these subsets and we can reduce the dimension of our cache by one. Let Y = {l : [l, r] A} { } = {y, y,..., y k }, where k n + and y i < y j, for i < j 4

Let max [i] =maximum weight solution to the subproblem {[l, r] A : r < y i }. Then by maximizing over all choices of last activity { 0 if i = max[i] = max {max[j] + w ([y j, r]) : [y j, r] A and r < y i } otherwise 4 6 4 8 3 5 7 5 i 3 4 5 y i 3 6 max 0 0 0 This gives us a much better iterative algorithm (O ( n ) ). Digging deeper by expanding max recursively, we see that successive values of max have quite a bit of computation redundancy. In the example above { max[] + w ([y, 4]), max[4] = max max[] + w ([y, 5]) max[5] = max max[] + w ([y, 4]), max[] + w ([y, 5]), max[3] + w ([y 3, 7]), max[4] + w ([y 4, 8]) } = max Just what is the precise overlap between max[i] and max [i + ]? max[4], max[3] + w ([y 3, 7]), max[4] + w ([y 4, 8]) a b c d e f When evaluating the maximization, max[i + ] considers all the ending activities that max[i] does, plus activities like c and e, whose right endpoint lies in the range [y i, y i+ ). So y i y i+ 0 { } if i = max[i] = max[i ], max otherwise max {max[j] + w ([y i, r]) : [y j, r] A and r [y i, y i ]} In particular, each activity contributes to the calculation of exactly one max[i]. 0 3 4 3 4 6 7 0 3 3 4 5 8 9 i 3 4 5 6 7 8 y i 3 5 7 9 max[i] 0 0 0 3 Removing this computational overhead leads to a very fast dynamic programming solution. 5

Algorithm FastDPMaxActivitySelect(A) : sort A by right endpoint : create Y {y, y,..., y k } from A 3: for i to k do 4: max[i] 0 5: for i to n do 6: binary search to find t such that r i [y t, y t+ ) 7: binary search{ to find s such that } l i = y s max[t ], 8: max[t] max max[s] + w ([l i, r i ]) What is the runtime of this algorithm? Lines and can be implemented in Θ (n log n) with comparison sorts. The loop on line 3 is O(n). The loop at line 5 executes n times. Within the loop, lines 6 and 7 take O (log n) time, and line 8 takes Θ() time. So the cost of the loop is O (n log n). Therefore, the total runtime is Θ (n log n). Note that the binary search on line 6 can be eliminated with some book-keeping. What about the binary search on line 7? 6