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

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

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

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

CS 361, Lecture 14. Outline

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.

CS325: Analysis of Algorithms, Fall Midterm

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

Math 391: Midterm 1.0 Spring 2016

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)

Today s Outline. CS 362, Lecture 4. Annihilator Method. Lookup Table. Annihilators for recurrences with non-homogeneous terms Transformations

CS2223 Algorithms D Term 2009 Exam 3 Solutions

Review Of Topics. Review: Induction

University of New Mexico Department of Computer Science. Midterm Examination. CS 261 Mathematical Foundations of Computer Science Spring, 2010

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

CS173 Running Time and Big-O. Tandy Warnow

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

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

Analysis of Algorithms - Using Asymptotic Bounds -

University of Toronto Department of Electrical and Computer Engineering. Final Examination. ECE 345 Algorithms and Data Structures Fall 2016

University of Illinois at Chicago Department of Computer Science. Final Examination. CS 151 Mathematical Foundations of Computer Science Fall 2012

data structures and algorithms lecture 2

Linear Selection and Linear Sorting

On my honor I affirm that I have neither given nor received inappropriate aid in the completion of this exercise.

Fall 2016 Test 1 with Solutions

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

CSCI 3110 Assignment 6 Solutions

CS 161 Summer 2009 Homework #2 Sample Solutions

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

Week 5: Quicksort, Lower bound, Greedy

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

Divide-and-Conquer Algorithms Part Two

CPSC 320 Sample Final Examination December 2013

Lecture 4. Quicksort

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

Central Algorithmic Techniques. Iterative Algorithms

CS 4104 Data and Algorithm Analysis. Recurrence Relations. Modeling Recursive Function Cost. Solving Recurrences. Clifford A. Shaffer.

Lecture 2: Asymptotic Notation CSCI Algorithms I

Tutorial Session 5. Discussion of Exercise 4, Preview on Exercise 5, Preparation for Midterm 1. Running Time Analysis, Asymptotic Complexity

MIDTERM I CMPS Winter 2013 Warmuth

CSE 21 Practice Exam for Midterm 2 Fall 2017

Algorithm runtime analysis and computational tractability

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

Homework 1 Solutions

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

Written Homework #1: Analysis of Algorithms

Loop Convergence. CS 536: Science of Programming, Fall 2018

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

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

Tree Heap Priority Queue

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

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

Fundamental Algorithms

Fundamental Algorithms

COMP 250 Fall Midterm examination

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

Data Structures and Algorithms Chapter 3

Midterm 1 for CS 170

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

Lecture 3. Big-O notation, more recurrences!!

CS 61B Asymptotic Analysis Fall 2017

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

Data Structures and Algorithms Chapter 2

CS361 Homework #3 Solutions

Recursion: Introduction and Correctness

Methods for solving recurrences

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

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

Midterm: CS 6375 Spring 2015 Solutions

ASYMPTOTIC COMPLEXITY SEARCHING/SORTING

Recurrences COMP 215

1 Terminology and setup

Divide and Conquer Strategy

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Computational Complexity. This lecture. Notes. Lecture 02 - Basic Complexity Analysis. Tom Kelsey & Susmit Sarkar. Notes

Practice Midterm Exam

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

Data Structures and Algorithms Chapter 3

Insertion Sort. We take the first element: 34 is sorted

Midterm Exam 2 Solutions

5 + 9(10) + 3(100) + 0(1000) + 2(10000) =

Mergesort and Recurrences (CLRS 2.3, 4.4)

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

UC Berkeley, CS 174: Combinatorics and Discrete Probability (Fall 2008) Midterm 1. October 7, 2008

Problem Set 2 Solutions

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

Data Structures and Algorithms Winter Semester

Concrete models and tight upper/lower bounds

Solutions. Problem 1: Suppose a polynomial in n of degree d has the form

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)

Heaps and Priority Queues

Homework 1 Submission

Prelim 2[Solutions] Solutions. 1. Short Answer [18 pts]

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

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

Algorithms And Programming I. Lecture 5 Quicksort

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

Lecture 5: Loop Invariants and Insertion-sort

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

Transcription:

