Homework 1 Solutions

Similar documents
Fall 2016 Test 1 with Solutions

Homework 1 Submission

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

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

Written Homework #1: Analysis of Algorithms

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

CS Non-recursive and Recursive Algorithm Analysis

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

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

CSE332: Data Abstrac0ons Sec%on 2. HyeIn Kim Spring 2014

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

Lecture 1: Asymptotic Complexity. 1 These slides include material originally prepared by Dr.Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole.

Problem Set 1 Solutions

Asymptotic Analysis 1

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

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

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 1: Introduction

On my honor I affirm that I have neither given nor received inappropriate aid in the completion of this exercise.

Answer the following questions: Q1: ( 15 points) : A) Choose the correct answer of the following questions: نموذج اإلجابة

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

Module 1: Analyzing the Efficiency of Algorithms

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

Fast Convolution; Strassen s Method

COMP 2804 Assignment 2

Review 3. Andreas Klappenecker

Lecture 2. More Algorithm Analysis, Math and MCSS By: Sarah Buchanan

CS 161 Summer 2009 Homework #2 Sample Solutions

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 3. Θ Notation. Comparing Algorithms

How many hours would you estimate that you spent on this assignment?

Data Structure and Algorithm Homework #1 Due: 2:20pm, Tuesday, March 12, 2013 TA === Homework submission instructions ===

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

3.1 Asymptotic notation

Topic 17. Analysis of Algorithms

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

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

CSCI 3110 Assignment 6 Solutions

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Analysis of Algorithms

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

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

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

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

Data Structures and Algorithms Chapter 3

CSCE 222 Discrete Structures for Computing. Review for the Final. Hyunyoung Lee

Analysis of Algorithms

Lecture 2: Asymptotic Notation CSCI Algorithms I

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

CS/COE 1501 cs.pitt.edu/~bill/1501/ Integer Multiplication

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

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Divide and Conquer: Polynomial Multiplication Version of October 1 / 7, 24201

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

CSC236 Week 4. Larry Zhang

Analysis of Algorithms

Mid-term Exam Answers and Final Exam Study Guide CIS 675 Summer 2010

A design paradigm. Divide and conquer: (When) does decomposing a problem into smaller parts help? 09/09/ EECS 3101

CSE332: Data Abstrac0ons Sec%on 2. HyeIn Kim Winter 2013

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

A SUMMARY OF RECURSION SOLVING TECHNIQUES

CSCI Honor seminar in algorithms Homework 2 Solution

Data Structures and Algorithms Chapter 2

CS 4407 Algorithms Lecture 2: Growth Functions

Review for Midterm 1. Andreas Klappenecker

Divide and Conquer. Arash Rafiey. 27 October, 2016

Problem Set 1. MIT students: This problem set is due in lecture on Wednesday, September 19.

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

More Asymptotic Analysis Spring 2018 Discussion 8: March 6, 2018

Computational Complexity - Pseudocode and Recursions

Introduction to Divide and Conquer

1 Substitution method

Submit Growable Array exercise Answer Q1-3 from today's in-class quiz.

CS483 Design and Analysis of Algorithms

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

Searching. Sorting. Lambdas

Cpt S 223. School of EECS, WSU

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. January 16, 2019

Divide and Conquer. Andreas Klappenecker

EECS 477: Introduction to algorithms. Lecture 5

Module 1: Analyzing the Efficiency of Algorithms

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

LECTURE NOTES ON DESIGN AND ANALYSIS OF ALGORITHMS

Introduction to Computer Science Lecture 5: Algorithms

Algorithms and Programming I. Lecture#1 Spring 2015

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

Asymptotic Notation. such that t(n) cf(n) for all n n 0. for some positive real constant c and integer threshold n 0

Algorithms and Their Complexity

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)

CSE 591 Homework 3 Sample Solutions. Problem 1

CS 361, Lecture 14. Outline

Analysis of Multithreaded Algorithms

Chapter 1 Divide and Conquer Algorithm Theory WS 2016/17 Fabian Kuhn

COMP 9024, Class notes, 11s2, Class 1

CSC2100B Data Structures Analysis

