CS2223 Algorithms D Term 2009 Exam 3 Solutions

Similar documents
Dynamic Programming: Interval Scheduling and Knapsack

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

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation:

Fall 2016 Test 1 with Solutions

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

Divide-and-Conquer Algorithms and Recurrence Relations. Niloufar Shafiei

Mergesort and Recurrences (CLRS 2.3, 4.4)

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

University of New Mexico Department of Computer Science. Midterm Examination. CS 361 Data Structures and Algorithms Spring, 2003

Algorithms Test 1. Question 1. (10 points) for (i = 1; i <= n; i++) { j = 1; while (j < n) {

CSCI 3110 Assignment 6 Solutions

CPSC 320 Sample Final Examination December 2013

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

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

Dynamic Programming. Cormen et. al. IV 15

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

Advanced Analysis of Algorithms - Midterm (Solutions)

CS483 Design and Analysis of Algorithms

Quiz 3 Reminder and Midterm Results

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

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

CS Analysis of Recursive Algorithms and Brute Force

CSCI Honor seminar in algorithms Homework 2 Solution

Analysis of Algorithms

Chapter 4 Divide-and-Conquer

University of New Mexico Department of Computer Science. Final Examination. CS 362 Data Structures and Algorithms Spring, 2007

Divide and Conquer. Arash Rafiey. 27 October, 2016

1 Divide and Conquer (September 3)

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

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

CS 580: Algorithm Design and Analysis

Divide and Conquer CPE 349. Theresa Migler-VonDollen

CSE 421 Dynamic Programming

Homework 1 Submission

NOTE: You have 2 hours, please plan your time. Problems are not ordered by difficulty.

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

CS173 Running Time and Big-O. Tandy Warnow

Divide and Conquer. Recurrence Relations

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

Lecture 2: Divide and conquer and Dynamic programming

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Dynamic Programming( Weighted Interval Scheduling)

CS 4407 Algorithms Lecture 3: Iterative and Divide and Conquer Algorithms

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

CS383, Algorithms Spring 2009 HW1 Solutions

R ij = 2. Using all of these facts together, you can solve problem number 9.

Dynamic Programming (CLRS )

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Lecture 6: Recurrent Algorithms:

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

Fall 2017 November 10, Written Homework 5

Week 5: Quicksort, Lower bound, Greedy

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

Lecture 3. Big-O notation, more recurrences!!

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

Introduction to Divide and Conquer

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

CS325: Analysis of Algorithms, Fall Midterm

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

Chapter 4. Recurrences

Divide and Conquer Strategy

Asymptotic Analysis and Recurrences

COMP 382: Reasoning about algorithms

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

1. [10 marks] Consider the following two algorithms that find the two largest elements in an array A[1..n], where n >= 2.

Recurrence Relations

Divide & Conquer. CS 320, Fall Dr. Geri Georg, Instructor CS320 Div&Conq 1

Weighted Activity Selection

Written Homework #1: Analysis of Algorithms

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

COL106: Data Structures and Algorithms (IIT Delhi, Semester-II )

1 Quick Sort LECTURE 7. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

Class Note #14. In this class, we studied an algorithm for integer multiplication, which. 2 ) to θ(n

Lecture 12 : Recurrences DRAFT

Topic 17. Analysis of Algorithms

Algorithms Exam TIN093 /DIT602

Fast Sorting and Selection. A Lower Bound for Worst Case

CMPT 307 : Divide-and-Conqer (Study Guide) Should be read in conjunction with the text June 2, 2015

CS 161 Summer 2009 Homework #2 Sample Solutions

CS 470/570 Divide-and-Conquer. Format of Divide-and-Conquer algorithms: Master Recurrence Theorem (simpler version)

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

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

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

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

Data Structures and Algorithms Chapter 3

Recurrences COMP 215

6. DYNAMIC PROGRAMMING I

An analogy from Calculus: limits

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

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

Copyright 2000, Kevin Wayne 1

COE428 Notes Week 4 (Week of Jan 30, 2017)

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

Problem Set 2 Solutions

What we have learned What is algorithm Why study algorithm The time and space efficiency of algorithm The analysis framework of time efficiency Asympt

CS 4349 Lecture August 30th, 2017

Announcements. CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis. Today. Mathematical induction. Dan Grossman Spring 2010

Transcription:

CS2223 Algorithms D Term 2009 Exam 3 Solutions May 4, 2009 By Prof. Carolina Ruiz Dept. of Computer Science WPI PROBLEM 1: Asymptoptic Growth Rates (10 points) Let A and B be two algorithms with runtimes T A (n) = log a n and T B (n) = log b n, respectively, where a and b are two (possibly different) constants a > 1, b > 1. Prove that T A (n) = Θ(T B (n)). Hint: use the fact that log a n = log b n/ log b a (DO NOT SPEND MORE THAN 5 MINUTES ON THIS PROBLEM.) Alternate Solution 1: T A (n) = log a n = log b n/ log b a = (1/ log b a) log b n = k log b n where k = 1/ log b a is a constant. Note that log b a > 0 since a, b > 1. Hence T A (n) = Θ(log b n) = Θ(T B (n)) Alternate Solution 2: T A (n) = log a n, T B (n) = log b n T B (n) lim n + T A (n) = lim log b n n + log a n = lim log b n n + log b n/ log b a = lim log b a = log b a > 0 since a, b > 1 n + Hence, T A (n) = Θ(T B (n)) 1

PROBLEM 2: Divide and Conquer (45 points + 5 bonus points) Given a sorted array of distinct integers A[1..n], your job is to find out whether there is an index i such that A[i] = i (that is, whether there is a cell in the array whose content and whose index are the same). Here are a few examples: If If If A = -7-3 0 2 5 7 9 15 20 32 51 63 then the answer is i = 5. index: 1 2 3 4 5 6 7 8 9 10 11 12 A = -17-8 -1 0 7 13 82 236 index: 1 2 3 4 5 6 7 8 then the answer is no such i A = -17 2 3 4 9 13 82 236 then any one of the following 3 answers index: 1 2 3 4 5 6 7 8 would suffice: i=2, i=3, or i=4. Provide a divide and conquer algorithm to solve this problem in O(log n) time. Hint: Consider an arbitrary index k, with 1 k n. One of the following 3 cases holds: If k = A[k], then k is the answer! If k < A[k], then think of how k + 1 compares to A[k + 1], k + 2 compares to A[k + 2],..., etc. If k > A[k], then think of how k 1 compares to A[k 1], k 2 compares to A[k 2],..., etc. Construct your divide and-conquer solution following the steps below. 1. (20 Points) Explain the design of your algorithm clearly (10 points) and prove carefully that the resulting algorithm is correct (10 points). Remember that your divide and conquer algorithm must run in O(log n) time. You can assume that the length of the array, n, is a power of 2. Following the hint provided above: If k < A[k], then k + 1 < A[k] + 1 and since the array is sorted in increasing order and doesn t contain repetitions, then A[k] + 1 A[k + 1]. Hence, k + 1 < A[k] + 1 A[k + 1] and so k + 1 < A[k + 1]. The same argument holds for k + 2, k + 3,...,n. In summary, for every k +, k k + n, k + < A[k + ]. Hence, if k < A[k] and there is some index i such that A[i] = i, then it must be the case that i < k. If k > A[k], then k 1 > A[k] 1 and since the array is sorted in increasing order and doesn t contain repetitions, then A[k] 1 A[k 1]. Hence, k 1 > A[k] 1 A[k 1] and so k 1 > A[k 1]. The same argument holds for k 2, k 3,..., 1. In summary, for every k, 1 k k, k > A[k ]. Hence, if k > A[k] and there is some index i such that A[i] = i, then it must be the case that i > k. So given an array A[1..n] Our divide and conquer solution will select k as the midpoint of the array k = n/2. If k = A[k] then we return k. If k < A[k] then we recursive look for a solution in the first half of the array (i.e, A[1..k 1]). If k > A[k] then we recursive look for a solution in the second half of the array (i.e, A[k + 1..n]). 2

2. (10 Points) Write the algorithm in detailed pseudo-code. Here is our algorithm implementing the ideas above: Binary-Search-for-fixpoint(A[1...n]) { Return Aux-Binary-Search-for-fixpoint(A, 1, n) Aux-Binary-Search-for-fixpoint(A[u...v]) { If u > v Return no such i Else { Let k = floor((u + v)/2) If A[k] = k Return k Else If k < A[k] Return Aux-Binary-Search-for-fixpoint(A,u,k-1) Else // k > A[k] // Return Aux-Binary-Search-for-fixpoint(A,k+1,v) 3. (10 Points) Write a recurrence for the runtime T(n) of the algorithm. Explain your work. T(n) = T(n/2). 4. (10 Points) Solve the recurrence to show that your algorith is O(logn). For this, either use the recursion-tree method (= unrolling the recurrence), the substitution method (= guess + induction ), or the master theorem. Show your work and explain your answer. T(n) = T(n/2). Since this recurrence has the form T(n) = at( n/b )+O(n d ) for some constants a = 1, b = 2, and d = 0, and since d = 0 = log b a = log 2 1, then the Master Theorem tells us that T(n) = O(n d log n) = O(n 0 log n) = O(log n). For a solution using the substitution method (= guess + induction ), see the Solved Exercise 1 in the course textbook (page 242). 3

PROBLEM 3: Dynamic Programming (45 points + 5 bonus points) Suppose that your MP3 player has a disk capacity of M megabytes, where M is an integer (whole number). You have a list of n songs S 1, S 2, S 3,...,S n that you would like to download onto your MP3 player. The i th song S i takes up m i megabytes. The problem is that your MP3 player capacity, M, is less than the sum of all the m i s. That is, M < n i=1 m i. Assume that you want to maximize the disk utilization of your MP3 player (that is, you want to use as many of your M megabytes as possible). We proved in Homework 4 that a greedy algorithm that selects songs to download in decreasing size order (from largest to smallest) will NOT produce an optimal solution. Provide an optimal dynamic programming solution to this problem following the steps below. Note that the output of the algorithm should merely be the maximum disk utilization (a single numerical value, in megabytes). The subset of songs for which this maximum is attained, does not need to be returned. Here is the input output specification: Maximum disk utilization(m[1...n], M) Inputs: Array m[1...n] of song sizes, and positive integer disk capacity M. Output: Maximum disk utilization i S m i that can be attained over a subset S {1..n of songs without exceeding the disk capacity M. 1. (25 Points) Dynamic Programming Solution. (a) (20 points) Design a dynamic programming solution to the disk utilization problem, using the subproblems suggested by the hint that follows. Hint: Consider whether or not to add the i-th song to a disk of capacity j, assuming that the first i 1 songs and smaller disk capacities j < j have already been considered. To this end, define a 2-dimensional (n + 1) (M + 1) matrix (array) OPT[0..n][0..M], where OPT[i, j] = maximum disk utilization over a subset of the first i songs S 1..S i, assuming a maximum disk capacity of j megabytes. Find a recurrence relation that expresses the solution of OPT[i, j] in terms of the solutions of slightly smaller problems (i.e., other cells in the matrix OP T whose values can be calculated before). Include suitable boundary conditions for the recurrence relation. At each stage argue decisively that your solution is correct. Define OPT as in the hint. This corresponds to considering, for each i between 0 and n, and each j between 0 and M, the subproblem consisting of finding the maximum disk utilization that is possible by selecting songs from among the first i only, assuming disk capacity j. In order to derive a recurrence relation for OPT, consider whether to add the i-th song S i to a disk of capacity j, assuming that songs S 1..S i 1 have already been considered. If song S i doesn t fit on the disk at all, that is, if m i > j, and assuming that only the first i songs are allowed (we re targeting OPT[i, j] here), then of course the best that you can do is whatever the first i 1 songs allow: OPT[i, j] = OPT[i 1, j] if m i > j Otherwise, if song S i fits, then you have two options: you can either add S i to the disk, or not, and you will need to pick whichever of these two options provides a higher disk utilization. Note that if you do add S i to the disk, then the remaining 4

disk space j m i should be optimally utilized, and this must be done by picking from among the remaining songs, that is, only songs from among the first i 1: OPT[i, j] = max{opt[i 1, j], m i + OPT[i 1, j m i ] if m i j Finally, if i = 0 (base case), there are no songs to consider at all, and so the maximum disk utilization possible is 0. Putting all of the above considerations together, we arrive at the following recurrence relation (base case included): 0, if i = 0 OPT[i, j] = OPT[i 1, j], if m i > j max{opt[i 1, j], m i + OPT[i 1, j m i ], otherwise (b) (5 points) Based on the preceding part of this question, determine in what order you ll calculate the values of the cells in OPT. Explain. In light of the recurrence relation, all values OPT[i 1, j ] with 0 j j must have been calculated prior to attempting to calculate OP T[i, j]. Therefore, we first calculate the values OPT[0, j ], for 0 j M, then the values OPT[1, j ] in order of increasing j, then the values OPT[2, j ], and so on. 2. (12 Points) Algorithm. Write a dynamic programming algorithm (in detailed pseudo-code) implementing the solution you designed above. Maximum disk utilization(m[1...n], M) Inputs: Array m[1...n] of song sizes, and positive integer disk capacity M. Output: Maximum disk utilization i S m i that can be attained over a subset S {1..n of songs without exceeding the disk capacity M. { For j := 0 to M do OPT[0, j] = 0 For i := 1 to n do For j := 1 to M { If m i > j Then OPT[i, j] = OPT[i 1, j] Else OPT[i, j] = max{opt[i 1, j], m i + OPT[i 1, j m i ] return(op T[n, M]) 3. (13 Points) Time Complexity. Determine the asymptotic running time of your dynamic programming algorithm above, as a function of the input sizes n and M. Give the simplest possible big Theta expression for the running time. The initialization loop makes M + 1 passes, each of which involves a single assignment. Therefore, the total initialization time is Θ(M). A total of nm passes are made through the nested loops in which the values of the OP T matrix are updated. Each pass just 5

involves an if-else, an assignment, and possibly an addition and max evaluation. In either case, the total time per pass is bounded above and below by nonzero, finite constants (that do not depend on either n or M). It follows that the total time needed for the neted update loops is Θ(nM). Adding the time for the return statement at the end, we obtain a total running time Θ(nM). 6