CPS 231 Exam 2 SOLUTIONS

Similar documents
CSci 231 Homework 7. Red Black Trees. CLRS Chapter 13 and 14

CSci 231 Homework 7. Red Black Trees. CLRS Chapter 13 and 14

Midterm solutions. n f 3 (n) = 3

CSci 231 Final Review

Analysis of Algorithms

Range Searching and Windowing

Augmenting Data Structures

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

Lecture 6: Analysis of Algorithms (CS )

EXAM ELEMENTARY MATH FOR GMT: ALGORITHM ANALYSIS NOVEMBER 7, 2013, 13:15-16:15, RUPPERT D

Binary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...

Basic Data Structures (Version 7) Name:

Binary search trees :

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Binary Trees. Recursive definition. Is this a binary tree?

Lecture: Analysis of Algorithms (CS )

RB-Tree Augmentation. OS-Rank. OS-Select. Augment x with Size(x), where. Size(x) = size of subtree rooted at x Size(NIL) = 0

CS521 \ Notes for the Final Exam

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

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

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

CMSC351 - Fall 2014, Homework #2

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

Balanced Search Trees. CS 3110 Fall 2010

AVL Trees. (AVL Trees) Data Structures and Programming Spring / 17

Outline. Definition. 2 Height-Balance. 3 Searches. 4 Rotations. 5 Insertion. 6 Deletions. 7 Reference. 1 Every node is either red or black.

COMP Analysis of Algorithms & Data Structures

Binary Search Trees. Antonio Carzaniga. Faculty of Informatics University of Lugano. October 22, Antonio Carzaniga 1

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

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?

Amortized Analysis. A Simple Analysis. An Amortized Analysis. Incrementing Binary Numbers. A Simple Analysis

Solutions to Exam Data structures (X and NV)

Solutions to Problem Set 3

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

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

COMP Analysis of Algorithms & Data Structures

Solutions. Suppose we insert all elements of U into the table, and let n(b) be the number of elements of U that hash to bucket b. Then.

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CMPS 102 Solutions to Homework 6

COT 5407: Introduction to Algorithms. Giri Narasimhan. ECS 254A; Phone: x3748

CSCI 104 Log Structured Merge Trees. Mark Redekopp

Ensures that no such path is more than twice as long as any other, so that the tree is approximately balanced

Binary Search Trees, etc.

Lecture 5: Scheduling and Binary Search Trees

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

: Fundamental Data Structures and Algorithms. June 06, 2011 SOLUTIONS

TREES AND ORDERS OF GROWTH 7

Problem Set 5 Solutions

FINAL EXAM SOLUTIONS

COMP Analysis of Algorithms & Data Structures

Quiz 1 Practice Problems

Midterm Exam Review. CS231 Algorithms Handout # 23 Prof. Lyn Turbak November 5, 2001 Wellesley College

CS Fall 2010 B-trees Carola Wenk

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

CIS265/ Trees Red-Black Trees. Some of the following material is from:

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

Solutions to relevant spring 2000 exam problems

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

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

Binary Search Trees. Motivation. Binary search tree. Tirgul 7

CS:3330 (22c:31) Algorithms

Here is a recursive algorithm that solves this problem, given a pointer to the root of T : MaxWtSubtree [r]

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

CSE 101 Homework 5. Winter 2015

We assume uniform hashing (UH):

TREES. Trees - Introduction

16 Greedy Algorithms


CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

CS 173 [A]: Discrete Structures, Fall 2012 Homework 8 Solutions

2. (a) Explain the concept of virtual functions in C++ with suitable examples. (b) Explain the concept of operator overloading in C++.

Lecture 3: B-Trees. October Lecture 3: B-Trees

CS583 Lecture 12. Previously. Jana Kosecka. Amortized/Accounting Analysis Disjoint Sets. Dynamic Programming Greedy Algorithms

ECE608 - Chapter 16 answers

Outline. Computer Science 331. Heap Shape. Binary Heaps. Heap Sort. Insertion Deletion. Mike Jacobson. HeapSort Priority Queues.

8.1. Optimal Binary Search Trees:

Binary Search Trees > = 2014 Goodrich, Tamassia, Goldwasser. Binary Search Trees 1

CSE 2320 Section 002, Fall 2015 Exam 2 Time: 80 mins

Animations. 01/29/04 Lecture

Trees, Part 1: Unbalanced Trees

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

CS 251, LE 2 Fall MIDTERM 2 Tuesday, November 1, 2016 Version 00 - KEY

