Docsity

CS211 Spring 2005 Prelim Exam 2, Study notes of Computer Science

Toggle sidebar
bg1
CS211 Spring 2005
Prelim 2
April 19, 2005
Name NetId
Instructions
Write your name and Cornell netid above. There are 8 questions on 11 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 7 8 Σ
Score /15 /10 /20 /10 /15 /10 /5 /15 /100
Grader
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CS211 Spring 2005 Prelim Exam 2 and more Study notes Computer Science in PDF only on Docsity!

CS211 Spring 2005

Prelim 2

April 19, 2005

Name NetId

Instructions

Write your name and Cornell netid above. There are 8 questions on 11 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!

Score /15 /10 /20 /10 /15 /10 /5 /15 / Grader

  1. (15 points) Write a recursive binary search method to find an object v in a sorted array a of comparables. The procedure should return true if object v is found in the array, and false if it is not found. Be sure to handle special cases.

//lo and hi are end points of the search interval in the array public static boolean binarySearch (Comparable[] a, int lo, int hi, T v) {

  1. (20 points) Asymptotic Complexity

(a) (5 points) If f (n) = O(n log n) and g(n) = O(n log n), prove that f (n) + g(n) = O(n log n). Argue in terms of witness pairs.

(b) (5 points) Sort the following in order of increasing asymptotic complexity: O(n log n), O(n^2 log n), O(n(log n)^2 ), O(n), O(1), O(nn), O(n^1.^618 ), O(1. 618 n), O(log n), O(2n).

Complexity Ordering Function From The List Above least complex 2 3 4 5 6 7 8 9

most complex

(c) (5 points) What is the asymptotic complexity of the following code fragment? Give justification.

int count = 0; for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { if ((j % 2) == 0) { for (int k=i; k<n; k++) count++; } else { for (int l=0; l<j; l++) count++; } } }

  1. (10 points) List the sequence of nodes visited by depth-first and breadth-first traversals of the following graph starting at node A. When there is an arbitrary choice to make in either traversal, expand nodes in alphabetical order.

s s s s

s s s

s s s s

F G A J

D B C

K H E I

@

@@

(a) (5 points) Depth-First

(b) (5 points) Breadth-First

  1. (15 points) True or false?

(a) T F Quicksort has worse asymptotic complexity than mergesort. (b) T F Binary search is O(log n). (c) T F Linear search in an unsorted array is O(n), but linear search in a sorted array is O(log n). (d) T F Linear search in a sorted linked list is O(n). (e) T F If you are only going to look up one value in an array, asymptotic complexity favors doing linear search on the unsorted array over sort- ing the array and then doing binary search. (f) T F If all arc weights are unique, the minimum spanning tree of a graph is unique. (g) T F Binary search in an array requires that the array be sorted. (h) T F Insertion into an ordered list can be done in O(log n) time. (i) T F A good hash function is one which tends to distribute elements uni- formly throughout the hash table. (j) T F In practice, with a good hash function and non-pathological data, objects can be found in O(1) time if the hash table is large enough. (k) T F If a piece of code has asymptotic complexity O(g(n)), then at least g(n) operations will be executed whenever the code is run with pa- rameter n. (l) T F Given good implementations for different algorithms for some pro- cess such as sorting, searching, or finding a minimum spanning tree, you should always choose the algorithm with the better asymptotic complexity. (m) T F It is not possible for the depth-first and breadth-first traversal of a graph to visit nodes in the same sequence if the graph contains more than two nodes. (n) T F The maximum number of nodes in a binary tree of height H (H = 0 for leaf nodes) is 2H+1^ − 1. (o) T F In a complete binary tree, only leaf nodes have no children.

  1. (5 points) Enter the items A-K (in order) into the hash table given the hash values specified for each item.

hash(A) = 3 hash(B) = 1 hash(C) = 8 hash(D) = 1 hash(E) = 4 hash(F) = 1 hash(G) = 8 hash(H) = 7 hash(I) = 3 hash(J) = 8 hash(K) = 3

Hash Line 1st 2nd 3rd 4th 5th 6th 7th 0 1 2 3 4 5 6 7 8 9

  1. (15 points) Recall that the Enumeration interface is an older form of Iterator. An object of type Enumeration has methods

boolean hasMoreElements() V nextElement()

that correspond to the methods boolean hasNext() and V next(), respectively, of Iterator. Each of the classes java.util.Hashtable and java.util.Vector has a method elements() that returns an Enumeration of its data elements. The Enumeration of a Vector v enumerates the elements in the order in which they occur in the underlying array. For Hashtable, the elements are returned in no particular order. On the next page, define a class OrderedHashtable with methods put, get, containsKey, size, and elements that are asymptotically no less efficient than the corresponding methods of java.util.Hashtable, but for which the Enumeration is guaranteed to enumerate the elements in the same order in which they were in- serted into the OrderedHashtable. To add a new element x to the end of a Vector v, use v.add(x). Assume no duplicate elements are ever inserted. Hint. Store the data in a Vector, and use the Hashtable to store indices into the Vector.

(write answer on next page)