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

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

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

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

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, CS :30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

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

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

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

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

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

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

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

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

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

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

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

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

Prelim 2, CS2110. SOLUTION

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

Prelim 1. CS 2110, 13 March 2018, 5:30 PM Total Question Name Short answer

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

Prelim 1. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

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

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

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

Prelim 1. CS 2110, 14 March 2017, 7:30 PM Total Question Name Short answer. OO Recursion Loop invariants Max Score Grader

Prelim 1, Solution. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

Prelim 1. CS 2110, March 15, 2016, 5:30 PM Total Question Name True False. Short Answer

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

Prelim 1. CS 2110, March 15, 2016, 7:30 PM Total Question Name True False. Short Answer

Prelim 1. CS 2110, 14 March 2017, 5:30 PM Total Question Name Short answer. OO Recursion Loop invariants Max Score Grader

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

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

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

XC Total Max Score Grader

Homework Assignment #3 Graph

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

CSE332 Summer 2012 Final Exam, August 15, 2012

Data Structures Brett Bernstein

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Recitation 9. Prelim Review

Graphs - II. Announcements. Where did I leave that book? Where did I leave that book? Where did I leave that book? CS 2110, Fall 2016

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

CS211 Spring 2005 Prelim 2 April 19, Instructions

CS 251, LE 2 Fall MIDTERM 2 Tuesday, November 1, 2016 Version 00 - KEY

COS 226 Algorithms and Data Structures Fall Final

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

CS/ENGRD2110: Final Exam

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

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

CPSC 331 Term Test #2 March 26, 2007

CS/ENGRD2110: Prelim 2 SOLUTION

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

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

Final Examination CSE 100 UCSD (Practice)

CMSC132, Practice Questions

CS 314 Final Fall 2012

CS 170 Second Midterm ANSWERS 7 April NAME (1 pt): SID (1 pt): TA (1 pt): Name of Neighbor to your left (1 pt):

Course Review for Finals. Cpt S 223 Fall 2008

SORTING. Insertion sort Selection sort Quicksort Mergesort And their asymptotic time complexity

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

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

Algorithms. Algorithms. Algorithms 4.3 MINIMUM SPANNING TREES

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

Points off Total off Net Score. CS 314 Final Exam Spring 2016

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

Computer Science E-22 Practice Final Exam

Material covered. Areas/Topics covered. Logistics. What to focus on. Areas/Topics covered 5/14/2015. COS 226 Final Exam Review Spring 2015

Problem Score Maximum MC 34 (25/17) = 50 Total 100

CSE 100: GRAPH ALGORITHMS

CS200 Final Exam Fall 2007

GRAPHS Lecture 17 CS2110 Spring 2014

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

n 2 ( ) ( ) + n is in Θ n logn

( ). Which of ( ) ( ) " #& ( ) " # g( n) ( ) " # f ( n) Test 1

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE. Sample Final Exam

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

1 / 20 2 / 20 3 / 20 4 / 20 5 / 20

Cornell University Computer Science 211 Second Preliminary Examination 18 April 2006

COS 226 Algorithms and Data Structures Fall Midterm

PROGRAMMING IN C++ (Regulation 2008) Answer ALL questions PART A (10 2 = 20 Marks) PART B (5 16 = 80 Marks) function? (8)

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

Topics for CSCI 151 Final Exam Wednesday, May 10

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

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

CS4800: Algorithms & Data Jonathan Ullman

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions:

n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο

n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn)

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

Final Exam. COMP Summer I June 26, points

Midterm solutions. n f 3 (n) = 3

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

CS171 Final Practice Exam

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

Spanning Trees 4/19/17. Prelim 2, assignments. Undirected trees

Transcription:

Prelim 2 CS 2110, 24 April 2018, 5:30 PM 1 2 3 4 5 6 7 Total Question Name Short Heaps Tree Collections Sorting Graph answer Max 1 16 10 20 11 18 24 100 Score Grader The exam is closed book and closed notes. Do not begin until instructed. You have 90 minutes. Good luck! Write your name and Cornell NetID, legibly, at the top of every page! There are 6 questions on 8 numbe pages, front and back. Check that you have all the pages. When you hand in your exam, make sure your pages are still stapled together. If not, please use our stapler to reattach all your pages! We have scrap paper available. If you do a lot of crossing out and rewriting, you might want to write code on scrap paper first and then copy it to the exam so that we can make sense of what you handed in. Write your answers in the space provided. Ambiguous answers will be conside incorrect. You should be able to fit your answers easily into the space provided. In some places, we have abbreviated or condensed code to uce the number of pages that must be printed for the exam. In others, code has been obfuscated to make the problem more difficult. This does not mean that it s good style. Academic Integrity Statement: I pledge that I have neither given nor received any unauthorized aid on this exam. I will not talk about the exam with anyone in this course who has not yet taken Prelim 2. (signature) 1. Name (1 point) Write your name and NetID, legibly, at the top of every page of this exam. 1 of 8

2. Short Answer (16 points) (a) True / False (8 points) Circle T or F in the table below. (a) T F All search algorithms take O(n) time if the list is already sorted. (b) T F If a class implements Iterable, it must also implement Iterator. (c) T F It is best to store dense graphs in an adjacency matrix and sparse graphs in an adjacency list. (d) T F LinkedList<String> is not a subtype of LinkedList<Object> even though String extends Object. (e) T F An algorithm s time complexity can be both O(n) and O(n 2 ). (f) T F HashMap<String, int> mymap= new HashMap<>(); is valid Java. (g) T F Consider an undirected graph with set of edges E and set of vertices V. It is a tree iff E = V and there are no cycles. (h) T F Any directed graph can be topologically sorted. (b) GUI (3 points) What three steps are requi to listen to an event in a Java GUI? (c) Red-Black Trees (5 points) What is the full -black tree invariant? 2 of 8

