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

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

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

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

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

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, 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. 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, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph.

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

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

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

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

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

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

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 1. CS2110, October 2, 2014, 5:30 PM Extra Total Question TrueFalse Multiple Object Oriented

Prelim 2, CS2110. SOLUTION

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

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

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

XC Total Max Score Grader

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

Recitation 9. Prelim Review

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

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

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

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

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

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)

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

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

CS/ENGRD2110: Final Exam

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

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

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

CS161 - Final Exam Computer Science Department, Stanford University August 16, 2008

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

Faculty of Science FINAL EXAMINATION

CS/ENGRD2110: Prelim 2 SOLUTION

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

CS211 Spring 2005 Prelim 2 April 19, Instructions

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

Second Examination Solution

Data Structures Brett Bernstein

Midterm Exam 2 CS 455, Spring 2015

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016

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

CSE 247 Data Structures and Algorithms Fall Exam I

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

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

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

CS 2604 Data Structures Midterm Summer 2000 U T. Do not start the test until instructed to do so!

CS490 Quiz 1. This is the written part of Quiz 1. The quiz is closed book; in particular, no notes, calculators and cell phones are allowed.

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

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

NAME: c. (true or false) The median is always stored at the root of a binary search tree.

(the bubble footer is automatically inserted into this space)

UCSD CSE 101 MIDTERM 1, Winter 2008

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

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

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

CS 314 Final Fall 2012

CS61BL: Data Structures & Programming Methodology Summer 2014

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

COS 226 Algorithms and Data Structures Fall Midterm

COS 226 Algorithms and Data Structures Fall Final

Quiz 2. Problem Parts Points Grade Grader. Total 120. Name: Recitation: Jayant 12 PM. Christina 10 AM. Jayant 1 PM. Jason 2 PM.

CS 314 Final Fall 2011

CS4800: Algorithms & Data Jonathan Ullman

CS 3114 Data Structures and Algorithms READ THIS NOW!

CS302 Data Structures using C++

Final Examination CSE 100 UCSD (Practice)

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

CSE 100 Practice Final Exam

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

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

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

CISC 320 Midterm Exam

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

Largest Online Community of VU Students

CSE 373: Practice Final

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

ECE242 Data Structures and Algorithms Fall 2008

Exam Sample Questions CS3212: Algorithms and Data Structures

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

CSE373 Fall 2013, Midterm Examination October 18, 2013

GRAPHS Lecture 19 CS2110 Spring 2013

Homework Assignment #3 Graph

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

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

CSE 21 Summer 2017 Homework 4

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

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

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

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

Transcription:

Prelim CS 110, April 6, 016, 5:0 PM 1 5 Total Question True/False Complexity Heaps Trees Graphs Max 10 0 0 0 0 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 at the top of every page! There are 5 questions on 10 numbered 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 considered incorrect. You should be able to fit your answers easily into the space provided. In some places, we have abbreviated or condensed code to reduce 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 its good style. Academic Integrity Statement: I pledge that I have neither given nor received any unauthorized aid on this exam. (signature) 1 of 10

1. True / False (10 points) Circle T or F in the table below. a) T F A bipartite graph is always planar. b) T F Suppose a and b are objects. If a.equals(b) evaluates to true, then a.hashcode() == b.hashcode() must also evaluate to true. c) T F Suppose a and b are objects. If a.equals(b) evaluates to false, then a.hashcode() == b.hashcode() must also evaluate to false. d) T F The vertices of a finite graph can be topologically sorted if and only if the graph is acyclic. e) T F In the worst case, insertion sort and quick-sort have the same run time. f) T F HashSet<String> is a subtype of HashSet<Object>, just as String[] is a subtype of Object[]. g) T F Method m() processes a list of size n using nested for loops. Therefore, the run time of m() is O(n ). h) T F If a graph will have thousands of vertices, each having at most 5 neighbors, you should implement the graph using a matrix rather than adjacency lists. i) T F A method that computes a result in O(n) time will always run faster than a method that computes a result in O(n ) time. j) T F A connected graph with n nodes has at least n 1 edges. of 10

. Complexity (0 points) (a) points For each of the functions f below, state the function g(n) such that f(n) is O(g(n)). g(n) should be as simple and tight as possible. For example, one could say that f(n) = n is O(n ) or O(n ), but the best answer, the answer we want, is O(n ). (i) f(n) = 5 log n + n g(n) = (ii) f(n) = n + 000n + g(n) = (iii) f(n) = 9n log n + n g(n) = (iv) f(n) = 100 + 5 n g(n) = (b) points Recall that we proved in recitation that f(n) = n + 6 is O(n). In a similar manner, prove that f(n) = n + 00n is O(n ). (c) points What is the simplest and tightest time-complexity class of the following function? For example, if the function takes n time, write O(n ). Be careful! Consider the complexity of all operations and function calls below. /** Return the max of the values in list. * Precondition: list.size() > 0 */ public static int max(linkedlist<integer> list) { int max= Integer.MIN_VALUE; int n= list.size(); for (int i= 0; i < n; i++) { } int current= list.get(i); if (current > max) max= current; } return max; of 10

