ECE608 - Chapter 8 answers

Similar documents
HOMEWORK 4 CS5381 Analysis of Algorithm

8 SortinginLinearTime

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

Chapter 8 Sort in Linear Time

Design and Analysis of Algorithms

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

Algorithms Chapter 8 Sorting in Linear Time

CS502 Midterm Spring 2013 (26 May-05 June)

Lower bound for comparison-based sorting

Can we do faster? What is the theoretical best we can do?

Chapter 8 Sorting in Linear Time

II (Sorting and) Order Statistics

Midterm 2. Read all of the following information before starting the exam:

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

Can we do faster? What is the theoretical best we can do?

Lecture: Analysis of Algorithms (CS )

Comparison Based Sorting Algorithms. Algorithms and Data Structures: Lower Bounds for Sorting. Comparison Based Sorting Algorithms

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

COT 6405 Introduction to Theory of Algorithms

Algorithms and Data Structures: Lower Bounds for Sorting. ADS: lect 7 slide 1

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

Other techniques for sorting exist, such as Linear Sorting which is not based on comparisons. Linear Sorting techniques include:

The complexity of Sorting and sorting in linear-time. Median and Order Statistics. Chapter 8 and Chapter 9

The divide and conquer strategy has three basic parts. For a given problem of size n,

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Introduction to Algorithms

Lecture 2: Getting Started

Chapter 4: Sorting. Spring 2014 Sorting Fun 1

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

EECS 2011M: Fundamentals of Data Structures

Fast Sorting and Selection. A Lower Bound for Worst Case

Introduction to Algorithms

Lecture 3. Recurrences / Heapsort

COMP Analysis of Algorithms & Data Structures

O(n): printing a list of n items to the screen, looking at each item once.

ECE608 - Chapter 16 answers

Lower Bound on Comparison-based Sorting

COMP Analysis of Algorithms & Data Structures

Sorting. CMPS 2200 Fall Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk

MergeSort. Algorithm : Design & Analysis [5]

15.4 Longest common subsequence

CSC 273 Data Structures

How fast can we sort? Sorting. Decision-tree example. Decision-tree example. CS 3343 Fall by Charles E. Leiserson; small changes by Carola

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

CS 303 Design and Analysis of Algorithms

Sorting and Selection

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

ECE608 - Chapter 15 answers

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

Algorithms and Data Structures for Mathematicians

Sorting Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

CS473 - Algorithms I

Question 7.11 Show how heapsort processes the input:

15.4 Longest common subsequence

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

COMP Analysis of Algorithms & Data Structures

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

SORTING AND SELECTION

Scribe: Virginia Williams, Sam Kim (2016), Mary Wootters (2017) Date: May 22, 2017

Introduction to Algorithms I

Greedy Algorithms. CLRS Chapters Introduction to greedy algorithms. Design of data-compression (Huffman) codes

/463 Algorithms - Fall 2013 Solution to Assignment 3

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

Algorithms Dr. Haim Levkowitz

Analysis of Algorithms - Quicksort -

Module 2: Classical Algorithm Design Techniques

COMP Analysis of Algorithms & Data Structures

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

Algorithms - Ch2 Sorting

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Introduction to Algorithms

Count Sort, Bucket Sort, Radix Sort. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Wellesley College CS231 Algorithms September 25, 1996 Handout #11 COMPARISON-BASED SORTING

Introduction. e.g., the item could be an entire block of information about a student, while the search key might be only the student's name

I/O Model. Cache-Oblivious Algorithms : Algorithms in the Real World. Advantages of Cache-Oblivious Algorithms 4/9/13

EE 368. Weeks 5 (Notes)

COMP 352 FALL Tutorial 10

Search Trees. Undirected graph Directed graph Tree Binary search tree

CS 5321: Advanced Algorithms Sorting. Acknowledgement. Ali Ebnenasir Department of Computer Science Michigan Technological University

Sample Solutions to Homework #2

Sorting & Growth of Functions

INDIAN STATISTICAL INSTITUTE

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

Chapter 6 Heap and Its Application

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

Lecture 5: Sorting Part A

Introduction to Algorithms 6.046J/18.401J

Lecture 9 March 4, 2010

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

Module 3: Sorting and Randomized Algorithms

4. Sorting and Order-Statistics

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Practical Session #11 Sorting in Linear Time

Data Structures and Algorithms Week 4

data structures and algorithms lecture 6

11/8/2016. Chapter 7 Sorting. Introduction. Introduction. Sorting Algorithms. Sorting. Sorting

Transcription:

¼ À ÈÌ Ê ÈÊÇ Ä ÅË ½µ º½¹ ¾µ º¾¹ µ º ¹½ µ º ¹ µ º ¹ µ º ¹¾ µ ¹ µ ¹ ½