3. Heaps (10 Points) (a) 6 points State below each of the trees whether it is a valid min-heap (the priorities are the values). If it is not, state which invariant is unsatisfied. 3 5 11 7 4 6 13 13 15 13 8 5 22 7 15 15 12 16 22 1: 2: 3: (b) 4 points The int array b below corresponds to a max-heap of integers in which the values are the priorities. Suppose the heap has size 7 and b[0..6] contains these 7 integers. b = [22 9 15 7 6 10 8] Draw the state of b after calling b.poll(). Give your solution as an array. 4. Trees (20 Points) (a) 2 points What is the worst case time complexity for inserting into a: Binary Search Tree with n nodes. Red Black Tree with n nodes. (b) 4 points Write down the order in which this tree s nodes are printed in preorder, inorder, and postorder traversals. A E Z N C H K 3 of 8

(c) 4 points Draw the binary search tree that results from inserting the following nodes, one by one, into an initially empty tree: 5, 7, 3, 2, 9, 6. (d) 10 points Class PhD, to the right, represents a PhD and their PhD committee. In the tree to the right, Alice is generation 0. Her committee members are Bob, Carol, and Dan; they are generation 1. Bob has a committee of 1, Eve, and Dan has a committee of 2, Frank and Grace. Eve, Frank, and Grace are generation 2. Etcetera. Complete method oldestgen below. public class PhD { private String name; private Set<PhD> committee;... Alice / \ Bob Carol Dan \ Eve Frank Grace /** Return the maximum number of generations that this PhD can trace * back in their committee history * * For example, in the tree above to the right, * Alice.oldestGen() = 2 // Tree contains some of Alice's committee * // member's committee member(s) * Bob.oldestGen() = 1 // Tree contains some committee member(s) for Bob * Carol.oldestGen() = 0 // Tree contains no committee members for Carol * Frank.oldestGen() = 0 // Tree contains no comittee members for Frank*/ public int oldestgen() { 4 of 8

5. Collections and Interface (11 Points) Class MapBag E, decla below, implements a bag like a set but elements can be in it more than once. Implement methods contains, add, and remove. Assume all other methods requi by Collection have already been implemented. /** An instance is a bag --an unorde collection in which there can be * multiple instances of the same element. */ public class MapBag<E> implements Collection<E> { private int size; private Map<E, Integer> map; /** Constructor: An empty bag. */ public MapBag() { map= new HashMap<E, Integer>(); /** Return whether this bag contains ob. */ public boolean contains(e ob) { // Number of elements in this bag. // Mapping of element in this bag to // its number of occurrences (which is > 0) /** Add e to the bag. Return whether the map was modified in any way. */ public boolean add(e e) { /** Remove ob from the map if present. Return whether it was removed. */ public boolean remove(e ob) { 5 of 8

6. Sorting (18 Points) (a) 4 points. Consider the following class File. A file processing system requires files to arrive for processing in decreasing order of priority. If two files have the same priority, process the larger one first. Complete method compareto(...); it must implement the standard compareto specification. /** An instance represents a comparable file object*/ public class File implements Comparable<File> { private String name; private int priority; private int size;... /** Compare this with ob, as in the standard compareto specification. */ @Override public int compareto(file ob) { (b) 6 points. Using method compareto(), complete method insertionsort(), below. That is, complete the comment that begins the body of the for-loop as a high-level statement saying what is done to ensure that the loop invariant remains true. Then implement that high-level statement. public void insertionsort(file[] b) { // invariant: b[0..i-1] is sorted for (int i= 0; i < b.length; i= i+1) { (c) 2 points. What is the worse-case time and expected time of insertion sort? (d) 6 points State the tightest expected space complexity of quicksort, mergesort, and selectionsort. For quicksort, assume it is the version that uces the space as much as possible. quicksort: mergesort: selectionsort: 6 of 8

7. Graphs (24 Points) A coloring of a graph is an assignment of colors to the nodes of a graph. An edge is properly colo if the two nodes it connects have different colors. A proper coloring is a coloring in which all edges are properly colo. (a) 3 points Which of the following are proper colorings? gray gray blue blue blue blue gray (c) 4 points The chromatic number of a graph G, written as (χ G ), is the minimum number of colors needed to properly color the graph. For each of the graphs below, write down χ G and properly color the graph using χ colors. To assign a color to a node, write the name of the color next to the node, as in part (a). (d) 4 points Draw a graph whose chromatic number is the same as the number of nodes in the graph (every node must get a different color). Use at least 4 nodes. 7 of 8

(e) 7 points Complete method isproper, below. The graph is undirected and connected. Starting with all nodes unchecked and u some node of the graph, the call isproper(u) will determine whether the graph is properly colo. Use the English phrases n was checked and check n to test whether a node n has already been traversed and to mark it as checked, respectively. Also, use a loop for each neighbor m of n. /** Let E be the set of edges that are reachable along unchecked paths from u. * Return true iff all edges in E are properly colo. * Precondition: All edges leaving a checked node are properly colo. */ static public boolean isproper(node n) { (f) 1 point. Method isproper traverses the graphs in some fashion. It is similar to one of the following. Circle the one to which it is similar. dfs. bfs. shortest path. prim. kruskal. (g) 5 points. Two parts of the invariant of the shortest path algorithm are: There are no edges from the settled set to the far-off set. For each node u in the settled set, d[u] is the length of the shortest path from the start node to node u. State (1) the third part of the invariant and (2) the theorem that is proved about the invariant. 8 of 8