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

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

CSE 373 Autumn 2012: Midterm #2 (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)

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

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

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

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 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

CSE373 Fall 2013, Midterm Examination October 18, 2013

ECE242 Data Structures and Algorithms Fall 2008

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

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CS350: Data Structures Heaps and Priority Queues

CSE373 Winter 2014, Midterm Examination January 29, 2014

Heap Model. specialized queue required heap (priority queue) provides at least

CSE332 Summer 2010: Midterm Exam Sample Solutions

1. AVL Trees (10 Points)

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Data Structures and Algorithms

COS 226 Midterm Exam, Spring 2009

Priority Queues (Heaps)

Priority Queues (Heaps)

CE 221 Data Structures and Algorithms

CSE332 Summer 2010: Final Exam

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

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

CSE 373: Practice Final

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

COS 226 Algorithms and Data Structures Fall Midterm

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

The priority is indicated by a number, the lower the number - the higher the priority.

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

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

CSE 373: Data Structures and Algorithms

COMP 250 Midterm #2 March 11 th 2013

CSE 332 Autumn 2016 Final Exam (closed book, closed notes, no calculators)

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

CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: Instructions

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

Practice Midterm Exam Solutions

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

CSE 100 Disjoint Set, Union Find

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

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

Q1 Q2 Q3 Q4 Q5 Q6 Total

Binary Heaps. CSE 373 Data Structures Lecture 11

CSCI2100B Data Structures Heaps

Priority queues. Priority queues. Priority queue operations

Data Structures Week #8. Heaps (Priority Queues)

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

COS 226 Midterm Exam, Spring 2011

Priority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments

ECE 2035 A Programming HW/SW Systems Spring problems, 5 pages Exam Three 13 April Your Name (please print clearly)

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Linda Shapiro Spring 2016

CSE 100: UNION-FIND HASH

Technical University of Denmark

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

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

Algorithms and Data Structures

CMSC 341 Lecture 14: Priority Queues, Heaps

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

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

COMP Data Structures

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 1

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Binary heaps (chapters ) Leftist heaps

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

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Nicki Dell Spring 2014

Computer Science E-22 Practice Final Exam

INF2220: algorithms and data structures Series 4

Priority Queues and Heaps

Implementations. Priority Queues. Heaps and Heap Order. The Insert Operation CS206 CS206

CSE 214 Computer Science II Heaps and Priority Queues

CSE 373: Floyd s buildheap algorithm; divide-and-conquer. Michael Lee Wednesday, Feb 7, 2018

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

CSE 373 Data Structures and Algorithms. Lecture 15: Priority Queues (Heaps) III

Leftist Heaps and Skew Heaps. (Leftist Heaps and Skew Heaps) Data Structures and Programming Spring / 41

UNIT III BALANCED SEARCH TREES AND INDEXING

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 28 November 2012

Priority Queues Heaps Heapsort

Figure 1: A complete binary tree.

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

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

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014

CS171 Final Practice Exam

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

York University AK/ITEC INTRODUCTION TO DATA STRUCTURES. Final Sample II. Examiner: S. Chen Duration: Three hours

CSE 332 Winter 2018 Final Exam (closed book, closed notes, no calculators)

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues. Aaron Bauer Winter 2014

9. Heap : Priority Queue

CSE373 Spring 2015: Final

Transcription:

Name: Email address: CSE 373 Autumn 2010: Midterm #2 (closed book, closed notes, NO calculators allowed) Instructions: Read the directions for each question carefully before answering. We may give partial credit based on the work you write down, so if time permits, show your work! Use only the data structures and algorithms we have discussed in class or that were mentioned in the book so far. Note: For questions where you are drawing pictures, please circle your final answer for any credit. Good Luck! Total: 61 points. Time: 50 minutes. Question Max Points 1 11 2 10 3 10 4 6 5 8 6 16 Total 61 Score Page 1 of 10

1) [11 points total] Disjoint Sets: Given the array representation of up-trees discussed in class (where size[x] = total number of values in set x, and up[x] == -1 indicates that node x is a root node), for example (size is unspecified for non-root nodes): 1 3 7 2 5 4 up a) [6 points] Fill in the code below for Union by Size (weighted Union) (does not have to be perfect Java code). (On a tie in size, the set referred to by the second parameter should be added to the set referred to by the first.) int up[n]; int size[n]; 1 2 3 4 5 6 7 8 9-1 1-1 7 7 4-1 6 6 size 2 1 6 8 9 void Union(int x, int y) { // Assuming x and y are roots of two different sets, // fill in code to implement union by size (weight) // Break ties in size by adding the set y // to the set x. int wx = size[x]; int wy = size[y]; 6 } if (wx < wy) { // x should be added to y up[x] = y; size[y] = wx + wy; } else { // y should be added to x up[y] = x; size[x] = wx + wy; } Page 2 of 10 2

1) (cont) b) (1 pts) Now, draw the set of uptrees that results from executing a find(9) with path compression on the sets shown in on the previous page. You may update the set of uptrees on the previous page or redraw the sets here. If no changes occur, state that. c) (2 pts) Update the array to reflect the changes (if any) caused by that find operation. You may update the array on the previous page or redraw the array here. If no changes occur, state that. 1 2 3 4 5 6 7 8 9-1 1-1 7 7 7-1 up 6 7 2 1 6 d) (2 pts) What is the worst case running time of a single find operation if union by size and path compression are used? N = total # of elements in all sets. (no explanation required) O(log N) Page 3 of 10

