Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

Similar documents
Data selection. Lower complexity bound for sorting

Data Structures and Algorithms

1 Terminology and setup

Week 5: Quicksort, Lower bound, Greedy

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

Data Structures and Algorithms CSE 465

Lecture 4. Quicksort

Selection and Adversary Arguments. COMP 215 Lecture 19

Concrete models and tight upper/lower bounds

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

Algorithms And Programming I. Lecture 5 Quicksort

Central Algorithmic Techniques. Iterative 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

Design and Analysis of Algorithms

Quick Sort Notes , Spring 2010

Review Of Topics. Review: Induction

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

CS361 Homework #3 Solutions

CS Data Structures and Algorithm Analysis

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

Analysis of Algorithms CMPSC 565

Partition and Select

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

CS173 Lecture B, November 3, 2015

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

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

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

Algorithms and Data Structures 2014 Exercises week 5

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

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Exercise Sheet #1 Solutions, Computer Science, A M. Hallett, K. Smith a k n b k = n b + k. a k n b k c 1 n b k. a k n b.

CSCE 750, Spring 2001 Notes 2 Page 1 4 Chapter 4 Sorting ffl Reasons for studying sorting is a big deal pedagogically useful Λ the application itself

CS1800: Mathematical Induction. Professor Kevin Gold

P, NP, NP-Complete, and NPhard

Fast Sorting and Selection. A Lower Bound for Worst Case

MATH 2200 Final Review

5. DIVIDE AND CONQUER I

Quicksort. Where Average and Worst Case Differ. S.V. N. (vishy) Vishwanathan. University of California, Santa Cruz

COMP 250: Quicksort. Carlos G. Oliver. February 13, Carlos G. Oliver COMP 250: Quicksort February 13, / 21

Analysis of Algorithms. Randomizing Quicksort

Bucket-Sort. Have seen lower bound of Ω(nlog n) for comparisonbased. Some cheating algorithms achieve O(n), given certain assumptions re input

CIS Spring 2018 (instructor Val Tannen)

Fundamental Algorithms

Fundamental Algorithms

CS 161 Summer 2009 Homework #2 Sample Solutions

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

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

Sorting and Searching. Tony Wong

An analogy from Calculus: limits

MITOCW Lec 11 MIT 6.042J Mathematics for Computer Science, Fall 2010

Proof Techniques (Review of Math 271)

1 Divide and Conquer (September 3)

1. Basic Algorithms: Bubble Sort

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM

CSCI 3110 Assignment 6 Solutions

NP-Completeness. Until now we have been designing algorithms for specific problems

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

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

Algorithms, CSE, OSU Quicksort. Instructor: Anastasios Sidiropoulos

Average Case Analysis of QuickSort and Insertion Tree Height using Incompressibility

data structures and algorithms lecture 2

CS Algorithms and Complexity

Graph Theory. Thomas Bloom. February 6, 2015

Linear Selection and Linear Sorting

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

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

Algorithm Design and Analysis

CS161: Algorithm Design and Analysis Recitation Section 3 Stanford University Week of 29 January, Problem 3-1.

CS 2110: INDUCTION DISCUSSION TOPICS

CS1800 Discrete Structures Spring 2018 February CS1800 Discrete Structures Midterm Version A

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

5. DIVIDE AND CONQUER I

A non-turing-recognizable language

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

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

1 Closest Pair of Points on the Plane

CSC 344 Algorithms and Complexity. Proof by Mathematical Induction

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

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

Quicksort algorithm Average case analysis

Problem Set 2 Solutions

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

Part V. Matchings. Matching. 19 Augmenting Paths for Matchings. 18 Bipartite Matching via Flows

Math 391: Midterm 1.0 Spring 2016

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

1. Basic Algorithms: Bubble Sort

Reading and Writing. Mathematical Proofs. Slides by Arthur van Goetham

Analysis of Algorithms - Using Asymptotic Bounds -

Quiz 3 Reminder and Midterm Results

CSC236 Week 3. Larry Zhang

CSE 421, Spring 2017, W.L.Ruzzo. 8. Average-Case Analysis of Algorithms + Randomized Algorithms

Mathematics-I Prof. S.K. Ray Department of Mathematics and Statistics Indian Institute of Technology, Kanpur. Lecture 1 Real Numbers

CSE 202 Homework 4 Matthias Springer, A

Section Notes 8. Integer Programming II. Applied Math 121. Week of April 5, expand your knowledge of big M s and logical constraints.

CSE 321 Solutions to Practice Problems

Solutions. Prelim 2[Solutions]

Design and Analysis of Algorithms

Advanced Analysis of Algorithms - Midterm (Solutions)

