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

Similar documents
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 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

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

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

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

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

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

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

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

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

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

Recitation 9. Prelim Review

Prelim 2 Solution. CS 2110, 24 April 2018, 7: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, April 26, 2016, 5:30 PM

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

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

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

Prelim 2, CS2110. SOLUTION

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

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

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

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

CS211 Spring 2005 Prelim 2 April 19, Instructions

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

Homework Assignment #3 Graph

CS/ENGRD2110: Final Exam

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

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

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

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

Data Structures Brett Bernstein

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

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

Graphs - II CS 2110, Spring 2016

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

Computer Science E-22 Practice Final Exam

Final Exam in Algorithms and Data Structures 1 (1DL210)

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

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

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?

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

Graphs - II CS 2110, Spring 2016

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

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

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

Data Structure and Algorithm, Spring 2013 Midterm Examination 120 points Time: 2:20pm-5:20pm (180 minutes), Tuesday, April 16, 2013

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

CS171 Final Practice Exam

GRAPHS Lecture 17 CS2110 Spring 2014

CS171 Final Practice Exam

Problem Pts Score Grader Problem Pts Score Grader

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

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

Design and Analysis of Algorithms - - Assessment

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

CSL 201 Data Structures Mid-Semester Exam minutes

CSE 373 Final Exam 3/14/06 Sample Solution

CISC 320 Midterm Exam

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

Data Structures Question Bank Multiple Choice

CMSC132, Practice Questions

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

Final Exam in Algorithms and Data Structures 1 (1DL210)

Review for Midterm Exam

ECE250: Algorithms and Data Structures Final Review Course

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

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY THIRD SEMESTER B.TECH DEGREE EXAMINATION, JULY 2017 CS205: DATA STRUCTURES (CS, IT)

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

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

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

Course Review for Finals. Cpt S 223 Fall 2008

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

Introduction to Algorithms May 14, 2003 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser.

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

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

CS61BL. Lecture 5: Graphs Sorting

CS211 Spring 2005 Prelim 1 March 10, Solutions. Instructions

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

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

INSTITUTE OF AERONAUTICAL ENGINEERING

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

1 (5pts) 2 (20 pts) 3 (20 pts) 4 (10 pts) 5 (15 pts) 6 (30 pts) Total NAME: CU ID: Recitation instructor/time

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

Exam Datenstrukturen und Algorithmen D-INFK

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

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY

Second Examination Solution

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

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018

COMP 251 Winter 2017 Online quizzes with answers

CS521 \ Notes for the Final Exam

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

Spanning Trees. Lecture 22 CS2110 Spring 2017

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

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

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

CSci 231 Final Review

Topics for CSCI 151 Final Exam Wednesday, May 10

Transcription:

NAME: NETID: CS2110 Spring 2015 Prelim 2 April 21, 2013 at 5:30 0 1 2 3 4 Total Score /1 /20 /41 /15 /23 Grader There are 5 questions numbered 0..4 on 8 pages. Check now that you have all the pages. Write your answers in the boxes provided. Use the back of the pages for workspace or for answers if you need more room. 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! Question 0. (1 points) Write your name and Cornell netid, legibly, on every page. 1

1. True- false questions (20 points) a T F Dijkstra s algorithm computes the all pairs shortest paths in a directed graph with positive edge weights. That is, running the algorithm a single time, starting from some single vertex, it will compute the min distance from x to y for all nodes x and y in the graph. b T F An occurrence of keyword this in a static method will cause a compile-time error. c T F A non-abstract subclass of an abstract class C must provide definitions for all the abstract methods of C. d T F Every directed acyclic graph (dag) has a unique topological sort. e T F A binary search tree to implement a set of ints is faster than a hash tables in practice. f T F The invariant of a loop has to be true until the loop condition is false then the invariant can be false. g T F If the same element is inserted several times into a Java List, the list will contain only a single instance of that element. h T F Insertion sort of an array of size n has worst-case running time O(n log n) i T F Breadth-first search uses a stack. j T F If X is an object of type A, and B is neither a supertype of A nor a subtype of A, then (B)X will cause X to be converted into an object of type B. This is a form of autoboxing. k T F If the edges of a connected undirected graph are all different, Prim s and Kruskal s algorithm construct the same spanning tree. l T F If a graph is drawn so that edges cross, the graph is not planar. m T F The complexity of inserting an item into a heap containing N items is O(log(N)) n T F Quicksort used to sort a random vector with no repeats has expected performance O(n log n), but worst- case performance O(n*n). o T F When the weights on all edges of a graph are 1, Dijkstra s shortest- path algorithm does the same thing as DFS (depth- first search). p T F LinkedList<Integer> is not a subclass of LinkedList<Object>, but Integer[] is a subclass of Object[]. q T F Nested for loops always result in complexity O(N 2 ) r T F Suppose that X and Y are different objects of type A and that X.equals(Y) is false. Then X.hashcode()!= Y.hashcode(). That is, different objects have different hash codes. s T F Fields have initial values, but local variables do not. t T F The default layout manager for a JPanel is BorderLayout. 2