CS173 Running Time and Big-O. Tandy Warnow

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

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

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

An analogy from Calculus: limits

C241 Homework Assignment 8

Transcription:

CS3510 Design & Analysis of Algorithms Section A Homework 1 Solutions Released 4pm Friday Sep 8, 2017 This homework has a total of 4 problems on 4 pages. Solutions should be submitted to GradeScope before 3:00pm on Wednesday, September 6, 2017. It will be marked out of 20, you can earn up to 21 = 1 + 5 + 8 + 3 + 4 points. Late policy: each student can use up to two late days on each homework, for a total of four late days across all four homeworks. If you choose not to submit a typed write-up, please write neat and legibly. Collaboration is allowed/encouraged on problems, however each student must independently complete their own write-up, and list all collaborators. No credit will be given to solutions obtained verbatim from the Internet or other sources. 0. [1 point, only if all parts are completed] (a) Submit either a scan or photo of your homework in a single pdf file on GradeScope. (b) Words on the scan are clearly readable. (c) The answer to each question except question 0 should start on a new page. 1. (5 points) Big-O Notation (a) (1 point) Formally prove that Big O is transitive by relation. That is, if f(n) O(g(n)) and g(n) O(h(n)), then, f(n) O(h(n)). By definition of big-o, there exists constants C 1 and C 2 such that for all n we have and Multiplying the second one by C 1 then gives: which together with the first condition gives: f (n) C 1 g (n) g (n) C 2 h (n). C 1 g (n) C 1 C 2 h (n), f (n) C 1 g (n) C 1 C 2 h (n), 1