University of New Mexico Department of Computer Science Midterm Examination CS 361 Data Structures and Algorithms Spring, 2003 Name: Email: Print your name and email, neatly in the space provided above; print your name at the upper right corner of every page. Please print legibly. This is an closed book exam. You are permitted to use only two pages of cheat sheets that you have brought to the exam. Nothing else is permitted. Do all four problems in this booklet. Show your work! You will not get partial credit if we cannot figure out how you arrived at your answer. Write your answers in the space provided for the corresponding problem. Let us know if you need more paper. Don t spend too much time on any single problem. If you get stuck, move on to something else and come back later. If any question is unclear, ask us for clarification. Question Points Score Grader 1 40 2 20 3 20 4 20 Total 100

CS 361 Midterm Spring, 2003 2 Name: 1. Short Answer (40 points) True or False: (circle one, 4 points each) (a) True or False: In a max-heap, the element with smallest key is always at the rightmost leaf node of the heap? Solution: F: it s always at a leaf node but not necessarily the rightmost leaf node (b) True or False: The height of a heap on n nodes is Ω(log n)? Solution: T: it s Θ(log n) (c) True or False: In a max-heap, the element with the largest key is always at the root of the heap? Solution: T (d) True or False: Mergesort is asymptotically faster than heapsort (i.e. the big-o runtime of heapsort is better than the big-o runtime of mergesort)?solution: F: They are both O(n log n) time (e) True or False: Heapsort requires O(n) extra space (not counting the space to store the array to be sorted)? Solution: F: It requires O(1) extra space, it s an in-place sorting algorithm Short Answer: For each function below, give a Θ() expression that is as simplified as possible. Justify your answers briefly. Circle your final answer. (4 points each) (a) n 2 log n n n + 10 log 10 n Solution: Θ(n 2 log n) since this is the fastest growing term (b) log 2 n 5 + 10 log n 10 Solution: Θ(log 2 n), since log 2 n 5 = 25 log 2 n, and this is the fastest growing term (c) n log 3 n + log 4 n Solution: Θ( n log 3 n) since n grows faster than log n (d) n n i=0 5 i Solution: Θ(n) since n i=0 5 i = O(1) (e) 2 log 4 n Solution: Θ( n) since log 4 n = log 2 n/ log 2 4 = log 2 n/2, so 2 log 4 n = Θ( n)

CS 361 Midterm Spring, 2003 3 Name: 1. Short Answer (40 points), continued.

CS 361 Midterm Spring, 2003 4 Name: 2. Annihilators and Recurrence Trees (20 points) Consider the recurrence: T (n) = 3T (n/3) + n 2 (and T (n) = Θ(1) for n a constant) (a) Use the recurrence tree method to get a guess (i.e. simplest possible big-o) on the solution to this recurrence. You need not prove your guess correct. (b) Now use annihilators (and change of variable) to get a tight upperbound (i.e. simplest possible big-o) on the solution to this recurrence. Solution: Recurrence Tree: T (n) = 3T (n/3) + n 2, T (n/3) = 3T (n/9) + (n/3) 2, T (n/9) = 3T (n/27) + (n/9) 2. Writing this out in a recurrence tree, we get that the zero level is one n 2, the first level is three n 2 /9 s, the second level is 9 n 2 /81 s. In general, the i-th level sums to n 2 /3 i. There are log 3 n levels, so the sum of all of them is log 3 n 1 n 2 i=0 infinity 1/3 i n 2 Thus the solution to the recurrence is O(n 2 ) Annihilators: Let n = 3 i and t(i) = T (3 i ). Then i=0 1/3 i (1) = n 2 (3/2) (2) t(i) = 3t(i 1) + 3 2i (3) t(i) = 3t(i 1) + 9 i (4) The annihilator for this is (L 3)(L 9), and thus from the lookup table, the form of the recurrence is: The reverse transformation gives that This is O(n 2 ) t(i) = c 1 3 i + c 2 9 i (5) t(i) = c 1 3 i + c 2 (3 i ) 2 (6) T (n) = c 1 n + c 2 n 2

CS 361 Midterm Spring, 2003 5 Name: 2. Annihilators and Recurrence Trees (20 points), continued.