Balanced Trees Part One

Problem Set 5 Due: Friday, November 2

CPSC 320 Midterm 2 Thursday March 13th, 2014

Amortized Analysis. Andreas Klappenecker. [partially based on the slides of Prof. Welch]

ECE250: Algorithms and Data Structures AVL Trees (Part A)

Outline for Today. Why could we construct them in time O(n)? Analyzing data structures over the long term.

CS 315 Data Structures mid-term 2

CSE 332, Spring 2010, Midterm Examination 30 April 2010

CSCI Analysis of Algorithms I

CS 361, Lecture 21. Outline. Things you can do. Things I will do. Evaluation Results

INF2220: algorithms and data structures Series 1

CS102 Binary Search Trees

Priority Queues. Priority Queues Trees and Heaps Representations of Heaps Algorithms on Heaps Building a Heap Heapsort.

403: Algorithms and Data Structures. Heaps. Fall 2016 UAlbany Computer Science. Some slides borrowed by David Luebke

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Greedy Algorithms. Alexandra Stefan

Binary Trees

V Advanced Data Structures

Transcription:

CPS 231 Exam 2 SOLUTIONS Fall 2003 1:00-2:25, Tuesday November 20th Closed book exam NAME: Problem Max Obtained 1 10 2 25 3 (a) 15 3 (b) 15 3 (c) 10 4 (a) 10 4 (b) 15 4 (c) 10 Total 110 1

[10 points ] Problem 1: 1. Is it true that in the worst case, a red-black tree insertion requires O(1) rotations? Solution: True, at most two rotations are performed. 2. Is it true that walking a red-black tree with n nodes in in-order takes Θ(n log n) time? Solution: False, an in-order tree walk takes Θ(n) time. 3. Given a red-black tree with n elements, how fast can you sort them using the tree? Solution: Θ(n) using an in-order traversal. 4. How fast can we build a red-black tree with n elements? Solution: Each insertion into a red-black tree takes O(log n) time and we insert n elements, so we can build the tree in time O(n log n). 5. If a data structure supports an operation foo such that a sequence of n foo s takes O(n log n) time in the worst case, then the amortized time of a foo operation is Θ( ) while the actual time of a single foo operation could be as low as Θ( ) and as high as Θ( ). Solution: The amortized time of a foo operation is Θ(log n) while the actual time of a single foo operation could be as low as Θ(1) and as high as Θ(n log n). 2

[25 points ] Problem 2: In this problem we consider a data structure D for maintaining a set of integers under the normal Init, Insert, Delete, and Find operations, as well as a Count operation, defined as follows: Init(D): Create an empty structure D. Insert(D,x): Insert x in D. Delete(D,x): Delete x from D. Find(D,x): Return pointer to x in D. Count(D,x): Return number of elements larger than x in D. Describe how to modify a standard red-black tree in order to implement D such that Init is supported in O(1) time and Insert, Delete, Find, and Count are supported in O(log n) time. Solution: We can implement D using a red-black tree with an additional field size stored at each node. The size field maintains the size of the subtree rooted at x. If we set the size of leaf (nil) nodes to zero then we may define size for any node x by size(x) = size(right(x)) + size(lef t(x)) + 1, By Theorem 14.4 in CLRS, if we augment a red-black tree of n nodes with a field f at each node x such that f can be computed using only the information stored in x, left(x), and right(x), then we can maintain f at all nodes during insertion and deletion in O(log n) time. In particular, we can maintain size so that Insert(D,x) and Delete(D,x) are supported in O(log n) time. Init(D) requires creating a nil node (null pointer) with size zero, which is done in O(1) time. Find(D,x) is supported in O(log n) time, as we know a tree search requires time proportional to the height of the tree, which for a red-black tree is O(log n), and that size is not affected at any node during the search. For Count(D,x) we maintain a count variable c which is initially zero and start at the root of the tree. At any node r, if the key of x is less than the key of r, we add to c the size of r s right subtree plus one (to count r) and move left. If they key of x is greater than the key of r, then we move right. If the keys are equal we return c plus the size of r s right subtree. If x is not in the tree, then we will travel to a leaf, r will be nil, and we return c. An implementation for Count(D,x) could be: Count[D,x] if x = nil return c = 0 r = root(d) while r!= nil if key(x) < key(r) then 3

