Total Score /15 /20 /30 /10 /5 /20 Grader

Similar documents
Total Score /15 /20 /30 /10 /5 /20 Grader

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

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

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

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

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

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

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

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

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

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

Prelim 2. CS 2110, 24 April 2018, 5:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Prelim 2 Solution. CS 2110, 24 April 2018, 5:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

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

Prelim 2 SOLUTION. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

Computer Science E-22 Practice Final Exam

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

Final exam. CS 2110, December 15, 2016, 9:00AM

Total Score /20 /20 /20 /25 /15 Grader

Prelim 2 Solution. CS 2110, 24 April 2018, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph.

Prelim 2, CS2110. SOLUTION

ext Total Score /20 /20 /20 /20 /20 /5 Grader

XC Total Max Score Grader

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

Prelim 2. CS 2110, April 26, 2016, 5:30 PM Total Question True/False Complexity Heaps Trees Graphs Max Score Grader

Prelim 2, CS :30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

Prelim 2 Solution. CS 2110, April 26, 2016, 5:30 PM

Prelim 2. CS 2110, 5:30 PM, November 17, 2016

Prelim 2. CS 2110, 24 April 2018, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph.

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

CS/ENGRD2110: Final Exam

Prelim 2. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs. Max Score Grader

CSE332 Summer 2012 Final Exam, August 15, 2012

Prelim 2 Solution. CS 2110, April 26, 2016, 7:30 PM

Second Examination Solution

Final Exam. CS2110 Spring May 12, Total. Max Score

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

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

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

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

Prelim 1. CS2110, October 2, 2014, 5:30 PM Extra Total Question TrueFalse Multiple Object Oriented

COS 226 Algorithms and Data Structures Fall Midterm

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

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 25: Review and Open Problems

Recitation 9. Prelim Review

CS171 Final Practice Exam

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

Final Examination CSE 100 UCSD (Practice)

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

CS 216 Fall 2007 Final Exam Page 1 of 10 Name: ID:

Exam 3 Practice Problems

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

CSE373 Fall 2013, Midterm Examination October 18, 2013

Course Review for Finals. Cpt S 223 Fall 2008

Exam Data structures DAT036/DAT037/DIT960

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

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

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

COS 226 Midterm Exam, Spring 2009

CS302 Data Structures using C++

CS211 Spring 2005 Prelim 1 March 10, Solutions. Instructions

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

CS171 Final Practice Exam

Homework Assignment #3 Graph

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

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

THREADS AND CONCURRENCY

COMP 250 Midterm #2 March 11 th 2013

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

Largest Online Community of VU Students

Exam Sample Questions CS3212: Algorithms and Data Structures

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

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

CS211 Spring 2005 Prelim 2 April 19, Instructions

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

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

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

Topics for CSCI 151 Final Exam Wednesday, May 10

CS 314 Exam 2 Spring 2018

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

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

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

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

CS 373: Combinatorial Algorithms, Spring 1999

CS 61BL Data Structures & Programming Methodology

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.

CS 104 (Spring 2014) Final Exam 05/09/2014

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

CS 455 Midterm Exam 2 Fall 2015 [Bono] Nov. 10, 2015

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

End-Term Examination Second Semester [MCA] MAY-JUNE 2006

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

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

CS 314 Exam 2 Spring

CS 161 Fall 2015 Final Exam

cs Java: lecture #6

COS 226 Algorithms and Data Structures Spring Midterm

10/31/18. About A6, Prelim 2. Spanning Trees, greedy algorithms. Facts about trees. Undirected trees

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

Transcription:

NAME: NETID: CS2110 Fall 2009 Prelim 2 November 17, 2009 Write your name and Cornell netid. There are 6 questions on 8 numbered pages. Check now that you have all the pages. Write your answers in the boxes provided. Use the back of the pages for workspace. Ambiguous answers will be considered incorrect. The exam is closed book and closed notes. Do not begin until instructed. You have 90 minutes. Good luck! 1 2 3 4 5 6 Total Score /15 /20 /30 /10 /5 /20 Grader 1

