MIDTERM Fundamental Algorithms, Spring 2008, Professor Yap March 10, 2008

Similar documents
Hon.Algorithms (CSCI-GA 3520), Professor Yap Fall 2012 MIDTERM EXAM. Oct 11, 2012 SOLUTION

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

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2013

CS361 Homework #3 Solutions

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

University of New Mexico Department of Computer Science. Final Examination. CS 362 Data Structures and Algorithms Spring, 2007

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

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

Statistics 253/317 Introduction to Probability Models. Winter Midterm Exam Friday, Feb 8, 2013

Asymptotic Analysis and Recurrences

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

CS 170 Algorithms Spring 2009 David Wagner Final

Final exam (practice) UCLA: Math 31B, Spring 2017

Chapter 4 Divide-and-Conquer

CS Data Structures and Algorithm Analysis

Data Structures and Algorithms Chapter 3

Review Of Topics. Review: Induction

ECE608 Midterm 1 Spring 2009 THEN PUT YOUR ADDRESS ON EVERY PAGE OF THE EXAM! NAME: Neatness counts. We will not grade what we cannot read.

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

Data Structures and Algorithms CSE 465

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)

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

Module 1: Analyzing the Efficiency of Algorithms

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

Name: Student ID: Instructions:

Data Structures and Algorithms Winter Semester

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

Statistics 433 Practice Final Exam: Cover Sheet and Marking Sheet

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

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

Analysis of Algorithms - Using Asymptotic Bounds -

Midterm 1 for CS 170

Computational Complexity

NATIONAL UNIVERSITY OF SINGAPORE CS3230 DESIGN AND ANALYSIS OF ALGORITHMS SEMESTER II: Time Allowed 2 Hours

COMPSCI 611 Advanced Algorithms Second Midterm Exam Fall 2017

Examination paper for TDT4120 Algorithms and Data Structures

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

CS 70 Discrete Mathematics and Probability Theory Fall 2016 Seshia and Walrand Midterm 1 Solutions

CMSC Discrete Mathematics FINAL EXAM Tuesday, December 5, 2017, 10:30-12:30

Design and Analysis of Algorithms March 12, 2015 Massachusetts Institute of Technology Profs. Erik Demaine, Srini Devadas, and Nancy Lynch Quiz 1

University of Connecticut Department of Mathematics

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

Module 1: Analyzing the Efficiency of Algorithms

Math 110 (Fall 2018) Midterm II (Monday October 29, 12:10-1:00)

Data Structures and Algorithms

Multiple Choice Answers. MA 114 Calculus II Spring 2013 Final Exam 1 May Question

CS 361, Lecture 14. Outline

Multiple Choice Answers. MA 110 Precalculus Spring 2016 Exam 1 9 February Question


MIDTERM I CMPS Winter 2013 Warmuth

Midterm 2. Your Exam Room: Name of Person Sitting on Your Left: Name of Person Sitting on Your Right: Name of Person Sitting in Front of You:

Introduction to Algorithms 6.046J/18.401J

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

Final exam (practice) UCLA: Math 31B, Spring 2017

University of Toronto MAT137Y1 Calculus! Test 2 1 December 2017 Time: 110 minutes

Class Note #20. In today s class, the following four concepts were introduced: decision

Fall 2016 Test 1 with Solutions

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

Math 113 Winter 2005 Departmental Final Exam

Problem 5. Use mathematical induction to show that when n is an exact power of two, the solution of the recurrence

CS Data Structures and Algorithm Analysis

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

Design and Analysis of Algorithms May 12, 2011 Massachusetts Institute of Technology. Practice Final Exam. Problem Title Points Parts Grade Initials

2.2 Asymptotic Order of Growth. definitions and notation (2.2) examples (2.4) properties (2.2)

ECE250: Algorithms and Data Structures Analyzing and Designing Algorithms

CS103 Handout 12 Winter February 4, 2013 Practice Midterm Exam

(tree searching technique) (Boolean formulas) satisfying assignment: (X 1, X 2 )

