Advanced Analysis of Algorithms - Homework I (Solutions)

Similar documents
Advanced Analysis of Algorithms - Midterm (Solutions)

CS 350 Midterm Algorithms and Complexity

Analysis of Algorithms - Midterm (Solutions)

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

CS Non-recursive and Recursive Algorithm Analysis

Problem Set 1 Solutions

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

Data selection. Lower complexity bound for sorting

Review Of Topics. Review: Induction

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

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

Homework 1 Submission

data structures and algorithms lecture 2

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

Optimization Methods in Finance - Homework II (Solutions)

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

Written Homework #1: Analysis of Algorithms

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

Divide and Conquer. Arash Rafiey. 27 October, 2016

COMP Analysis of Algorithms & Data Structures

Fundamental Algorithms

Fundamental Algorithms

COMP Analysis of Algorithms & Data Structures

Lecture 4. Quicksort

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

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

CS 4407 Algorithms Lecture 2: Growth Functions

Please give details of your answer. A direct answer without explanation is not counted.

CS473 - Algorithms I

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

Mergesort and Recurrences (CLRS 2.3, 4.4)

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

CS173 Running Time and Big-O. Tandy Warnow

Divide and Conquer CPE 349. Theresa Migler-VonDollen

Module 1: Analyzing the Efficiency of Algorithms

Big-O Notation and Complexity Analysis

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Design and Analysis of Algorithms Department of Mathematics MA21007/Assignment-2/Due: 18 Aug.

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

COMP 382: Reasoning about algorithms

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Ch 01. Analysis of Algorithms

Review 1. Andreas Klappenecker

Review 3. Andreas Klappenecker

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

3.1 Asymptotic notation

Analysis of Algorithms - Using Asymptotic Bounds -

Ch01. Analysis of Algorithms

Asymptotic Analysis and Recurrences

Recommended readings: Description of Quicksort in my notes, Ch7 of your CLRS text.

Inf 2B: Sorting, MergeSort and Divide-and-Conquer

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

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

CPSC 320 Sample Final Examination December 2013

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

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

Chapter 4 Divide-and-Conquer

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

CSE548, AMS542: Analysis of Algorithms, Spring 2014 Date: May 12. Final In-Class Exam. ( 2:35 PM 3:50 PM : 75 Minutes )

Outline. 1 Merging. 2 Merge Sort. 3 Complexity of Sorting. 4 Merge Sort and Other Sorts 2 / 10

Fall 2016 Test 1 with Solutions

ASYMPTOTIC COMPLEXITY SEARCHING/SORTING

Divide and Conquer Algorithms

1 Divide and Conquer (September 3)

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

COMP 250 Fall Midterm examination

Introduction to Divide and Conquer

Algorithms And Programming I. Lecture 5 Quicksort

Week 5: Quicksort, Lower bound, Greedy

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

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

Data Structures and Algorithms CSE 465

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

Computational Complexity

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

Data Structures and Algorithms Chapter 2

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

EECS 477: Introduction to algorithms. Lecture 5

CSCI Honor seminar in algorithms Homework 2 Solution

Data Structures and Algorithms Running time and growth functions January 18, 2018

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

CS2223 Algorithms D Term 2009 Exam 3 Solutions

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

Outline. 1 Introduction. Merging and MergeSort. 3 Analysis. 4 Reference

Enumerate all possible assignments and take the An algorithm is a well-defined computational

Divide and Conquer Algorithms

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

Arbitrary-Precision Division

Fast Sorting and Selection. A Lower Bound for Worst Case

Homework 1 Solutions

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Analysis of Algorithms

CSE 591 Homework 3 Sample Solutions. Problem 1

Quicksort (CLRS 7) We previously saw how the divide-and-conquer technique can be used to design sorting algorithm Merge-sort

Asymptotic Algorithm Analysis & Sorting

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

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

COMP 9024, Class notes, 11s2, Class 1

COMP 355 Advanced Algorithms

Divide-and-Conquer. Reading: CLRS Sections 2.3, 4.1, 4.2, 4.3, 28.2, CSE 6331 Algorithms Steve Lai

Transcription:

Advanced Analysis of Algorithms - Homework I (Solutions) K. Subramani LCSEE, West Virginia University, Morgantown, WV {ksmani@csee.wvu.edu} 1 Problems 1. Given an array A of n integer elements, how would you find the second smallest element in n + log n comparisons. Consider the following algorithm: Function FIND-MIN(A, low, high) 1: n = high low + 1 : S min = φ 3: if (n = 1) then 4: min w =A[1] 5: S min = φ. 6: return(min w, S min ) 7: end if 8: if (n = ) then 9: if (A[1] A[]) then 10: min w =A[1] 11: Add A[] to S min 1: return(min w, S min ) 13: else 14: min w = A[]. 15: Add A[1] to S min 16: return(min w, S min ) 17: end if 18: end if 19: {We know that n 3} 0: mid = high+low 1: (lmin w, ls min ) = FIND-MIN(A, low, mid) : (rmin w, rs min ) = FIND-MIN(A, mid + 1, high) 3: if (lmin w rmin w ) then 4: min w = lmin w 5: S min = ls min rmin 6: else 7: min w = rmin w 8: S min = rs min lmin 9: end if 30: return(min w, S min ) Algorithm 1.1: Finding the two smallest elements in an array 1