c = c + size(right(r)) + 1 r = left(r) else if key(x) > key(r) r = right(r) else if key(x) = key(r) return c + size(right(r)) end if end while return c The value c returned by Count will be the number of elements larger than x in D. This implementation of Count correctly handles the case when x is not in D. It suffices to explain in words without pseudocode the main idea of the Count implementation. We now have to show that Count is supported in O(log n) time. Starting at the root, we compare the key of x with the key of r. The procedure terminates if the keys are equal or we encounter a nil node. Else it travels to one of the children of r. At each node O(1) work is performed. The nodes encountered form a path downward from the root and thus the running time of Count is proportional to the height of the tree, which is O(log n). [40 points ] Problem 3: A pharmacist has W pills and n empty bottles. Let {p 1,p 2,...,p n } denote the number of pills that each bottle can hold. a) Describe a greedy algorithm, which, given W and {p 1,p 2,...,p n }, determines the fewest number of bottles needed to store the pills. Prove that your algorithm is correct (that is, prove that the first bottle chosen by your algorithm will be in some optimal solution). Solution: Sort the n bottles in non-increasing order of capacity. Pick the first bottle, which has largest capacity, and fill it with pills. If there are still pills left, fill the next bottle with pills. Continue filling bottles until there are no more pills. We can sort the bottles in O(n log n) and it takes time O(n) to fill the bottles, so our greedy algorithm has running time O(n log n). To show correctness, we want to show there is an optimal solution that includes the first greedy choice made by our algorithm. Let k be the fewest number of bottles needed to store the pills and let S be some optimal solution. Denote the first bottle chosen by our algorithm by p. If S contains p, then we have shown our bottle is in some optimal solution. Otherwise, suppose p is not contained in S. All bottles in S are smaller in capacity than p (since p is the largest bottle) and we can remove any of the bottles in S and empty its pills into p, creating a new set of bottles S = S {p} {p } that also contains k bottles the same number of bottles as in S. Thus S is an optimal solution that includes p and we have shown there is always an optimal solution that includes the first greedy choice. Because we have shown there always exists an optimal solution that contains p, the problem is reduced to finding an optimal solution to the subproblem of finding k 1 bottles in {p 1,p 2,...,p n } {p } to hold W p pills. The subproblem is of the same form as the original 4

problem, so that by induction on k we can show that making the greedy choice at every step produces an optimal solution. b) How would you modify your algorithm if each bottle also has an associated cost c i, and you want to minimize the total cost of the bottles used to store all the pills? Give a recursive formulation of this problem (formula is enough). You do not need to prove correctness. (Hint: Let MinPill[i,j] be the minimum cost obtainable when storing j pills using bottles among 1 through i. Thinking of the 0-1 knapsack problem formulation may help. ) Solution: We want to find the minimum cost obtainable when storing j pills using bottles chosen from the set bottle 1 through bottle i. This occurs either with or without bottle i. The first case is given by the cost c i of storing p i pills in bottle i plus the minimum cost to store j p i pills among some subset of bottles 1 through i 1. The second case is given by the minimum cost obtainable when storing j pills among some subset of bottles 1 through i 1. The minimum of the first and second cases is the optimal solution at the i th bottle. { 0, j <= 0 or i <= 0, MinPill[i,j] = min{c i + MinPill[i 1,j p i ],MinPill[i 1,j]} otherwise. M inp ill[n, W] solves our problem. An implementation for MinPill[i,j] could be as follows: MinPill[i,j] if j <= 0 or i <= 0 return 0; else with = c_i + MinPill[i-1, j-p_i] without = MinPill[i-1,j] return min{with, without} c) Describe briefly how you would design an algorithm for it using dynamic programming and analyse its running time. We want to design a dynamic programming algorithm to compute MinPill[n,W]. The idea is to avoid repeated calculations of subproblems by solving every subproblem M inp ill[i, j] just once and saving its solution in a table. We create such a table of size n W and initialize its entries to null. We modify the function MinPill[n,W] to check the table before making a recursive call to see if the value has been computed already. If so, we return the value. Else we have to make the recursive call and store the result in the table. From the recursive formulation given in (b) we see the cost to compute MinPill[i,j] is O(1) (we are finding the minimum of two values) not counting recursive calls and we fill each entry in the table at most once. The running time of the dynamic programming algorithm is then O(nW) to create the table added to the O(1) work to compute each of the nw entries, for a total running time of O(nW). An implementation could be as follows: 5