2. Short- answer questions (28 points). (a) (5 points) Write the definition of asymptotic complexity; that is write down when f(n) is O(g(n)) for functions f(n) and g(n). (b) (5 points) Prove that n + 100 is O(n) (c) (5 points) The preorder of a tree with 5 nodes is A B C E D. The inorder is B A E C D. Draw the tree: 3

(d) (5 points) Consider hashing using open addressing (not chaining, which uses linked lists). When removing an value, the array element it occupies is not set to null; instead, a flag is set to indicate that the element is no long in the set. Explain why in one or two sentences (what would go wrong if we set the array element to null?) (e) (5 points) List the sequence of nodes visited by BFS (the breadth- first search algorithm) starting at A of the following graph. When a choice of nodes has to be made, choose them in alphabetical order A G C B F H E D Put the nodes in the order they are visited here: (f) (5 points) For the graph in part (e), state the order of nodes visited by DFS (the recursive depth- first search algorithm, not the iterative one), starting at A. When a choice of nodes has to be made, choose them in alphabetical order. Put the nodes in the order they are visited here: (g) (5 points) Selectionsort, to sort array b[0..b.length- 1], has this invariant: b[0..k- 1] is sorted and every value of b[0..k- 1] <= every value of b[k..] Write down the body of the loop. We expect it to be at the same level of abstraction as we have described it in lecture. We do not want to see an inner loop. 4

(h) (6 points) An array segment b[h..k] contains weights on edges of a graph. The weights can be negative, 0, or positive. We want a loop with initialization that swaps the values so that all the negative weights are to the left and all the positive weights to the right. Below is the pre- and post- condition for the loop. Below the pre- and post- condition, draw 3 of the 4 possible loop invariants that arise by combining the pre- and post- condition. You do not have to put variables in to mark boundaries. Do not write the initialization or the loop. pre: b h k h k? post: b < 0 0 > 0 5

3. (15 points) Create a min- heap of ints (of maximum size eight) by inserting five values 60, 7, 10, 20, - 5 in the order shown (leaving room for three more). Draw the resulting heap first as a tree (exactly as seen in class) and then as it is stored in an array (or ArrayList). (a) 5 points. The heap: (b) 5 points. The corresponding array (or ArrayList) (put a slash / through empty elements): (c) 5 points. Suppose poll() is now called once. Put the polled value here and below, show the array (or ArrayList) after that call on poll(). 6

4. (23 points) Recursion and asymptotic complexity. For this question, consider trees whose nodes, of class Node, contain three fields: value: the value at this node. Its type is some class not a primitive type left: left subtree (null if empty left subtree) right: right subtree (null if empty right subtree) (a) (6 points) Consider a binary search tree (BST) implemented using class Node. Unfortunately, the person who created a BST used > instead of < for comparison, so the tree got put in kind of backward. For example, the inorder traversal of one BST should have been (1, 3, 4, 5, 6, 9) but was (9, 6, 5, 4, 3, 1). Write method reverse, below, which can be used to rectify the situation. You can use the high- level statement Swap b and c to swap the values of two variables b and c. Hint: values in the nodes should not be changed or compared with each other. /** Precondition: t is a binary search tree. * If t!= null, change as many nodes of t as necessary to reverse * the inorder traversal of the tree. */ public static void reverse(node t) { } (b) (6 points) Write the body of function equals, given below. /** Return true iff tree t equals tree s meaning they have the same shape and same values in their corresponding nodes. */ public static boolean equals(node s, Node t) { } 7

(c) (5 points) Suppose we use this function equals to test equality of M balanced BSTs each containing N nodes. That is, we want to test whether all these M balanced BSTs are identical. What is the worst- case complexity of the resulting program, expressed in terms of N and M? Justify your answer by briefly explaining how you arrived at it. (d) (6 points). Below is a version of depth- first search, explained at a high level of abstraction. The specification is correct it is what we want but the algorithm has errors. Fix them. You may explain what is wrong below the algorithm and cross off things in it. Just keep things legible. /** Node u is unvisited. Visit all nodes that are REACHABLE from u. */ public static void dfs(int u) { } for all edges (u, v) leaving u: } if (v is unvisited) { visit node v; dfs(v); visit node u 8