ECE608 - Chapter 8 answers (1) CLR 8.1-3 If the sort runs in linear time for m input permutations, then the height h of those paths of the decision tree consisting of the m corresponding leaves and their ancestors must be linear. Hence, we can use the same argument as in the proof of Theorem 8.1 to show that this is impossible for m = n!, n! n!, or. Wehave2 h m, which gives 2 n 2 n h lg m, and so for all possible m s given here, lg m =Ω(nlg n), so h =Ω(nlg n). In particular, (i) lg( n! )=lg(n!) 1 (n lg n n lg e) 1 2 (ii) lg( n! )=lg(n!) lg n (n lg n n lg e) lg n n (iii) lg( n! 2 n )=lg(n!) n (n lg n n lg e) n (2) CLR 8.2-4 For the preprocessing step, compute the C array as in lines 1 through 7 of Counting- Sort. Then for any query Num-in-Range(a, b), we simply return C[b] C[a 1], where C[0] = 0. The query requires O(1) time to answer. (3) CLR 8.3-1 1

COW SEA TAB BAR DOG TEA BAR BIG SEA MOB EAR BOX RUG TAB TAR COW ROW DOG SEA DIG MOB RUG TEA DOG BOX DIG DIG EAR TAB BIG BIG FOX BAR BAR MOB MOB EAR EAR DOG NOW TAR TAR COW ROW DIG COW ROW RUG BIG ROW NOW SEA TEA NOW BOX TAB NOW BOX FOX TAR FOX FOX RUG TEA (4) CLR 8.3-3 Basis: If d = 1, there is only one digit, so sorting on that digit sorts the array. Inductive step: Assuming that RADIX-SORT works for d 1 digits, we will show that it works for d digits. RADIX-SORT sorts separately on each digit, starting from digit 1. Thus RADIX- SORT of d digits, which sort on digits 1,...,d is equivalent to RADIX-SORT of the low-order d 1 digits followed by a sort on digit d. By our induction hypothesis, the sort of the low-order d 1 digits works, so just before the sort on digit d, the elements are in order according to their low-order d 1 digits. The sort on digit d will order the elements by their dth digit. Consider two elements, a and b, withdth digits a d and b d respectively. 2

If a d <b d, the sort will put a before b, which is correct, since a<bregardless of the low-order digits. If a d >b d, the sort will put a after b, which is correct, since a>bregardless of the low-order digits. If a d = b d, the sort will leave a and b in the same order they were in, because it is stable. But that order is already correct, since the correct order of a and b is determined by the low-order d 1 digits when their dth digits are equal, and the elements are already sorted by their low-order d 1 digits. If the intermediate sort were not stable, it might rearrange elements whose dth digits were equal elements that were in the right order after the sort on their lower-order digits. (5) CLR 8.3-4 (note: Same argument can be used with d = 3 instead of d = 2) With n input elements having values between 0 and n 2 1, we represent the numbers with a two-digit place notation using n as the base(radix). For example, when n = 1000, 0 is represented as (000, 000), 999, 999 is represented as (999, 999), and 623 is represented as (000, 623), 999 is represented as (000, 999), 1, 000 is represented as (001, 000), etc. Then, by using RADIX-SORT to sort the above inputs with d =2 and k = n, the running time is Θ(dn + dk) =Θ(2n +2k) =Θ(2n +2n) =Θ(n). (6) CLR 8.4-2 The worst case occurs when every element falls into the same bucket. When this occurs, INSERTION-SORT in the worst case takes Θ(n 2 ). So the worst case running time is determined by the worst case running time of the sorting algorithm used. If we use MERGE-SORT instead of INSERTION-SORT, then the worst case running time is Θ(n lg n), while we still preserve the linear expected running time because E[n i lg n i ] E[n 2 i ]=Θ(1). 3

(7) CLR 8-3 (a) The first thing to note is that numbers with fewer digits are smaller than numbers with more digits. So the numbers first need to sorted on the basis of how many digits they have. Say that the maximum number of digits any number has is D. Thenwe first sort the numbers into D sub-arrays, such that the first sub-array consists of all numbers with one digit, the second one with two digits... and so on. The procedure SORT-BY-DIGITS given below performs this task in O(n) SORT-BY-DIGITS (A, B, C, D) 1. for i 0toD 2. do C[i] 0 3. for j 1tolength[A] 4. do C[digits[A[j]]] C[digits[A[j]]] + 1 5. for i 1toD 6. do C[i] C[i]+C[i 1] 7. Save a copy of the C array in O(D) time 8. for j length[a] downto1 9. do B[C[digits[A[j]]]] A[j] 10. C[digits[A[j]]]C[digits[A[j]]] SORT-BY-DIGITS takes O(n) because each of the for loops either iterates n times or D times and even if each number has 1 digit D is less than n. We assume that the contents of the array C were saved before the for-loop of line 8-9, this is acceptable as it will take O(D) time. Once this is done, we can run radix sort on each of the subarrays. There are D subarrays, let n k denote the number of elements in the k th subarray and k be the number of digits of each elements in the k th sub-array. The following procedure sorts all sub-arrays in O(n) time. SORT-SUBARRAYS (A, B, C, D) 1. for k0 tod 1 4

