CS 102: SOLUTIONS TO DIVIDE AND CONQUER ALGORITHMS (ASSGN 4)

Similar documents
The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Closest Pair Problem

CS 103X: Discrete Structures Homework Assignment 3 Solutions

Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

Full and Complete Binary Trees

We can express this in decimal notation (in contrast to the underline notation we have been using) as follows: b + 90c = c + 10b

5.4 Closest Pair of Points

HOMEWORK 5 SOLUTIONS. n!f n (1) lim. ln x n! + xn x. 1 = G n 1 (x). (2) k + 1 n. (n 1)!

Analysis of Binary Search algorithm and Selection Sort algorithm

11 Multivariate Polynomials

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Basic Proof Techniques

The BBP Algorithm for Pi

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Lecture 3: Finding integer solutions to systems of linear equations

Algorithm Analysis [2]: if-else statements, recursive algorithms. COSC 2011, Winter 2004, Section N Instructor: N. Vlajic

The last three chapters introduced three major proof techniques: direct,

Chapter 3. if 2 a i then location: = i. Page 40

A Note on Maximum Independent Sets in Rectangle Intersection Graphs

CONTENTS 1. Peter Kahn. Spring 2007

8 Primes and Modular Arithmetic

Binary Search Trees CMPSC 122

CS473 - Algorithms I

Binary Number System. 16. Binary Numbers. Base 10 digits: Base 2 digits: 0 1

Section IV.1: Recursive Algorithms and Recursion Trees

Binary Multiplication

MATH 22. THE FUNDAMENTAL THEOREM of ARITHMETIC. Lecture R: 10/30/2003

Lecture 1: Course overview, circuits, and formulas

CHAPTER 5 Round-off errors

MATH10040 Chapter 2: Prime and relatively prime numbers

Sequential Data Structures

INCIDENCE-BETWEENNESS GEOMETRY

CS473 - Algorithms I

8 Divisibility and prime numbers

Quotient Rings and Field Extensions

Math 319 Problem Set #3 Solution 21 February 2002

Homework until Test #2

PUTNAM TRAINING POLYNOMIALS. Exercises 1. Find a polynomial with integral coefficients whose zeros include

Algorithms. Margaret M. Fleck. 18 October 2010

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Matrix Algebra. Some Basic Matrix Laws. Before reading the text or the following notes glance at the following list of basic matrix algebra laws.

6. Standard Algorithms

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Near Optimal Solutions

Arkansas Tech University MATH 4033: Elementary Modern Algebra Dr. Marcel B. Finan

3. Mathematical Induction

Converting a Number from Decimal to Binary

SYSTEMS OF PYTHAGOREAN TRIPLES. Acknowledgements. I would like to thank Professor Laura Schueller for advising and guiding me

Kapitel 1 Multiplication of Long Integers (Faster than Long Multiplication)

Lecture Notes on Linear Search

CONTINUED FRACTIONS AND PELL S EQUATION. Contents 1. Continued Fractions 1 2. Solution to Pell s Equation 9 References 12

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

AN ANALYSIS OF A WAR-LIKE CARD GAME. Introduction

Dynamic Programming Problem Set Partial Solution CMPSC 465

Dynamic Programming. Lecture Overview Introduction

a 11 x 1 + a 12 x a 1n x n = b 1 a 21 x 1 + a 22 x a 2n x n = b 2.

So let us begin our quest to find the holy grail of real analysis.

Appendix: Solving Recurrences [Fa 10] Wil Wheaton: Embrace the dark side! Sheldon: That s not even from your franchise!

Section 4.2: The Division Algorithm and Greatest Common Divisors

PRIME FACTORS OF CONSECUTIVE INTEGERS

CSE 421 Algorithms: Divide and Conquer

C H A P T E R Regular Expressions regular expression

Data Structures Fibonacci Heaps, Amortized Analysis

Lecture 13 - Basic Number Theory.

Solution to Homework 2

Classification - Examples

Sample Induction Proofs

Chapter 3. Cartesian Products and Relations. 3.1 Cartesian Products

MATH 4330/5330, Fourier Analysis Section 11, The Discrete Fourier Transform

God created the integers and the rest is the work of man. (Leopold Kronecker, in an after-dinner speech at a conference, Berlin, 1886)

Biostatistics 615/815

Handout NUMBER THEORY

A. The answer as per this document is No, it cannot exist keeping all distances rational.