The above algorithm returns the smallest element in the whole array min w and a set S min of candidate elements for the second minimum element. The number of comparisons is characterized by the following recurrence relation: T (1) = 0 T () = 1 T (n) = T ( n ) + 1 This recurrence is easily solved to get T (n) = n 1. The size of the candidate set S min can be characterized by the following recurrence: G(1) = 0 G() = 1 G(n) = 1 + G( n ) G(n) is easily seen to be log n. We can find the smallest element in S min using at most log n comparisons; it thus follows that the second smallest element can be found in n + log n comparisons.. Indicate whether each of the following identities is true or false, giving a proof if true and a counterexample otherwise. (a) f(n) + o(f(n)) Θ(f(n)). (b) (f(n) O(g(n))) (g(n) O(h(n))) (f(n) O(h(n))). (c) log 1/ɛ n O(n ɛ ), ( ɛ) 0 < ɛ < 1. (d) n Ω(5 log e n ). (a) The key observation is that o(f(n)) O(f(n)). Also, f(n) f(n) + o(f(n)); it follows that f(n) + o(f(n) Θ(f(n)). (b) The premises state that f(n) c 1 g(n) and g(n) c h(n). It follows that f(n) c 1 c h(n) and hence f(n) O(h(n)). (c) Observe that The identity is therefore true. (d) Observe that It therefore follows that the identity is true. log lim 1/ɛ n n n ɛ 1 ɛ log log n = lim n ɛ log n = 0 by applying L Hospital s rule lim n n 5 log n n log = lim n log n 5 by applying L Hospital s rule

Function FIND-KLARGEST(A, k, n) 1: We assume that the array elements are stored in A[1] through A[n] and that k is an integer [1, n]. We also assume without loss of generality, we assume that the numbers are distinct. : if (n = 1) then 3: {k has to be 1 as well} 4: return(a[n]) 5: end if 6: We consider a variation of the PARTITION() procedure in which elements larger than the pivot are thrown in the left subarray and elements smaller than the pivot are thrown in the right subarray. 7: Partition A using A[1] as the pivot, using the above PARTITION() procedure. Let j denote the index returned by PARTITION(). {As per the mechanics of PARTITION(), elements A[1] through A[j 1] are greater than A[j] and elements A[j + 1] through A[n] are smaller than A[j].} 8: Copy the elements larger than A[j] into a new array C and the elements smaller than A[j] into a new array D. 9: if ((k = j) then 10: return(a[j]) 11: else 1: if (k < j) then 13: return( FIND-KLARGEST(C, k, (j 1))) 14: else 15: return( FIND-KLARGEST(D, k j, (n j))) 16: end if 17: end if Algorithm 1.: Selection through Partition 3. Devise a Divide-and-Conquer procedure for computing the k th largest element in an array of integers. Analyze the asymptotic time complexity of your algorithm. (Hint: Use the Partition procedure discussed in class.) Algorithm (1.) represents a Divide-and-Conquer strategy for our problem. The worst case running time of the algorithm is captured by the recurrence: T (1) = O(1) T (n) = T (n 1) + O(n) This implies that algorithm runs in time O(n ) in the worst case. 3

4. Argue the correctness of the MERGE() procedure discussed in class. (Hint: Write a recursive version of MERGE() and then use induction.) Function MERGE(A, l 1, h 1, B, l, h, C, l 3, h 3 ) 1: {We assume that the arrays A[l 1 h 1 ] and B[l h ] are being merged into the array C[l 3 h 3 ]. } : if (A is empty) then 3: Copy B into C 4: return 5: end if 6: if (B is empty) then 7: Copy A into C 8: return 9: end if 10: if (A[l 1 ] B[l ]) then 11: C[l 3 ] =A[l 1 ] 1: MERGE(A, l 1 + 1, h 1, B, l, h, C, l 3 + 1, h 3 ) 13: else 14: C[l 3 ] =B[l ] 15: MERGE(A, l 1, h 1, B, l + 1, h, C, l 3 + 1, h 3 ) 16: end if Algorithm 1.3: Recursive Merge To prove the correctness of the algorithm, we use induction on the sum s of the elements in A and B. Clearly if s = 1, the algorithm functions correctly, since it copies the non-empty array into C. Assume that the algorithm works correctly when 1 s k, for some k > 1. Now consider the case in which s = k + 1. In this case, Step (10 :) of the algorithm moves the smallest element in both A and B into the first position in C. This is followed by a recursive call on arrays whose total cardinality is at most k. By the inductive hypoethesis, the recursive calls work correctly and since C[l 3 ] is already in its correct place, we can conclude that the arrays A and B have been correctly merged into array C. 5. What is the value returned by Algorithm (1.4) when called with n = 10? Function LOOP-COUNTER(n) 1: count = 0 : for (i = 1 to n) do 3: for (j = 1 to i) do 4: for (k = 1 to j) do 5: count + + 6: end for 7: end for 8: end for 9: return(count) Algorithm 1.4: Loop Counter For arbitrary n, it is clear that the value of count is given by: count(n) = Σ n i=1σ i j=1σ j k=1 1 4

It follows that count(10) = 55 8 = 0. = Σ n i=1σ i j=1j = Σ n i (i + 1) i=1 = 1 [Σn i=1i + Σ n i=1i] = 1 (n + 1) (n + 1) [n + 6 n (n + 1) = [ n + 1 + 1] 4 3 n (n + 1) ] 5