What we have learned What is algorithm Why study algorithm The time and space efficiency of algorithm The analysis framework of time efficiency Asympt

Algorithm Design and Analysis

Introduction to Algorithms March 10, 2010 Massachusetts Institute of Technology Spring 2010 Professors Piotr Indyk and David Karger Quiz 1

1. For each statement, either state that it is True or else Give a Counterexample: (a) If a < b and c < d then a c < b d.

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science ALGORITHMS FOR INFERENCE Fall 2014

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

COMPSCI 611 Advanced Algorithms Second Midterm Exam Fall 2017

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

CS 170, Fall 1997 Second Midterm Professor Papadimitriou

Lecture VI AMORTIZATION

CS325: Analysis of Algorithms, Fall Midterm

APPM 2360 Spring 2012 Exam 2 March 14,

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2006

Math 1132 Practice Exam 1 Spring 2016

6.046 Recitation 2: Recurrences Bill Thies, Fall 2004 Outline

Midterm: CS 6375 Spring 2015 Solutions

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

Data selection. Lower complexity bound for sorting

Math 163 (23) - Midterm Test 1

CS 161 Summer 2009 Homework #2 Sample Solutions

Methods for solving recurrences

Exam 1. March 12th, CS525 - Midterm Exam Solutions

Midterm 1 practice UCLA: Math 32B, Winter 2017

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

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

CS F-01 Algorithm Analysis 1

Quiz 3 Reminder and Midterm Results

CS/SE 2C03. Sample solutions to the assignment 1.

HKUST. MATH1013 Calculus IB. Directions:


CS103 Handout 08 Spring 2012 April 20, 2012 Problem Set 3

Transcription:

INSTRUCTIONS: MIDTERM Fundamental Algorithms, Spring 2008, Professor Yap March 10, 2008 0. This is a closed book exam, with one 8 x11 (2-sided) cheat sheet. 1. Please answer ALL questions (there is ONE extra credit question) 2. Please write clearly. Use complete sentences. This CAN AFFECT your grade. If necessary, consider printing. 3. Write answers ONLY on the front side of each booklet page. Use the reverse side for scratch and to show your working. 4. Read carefully, think deeply, write sparsely. 5. Begin each question in its own page. SHORT QUESTIONS (4 Points Each) No proofs required for this part (one line of informal justification can be entertained). Just state upper and lower bounds for the following sums or recurrence functions. Ideally, we want Θ-bounds. (a) n i=1 i3 (i!) (b) n i=1 i3 2 i (c) T(n) = 3T(n/10) + n/log n. (d) T(n) = 10T(n/3) + n log n. (e) Order these 7 functions in increasing Θ-order: n 2 lg n, n2 lg lg n, 2 lg n, 2 n, 4 lg n, n!, n lg n, (f) What is the minimum number of nodes in an AVL tree of height 4? (g) What is the minimum number of nodes in a (3,4)-tree of height 3? (h) Begin with the AVL tree in Figure 1, and insert the key 8.8. Show the final AVL tree. 12 5 14 3 8 13 15 1 4 7 10 16 2 6 9 11 Figure 1: An AVL tree (i) Begin with the AVL tree in Figure 1, and delete the key 15. Show the final AVL tree. 1

