CSE 332, Spring 2010, Midterm Examination 30 April 2010

Similar documents
CSE373 Fall 2013, Midterm Examination October 18, 2013

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

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

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

CSE373 Winter 2014, Midterm Examination January 29, 2014

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

CSE332 Summer 2010: Midterm Exam Sample Solutions

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

Midterm solutions. n f 3 (n) = 3

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

Practice Midterm Exam Solutions

CSE 373 Sample Midterm #2 (closed book, closed notes, calculators o.k.)

Multiple-choice (35 pt.)

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CSE505, Fall 2012, Midterm Examination October 30, 2012

Spring 2002: Fundamental Structures of Computer Science

CSE341, Fall 2011, Midterm Examination October 31, 2011

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

CSE 373 Spring 2010: Midterm #2 (closed book, closed notes, NO calculators allowed)

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

CSE 332, Spring 2010, Final Examination 8 June 2010

Analysis of Algorithms

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

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

Quiz 1 Practice Problems

CSE wi: Practice Midterm

Binary Heaps in Dynamic Arrays

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

Section 05: Solutions

CSE332, Spring 2012, Final Examination June 5, 2012

(D) There is a constant value n 0 1 such that B is faster than A for every input of size. n n 0.

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.

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

Priority Queues and Binary Heaps

Course Review. Cpt S 223 Fall 2009

UNIVERSITY REGULATIONS

CSE 303, Spring 2005, Midterm Examination 29 April Please do not turn the page until everyone is ready.

Quiz 1 Solutions. Asymptotic growth [10 points] For each pair of functions f(n) and g(n) given below:

Recall: Properties of B-Trees

CSE331 Winter 2014, Midterm Examination February 12, 2014

CSE331 Winter 2014, Midterm Examination February 12, 2014

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Final Exam in Algorithms and Data Structures 1 (1DL210)

Section 05: Midterm Review

Course Review for Finals. Cpt S 223 Fall 2008

CSE 505, Fall 2008, Midterm Examination 29 October Please do not turn the page until everyone is ready.

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

CSE 373 Autumn 2010: Midterm #2 (closed book, closed notes, NO calculators allowed)

ECE250: Algorithms and Data Structures Midterm Review

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE341 Spring 2016, Midterm Examination April 29, 2016

Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1

CSE Data Structures and Introduction to Algorithms... In Java! Instructor: Fei Wang. Mid-Term Exam. CSE2100 DS & Algorithms 1

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

Section 1: True / False (1 point each, 15 pts total)

COS 226 Midterm Exam, Spring 2009

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

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

c) A perfect ternary tree of height h? (In a ternary tree, each node may have up to three children.)

Figure 1: A complete binary tree.

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

Section 05: Solutions

Algorithms. AVL Tree

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Computer Science Foundation Exam

CSE341 Spring 2017, Midterm Examination April 28, 2017

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

INSTITUTE OF AERONAUTICAL ENGINEERING

We don t have much time, so we don t teach them [students]; we acquaint them with things that they can learn. Charles E. Leiserson

CSE341 Spring 2016, Midterm Examination April 29, 2016

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

Data Structures Question Bank Multiple Choice

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Test #2. Login: 2 PROBLEM 1 : (Balance (6points)) Insert the following elements into an AVL tree. Make sure you show the tree before and after each ro

COMP Analysis of Algorithms & Data Structures

CMSC351 - Fall 2014, Homework #2

Midterm II Exam Principles of Imperative Computation Frank Pfenning. March 31, 2011

CLO Assessment CLO1 Q1(10) CLO2 Q2 (10) CLO3 Q4 (10) CLO4 Q3a (4)

Assume you are given a Simple Linked List (i.e. not a doubly linked list) containing an even number of elements. For example L = [A B C D E F].

COMP Analysis of Algorithms & Data Structures

Algorithms, Spring 2014, CSE, OSU Lecture 2: Sorting

Second Examination Solution

Problem Set 6 Due: 11:59 Sunday, April 29

CS2 Algorithms and Data Structures Note 6

Binary Search Trees Treesort

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

Priority Queues Heaps Heapsort