SECTION 10-2 Mathematical Induction

Real Roots of Univariate Polynomials with Real Coefficients

Kevin James. MTHSC 412 Section 2.4 Prime Factors and Greatest Comm

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

Applications of Fermat s Little Theorem and Congruences

28 Closest-Point Problems

MATH Fundamental Mathematics IV

CHAPTER 5. Number Theory. 1. Integers and Division. Discussion

Data Structures. Algorithm Performance and Big O Analysis

Single machine parallel batch scheduling with unbounded capacity

PYTHAGOREAN TRIPLES KEITH CONRAD

Math 4310 Handout - Quotient Vector Spaces

Automata and Formal Languages

26 Integers: Multiplication, Division, and Order

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

2) Write in detail the issues in the design of code generator.

Page 331, 38.4 Suppose a is a positive integer and p is a prime. Prove that p a if and only if the prime factorization of a contains p.

This Unit: Floating Point Arithmetic. CIS 371 Computer Organization and Design. Readings. Floating Point (FP) Numbers

Closest Pair of Points. Kleinberg and Tardos Section 5.4

Transcription:

CS 10: SOLUTIONS TO DIVIDE AND CONQUER ALGORITHMS (ASSGN 4) Problem 1. a. Consider the modified binary search algorithm so that it splits the input not into two sets of almost-equal sizes, but into three sets of sizes approximately one-third. Write down the recurrence for this ternary search algorithm and find the asymptotic complexity of this algorithm. b. Consider another variation of the binary search algorithm so that it splits the input not only into two sets of almost equal sizes, but into two sets of sizes approximately one-third and two-thirds. Write down the recurrence for this search algorithm and find the asymptotic complexity of this algorithm. Solution. a. The recurrence for normal binary search is T (n) = T (n/)+1. This accounts for one comparison (on an element which partitions the n-element list of sorted keys into two n -element sets) and then a recursive call on the appropriate partition. For ternary search we make two comparisons on elements which partition the list into three sections with roughly n/3 elements and recurse on the appropriate partition. Thus analogously, the recurrence for the number of comparisons made by this ternary search is: T 3 (n) = T 3 (n/3) +. However, just as for binary search the second case of the Master Theorem applies. We therefore conclude that T 3 (n) Θ(log(n)). b. We now consider a slightly modified take on ternary search in which only one comparison is made which creates two partitions, one of roughly n/3 elements and the other of n/3. Here the worst case arises when the recursive call is on the larger n/3-element partition. Thus the recurrence corresponding to this worst case number of comparisons is T 3w (n) = T 3w (n/3) + 1 but again the second case of the Master Theorem applies placing T 3w Θ(log n). The best case is exactly the same (and thus so must also be the average). It is interesting to note that we will get the same results for general k-ary search (as long as k is a fixed constant which does not depend on n) as n approaches infinity. The recurrence becomes one of T k (n) = T k (n/k) + k 1 ( ) (k 1)n T kw (n) = T kw + 1 k depending on whether we are talking about part (a) or part (b). For each recurrence case two of the Master Theorem applies. Problem. Consider the following variation on Mergesort for large values of n. Instead of recursing until n is sufficiently small, recur at most a constant r times, 1

CS 10: SOLUTIONS TO DIVIDE AND CONQUER ALGORITHMS (ASSGN 4) and then use insertion sort to solve the r resulting subproblems. (asymptotic) running time of this variation as a function of n? What is the Solution. Each call to Mergesort on n elements makes recursive calls of size n/, if we recurse at most r times and then revert to the basic (ad-hoc) method, the modified Mergesort creates r subproblems of size n. If the ad-hoc algorithm runs r in time Θ(n ) (like selection or insertion sort), this modified mergesort will take at least r ( n ) = n / r time for large instances (n > r n r 0 ). Since r is constant, so is r, and the running time is then Ω(n ), which is asymptotically worse than the standard mergesort. In essence, this modification requires that we solve Θ(n) size problems with the ad-hoc method. If the ad-hoc method is polynomial, then the modification to divide-and-conquer method cannot be more than a constant factor better than simply applying the ad-hoc method to the original problem. Problem 3. Design and analyze a divide and conquer MAXMIN algorithm that uses 3n comparisons for any n. Solution. The divide and conquer algorithm we develop for this problem is motivated by the following observation. Suppose we knew the maximum and minimum element in both of the roughly n sized partitions of an n-element (n ) list. Then in order to find the maximum and minimum element of the entire list we simply need to see which of the two maximum elements is the larger, and which of the two minimums is the smaller. We assume that in a 1-element list the sole element is both the maximum and the minimum element. With this in mind we present the following psuedocode for the max/min problem. procedure maxmin(a[1...n] of numbers) -> (min, max) begin if (n == 1) return (A[1], A[1]) if (n == ) if( A[1] < A[]) return (A[1], A[]) return (A[], A[1]) (max_left, min_left) = maxmin(a[1...(n/)]) (max_right, min_right) = maxmin(a[(n/ +1)...n]) if (max_left < max_right) max = max_right max = max_left if (min_left < min_right) max = min_left min = min_right return (min, max) end