(b) (1 point) Find the complexity of the following set loops, where n is given as input: i <-- n; while(i > 1) { j = i; //%% CAUTION: this DOES NOT START AT 0 while (j < n) { k <-- 0; while (k < n) { k = k + 2; } j <-- j * 2; } i <-- i / 2; } Express your answer using the Θ( ) notation. The innermost lopp takes Θ(n) whenever it is called. The outer most loop takes Θ(log n) steps, and between steps log n/2 and log n ( a total of Θ(log n) of steps), we have i < n 1/2. In each such step, j gets doubled Θ(log n) times before it reaches n. So we get a total of Θ(log 2 n) iterations, each costing Θ(n), for a total of Θ(n log 2 n). (c) (1 point) Prove or disprove: there does not exist a pair of functions f(n) and g(n) such that f(n) O(g(n)) and f(n) Ω(g(n)). False, f(n) = g(n) = n are a pair of such functions. (d) (1 point) Prove or disprove: n 2 log 10 n O(n 2.1 ). True. This follows from combining n 2 O(n 2 ) with log 10 n O(n 0.1 ). (e) (1 point) Prove or disprove: 2 2n O(2 n ). False, for any constant C, once we have 2 n > C, we get 2. (8 points) Master Theorem 2 2n > C2 n. The master theorem applies to algorithms with recurrence relations in the form of T (n) = at (n/b) + O(n d ) 2

for some constants a > 0, b > 1, and d >= 0 Find the asymptotic (big-o notation) running time of the following algorithms using Master theorem if possible. State the runtime recurrence if it s not given, and if Master theorem is applicable, explicitly state the parameters a, b and d. Otherwise, give a quick reason that the recurrence relation is not solvable using Master theorem. (a) (2 points) An algorithm with the run-time recurrence: a = 3, b = 4, d = 1. log b a = log 4 3.79 < 1, so O(n). T (n) = 3T (n/4) + O (n) (b) (2 points) An algorithm with the run-time recurrence: T (n) = 8T (n/4) + O ( n 1.5) T (n) = 3T (n/4) + O (1) a = 3, b = 4, d = 0. log b a = log 4 3.79 > 0, so total runtime is O(n.79 ). a = 8, b = 4, d = 1. log b a = log 4 8 = 1.5 < 1, so O(n 1.5 log n). (c) (2 points) An algorithm solves problems by diving a problem of size n into 3 sub-problems of one-fourth the size and recursively solves the smaller sub-problems. It takes constant time to combine the solutions of the sub-problems. T (n) = 3T (n/4) + O (1) a = 3, b = 4, d = 0. log b a = log 4 3.79 > 0, so total runtime is O(n.79 ). GRADING: There was a typo in the August 23 notes (now fixed) that said Master Theorem only applied for d 1. Anyone who then claimed that Master Theorem did not apply for this problem because d < 1 should have still received full points. 3

(d) (2 points) An algorithm solves problems by diving a problem of size n into 2 n sub-problems of half the size and recursively solves the smaller sub-problems. It takes linear time to combine the solutions of the sub-problems. T (n) = 2 n T (n/2) + O (n). Not solvable by Master theorem since a = 2 n is not a constant. 3. (3 points) Divide and conquer After learning about the Stooge sort (https://en.wikipedia.org/wiki/stooge_sort), Buzz would like to improve its performance. This led to the Buzzsort, with pseudocode as follows: (a) If the sequence length is at most 4, sort it using bubble sort. (b) Else: i. Divide the list into 5 pieces evenly, by scanning the entire list. ii. (recursively) sort the first 3/5 of the list. iii. (recursively) sort the last 3/5 of the list. iv. (recursively) sort the first 3/5 of the list. For example, on the input sequence The first recursive sort produces the second sort produces and the last produces 1, 5, 3, 2, 4 1, 3, 5, 2, 4, 1, 3, 2, 4, 5, 1, 2, 3, 4, 5. (a) (2 points) Write down a runtime recurrence for Buzzsort and analyze its asymptotic running time. ( ) 3 T (n) = 3T 5 n + O (n). This fits into the requirements of Master theorem with a = 3, b = 5, and d = 1. log 3 5 3 3 2.15 > 1, so the running time is O(n 2.16 ). 4

(b) (1 point) Give an example sequence on 5 or 10 integers where Buzzsort does not terminate with the correct answer. on the input sequence 5, 4, 3, 2, 1. The first recursive sort produces the second sort produces and the third produces which is not sorted. 4. (4 points) Fast multiplication and convolution. 3, 4, 5, 2, 1, 3, 4, 1, 2, 5, 1, 3, 4, 2, 5, We show several additional applications of fast multiplications of integers. A degree d polynomial is the function p (x) = a 0 + a 1 x + a 2 x 2 +... a d x d. These objects multiply just like integers, except without carries. That is if we multiply degree d polynomials p and q with coefficients a 0... a d and b 0... b d respectively, the coefficient of x i in the result is a j b i j. max{0,i d} j min{i,d} (a) (1 point) Show that if p(x) and q(x) are degree n polynomials with integer coefficients in the range [0, n], all coefficients in the product p(x) q(x) are integers in the range [0, O(n 3 )]. Each of the a j b i j term is at most n 2, n of them gives O(n 3 ). (b) (2 points) Show using an extension of Karatsuba s algorithm that two degree n polynomials can still be multiplied in O(n 1.6 ) time or better. You may assume that integer arithemtics involving poly(n) sized numbers take O(1) time. We modify Karatsuba s algorithm by passing around polynomials. The operations of addition / subtraction still works in linear time, and the key identity becomes: [ p1 (x) x k + p 2 (x) ] [ q 1 (x) x k + q 2 (x) ] = ( x 2k x k) p 1 (x) q 1 (x) ( x k 1 ) p 2 (x) q 2 (x) + x k [p 1 (x) + p 2 (x)] [q 1 (x) + q 2 (x)]. So the same divide-and-conquer scheme still works. 5

(c) (1 points) The shifted dot products of two sequences y 0... y n and z 0... z n for each shift s is given by n s y i z i+s. i=0 Show (via equations) that the s-shifted dot product is precisely the coefficient of x n s in the product of the polynomials with coefficients and a 0 = y 0, a 1 = y 1,..., a n = y n. b 0 = z n, b 1 = z n 1,..., b n = z 0. Note that the second polynomial takes the coefficients in the reverse order. Aside: these values are quite useful in performing approximate string matching. Plugging in a i = y i and b i = z n i into the coefficient for n s in their polynomial product gives: a i b n s i = y i z n (n s i). = y i z s+i. 0 i n s 0 i n s 0 i n s The last term is exactly the s-shifted dot product between y and z. 6