CSC 2414 Lattices in Computer Science September 27, Lecture 4. An Efficient Algorithm for Integer Programming in constant dimensions

Asymptotic Analysis and Recurrences

Transcription:

Algorithms and Data Structures 016 Week 5 solutions (Tues 9th - Fri 1th February) 1. Draw the decision tree (under the assumption of all-distinct inputs) Quicksort for n = 3. answer: (of course you should also *explain* why to students). What is the smallest possible depth of a leaf in a decision tree for a sorting algorithm? answer: The shortest possible depth is n 1. To see this, observe that if we have a root-leaf path (say p r l ) with k comparisons, we cannot be sure that the permutation π(l) at the leaf l is the correct one. proof: To see this consider a graph of n nodes, each node i representing A[i]. Draw a (directed) edge from i to j if we compare A[i] with A[j] on the path from root to l. Note that for k < n 1, this graph on {1,..., n} will not be connected. Hence we have two components C 1 and C and we know nothing about the relative order of array elements indexed by C 1 against elements indexed by C. therefore there cannot be a single permutation π that sorts all inputs passing these k tests - so π(l) is wrong for some arrays which lead to leaf l. 1

3. Intuition: In doing this kind of question, you should always think of choosing comparisons which will carry most information - ie the result of the comparison (< or >) will split our current possible permutations as close to half as possible. (a) Let the numbers to be sorted be x, y, z, w. Here is the algorithm. 1. Compare (x, y).. Compare (z, w). 3. Compare (winner(1), winner ()). 4. Compare (loser(1), loser()). 5. Compare (loser(3), winner(4)). Output: winner(3), winner(5), loser(5), loser(4). (b) Assume wlog all four inputs are distinct. There are 4! = 4 different permutations of 4 inputs, all are possible outputs. We model this as usual as a binary decision tree with at least 4 leaves (to cover each permutation). The length of a root-leaf path in the decision tree corresponds to the number of comparisons done in sorting that particular permutation. Suppose that we have a binary tree with height l. Then this tree has at most l leaves. To solve our 4-sort problem, we require l 4, hence we need l lg 4 > 4 (to show lg 4 > 4 without calc, just observe lg 16 = 4). Since path-length corresponds to no-of-comparisons, we need a tree which for some inputs does more than 4 comparisons. 4. Show that there is no comparison sort whose running time linear for at least half of n! inputs of length n. What about a fraction of 1/n of the inputs of length n? What about a fraction of 1/ n? Answer: 1st case: First case (1/ of all n! inputs) is not much different from what s in the notes. Have a try, and if stuck, ask me. nd case: Take the second case - we would like an algorithm S which sorts in cn time, for some c > 0, on at least a 1/n fraction of all n! input permutations. For nd case, I will prove by contradiction. So suppose there is some sorting algorithm S which sorts in cn time every permutation in some set P S n, such that P n!/n. Consider the decision tree of S, and restrict it to the leaves which are labelled by elements of P - by this, I mean that we remove every leaf not in P, every internal node which has no elements of P below it, and every edge which has no elements of P below

it. Let h P denote the height of the restricted tree T P. We will now derive a lower bound on h P. Observe that as it stands, the restricted tree T P on P is not a binary tree, as there may be many vertices which have just one child. We take care of this by contracting any degree- vertices (except the root), ie by identifying the edges passing through such a vertex. Finally, if the root node has degree 1 in T P, we will delete it and its outgoing edge. These contractions and prunings create a binary tree T P (though not necessarily a complete or near-complete binary tree). The point to note is that T P will have height h P such that h P h P. Now we lower bound h P. By h P h P this will automatically give us the exact same lower bound on h P. We have a binary tree of height h P, hence it can have at most h P leaves. Since P n!/n, it must contain at least n!/n leaves. Hence we require h P n!/n, and by hp h P, we certainly require h P n!/n = (n 1)!. This is equivalent to h P lg((n 1)!). Then by (n 1)! (n 1) (n 1)/, we require h P lg((n 1) (n 1)/ ) = ((n 1)/) lg(n 1). note: I think it is clear that (n 1)/ lg(n 1) is non-linear (ie is not O(n)). To prove rigorously, imagine I am comparing against cn, for any given c (I m doing this because in the definition of O( ), we have the power to choose c). I will now show that regardless of which c > 0 we are working with, that for sufficiently large n, we have ((n 1)/) lg(n 1) > cn. Take n 0 = c+ + 1. Then for all n > n 0, ((n 1)/) lg(n 1) ((n 1)/) lg(n 0 1) = n 1 (c + ) c(n 1) + ( c+ ) = cn + ( c+1 c) > cn. Therefore, regardless of which c > 0 we chose, our corresponding definition of n 0 gives h P h P ((n 1)/) lg(n 1) > cn for all n n 0. Hence we have a contradiction. So no sorting algorithm can sort 1/nth of its inputs in linear time. 3rd case: For the case when we ask about 1/ n, the answer is *still* no (no sorting alg takes just linear time on this fraction). It s a bit harder though. When we are working with h P, our assumption changes, and now we have the condition h P h P lg(n!/ n ), 3