SOLUTION: Note that we provide justifications below, but you need not. (a) Θ(n 3 (n!)). REMARK: Justification is by our summation rule for exponentially large sums. Of course, you did not have to say this. Our instructions said to JUST STATE the bound. Put any derivation on the work sheet if you want, but NOT on the answer part (some students write several pages for one subpart!). Some students think this is a polynomial sum. (b) Θ(1). REMARK: Justification is by our summation rule for exponentially small sums. Some students say that this is O(n 3 /2 n ). Why is this wrong? (c) Θ( n lg n). REMARK: To justify, you need to see that log 10 3 < log 9 3 = 1/2. Thus the driving functions dominates. You also need to check the regularity condition: if f(n) = n. then for some 0 < c < 1, cf(n) 10f(n/3). (d) Θ(n α ) where α = log 3 (10) > 2. REMARK: Again by Master Theorem. You need to see that log 3 10 > log 3 9 > 2. (e) 2 lg n (= n) n2 lg n 4lg n (= n 2 ) n 2 lg lg n n lg n (= 2 log 2 n ) 2 n n!(= 2 Θ(n log n) ). COMMON mistakes: Students did not recognize 2 lg n = n, or 4 lg n = n 2. You should know a standard transformation here: a log b c = c log b a for all a,b,c. Some think that 2 n n log n. To see that this is wrong, take log of both sides, and you will get n C + log 2 n for some C. This is clearly wrong. Some think n 2 lg lg n n. I see how you come to this conclusion you think lg lg n grows so slowly that it overwhelms that fact that n 2 grows faster than n...but you cannot argue this way. The correct way is to order a product of powers in lexicographic order we consider the most important power which is different in the 2 products. Then the decision is determined by the power of this term only! (f) There are 12 nodes. Use the formula that µ(h + 1) = µ(h 1) + µ(h), where µ( 1) = 0, µ(0) = 1. (g) There are 26 nodes. Use the fact that the root must have 2 children and the remaining internal nodes have 3 children each. STANDARD MISTAKE: you assume the root must have 3 children. (h) First insert 8.8 into the tree, then do rotate 2 (8). This is basically the same as the insertion of 9.5, described in Figure 14 in Lecture III. (i) First delete 15 from the tree, then do rotate(5). This is basically the same as the deletion of 13, described in Figure 16 in Lecture III. REGULAR QUESTIONS 1. (4 Points Each) If true, prove it. If false, give counter example. For this problem, assume that f(x) > 0 for all x. (a) T/F: If f is polynomial-type, so is f a for any real a > 0. (b) T/F: If f is polynomial-type, so is lg f. (c) T/F: If f is exponential-type, then f a is exponential-type for any real a > 0. (d) T/F: If f is a increasing exponential-type, so is 2 f. 2

SOLUTION: (a) True: If f is polynomial-type, so is f a for any real a > 0. Pf: Let f(n) Cf(n/2) (ev.) for some C > 0. Then f(n) a C a f(n/2) a. (b) False: If f is polynomial-type, so is lg f. Counter example: let f(n) = (n 1)/n = 1 (1/n). Then f(n) 2f(n/2) but lg f(n) < 0 means that lg f(n) O(f(n/2)). REMARK: Some students give 1/n as a counter-example. Why is this not polynomial-type? (c) True: If f is exponential-type, then f c is exponential-type for any real c > 0. Pf: For any K > 1, if f(n 1) Kf(n 1) implies f(n) c K c f(n 1) c and K c > 1. For any k < 1, if f(n 1) kf(n 1) implies f(n) c k c f(n 1) c and k c < 1. REMARK: do not forget that you need to show this for exponentially large as well as exponentially small functions. (d) True: If f is increasing exponentially, so is 2 f. Pf: Suppose f(n 1) Kf(n 1) for some K > 1. Note that f(n) c (ev.) for any c > 0. Hence Kf(n) (K 1)c + f(n) (ev.). Thus where K := 2 (K 1)c > 1. 2 f(n) 2 Kf(n 1) 2 (K 1)c+f(n 1) (ev.) K 2 f(n 1) (e) True: If f is a shrinking exponential-type, so is 2 f. Pf: Suppose f(n 1) kf(n 1) for some 0 < k < 1. Note that kf(n) k+f(n) (ev.) since f(n) 0. Thus where 2 k > 1. 2 f(n) 2 kf(n 1) 2 k+f(n 1) (ev.) = 2 k 2 f(n 1) 2 f(n) 2 k 2 f(n 1) 2. (2+8 Points) The following statement is a fact: a planar graph on n vertices has at most 3n 6 edges. Let us restate it as follows: (G is a planar graph and has n vertices) (G has 3n 6 edges). (i) State the contra-positive of this statement. (ii) The complete graph on 5 vertices, denoted by K 5 is shown in Figure 2. Using the contra-positive statement in part (i), prove that K 5 is not planar. SOLUTION: (i) Constra-positive: (G has > 3n 6 edges) (G is not a planar graph or does not have n vertices). (ii) Let n = 5. Then K 5 has > 3n 6 = 9 edges. By the contra-positive of (i), we conclude that K 5 is not a planar graph or does not have 5 vertices. Since it does have 5 vertices, it must not be planar. 3