1. (15 points) You probably know the game of Towers of Hanoi. Here s a picture of a typical initial configuration: there is a stack of N disks on the first of three poles (call them A, B and C) and your job is to move the disks from pole A to pole B without ever putting a larger disk on top of a smaller disk. The game can be solved recursively using the code shown below. class Hanoi { public static void main (String args[]) { playhanoi(integer.parse(args[0]), A, B, C ); // Moves n disks from pole from to pole to using other as a // temporary place to hold n-1 disks static void playhanoi(int n, String from, String to, String other) { if (n > 0) { playhanoi (n-1, from, other, to); System.out.printf("Move one disk from pole %s to pole %d\n, from, to); playhanoi (n-1, other, to, from); (a) 5 points. If moving a single disk from one pole to another is the operation we re counting, give a formula for the cost of moving N disks in terms of the cost of moving N-1 disks. (b) 5 points. Solve the formula in (a), obtaining a polynomial that gives the cost. (c) 5 points. Simplify formula (b). Hint: Think about the binary representation of an integer. 2

2. (20 points) True or false? a T F Multicore machines have a few cores, usually 2 or 4 and perhaps as many as 16. Nonetheless, the complexity of comparison sorting on such a system is still at least O(n log n). b T F For a program to gain a speedup by exploiting concurrency on a multicore machine it must use threads c T F A heap data structure (as used in HeapSort) would be a fantastic choice for implementing a priority queue. d T F Items pop from a stack in last in, first out or LIFO, order e T F If you incorrectly implement the priority queue interface (your version of poll() has a bug and sometimes doesn t return the smallest element), Java s type checking will catch your mistake and the code won t compile. f T F If the same element is inserted multiple times into a Java set, the set will only contain a single instance of that element. g T F If the same element is inserted multiple times into a Java list, the list will only contain a single instance of that element. h T F When you refresh a GUI the user might not see the actual updated screen immediately. i T F Java won t garbage collect an object if there is any way to reference that object from some active thread in your program. j T F A cache typically contains some limited set of previously computed results and is used to avoid recomputing those results again and again. k T F If a method always takes exactly 10 minutes to compute something, we would say that it has complexity O(10mins). l T F The complexity of looking up an item in a balanced BST containing N items is O(log(N) ) m T F The complexity of looking up k items in a balanced BST containing N items is O(k log(n)), which is the same as O(log(N k )). n T F If you use a very badly chosen hash function, then looking up an item in a HashMap might have complexity much worse than O(1) o T F The worst case complexity of QuickSort is O(N 2 ) but normally, it runs in time O(N log N) p T F If a method does something that requires N 2 operations and then does something else that requires N log N operations but does it N/2 times, the complexity of the method is O(N 2 ) q T F Nested for loops always result in complexity O(N 2 ) r T F If Dog and Cat are two classes that extend class Animal, then if cleopatra is an instance of Cat, (Dog)((Animal) cleopatra).bark() will upcast cleopatra to Animal, then downcast to Dog, and then run its bark() method. Assume that only class Dog defines the bark method. s T F In a recursive procedure the base case does all the work. t T F One difference between Java and other languages is that in Java, type checking is used to create documentation, also known as Java Doc. 3

3. (30 points) Suppose we create a min-heap (of maximum size eight) by inserting five elements 9, 17, 5, 31, 2 in the order shown (leaving room for three more). Draw the resulting balanced binary heap (exactly as seen in class) and then fill in the vector with the elements in heap representation (again, using the vector representation of a heap seen in class). (a) 10 points. The heap: (b) 10 points. The corresponding vector (put a slash through empty elements): (c) 10 points. Now show the vector after we call poll() once, to extract the smallest element. 4

4. (10 points) Run Kruskal s and Prim s minimum spanning tree algorithms, as presented in class, by hand on the graphs shown below and draw the resulting graphs for us. These algorithms select edges to include as they run. Darken the selected edges and label them 1, 2, 3 so that we can see the order in which they were selected. Circle your edge numbers so that we can t confuse them with the graycolored edge weights we provided. (a) 5 points. Kruskal s Algorithm 11 (b) 5 points. Prim s Algorithm. Start at vertex G. 11 Start here! 5

5) 5 points. Run Dijkstra s algorithm on the graph below. Start Dijkstra s algorithm at node A and mark each node with the distance computed by Dijkstra s method. Be careful when writing the node distances so that we can read what you write and won t get confused by the gray edge weights we provided. 6

6. (20 points) Suppose that we represent a dungeon as an undirected graph. Each node is a room which may contain treasures. Adjacent rooms are stored as an adjacency list. Note that this graph could have cycles. Our goal is to print directions from a start room to the treasure. If we started in the ATRIUM and we were looking for a RING in the SECRET PASSAGE BEHIND THE WALL, our directions would look like this: From ATRIUM go to DUNGEON. From DUNGEON go to BROOM CLOSET. From BROOM CLOSET go to SECRET PASSAGE BEHIND THE WALL In SECRET PASSAGE BEHIND THE WALL is the RING you seek. Or, if the dungeon does not contain the RING: Sorry, master, I can t find you any RING. You can use helper methods with additional arguments and local variables, but do not create any class variables. LinkedList<E> is a pre-implemented doubly-linked list generic; it defines an addfirst(e e) and an addlast(e e) method, a size() method, a contains(e e) method that returns true or false, and a delete(e e) method. Iterating on such a list visits elements in order. a-i) 3 points. Read part (b). Explain how you will solve it here. Include details about which data structures you will use and how you will use them. Hint: Your master (e.g.: your grader) will know graph algorithms well. Focus your efforts not on explaining these algorithms, but explaining how you will implement them. If an algorithm has a name, use that name in your answer. Limit: 3 sentences. a-ii) 2 points. Would your answer to (a-i) change if we had told you that all Room-to-Room connections are of the same length (call it 1 meter) and then asked you to find the shortest path to the treasure? If not, explain why; if so, tell us what you would do differently. 7

b) 10 points. Now write a method, LinkedList<Room> findtreasurepath(string goal), that returns a LinkList giving the path to the treasure, where the first node is the starting room and the last node is the treasure room. If no path exists, the method returns null. (Hint: Your master suggests recursion!) class Room { // Don t add more class variables You may assume that these three are never null String name; // Name of this room ArrayList<String > treasures; // Treasures contained in this room. Empty list if none ArrayList<Room > adjacent; // Rooms reachable from this room. Empty list if none public LinkedList<Room> findtreasurepath(string goal) { 8

c) 5 points. Write a method, void printtreasurepath(linkedlist<room> path), that prints the treasure path in the format we specified earlier. It gets called with a result generated by findtreasurepath(). Once again, you can use helper methods and local variables but not additional class variables. (Hint: Even if you had trouble with part (b) you can still attempt part (c).) You may find string formatting (supported by String.format() and System.out.printf()) useful. For example, if the value of String name is Ken, String.format( Hi, %s!, name) would return this String: Hi, Ken!. System.out.printf( Hi, %s!\n, name) prints that line on the console, followed by a newline. public void printtreasurepath(string goal, LinkedList<Room> path) { 9