since the size of the set P is now only required to be a n fraction. We then apply our usual formula n! n n/. Therefore it must be the case that h P h P lg(n n/ / n ). Now observe that n = 4 n/, so the condition is exactly equivalent to asking for h P h P lg(( n 4 )n/ ). = n lg(n 4 ) = n (lg(n) lg(4)) = n (lg(n) ) As in the solution to the nd case I d say it was obvious that this is non-linear... You might or might-not want to do a rigorous proof of non-linearity in relation to some arbitrary cn. An interesting thing is that actually the n 0 of nd case will work here. This is just luck, maybe helped a bit by the fact that although n (lg(n) ) is (a bit) smaller than the value for nd case, it is nevertheless neater (in terms of working with lg etc). 5. Tutor, for this question please follow the exact version of Partition from the slides - if you use a different version, you may get not get non-stability (or may get an easier example). I tried to achieve this with just three items but could not see how... Example: the array 6 a, 4 a, 6 b, 4 b. At the top-level, 4 b is the pivot. Walking from the left, the first A[j] selected for swapping (as <= 4) is j = with A[] = 4 a. i has been sitting to the left of the array (it did not move during j = 1) so it advances to i 1. A[1] = 6 a and A[] = 4 a get swapped, to give the new order 4 a, 6 a, 6 b, 4 b. So far so good. Now j = 3 has A[3] = 6 b so nothing is done; this is the last index we must consider for j so we exit the loop. After exiting loop, i = 1, so we swaps A[] = 6 a and A[4] = 4 b and return the array 4 a, 4 b, 6 b, 6 a with i + 1 = as the split point. So next we have two calls with an 1-element array 4 a, and a -element array 6 b, 6 a. This version of Partition will end up swapping 6 b with itself on the second call. So the final output will be 4 a, 4 b, 6 b, 6 a. 4

hence not stable. Your students might find a simpler example. 6. Intuition: A good way to first get a feel for this question is to consider the no-of-pivots corresponding to the Best-case (equal splits all the way) and worst-case (array sorted) for Running Time of non-random quicksort. In fact these turn out to be best-and-worst cases for pivots also (again in the in non-random quicksort case, which is our question). Lemma: We can show that (no matter how we choose the pivots), we use between (n 1)/ and max{0, n 1} pivots to sort an array of size n (the reason the max is there is to take care of n = 0). Proof is by induction. n = 1. We have 0 pivots, with 0 equal to (n 1)/ and max{0, n 1}. So ok here. n > 1. Suppose true for all k < n (I.H.), now we show for n. Suppose we split into two partitions of size i and n i 1, and assume wlog that i is smallest, possibly zero (this guarantees n i 1 is not zero). Then piv(n) = piv(i) + 1 + piv(n i 1). For lower bound we know piv(i) (i 1)/, and piv(n i 1) (n i )/. So piv(n) 1 + (i 1)/ + (n i )/. Best way of finishing this is to do case analysis on odd/evenness of n and i. In all 4 cases you will get a lower bound of (n 1)/ (which is only met for n odd, i odd). For upper bound, we observe that piv(n) 1 + max{0, i 1} + (n i ) (n 1). (we only have one max because we know the rhs has n i 1 > 0) Worst case: Take an array in sorted order 1,, 3,..., n. At each step, we will split into a subarray of length n 1, then the pivot, and an empty subarray. Hence we use n 1 pivots. Best case: take an array of length k 1 for some k. The array is arranged so that the final element is k 1 and such that all elements less than k 1 are in the first k 1 positions, and all elements greater than this are in the last k 1 positions (also this is true *recursively*, I don t have time to write details). Then, the first pivot splits the array exactly into two parts of equal size k 1 1, with the pivot in the middle. Applied recursively, this means we use k 1 1 = (n 1)/ calls - I m not going to prove this, but check n = 15 as an example. 5

7. Show how to sort n integers in the range {1,..., n } in O(n) time. answer: This is a simple application of the Radix Sort Theorem of lecture 9. The theorem states that if we have numbers represented by b bits, we can sort in time Θ(n b/lg(n) ) time. When our numbers are the integers between 1 and n, the numbers of bits needed for the representation is b = lg(n). Then b/lg(n) 4. So Radix sort (with bits taken in lg(n) size blocks) runs in Θ(4n) = Θ(n). Mary Cryan 6