CS 10: SOLUTIONS TO DIVIDE AND CONQUER ALGORITHMS (ASSGN 4) 3 Let T (n) be the number of comparisons performed by the maxmin procedure. When n = 1 clearly there are no comparisons. Thus we have T (1) = 0. Similarly, T () = 1. Otherwise when n > clearly T (n) = T (n/) + since maxmin performs two recursive calls on partitions of roughly half of the total size of the list and then makes two further comparisons to sort out the max/min for the entire list. (Of course, to be pedantic there should be floors and ceilings in the recursive function, and something should be said about the fact that the following proof is only for n which are powers of two and how this implies the general result. This is omitted.) We next show that T (n) = 3n for all n which are powers of. The proof is by induction on n. Base Case: (n=): from the recursive definition we have that T () = 1. Similarly, we have that 3 = 3 = 1, thus verifying the base case. Inductive Step: Let n > and n = j for some integer j. Assume that T (k) = 3k for all k = j for some integer j and k < n. We want to show that this assumption implies that T (n) = 3n for all positive n which are powers of. We start with the given recurrence T (n) = T (n/) + 3(n/) = ( ) + by the inductive hypothesis with k = n/. This gives 3(n/) T (n) = 4 + 3n = showing the desired result. done. By the principle of mathematical induction we are Problem 4. Assume you have an array A[1..n] of n elements. A majority element of A is any element occurring in more than n/ positions (so if n = 6 or n = 7, any majority element will occur in at least 4 positions). Assume that elements cannot be ordered or sorted, but can be compared for equality. (You might think of the elements as chips, and there is a tester that can be used to determine whether or not two chips are identical.) Design an efficient divide and conquer algorithm to find a majority element in A (or determine that no majority element exists). Aim for an algorithm that does O(n lg n) equality comparisons between the elements. A more difficult O(n) algorithm is possible, but may be difficult to find. Solution. A Θ(n log n) running time divide and conquer algorithm: The algorithm begins by splitting the array in half repeatedly and calling itself on each half. This is similar to what is done in the merge sort algorithm. When we get down to single elements, that single element is returned as the majority of

CS 10: SOLUTIONS TO DIVIDE AND CONQUER ALGORITHMS (ASSGN 4) 4 its (1-element) array. At every other level, it will get return values from its two recursive calls. The key to this algorithm is the fact that if there is a majority element in the combined array, then that element must be the majority element in either the left half of the array, or in the right half of the array. There are 4 scenarios. a. Both return no majority. Then neither half of the array has a majority element, and the combined array cannot have a majority element. Therefore, the call returns no majority. b. The right side is a majority, and the left isn t. The only possible majority for this level is with the value that formed a majority on the right half, therefore, just compare every element in the combined array and count the number of elements that are equal to this value. If it is a majority element then return that element, return no majority. c. Same as above, but with the left returning a majority, and the right returning no majority. d. Both sub-calls return a majority element. Count the number of elements equal to both of the candidates for majority element. If either is a majority element in the combined array, then return it. Otherwise, return no majority. The top level simply returns either a majority element or that no majority element exists in the same way. To analyze the running time, we can first see that at each level, two calls are made recursively with each call having half the size of the original array. For the non-recursive costs, we can see that at each level, we have to compare each number at most twice (which only happens in the last case described above). Therefore, the non-recursive cost is at most n comparisons when the procedure is called with an array of size n. This lets us upper bound the number of comparisons done by T (n) defined by the recurrence T (1) = 0 and T (n) = T (n/) + n. We can then determine that T (n) Θ(n log n) as desired using Case of the Master Theorem. Problem 5. Consider the problem of multiplying two large n-bit integers in a machine where the word size is one bit. Solution. a. Consider the problem of multiplying two large n-bit integers in a machine where the word size if one bit. Describe the straightforward algorithm that takes n bit-multiplications. This algorithm is the algorithm that we all learned in grade school to multiply two numbers. procedure: multiply(a,b) // assume A,B each have n bits // the lower order bit of A will be written A[0], // the highest will be A[n], same for B let result = 0 let temp = 0 for j = 1 to n do for i = 1 to n do