Quiz 1 Practice Problems

CSE 373 Autumn 2012: Midterm #2 (closed book, closed notes, NO calculators allowed)

CS 2150 (fall 2010) Midterm 2

Data Structure and Algorithm, Spring 2013 Midterm Examination 120 points Time: 2:20pm-5:20pm (180 minutes), Tuesday, April 16, 2013

CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees. Linda Shapiro Spring 2016

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

Student number: Datenstrukturen & Algorithmen page 1

Transcription:

CSE 332, Spring 2010, Midterm Examination 30 April 2010 Please do not turn the page until the bell rings. Rules: The exam is closed-book, closed-note. You may use a calculator for basic arithmetic only. Please stop promptly at 3:20. You can rip apart the pages, but please staple them back together before you leave. There are 100 points total, distributed unevenly among 9 questions (many with multiple parts). Advice: Read questions carefully. Understand a question before you start writing. Write down thoughts and intermediate steps so you can get partial credit. But clearly circle your final answer. The questions are not necessarily in order of difficulty. Skip around. If you have questions, ask. Relax. You are here to learn. 1

1. (8 points) For each function below, give an asymptotic upper bound using big-oh notation. Your answer should be as tight and simple as possible. (a) f(n) = 4n 3 + 5n 2 log n (b) f(n) = 3n + 4 log n + 2n (c) f(n) = 1.6 n + n 5 (d) f(n) = 2 log log n + log(n/2) (a) O(n 3 ) (b) O(n) (c) O(1.6 n ) (d) O(log n) 2

2. (12 points) Consider this recurrence equation: T (1) = 7 T (n) = 4n + T (n 1) for n > 1 (a) Prove that for all integers n 1, T (n) = 2n 2 + 2n + 3 (b) True or false: i. T (n) is O(n 2 ) ii. T (n) is Ω(n 2 ) iii. T (n) is Θ(n 2 ) (a) Proof by induction on n (b) Base case n = 1: T (1) = 7 = 2 1 2 + 2 1 + 3 Inductive case n = k + 1 > 1: T (n) = 4n + T (n 1) by definition = 4n + 2(n 1) 2 + 2(n 1) + 3 by induction = 4n + 2n 2 4n + 2 + 2n 2 + 3 by factoring = 2n 2 + 2n + 3 by cancelling terms i. true ii. true iii. true 3

3. (16 points) Suppose there is a binary min-heap with exactly 4 nodes, containing items with priorities 5, 6, 7, and 8. (a) Show every possible binary min-heap that could match this description. appropriate tree and the array representation. For each, draw the (b) Argue briefly in English that your answer to part (a) is not missing anything, meaning no other binary min-heap is possible. (c) For one of your answers to part (a), show the result of 4 successive deletemin operations. Clearly indicate which heap you are starting with. You can just draw the tree (not the array). (a) Three possibilities: 5 ------------------- 5 ------------------- / \ 5 6 7 8 / \ 5 7 6 8 6 7 ------------------- 7 6 ------------------- / / 8 8 / 7 5 ------------------- / \ 5 6 8 7 6 8 ------------------- (b) All 4-node heaps must have the shape of the 3 heaps in part (a). The minimum node, 5, must be at the root. The maximum node, 8, must be at a leaf. The only heap meeting these rules and not listed above does not satisfy the heap property because 7 is above 6: / 6 5 / \ 7 8 (c) 5 6 7 / \ / \ / 6 7 ---> 8 7 ---> 8 / 8 ---> 8 --> <empty tree> 4

4. (10 points) Give a very short description of what the method mystery returns (not how it does it). Assume the Node is a binary search tree and make sure this is relevant to your answer. class Node { int value; Node left; Node right; static int mystery(node n) { if(n==null) throw new IllegalArgumentException(); if(n.left == null) return leftmost(n.right); if(n.left.left == null && n.left.right == null) return n.value; return mystery(n.left); } } // return leftmost descendant of n static int leftmost(node n) { if(n==null) throw new IllegalArgumentException(); if(n.left == null) return n.value; return leftmost(n.left); } mystery returns the second smallest value in the binary search tree rooted at n (throwing an exception if the tree has fewer than two elements). 5

