CSE373 Fall 2013, Second Midterm Examination November 15, 2013

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

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

CSE373 Winter 2014, Final Examination March 18, 2014 Please do not turn the page until the bell rings.

CSE373 Winter 2014, Final Examination March 18, 2014 Please do not turn the page until the bell rings.

CSE373 Fall 2013, Midterm Examination October 18, 2013

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 332, Spring 2010, Midterm Examination 30 April 2010

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

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

CSE332 Summer 2012 Final Exam, August 15, 2012

Practice Midterm Exam Solutions

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

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

CSE331 Winter 2014, Midterm Examination February 12, 2014

CSE 373: Practice Final

CSE331 Winter 2014, Midterm Examination February 12, 2014

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

CSE351 Winter 2016, Final Examination March 16, 2016

1. AVL Trees (10 Points)

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

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

CSE wi: Practice Midterm

COS 226 Algorithms and Data Structures Fall Midterm

CS171 Final Practice Exam

CSE373 Spring 2015: Final

CSE331 Fall 2014, Final Examination December 9, 2014 Please do not turn the page until 2:30. Rules:

CSE 332, Spring 2010, Final Examination 8 June 2010

CS171 Final Practice Exam

Section 05: Solutions

CSE341 Spring 2017, Final Examination June 8, 2017

CSE332 Summer 2010: Midterm Exam Sample Solutions

Solutions to Exam Data structures (X and NV)

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

CSE331 Spring 2015, Final Examination June 8, 2015

CSE332, Spring 2012, Final Examination June 5, 2012

Final Examination CSE 100 UCSD (Practice)

Queues. Queue ADT. Queue Specification. An ordered sequence A queue supports operations for inserting (enqueuing) and removing (dequeuing) elements.

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE331 Winter 2014, Final Examination March 17, 2014 Please do not turn the page until 8:30. Rules:

Course Review for Finals. Cpt S 223 Fall 2008

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

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

COMP171. Hashing.

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

HASH TABLES. Hash Tables Page 1

CSE341 Spring 2017, Final Examination June 8, 2017

Second Examination Solution

CSE373: Data Structures & Algorithms Lecture 17: Hash Collisions. Kevin Quinn Fall 2015

CS 206 Introduction to Computer Science II

Fall 2017 Mentoring 9: October 23, Min-Heapify This. Level order, bubbling up. Level order, bubbling down. Reverse level order, bubbling up

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

6.7 b. Show that a heap of eight elements can be constructed in eight comparisons between heap elements. Tournament of pairwise comparisons

stacks operation array/vector linked list push amortized O(1) Θ(1) pop Θ(1) Θ(1) top Θ(1) Θ(1) isempty Θ(1) Θ(1)

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

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

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

Standard ADTs. Lecture 19 CS2110 Summer 2009

EECS 311 Data Structures Midterm Exam Don t Panic!

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

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

CSE341, Fall 2011, Midterm Examination October 31, 2011

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

Introducing Hashing. Chapter 21. Copyright 2012 by Pearson Education, Inc. All rights reserved

CSE351 Spring 2018, Final Exam June 6, 2018

HASH TABLES cs2420 Introduction to Algorithms and Data Structures Spring 2015

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

CS61B, Fall 2015 Final Examination (with corrections) P. N. Hilfinger

CSE 100: UNION-FIND HASH

! A Hash Table is used to implement a set, ! The table uses a function that maps an. ! The function is called a hash function.

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

Section 05: Midterm Review

Linked lists (6.5, 16)

About this exam review