MinPill[i,j] if table(i,j)!= null then return table(i,j) end if if p_i <= j then with = c_i + MinPill[i-1, j-p_i] else with = c_i end if without = MinPill[i-1,j] table(i,j) = min{with,without} return table(i,j) 6

[25 points ] Problem 4: In this problem we look at the amortized cost of insertion in a dynamic table. Initially the size of the table is 1. The cost of insertion is 1 if the table is not full. When an item is inserted into a full table, it first expands the table and then inserts the item in the new table. The expansion is done by allocating a table of size 3 times larger than the old one and copying all the elements of the old table into the new table. a) What is the cost of the i-th insertion? Solution: We are given that the cost of an insertion is 1 if the table is not full. If the table is full then we have to copy the elements in the (old) table into the new one and insert the i th element into the new table, so the cost of the (expensive) i th insertion is i. More precisely, we know that the size of a new table is three times that of the old table and initially the table is of size 1, so after k expansions the table is of size 3 k, k = 0,1,2,... and the actual cost is i = 3 k + 1. { i if i = 3 cost of insertion i = k + 1,k = 0,1,2,... 1 otherwise b) Using the accounting method, prove that the amortized cost of an insert in a sequence of n inserts starting with an empty table is O(1). Solution: In the accounting method we assign differing charges to the various operations performed. The amount we charge an operation is its amortized cost. If the amortized cost of an operation is higher than its actual cost, then the difference is referred to as credit that is distributed among elements in the system to pay for future operations. In this problem the only operation is insertion. To calculate the amortized cost for insertion, we assume that after an expensive operation is performed expanding the table all the credit is used up and we need to accumulate enough credit from the insertions between table expansions to pay for next expansion. Expanding a table of size 3 k gives a new table of size 3 3 k = 3 k+1. Following this expansion, the 3 k elements we just copied have no credit to pay for future operations. There will 2 3 k inserts into the table before it becomes full again and the cost of inserting element 2 3 k + 1 is 3 k+1 + 1. Thus, to pay for the expensive insert the minimum credit c needed per each of the 2 3 k inexpensive insertions is the solution to the equation ( c 2 3 k) + 1 = 3 k+1 + 1, which is c = 3/2. This is the smallest c for which the total credit in the system at any time is non-negative. The actual cost of an insertion when the table is not full is 1, so we can assign an insertion operation an amortized cost of 5/2. 7

EXTRA CREDIT 10 points b) Prove the same amortized cost by defining an appropriate potential function. You can use the standard notation num(t) for the number of elements in the table T and size(t) for the total number of slots (maximum size) of the table. Solution: We assume that the credit in the system ( potential ) is 0 after performing an expensive operation and increases with each subsequent inexpensive operation to the actual cost of the next expensive operation. For i = 1..n, let c i be the actual cost of operation i and let D i represent the system (or data structure) that results after the i th operation is performed on D i 1. The amortized cost a i of the i th operation with respect to Φ is a i = c i + Φ(D i ) Φ(D i 1 ), where Φ(D i ) represents the potential in the system after operation i. We know from (a) and (b) that if all elements inserted between table expansions have 3/2 credits then we can pay for an expensive insertion into a full table (the table is full when num(t) = size(t)). The table is 1/3 full after expansion, that is, num(t) = size(t)/3, and the potential in the system is 0 (the potential contributed by the size(t)/3 elements in the table is already used). In general, when we have num(t) elements in the table, num(t) size(t)/3 insertions have each contributed 3/2 to the potential in the system. Thus we can define our potential function as Φ(D i ) = 3 2 ( num(t) size(t) 3 When num(t) = size(t)/3, Φ(D i ) = 0 and when num(t) = size(t), Φ(D i ) = size(t). The table is at least 1/3 filled at any time, so that num(t) size(t)/3, and num(t) size(t) since when num(t) = size(t) a new table is created at the next insertion, thus Φ(D i ) 0 for all i. To prove the same amortized cost as in (b), we consider the two possible cases when performing an insert operation. If the table is not full, we have a i+1 = c i+1 + Φ(D i+1 ) Φ(D i ) = 1 + 3/2(num(T) + 1 size(t)/3) 3/2(num(T) size(t)/3) = 5/2. If the table is full (i.e. num(t) = size(t)), a i+1 = c i+1 + Φ(D i+1 ) Φ(D i ) = num(t) + 1 + 3/2(num(T) + 1 size(t)) 3/2(num(T) size(t)/3) = 5/2 + num(t) size(t) = 5/2. ). 8