(d) 6 points For each of the following tasks, state the expected and worst-case time complexity. If the expected and worst-case time complexities are different, describe a situation in which the task will take the worst-case time. (i) ( points) Use quick-sort to sort an array of size n. Expected run time: Worst-case run time: (ii) ( points) Search for a value in a sorted array of size n. Expected run time: Worst-case run time: (iii) ( points) Check if a hash set of size n contains a particular value. Expected run time: Worst case run time: (e) 1 points Suppose we need to choose a data structure to store a changing collection of values. For each of the usage patterns listed below, mark an X in the following table to indicate which data structure serves that usage pattern best in terms of average-case time complexity. There is exactly one best data structure for each usage pattern. Usage Pattern ArrayList LinkedList HashSet Balanced BST Heap (i) (ii) (iii) (iv) (v) (vi) The usage patterns: (i) ( points) Adding and removing elements from indices clustered near the end of the collection (ii) ( points) Processing the elements in sorted order (iii) ( points) Processing the elements in a priority order (iv) ( points) Retrieving elements at several unpredictable indices (v) ( points) Checking if the collection contains various values (vi) ( points) Removing certain elements while iterating through all elements (and preserving the ordering of the remaining elements). of 10

. Heaps (0 points) Consider creating a max-heap of ints by adding the following elements in the order presented:, 10, 5, -,, 71, 1 (a) 8 points Below, draw the resulting heap as a tree. Note: If you provide a valid heap for these values but not the one that arises from adding the elements in order, you get points and can still get full credit for the subsequent problems. (b) 6 points Draw the heap you provided as an array in the table below. 0 1 5 6 (c) points Now we change the value of the element with value 5 to -1. Repair the heap you presented using the operations from class and draw the resulting heap as a tree below. Note: If you provide a valid heap for these values but not the one that arises from performing this operation, you get 1 point and can still get full credit for the subsequent problem. (d) points Draw the heap you provided as an array in the table below. 0 1 5 6 5 of 10

. Trees (0 points) (a) points Is the tree above balanced? (b) 6 points Provide the pre-order, post-order, and in-order traversals of the tree above. Specifically, list the order in which the nodes of the tree are processed in each traversal. You should represent a node by the letter it contains, but please leave at least one space between letters for readability (e.g. a b c, not abc). (i) ( points) Pre-Order: (ii) ( points) Post-Order: (iii) ( points) In-Order: 6 of 10

(c) points Draw the BST (binary search tree) resulting from adding the following values one by one to an empty tree: e, h, c, g, a, f, d, b Note: If you provide a valid BST for these values but not the one that arises from adding the elements in order, you get 1 point. 7 of 10

(d) 8 points Now that you have practiced adding values to a BST, implement method add of class BSTNode below. /** An instance of this class represents a Binary Search Tree */ public class BSTNode { private int value; // the element of this BSTNode private BSTNode left; // left child of this BSTNode (null for an empty tree) private BSTNode right; // right child of this BSTNode (null for an empty tree) /** Constructor: a binary search tree containing only e */ public BSTNode(int e) { value= e; } /** Add e to this binary search tree */ public void add(int e) { } } 8 of 10

5. Graphs (0 points) (a) 10 points You are given a (weighted) directed graph, duplicated in the first column below for each subproblem so that you may mark it up with notes (which will be ignored by the graders). For each subproblem, write your answer in the second column, NOT THE FIRST COLUMN! When you need to choose among vertices to visit first, choose in alphabetical order. b 1 c d (i) ( points) In what order are the nodes visited by DFS, starting from vertex a? a 7 e 8 f 9 g 9 h b 1 c d (ii)( points) In what order are the nodes visited by BFS, starting from vertex a? a 7 e 8 f 9 g 9 h b 1 c d (iii) ( points) In what order are the nodes removed from the frontier in Dijkstra s algorithm, starting from vertex a? a 7 e 8 f 9 g 9 h 9 of 10

(b) 10 points The graph algorithms we have covered have applications beyond the problems for which they were originally intended. For example, although DFS and BFS are search algorithms, they are just ways to traverse a graph. So besides reachability, they can be used for distance calculation and cycle detection. And, topological sort can be used to determine shortest paths in DAGs. For each problem listed below, mark an X in the following table to indicate which graph algorithm solves the problem best in terms of worst-case time complexity. There is exactly one best graph algorithm for each problem. The problems: Problem Topological sort DFS BFS Dijkstra (i) (ii) (iii) (iv) (v) (i) ( points) You have a database of U.S. cities. For each pair of cities, it indicates whether a direct flight exists between them. You have to write a program to calculate the minimum number of connections needed to fly from one city to another. For example, you can t fly from Ithaca to San Francisco directly, but you can fly to Philadelphia and then to San Francisco, so the minimum number of connections is 1. (ii) ( points) You have a list of courses you want to take at Cornell. Some are prerequisites of others. You want to find a sequence in which to take these classes without violating any prerequisite requirement. (iii) ( points) You are intrigued by the concept of six degrees of separation : any two people on the planet are connected through at most five intermediate acquaintances. You want to test this theory at Cornell. You have a database that tells you whether any two Cornell students are friends. You want to find a student who is furthest away from you, i.e. the minimum number of intermediate friends needed to relate that student to you is greater than or equal to that of any other student. (iv) ( points) You are traveling on a desert. Your map gives the locations of oases, and you can calculate the distance between any two of them. You can walk only k miles without stopping at an oasis before you dehydrate, and every mile takes you an hour to hike. You are standing at the northernmost oasis. You want to determine how to most quickly reach the southernmost oasis without dehydrating. (v) ( points) You are a maze designer, and you want to make sure your preliminary design isn t too challenging. Write a program that, assuming there are no cycles, determines the length of the longest path from the maze s entrance that doesn t eventually lead to the maze s exit. 10 of 10