2) [10 points total] Hashing: Draw the contents of the two hash tables below. Show your work for partial credit. If an insertion fails, please indicate which values fail and attempt to insert any remaining values. The size of the hash table is 7. The hash function used is H(k) = k mod 7 13, 17, 6, 24, 3 a) Separate chaining, where each b) Double hashing, where bucket points to a sorted linked list. H2(k) = 7 (k mod 5) 0 1 2 3 4 0 1 2 3 4 5 6 5 6 H2(6) = 7 (6 mod 5) = 7 1 = 6 H2(24) = 7 (24 mod 5) = 7 4 = 3 H2(3) = 7 (3 mod 5) = 7 3 = 4 Page 4 of 10 4

2) (cont) c) (1 pt) What is the load factor for the table a)? 5/7 d) (1 pt) What is the load factor for the table b)? 5/7 Page 5 of 10

3) [10 points total] Leftist Heaps: a) [8 pts] Draw the leftist heap that results from inserting: 75, 28, 7, 61, 15, 55, 19, 4 in that order into an initially empty heap. You are only required to show the final heap, although if you draw intermediate heaps, please circle your final result for ANY credit. Page 6 of 10 6

3) (cont) b) [2 pts] What is the null path length of the root in your final heap from part a)? Page 7 of 10

4) [6 points] Skew Heaps Draw the skew heap that results from doing a deletemin on the skew heap shown below. You are only required to show the final tree, although if you draw intermediate trees, please circle your final result for ANY credit. Page 8 of 10 8

5) [8 points] Memory Hierarchy & Locality: Examine the code example below: c[1] = 5; z = 68; j = 2; while (j < 2000) { a[j] = a[j] + a[j-1]; z = b[10] + z; System.out.println( d[j] = + d[j]); j = j + 1; } f[25] = 60; y = c[2000] + c[2001] + c[2002] + c[2003] Considering only their use in the code segment above, for each of the following variables, indicate below what type of locality (if any) is demonstrated. Please circle all that apply (you may circle more than one item for each variable): a spatial locality temporal locality no locality b spatial locality temporal locality no locality c spatial locality temporal locality no locality d spatial locality temporal locality no locality f spatial locality temporal locality no locality j spatial locality temporal locality no locality y spatial locality temporal locality no locality z spatial locality temporal locality no locality Page 9 of 10

6) [16 points total] Running Time Analysis: Describe the most time-efficient way to implement the operations listed below. Assume no duplicate values and that you can implement the operation as a member function of the class with access to the underlying data structure. Then, give the tightest possible upper bound for the worst case running time for each operation in terms of N. **For any credit, you must explain why it gets this worst case running time. You must choose your answer from the following (not listed in any particular order), each of which could be re-used (could be the answer for more than one of a) -f)). O(N 2 ), O(N ½ ), O(N log N), O(N), O(N 2 log N), O(N 5 ), O(2 N ), O(N 3 ), O(log N), O(1), O(N 4 ), O(N N ), O(N 6 ), O(N (log N) 2 ), O(N 2 (log N) 2 ) a) DecreaseKey(k, v) on a binary min heap containing N elements. Assume you have a reference to the key k. v is the amount that k should be decreased. Explanation: a) Using the reference to k, decrement its value by v (takes O(1)). Then percolate k up by comparing to its parent and swapping if necessary. In the worst case k is at the bottom row of the heap and has been decremented to become the smallest value in the heap and thus must move up O(log N) levels. Thus total running time is O(log N). b) Inserting an element in a hash table containing N elements where separate chaining is used and each bucket points to a sorted linked list. The table size = N. Explanation: b) To insert, do the hash function to find the bucket, band then search through the sorted linked list until you find a value greater than the value to be inserted or reach the end of the list. Insert the value at that point in the list. In the worst case all elements have been hashed to the same bucket, and the value you are inserting is the largest value in the hash table, causing you to search through all N values before adding the value at the end of the list. Finding the bucket takes O(1) but searching to the end of the sorted linked list takes O(N), so total running time is O(N). c) Converting a 4-heap (a d-heap where d=4) containing N elements into a binary min heap. Explanation: c) Unfortunately a 4-heap is not guaranteed to be a 2-heap. To convert a 4-heap to a binary min heap just run Floyds buildheap method on the 4-heap array. Buildheap takes O(N) since the sum of the heights that must be percolated down is O(N). d) Rehashing values from a hash table containing 2N elements (where separate chaining is used and each bucket points to a sorted linked list). The original hash table size = N, the new hash table is size 2N. Explanation: d) To rehash, go through all N buckets of the original hash table. For each bucket, go through each element in the bucket. For each element, recalculate the hash function to find the bucket in the new table, and insert the value into the new table. Examining all buckets and all linked list nodes in the original hash table takes time N to examine each bucket and time 2N to examine each element = O(N). You then have to reinsert each value. In the worst case all 2N values hash to the same bucket in both the original and the new hash table. If you read the values in original hash table lists in ascending order, and thus insert in ascending order (always at end of the list), then this means the inserts will take O(1) to calculate the hash function, but O(N) to insert into the linked list for each value, resulting in O(N 2 ) for the inserts. Page 10 of 10 10