2. RADIX-SORT( B[C[k]..C[k +1]],k+1) Each invocation of RADIX-SORT takes O(n k k) time, as there are n k numbers in the k th subarray and each has k digits. The total running time is given as: D T (n) = n k k Because the sum of all n k products is the number of digits in the entire array, we have: k=1 T (n) =O(n) (b) We are given a stream of n characters, thats makes up upto n strings. The strings are to be sorted in alphabetic order, and a short string of k characters is to be ordered before a longer string that has the same first k characters. We use a counting sort to sort the words based on their first letter. Then, for each initial letter, we recursively sort the words with that first letter using the sort algorithm we are designing here, but with the first letter of each word removed. If any one of these entries is just one letter long then we do not include it in the recursion but place it at the beginning of the results (when the recursion returns, place the first letter back on the front of each word). The base case of the recursion is when the set of words to sort is empty. Analysis: Each counting sort call is O(k + 26) = O(k) whenk words are sorted. Let T (n) be the worst-case cost for sorting total length n. For each letter a, letn a be the total length of the words starting with letter a, andc a be the number of words starting with a. The sort requires a recursive call of cost T (n a c a ). Also, there is a divide and recombine cost for each subproblem of size O(c a ). Also, there is a counting sort call at cost O(Sum a c a ). These last two kinds of cost can be combined as one charge or O(Sum a c a ). TherecurrenceforthesortisthusT (n) =Sum a T (n a c a )+k(sum a c a ), for some k. 5

Here,weknowthatSum a n a + c a = n, because every letter is either the first letter of a distinct word or one of the remaining letters passed on, and Sum a c a 1, because there is at least one word. We show by substitution that T(n) is dn for some constant d, for all n 1. In the base case, n=1, and the cost is k(1), so we can take d k. In the recursive case, we have T (n) Sum a d(n a c a )+k(sum a c a ) Sum a d(n a ) [using d k] dn, as desired. (8) CLR 8-6 (a) There are 2n elements in total. We select n elements from the total to obtain one of the arrays, and the remaining elements must automatically belong to the second array. Sothenumberofchoicesisthesameasifwewereselectingn items from 2n items. And this is exactly ( ( ) 2n n ). (b) Lets call the two arrays a and b. And name the elements of the array a 1 ; a 2 ::: and b 1 ; b 2 :::. The decision tree for merging of the two arrays a and b will be as follows, note that once all elements from any array have been exhausted no further comparisons are required: (a 1 b 1 ) (a 2 b 1 )(a 1 b 2 ) (a 3 b 1 )(a 2 b 2 )(a 2 b 2 )(a 1 b 3 ) :::::: (a n b 1 )(a n 1 b 2 ) ::::: (a n k b k ) ::::: (a 1 b n ) :::: (a n 1 b n 1 ) :::: :::: (a n b n 1 )(a n 1 b n ) :::: :::: (a n b n )(a n b n ) :::: 6

We see that the left-most and right-most ends of the tree have only depth of n comparisons as they continuously use up elements from only one array. Subtrees down the middle tend to use elements from both arrays and so are deeper. The deepest path results when one element is taken from either array in strict alternation and it results in the last leaf being 2n deep from the root. Every time two elements get consecutively chosen from the same array, the depth of the tree along that path reduces by 1. Thus the number of comparisons can be expressed as 2n -(number of times elements do not get taken from alternating arrays). Because each array has n elements, the number of times you can consecutively take from the same array is bounded by n. So the total number of comparisons is 2n - O(n). (c) Imagine three elements in the final merged list C, letscallthemc i ; c i+1 and c i+2. Lets say that c i is a j i.e., the j th element from the array A. And similarily c i+1 is b k. When c i was chosen from A and B to be placed as the next element in the merging of C, it was compared to the current head of the array B and found to be smaller. This current head of B must have been bk. To prove by contradiction lets say the current head was b <k, then in the final merged list C, at least one element from B must have come between a j and bk which contradicts our assumption that they are consecutive. Similariliy if the current head of B was b >k,thenc i+1 could only be some other element from B, while b k itself must have already been added to C at some earlier step and so it contradicts the assumption that c i+1 is b k. Therefore the current head of B must have been bk when a j was added to C and hence they must have been compared. (d) We note that every time two consecutive elements in C do not belong to the same array, they must have been compared against each other. So the maximum number of comparison are obtained when C strictly alternates between elements from A and B. This would result in a comparison at every element merging except for the last one (when the other array has gone empty and there is nothing to compare). Thus the worst case number of comparisons would be 2n 1. 7