(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other?

Most of this PDF is editable. You can either type your answers in the red boxes/lines, or you can write them neatly by hand.

COS 226 Midterm Exam, Spring 2009

CS 2150 (fall 2010) Midterm 2

Announcements. Submit Prelim 2 conflicts by Thursday night A6 is due Nov 7 (tomorrow!)

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course

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

CS61BL Summer 2013 Midterm 2

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

CS 112 Final May 8, 2008 (Lightly edited for 2011 Practice) Name: BU ID: Instructions GOOD LUCK!

CS 314 Final Fall 2012

CSE 143 SAMPLE MIDTERM

CS 3114 Data Structures and Algorithms READ THIS NOW!

MIDTERM EXAM THURSDAY MARCH

CSE341 Spring 2016, Midterm Examination April 29, 2016

Section 05: Solutions

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

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

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

You must print this PDF and write your answers neatly by hand. You should hand in the assignment before recitation begins.

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

Solution READ THIS NOW! CS 3114 Data Structures and Algorithms

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

Course Review. Cpt S 223 Fall 2009

Transcription:

CSE373 Fall 2013, Second Midterm Examination November 15, 2013 Please do not turn the page until the bell rings. Rules: The exam is closed-book, closed-note, closed calculator, closed electronics. Please stop promptly at 3:20. There are 102 points total, distributed unevenly among 8 questions (many with multiple parts): Question Max Earned 1 14 2 12 3 11 4 10 5 8 6 11 7 27 8 9 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. Make sure you get to all the problems. If you have questions, ask. Relax. You are here to learn.

1. (14 points) The uptrees used to represent sets in the union-find algorithm can be stored in two n-element arrays. The up array stores the parent of each node (or -1 if it has no parent). The weight array stores the number of items in a set if the node is the representative node of a set (else the weight entry for the node does not matter and can be anything). The following shows a collection of sets containing the numbers 1 through 12, without the weight array filled in: up -1 10-1 9 1 1 11-1 7 11-1 1 weight 1 2 3 4 5 6 7 8 9 10 11 12 (a) Draw a picture of the uptrees represented by the data in the up array shown above. (b) Fill in the weight array above so that all entries that need to be correct are correct. (c) Suppose we did not keep the weight array updated as operations are performed. As you did in part (a), the algorithm could compute weights as needed. Would this asymptotically slow down find operations that use path compression? (d) Suppose we did not keep the weight array updated as operations are performed. As you did in part (a), the algorithm could compute weights as needed. Would this asymptotically slow down union operations that use union-by-weight? (e) Show the result of performing the operation find(4) with path compression by doing both of the following: Redraw below any uptrees (from part (a)) that change as a result. Update the array representation as appropriate by drawing a single slash ( / ) through any numbers that change and writing the new number next to it. On next page

Note: As you did in part (a), should have read As you did in part (b) but we didn t notice this typo because nobody asked about it. (a) 1 3 8 11 / \ / \ 5 6 12 7 10 9 2 4 (b) The entries for indices 1, 3, 8, and 11 are 4, 1, 1, and 6 respectively. (c) No (d) Yes (e) The up indices for 4 and 9 are now 11: 11 / / \ 4 9 7 10 \ 2

2. (12 points) Short answer: (a) Your maze-creation homework used your union-find data structure. Your union-find data structure implemented union-by-weight, which can do one of two different things when unioning sets with equal weight. Does the choice affect what maze gets created? (b) Assume a find operation in union-find does path compression. Circle all of the following that are true: The operation that does the compression gets faster. The operation that does the compression gets slower, but only by a constant factor. The operation that does the compression gets asymptotically slower. Future operations may get faster. Future operations may get slower, but only by a constant factor. Future operations may get asymptotically slower. (c) What is log 2 (1, 000, 000, 000) to the nearest whole number? (d) What is log 2(1, 000, 000, 000) to the nearest whole number, where log 2 is the log-star operation we discussed when analyzing union-find? (e) When studying amortization, we learned a way to implement a queue using two stacks. Suppose such a queue is used with a total of x enqueue operations and y dequeue operations in some order. In the approach we studied, what is the asymptotic worst-case running time of a single dequeue operation in terms of x and/or y? In the approach we studied, what is the asymptotic worst-case total running time of all dequeue operations in terms of x and/or y? (f) Your friend says she does not like implementing algorithms that have amortized run-time guarantees because amortization makes it harder to debug her code. Why does this argument make no sense? (A one sentence answer is probably enough.) (a) No (b) The operation that does the compression gets slower, but only by a constant factor. Future operations may get faster. (c) 30 (d) 5 (we will also give full credit for 4) O(x) O(x + y) (e) Amortization is only about the analysis of the running time; it is not part of the code implementing the algorithm.

3. (11 points) Consider a hashtable with separate chaining with N buckets and k items currently in the table. (a) k/n is the definition of a term used when discussing hashing. What is this term? (b) Is it necessary that k < N? (c) What is the average number of items in a bucket? (d) In the worst-case, how many items could be in a single bucket? (e) If k > N, is it possible that any buckets are empty? (f) If we resize the table to a table of size 2N, what is the asymptotic running time in terms of k and N to put all the items in the new table? (a) load factor (b) No (c) k/n (d) k (e) Yes (f) O(k + N)

4. (10 points) (a) Fill in the contents of the hash table below after inserting the items shown. To insert the item k, use the hash function k % TableSize and resolve collisions with quadratic probing. Insert: 74, 924, 83, 113, 5 0 1 2 3 4 5 6 7 8 9 (b) We now consider looking up some items that are not in the table after doing the insertions above. For each, give the list of buckets that are looked at in order before determining that the item is not present. Include all the buckets examined, whether or not they contain an item. i. 65 ii. 76 iii. 100 (a) _ 83 74 924 5 113 (b) i. 5, 6, 9 ii. 6, 7, 0 iii. 0

5. (8 points) For each of the following errors when using hashing and hashtables, give the best answer as to what can go wrong. Notes: Not terminate is often described as go into an infinite loop. The choices are the same in each problem. (a) You are using open addressing with quadratic probing and you allow the table (whose size is a prime number) to become more than half full: i. A lookup operation may not terminate. ii. A lookup operation may not find a value that is actually in the table. iii. Both (i) and (ii). iv. Neither (i) nor (ii). (b) You are using open addressing with quadratic probing and you delete an item by removing it from the table and leaving the bucket it held empty. i. A lookup operation may not terminate. ii. A lookup operation may not find a value that is actually in the table. iii. Both (i) and (ii). iv. Neither (i) nor (ii). (c) You are putting objects of a class you defined into the Java standard library s hashtable. Your class overrides equals but not hashcode. i. A lookup operation may not terminate. ii. A lookup operation may not find a value that is actually in the table. iii. Both (i) and (ii). iv. Neither (i) nor (ii). (d) You write a really bad hash function that causes all objects to initially hash to the same bucket. i. A lookup operation may not terminate. ii. A lookup operation may not find a value that is actually in the table. iii. Both (i) and (ii). iv. Neither (i) nor (ii). Note: As we announced during the exam, what we meant is what problem would be caused by the error in each problem, assuming there are no other errors (i.e., without this problem, everything would work correctly). (a) i (b) ii (c) ii (d) iv

6. (11 points) Don t miss part (b). (a) The Java code below provides an adjacency-list representation for a directed graph where the n nodes are labeled with the numbers 0, 1,... n 1. Complete the started-for-you method printdoubleedges so that it prints one line for each pair of nodes i and j where there is an edge from i to j and an edge from j to i. Do not print self-edges (so i is not equal to j). The line printed should have the lesser number first, so we might see an output line like 13 17 but not 17 13. No line should be printed twice. You should need somewhere around 10 lines of code not necessarily exactly 10, but to give you a sense if you are writing far too much or far too little. public class ListNode { public int x; public ListNode next; public class Graph { // Adjacency list representation with n nodes where n is also the array length. // Out edges for node i are in array index i. private ListNode [] adjacencylists; public Graph() { //... constructor not shown; assume it is correct private void printpair(int i, int j) { System.out.println(i + " " + j); public void printdoubleedges() { for(int i=0; i < adjacencylists.length; i++) { ListNode dests = adjacencylists[i]; while(dests!= null) { // YOUR CODE GOES HERE (b) Give a tight asymptotic worst-case running-time bound for your code in terms of V the number of nodes, E the number of edges, and d the maximum out-degree of any node. (a) if(dests.x > i) { ListNode dest_list = adjacencylists[dests.x]; while(dest_list!= null) { if(dest_list.x == i) { printpair(i, dests.x); break; // not necessary for correctness dest_list = dest_list.next; dests = dests.next; (b) On next page

(b) O( V + d E ) (As announced during the exam, the question was poorly worded: we want the running time of printdoubleedges, including the time for the provided code.) We also gave full credit for O(d 2 V ) this bound comes from a more direct adding of the nested loops, but it is slightly less tight. (Lastly, we gave full credit for answers consistent with alternate algorithms in part (a).)

7. (27 points) These three questions about graphs all have the same subparts. Note that for parts (iii), (iv), and (v), your answer should be in terms of an arbitrary k, not assuming k = 4. (a) Suppose a directed graph has k nodes, where each node corresponds to a number (1, 2,..., k) and there is an edge from node i to node j if and only if i < j. i. Draw the graph (using circles and arrows) assuming k = 4. ii. Draw an adjacency matrix representation of the graph assuming k = 4. iii. In terms of k, exactly how many edges are in the graph? iv. Is this graph dense or sparse? v. In terms of k (if k is relevant), exactly how many correct results for topological sort that does this graph have? (b) Suppose a directed graph has k nodes and every possible edge except there are no edges from nodes to themselves i. Draw the graph (using circles and arrows) assuming k = 4. ii. Draw an adjacency matrix representation of the graph assuming k = 4. iii. In terms of k, exactly how many edges are in the graph? iv. Is this graph dense or sparse? v. In terms of k (if k is relevant), exactly how many correct results for topological sort that does this graph have? (c) Suppose a directed graph has k nodes, where one special node has an edge from itself to every other node except itself and there are no other edges at all in the graph. i. Draw the graph (using circles and arrows) assuming k = 4. ii. Draw an adjacency matrix representation of the graph assuming k = 4. iii. In terms of k, exactly how many edges are in the graph? iv. Is this graph dense or sparse? v. In terms of k (if k is relevant), exactly how many correct results for topological sort that does this graph have? On next page

(a) i. ii. (Note the rows and columns can be permuted) F T T T F F T T F F F T F F F F iii. (k 1)k/2 iv. dense v. 1 (b) i. ii. (Note the rows and columns can be permuted) iii. k 2 k iv. dense v. 0 F T T T T F T T T T F T T T T F (c) i. ii. (Note the rows and columns can be permuted) F T T T F F F F F F F F F F F F iii. k 1 iv. sparse v. (k 1)!, i.e., 1 2... (k 1).

8. (9 points) Dijkstra s algorithm for computing lowest-cost paths from a single source node is always correct for graphs without negative-cost edges. If a graph has negative-cost edges, the algorithm might or might not give the right answer. For each directed graph below: If running Dijkstra s algorithm with start node A produces the correct lowest-cost path to every other node, then say correct. Else list all nodes for which Dijkstra s algorithm produces the wrong path. For all such nodes, write the path Dijkstra s algorithm produces and write the correct lowest-cost path. You do not need to show your work. (a) (b) (a) correct (b) The path to node D should be A-B-D, but Dijkstra computes A-C-D. The path to node E should be A-B-D-E, but Dijkstra computes A-C-D-E.

An extra page in case you find it useful