Figure 2: K 5, the complete graph on 5 vertices 3. (10 Points) Let size(u) denote the number of nodes in the tree rooted at u. Say that node u is size-balanced if it is a leaf or else 1/2 size(u.left)/size(u.right) 2. Use shell programming to give an algorithm BALANCE(u) that returns TRUE iff every node below u is balanced. DO NOT assume any additional fields in the nodes. Please use the informal style used in our lecture notes (use indentation to indicate the logical structure of your program, and do not declare variables, etc.) SOLUTION: A common mistake is to assume that you already have the value size(u) for each node u. We will simply use the return shell called rpost(u) in the lecture notes. We assume that rpost(u) returns the size of the binary tree rooted at u if the tree is size-balanced; otherwise, rp OST(u) = 1. The required procedure BALANCE(u) can be easily constructed from rpost(u). Here is rpost again: rpost(u) BASE(u) L rpost(u.left) R rpost(u.right) V ISIT(u,L,R) We must program the BASE and VISIT macros: The macro VISIT macro is BASE(u): if (u = nil) return( 1) if (u.left = nil and u.right = nil) return(1) if (u.left = nil or u.right = nil) RETURN( 1) BASE(u, L, R): if ( 1 2 L/R 2) return(1 + L + R) else return( 1) 4. (EXTRA CREDIT ONLY) Suppose X 1,...,X n are n sorted lists, each with k elements. We want to the set X = n i=1 X i. Here is a sketch of an algorithm: at each phase, we merge pairs of lists. The phases stop when there is only one list left. In a general phase with m lists of size l each, the merging produce m/2 lists each of size 2l (assume m is even). The merging in this phase take time O(ml). 4

(a) Set up the recurrence for the complexity T(n,k) of this algorithm. (b) Show that T(n,k) = O(nk log n). HINT: domain transformation can be used but it is not necessary. (c) Using the Information Theoretic Bound, prove that T(n, k) = Ω(nk log n). (WARN- ING: This might be hard for many students.) SOLUTION: (a) The recurrence is T(n, k) = T(n/2, 2k) + nk when n is a power of 2. By our general practice, we will extend this recurrence for arbitrary real values of n. (b) Let N = lg n and K = lg k. Initially we assume n is a power of 2. We can transform T(n,k) to t(n,k) as follows: t(n,k) := T(2 N,2 K ) = t(n 1,K + 1) + 2 N+K = t(n 2,K + 2) + 2 2 N+K = = t(0,n + K) + N 2 N+K Now, t(0,n + K) = T(1,2 N+K ) = 0, clearly. Hence the solution to T(n,k) = lg n nk. When n is not a power of 2, we stop when we reach t({n},n +K) where 0 {N} < 1, and {N} is the fractional part of N. ALTERNATIVE SOLUTION (Millstone): we maintain a minimum priority queue which normally holds exactly n keys, one key from each of the n lists. To sort, we just deletemin() from the queue, note the list where the deleted item comes from, and insert the next item from that list. There are nk deletemin s and insert s, and each takes time O(lg n). This proves our upper bound. (c) Let C(n,k) denote the number of combinatorially distinct possible outcomes when we merge n lists each of size k. This number is the multinomial ( ) nk C(n, k) = k,k,...,k }{{} n = (nk)! (k!) n Using Stirling s approximation, it is easy to see that log C(n,k) = Ω(nk log n) 5