CS 361 Midterm Spring, 2003 6 Name: 3. Recursion and Recurrences (20 points) Consider the following function: int f(int n){ if (n==0) return 0; else if (n==1) return 1; else{ int val = 3*f(n-1); val -= f(n-2); val -= f(n-2); return val; } } (a) Let f(n) be the value returned by the function f when given input n. Write a recurrence relation for f(n): Solution: f(n) = 3f(n-1)-2f(n-2) (b) Now solve the recurrence for f(n) exactly using annihilators. (don t forget to check your solution) Solution: Let T n = f(n), and T = T n. Then T = T n (7) LT = T n+1 (8) L 2 T = T n+2 (9) Since T n+2 = 3T n+1 2T n, we know that L 2 T 3LT +2T = 0, and thus L 2 3L+2 = (L 2)(L 1) annihilates T. Thus f(n) is of the form: We know: f(n) = c 1 2 n + c 2 1 n f(0) = 0 = c 1 + c 2 (10) f(1) 1 = 2 c 1 + c 2 (11) so c 1 = 1, c 2 = 1 and thus f(n) = 2 n 1 Check: f(2) = 3 and 2 2 1 = 3.

CS 361 Midterm Spring, 2003 7 Name: 3. Recursion and Recurrences (20 points), continued. (c) Now let T (n) be the running time of the algorithm f on the previous page when given input n. Write a recurrence relation for T (n): Solution: T(n) = T(n-1)+2T(n-2)+k for some constant k (d) Now get a tight upperbound (i.e. big-o) on the solution for T (n) using annihilators. Solution: L 2 L 2 annihilates the homogeneous part (factoring this gives (L 2)(L+1). L 1 annihilates the homogeneous part. So the total annihilator is (L 2)(L+1)(L 1). The lookup table tells us that T (n) = c 1 2 n + c 2 ( 1) n + c 3 1 n So the upperbound on the solution is O(2 n )

CS 361 Midterm Spring, 2003 8 Name: 4. Loop Invariants (20 points) In this question, you will be proving the correctness of the procedure Heap-Increase-Key using loop invariants. Recall that this procedure takes a heap A as input, and increases the key of the i-th node of A to the value key. The procedure then ensures that the max-heap property(i.e. for all nodes j such that 1 < j heapsize (A), A[parent (j)] A[j]) is true for the new heap. The procedure is given below: Heap-Increase-Key (A,i,key) (a) if (key < A[i]) then error new key is smaller than current key (b) A[i] = key; (c) while (i>1 and A[Parent(i)] < A[i]) i. do exchange A[i] and A[Parent(i)] ii. i = Parent(i); Argue the correctness of Heap-Increase-Key using the following loop invariant: At the start of each iteration of the while loop, the array A[1..heap-size(A)] satisfies the maxheap property, except that there may be one violation: A[i] may be larger than A[Parent(i)] Show initialization,maintenance and termination for this loop invariant. (For termination, show that the max-heap property holds for A[1..heap-size(A)] after the while loop terminates) Solution: Initialization: At the start of the first iteration of the while loop, we have only changed the value of A[i] in the original heap. Thus A[1..heap-size(A)] satisfies the max-heap property except for the fact that A[i] may now be larger than A[Parent(i)] Maintenance: Let i be the value of i at the current iteration of the while loop. Now at the beginning of the current iteration of the while loop, the heap property holds for all A[1..heapsize(A)] except that A[i ] is larger than A[Parent(i )]. However, A[i ] and A[Parent(i )] are swapped during the current iteration. Thus the only new possible violation at the end of the loop iteration is that A[Parent(i )] is larger than A[Parent(Parent(i ))]. Setting i equal to Parent(i ) at the end of the loop body then reestablishes the invariant. Termination: We know that at the beginning of the last iteration of the while loop, the maxheap property held for A[1..heap-size(A)] except that there could be one violation: A[i] could be larger than A[Parent(i)] However, the while loop terminates only if i = 1 (i.e. there is no Parent(i)) or A[Parent(i)] A[i]. Thus, it s not the case that A[i] is larger than A[Parent(i)], and so the max-heap property holds with no violations!

CS 361 Midterm Spring, 2003 9 Name: 4. Loop Invariants (20 points), continued.