COS 226 Algorithms and Data Structures Fall Midterm Solutions

Similar documents
COS 226 Algorithms and Data Structures Fall Midterm

Priority Queues 1 / 15

COS 226 Midterm Exam, Spring 2009

COS 226 Algorithms and Data Structures Spring Midterm

COS 226 Algorithms and Data Structures Practice Design Questions

COS 226 Algorithms and Data Structures Spring Midterm

algorithm evaluation Performance

COS 226 Algorithms and Data Structures Fall Midterm

COS 226 Algorithms and Data Structures Spring Midterm

COS 226 Algorithms and Data Structures Spring Midterm Exam

COS 226 Midterm Exam, Spring 2011

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

Princeton University Computer Science COS226: Data Structures and Algorithms. Midterm, Fall 2013

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

About this exam review

Searching in General

COMP Data Structures

Computer Science 302 Spring 2007 Practice Final Examination: Part I

CS 445: Data Structures Final Examination: Study Guide

4.2 Sorting and Searching. Section 4.2

COS 226 Midterm Exam, Spring 2010

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

COS 226 Midterm Fall 2007

COS 226 Algorithms and Data Structures Fall Midterm

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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

Sorting. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi

Lecture 6 Sorting and Searching

Sorting Applications 1 / 9

07 B: Sorting II. CS1102S: Data Structures and Algorithms. Martin Henz. March 5, Generated on Friday 5 th March, 2010, 08:31

COS 226 Algorithms and Data Structures Spring Midterm

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

CMSC 341 Lecture 14: Priority Queues, Heaps

Final Examination CS 125 Introduction to Computer Science Fall Hours

4.1, 4.2 Performance, with Sorting

Unit Outline. Comparing Sorting Algorithms Heapsort Mergesort Quicksort More Comparisons Complexity of Sorting 2 / 33

Lecture 6 Binary Search

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

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

Algorithms. Algorithms 2.4 PRIORITY QUEUES. API and elementary implementations binary heaps heapsort event-driven simulation

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Programming II (CS300)

Programming II (CS300)

4.1 Performance. Running Time. The Challenge. Scientific Method

CS210 (161) with Dr. Basit Qureshi Final Exam Weight 40%

input sort left half sort right half merge results Mergesort overview

Running Time. Analytic Engine. Charles Babbage (1864) how many times do you have to turn the crank?

4.1 Performance. Running Time. The Challenge. Scientific Method. Reasons to Analyze Algorithms. Algorithmic Successes

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

Binary heaps (chapters ) Leftist heaps

Programming II (CS300)

CSci 231 Final Review

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

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

Sorting algorithms Properties of sorting algorithm 1) Adaptive: speeds up to O(n) when data is nearly sorted 2) Stable: does not change the relative

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

Largest Online Community of VU Students

Algorithms. Algorithms 2.4 PRIORITY QUEUES. API and elementary implementations binary heaps heapsort event-driven simulation (see videos)

Heapsort. Heap data structure

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

(b) If a heap has n elements, what s the height of the tree?

School of Computing National University of Singapore CS2010 Data Structures and Algorithms 2 Semester 2, AY 2015/16. Tutorial 2 (Answers)

CS221: Algorithms and Data Structures. Sorting Takes Priority. Steve Wolfman (minor tweaks by Alan Hu)

COP 3502 Spring 2018 Study Union Review

Midterm, Spring 2013

Divide and Conquer Algorithms: Advanced Sorting

Solutions to Exam Data structures (X and NV)

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

Lecture 19 Sorting Goodrich, Tamassia

Lecture 6: Divide-and-Conquer

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

Draw the resulting binary search tree. Be sure to show intermediate steps for partial credit (in case your final tree is incorrect).

(a) Write code to do this without using any floating-point arithmetic. Efficiency is not a concern here.

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

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

Priority Queues (Heaps)

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

Tables, Priority Queues, Heaps

Computer Science 302 Spring 2017 (Practice for) Final Examination, May 10, 2017

Parallel Sorting Algorithms

Prelim 2, CS2110. SOLUTION

CS 61B Midterm 2 Guerrilla Section Spring 2018 March 17, 2018

CISC 621 Algorithms, Midterm exam March 24, 2016

Lecture 5: Sorting Part A

cs2010: algorithms and data structures

ECE242 Data Structures and Algorithms Fall 2008

Total Score /1 /20 /41 /15 /23 Grader

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

Sorting (Weiss chapter )

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

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid. Fall 2018

NET/JRF-COMPUTER SCIENCE & APPLICATIONS. Time: 01 : 00 Hour Date : M.M. : 50

ext Total Score /20 /20 /15 /20 /25 /5 Grader

Binary Search. CS 5010 Program Design Paradigms Bootcamp Lesson 8.2

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

Prelim 2. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Transcription:

1 COS 226 Algorithms and Data Structures Fall 2010 Midterm Solutions 1. Analysis of algorithms. (a) For each expression in the left column, give the best matching description from the right column. B. 1 + 2 + 4 + 8 +... + N 2N C. 1 + 2 + 3 + 4 +... + N 1 2 N 2 B. 1 + 3 + 5 + 7 +... + N 1 4 N 2 C. 1 2 N 2 A. 1 2 N 2. B. O(N 2 ). C. Both A and B. D. Neither A nor B. C. 1 2 N 2 + 100N lg N B. N 2 D. N 3 (b) For each quantity in the left column, give the best matching description from the right column. B. Height of a weighted quick union data structure with N items. C. Height of a binary heap with N keys. A. Height of a left-leaning red-black BST with N keys. A. lg N in the best case. B. lg N in the worst case. C. Both A and B. D. Neither A nor B. C. Maximum function-call stack size when (top-down) mergesorting N keys. A. Maximum function-call stack size when quicksorting N keys. B. Number of compares to binary search in a sorted array of size N.

2 (c) 2 minutes. The order of growth of the running time is N 2 log N from the 1 2 N 2 calls to binary search. Thus, if the problem size increases by a factor of 10, the running time will increase by a bit more than a factor of 10 2. (d) 40 bytes. (8 bytes of object overhead, 4 bytes for the int, and 4 bytes for each of the 7 references) 2. 8 sorting algorithms. 0 5 7 6 4 3 9 2 8 1 3. Binary heaps. (a) (b) (c) - Y W M G U K C A F H P - - W U M G P K C A F H - - - Y W Q G U M C A F H P K 4. Red-black BSTs. (a) H, I, J, and K (b) RED (c) U M X G R V Z C L P T A E?

3 5. Hashing. 0 1 2 3 4 5 6 G B D A C E F 6. Bitonic max. (a) We use a binary-search like algorithm, where we compare the middle key to the neighboring key to its right. Depending on the result of the comparison, we recur in the left subarray or the right subarray (which is also bitonic). // find the max in the bitonic subarray a[lo] to a[hi] public static int max(int[] a, int lo, int hi) { if (hi == lo) return a[hi]; int mid = lo + (hi - lo) / 2; if (a[mid] < a[mid+1]) return max(a, mid+1, hi); else if (a[mid] > a[mid+1]) return max(a, lo, mid); else return a[mid]; We maintain the invariant that the subarray contains the maximum key. At each step, the size of the subarray decreases by a factor of 2, so the number of compares is logarithmic in N. (b) Here are the first four compares when finding the maximum of the following bitonic array: i 13 14 a[i] 10 34 56 76 87 80 70 66 56 30 28 25 20 15 11 1. compare a[7] to a[8] (66 to 56) 2. compare a[3] to a[4] (76 to 87) 3. compare a[5] to a[6] (80 to 70) 4. compare a[4] to a[5] (87 to 80)

4 7. Stable priority queue. Binary heap solution. The basic idea is to associate the integer timestamp i with the i th key that is inserted into the priority queue. To compare two keys, compare their keys as usual; if they are equal, break ties according to the associated timestamp. This ensures that if there are two equal keys, the one that was inserted first is considered smaller than the one that was inserted last. Here are two ways to implement this approach in Java. Implementation 1. We modify our standard MinPQ implementation as follows: Associate the integer timestamp i with the i th key that is inserted by creating a nested class StableKey. private class StableKey { private Key key; private long timestamp; public int compareto(stablekey that) { int cmp = this.key.compareto(that.key); if (cmp < 0) return -1; if (cmp > 0) return +1; return this.timestamp - that.timestamp; When comparing two keys in less(), break ties according to the timestamp field using the compareto() method above. Implementation 2. We modify our standard MinPQ implementation as follows: Associate the integer timestamp i with the i th key that is inserted by adding a parallel array long[] timestamp as an instance variable of StableMinPQ. Modify exch() so that whenever it exchanges pq[i] with pq[j], it also exchanges timestamp[i] with timestamp[j]. Modify less() so that it breaks ties according to timestamp[]. Binary search tree solution. An alternate solution is to use a red-black BST. Some care is needed because our implementation of RedBlackBST does not support duplicate keys without modification. To handle duplicate keys, we declare a RedBlackBST<Key, Queue<Key>>, where the value is a queue of all the keys equal to the key. To insert a key, we check if there is a key equal to it already in the BST. If there is, we add the key to the corresponding queue; if there is not, we add the key to the BST first. To delete the minimum key in the stable priority queue, we call min() to find the minimum, and return the first element of corresponding queue. We don t invoke the delmin() method of RedBlackBST unless the queue becomes empty.

5 Wrong solution. A tempting idea is to swap equal keys during sink() but not to swap equal keys during swim(). However, the following sequence of operations discredits this approach: insert C 1 insert B 1 insert A insert B 2 insert C 2 delmin (returns C 1 ) delmin (returns C 2 ) min (returns B 2 instead of B 1 )