CS 10: SOLUTIONS TO DIVIDE AND CONQUER ALGORITHMS (ASSGN 4) 5 temp[i] = B[i] * A[j] end for shift temp by (j - 1) bits to the left set result to result plus temp set temp = 0 end for return result The two for loops that surround the bit multiplication makes this algorithm do n bit-multiplications. b. Find a way to compute the product of the two numbers using three multiplications of n/ bit numbers (you will also have to do some shifts, additions, and subtractions, and ignore the possibility of carries increasing the length of intermediate results). Describe your answer as a divide and conquer algorithm. The algorithm that follows is motivated by the following example where two -digit (base 10) numbers are multiplied: a b * c d ------ ad bd ca cb ---------- ca (ad+bc) bd In other words: (assuming no carries) (a 10 1 + b 10 0 ) (c 10 1 + d 10 0 ) = ca 10 + (ad + bc) 10 1 + bd 10 0 Converting now to bits, if a, b, c, d are all n/-bit numbers then multiplying two n-but numbers (ab) and (cd) takes 4 multiplications of size n/, 3 shifts and 3 addition/subtraction operations. We can reduce the number of multiplications by noticing that: ad + bc = (a + b)(c + d) ac bd and therefore we have all the numbers we need with only three multiplications, and we can write down the divide and conquer algorithm (for binary numbers): procedure: DC_multiply(X,Y) // assume X,Y each have n bits, where n is a power of if X and Y are both 1 bit long, return X*Y; let k = n/; let a = X div ^k and b= X mod ^k (so a*^k + b = X) let c = Y div ^k and d= Y mod ^k (so c*^k + d = Y) // a,b,c,d are all n/-bit numbers temp_1 = DC_multiply(c,a) temp_ = DC_multiply(a+b,c+d) temp_3 = DC_multiply(b,d) return ( temp_1*^(k) + (temp_ - temp_1 - temp_3)*^k + temp_3 ) In this algorithm we have 3 recursive calls on n/-bit numbers, shifts and 6 additions/subtractions on Θ(n)-bit numbers (not counting the shifts/subtractions needed for the div and mod operations).

CS 10: SOLUTIONS TO DIVIDE AND CONQUER ALGORITHMS (ASSGN 4) 6 c. Assuming that adding/subtracting numbers takes time proportional to the number of bits involved, and shifting takes constant time. Using the above algorithm we can immediately write down the following recurrence for its running time. T (1) = 1 and T (n) = 3T ( n/ ) + G(n) for n > 1 where G(n) Θ(n) is the time needed for the additions, shifts, and overhead. Applying the master theorem we get Case 1: E = lg 3/ lg = lg 3, so T (n) Θ(n lg 3 ) (recall that lg 3 1.585). d. Now assume that we can find the product of two n-bit numbers using some number of multiplications of n/3-bit numbers (plus some additions, subtractions, and shifts). What is the largest number of n/3 bit number multiplications that leads to an asymptotically faster algorithms than the n/ divide and conquer algorithm above? The running time of a 5-multiplication method for two n-figure numbers has the recurrence relation T (n) = 5T (n/3) + G(n) where 5T (n/3) is the time to do the 5 recursive multiplications, and G(n) Θ(n) is the time needed for the additions, shifts, and overhead. Applying case 1 of the Master Theorem, we get T (n) Θ(n log 3 5 ), and log 3 5 1.465, so this is better than the -multiplication method. A 6-multiplication method would have the recurrence relation T (n) = 6T (n/3) + G(n), where 6T (n/3) is the time to do the six recursive multiplications and G(n) Θ(n) is the time needed for the additions, shifts, and overhead. Using the the Master Theorem on this recurrence yields T (n) Θ(n log 3 6 ), and log 3 6 = 1.631, which is asymptotically worse than the algorithm we developed for our -multiplication method. Therefore 5 is the largest number of n/3 bit number multiplications that leads to an asymptotically faster algorithms than the n/ divide and conquer algorithm above.