5. (12 points) (a) Starting with an empty AVL tree, insert the following keys in order, clearly indicating the final tree: 5, 10, 8, 4, 6, 2 (b) Beginning with the tree produced by part (a), show the AVL tree resulting from deleting 5, then 10, then 8. (a) double 5 --> 5 --> 5 --> 8 --> 8 --> 8 \ \ / \ / \ / \ 10 10 5 10 5 10 5 10 / / / \ 8 4 4 6 single --> 8 --> 5 / \ / \ 5 10 4 8 / \ / / \ 4 6 2 6 10 / 2 (b) 5 6 6 6 4 / \ / \ / \ / single / \ 4 8 --> 4 8 --> 4 8 --> 4 --> 2 6 / / \ / \ / / 2 6 10 2 10 2 2 A second simpler approach uses 5 s predcessor at the first step instead of its successor: 5 4 4 4 / \ / \ / \ / \ 4 8 --> 2 8 --> 2 8 --> 2 6 / / \ / \ / 2 6 10 6 10 6 6

6. (12 points) Consider a B-Tree as defined in lecture and the textbook where M = 64 and L = 6 and the number of nodes is n. In English, describe an O(log M n) algorithm for returning the 4 th smallest element of the tree. You may assume the root is not a leaf. Starting at the root, follow the left-most child of each node until the node s children are leaves. If the leftmost leaf has more than 3 elements, return element 3 of its array of data items. Else return element 0 of the array of data items in the leaf just to the right of the leftmost leaf. 7

7. (10 points) For both questions below, give an exact answer and briefly justify your answer. (a) What is the minimum number of data items in a B tree of height 4 (as defined in class and the textbook) with M = 4 and L = 4. (b) What is the minimum number of data items in an AVL tree of height 4? (a) We can use the equation from lecture or just rederive it for height 4 by putting the minimum number of children and data items everywhere: All internal nodes (including the root) can have as few as 2 children and the leaves can have as few as 2 data items. Because all leaves have to be at the same height, this is the same as a perfect binary tree of height 4 (2 4 = 16 leaves), with two data items at each leaf. So 32 data items. (b) Letting S(h) be the minimum number of nodes in an AVL tree of height h, recall that S( 1) = 0, S(0) = 1 and for h > 0, S(h) = 1 + S(h 1) + S(h 2). (This follows directly from the AVL balance property.) Therefore, we can simply calculate iteratively: S(1) = 2, S(2) = 4, S(3) = 7 and S(4) = 12. (You can also draw a minimal height-4 tree, but you need to justify that no height-4 tree with fewer nodes is balanced.) So 12 data items. 8

8. (13 points) Consider inserting data with integer keys 7, 9, 12, 18, 29 in that order into a hash table of size 11 where the hashing function is h(key)%11. Show a chaining hash table after doing the insertions: 0 1 2 3 4 5 6 7 8 9 10 Show an open addressing with linear probing hash table after doing the insertions. 0 1 2 3 4 5 6 7 8 9 10 Show an open addressing with quadratic probing hash table after doing the insertions. 0 1 2 3 4 5 6 7 8 9 10 (a) At index 1: a list holding 12 At index 7: a list holding 29, then 18, then 7 At index 9: a list holding 9 (b) X 12 X X X X X 7 18 9 29 (c) 29 12 X X X X X 7 18 9 X 9

9. (7 points) Consider these four implementation approaches for hash tables: Separate chaining Open addressing with linear probing Open addressing with quadratic probing Open addressing with double hashing (a) Suppose many items for a hash table are initially hashing to the same table index. Which implementation approach or approaches are likely to lead to very poor performance? (b) Suppose many items for a hash table are initially hashing such that the first two hash to index i, the next two to index i + 1, the next two to index i + 2, etc. Which implementation approach or approaches are likely to lead to very poor performance? (c) Suppose you are trying to use various hash table libraries for some data you have, but you are getting poor performance for all of them. What should you do to try to get better hash table performance? (a) Separate chaining, linear probing, and quadratic probing (b) Linear probing (c) Change your hash function 10