Analysis of Algorithms, 91 s3
Total Page:16
File Type:pdf, Size:1020Kb
UML CS 91.404 Final Exam Fall, 2004
NAME:______
FINAL EXAM
This exam is open books, notes and closed neighbors, calculators
The upper bound on exam time is 2.5 hours.
Please put all your work on the exam paper.
(Partial credit will only be given if your work is shown.)
Good luck!
PLEASE REMEMBER TO FILL OUT AN ON-LINE COURSE EVALUATION at http://teaching.cs.uml.edu/evaluations
1 of 15 UML CS 91.404 Final Exam Fall, 2004
1: (8 points) Function Order of Growth
(a) (4 points) List the 3 functions below in nondecreasing asymptotic order of growth:
nlg n nlg4 n 32 log9 n
1) 2) 3)
smallest largest
Given:
2 log9 n 4 1) f1(n) nlgn 2) f2 (n) 3 3) f3 (n) O(nlg n)
Circle TRUE or FALSE for each statement below & briefly justify your choice.
(b) (2 points) f2 (n) ( f3 (n))
TRUE FALSE
(c) (2 points) f2 (n) O( f1(n))
TRUE FALSE
2 of 15 UML CS 91.404 Final Exam Fall, 2004
2: (6 points) Recurrence
Find a tight upper and lower bound on the closed-form solution for the following recurrence:
3 n 17T 5n 2 n k T(n) 4 (1) n k where k is a small integer. That is, find a function g(n) such that T(n) (g(n)) .
3 of 15 UML CS 91.404 Final Exam Fall, 2004
3: (10 points) Sorting
Algorithm HYBRID below is a variation on BUCKET-SORT that calls COUNTING-SORT.
The algorithm’s inputs are: - An array A of integers; - The number of buckets b; - An integer parameter c.
HYBRID(A,b,c) for i 1 to length[A] do A[i] A[i]modc for i 0 to b 1 do for j 1 to length[A] do B[i][ j] 0 BNum[i] 0 for i 1 to length[A] do r (RANDOM (0,32768))modb BNum[r] BNum[r]1 B[r][BNum[r]] A[i] for i 0 to b 1 ▷Pass row i of B to COUNTING-SORT and store sorted result D back into row i of B. do B[i] COUNTING SORT(B[i], D, ______)
a) (6 points) Fill in the blank for what should be passed as the third argument to COUNTING-SORT and briefly explain your choice. If there is no legal choice, say so and explain why.
4 of 15 UML CS 91.404 Final Exam Fall, 2004 b) (4 points) Does the pseudocode resulting from (a) with the blank filled in cause the entries of A to appear in B in nondecreasing order? Briefly explain your reasoning.
5 of 15 UML CS 91.404 Final Exam Fall, 2004
4: (14 points) Red-Black Trees
Define a 3-red-black tree to be a red-black tree in which property (4) is modified as follows:
Instead of “if a node is red, then both its children are black”, it is “if a node is red and its parent is red, then both of its children are black.”
Consider the impact of this change. In particular, recall that Lemma 13.1 holds for red-black trees:
Lemma 13.1: A red-black tree with n internal nodes has height at most 2lg(n+1).
Is Lemma 13.1 true for a 3-red-black tree?
Circle YES or NO & justify your choice.
YES NO
6 of 15 UML CS 91.404 Final Exam Fall, 2004
this page intentionally left blank for work
7 of 15 UML CS 91.404 Final Exam Fall, 2004
5: (10 points) Hash Tables
MysteryHash(k,m) if k m then return k k else return MysteryHash ,m m
(6 points) Circle the bound that best describes the worst-case asymptotic running time of MysteryHash and briefly justify your choice:
(a) (k) (b) (m) (c) (km)
(d) (lgk) (e) (lgm) (f) (k2)
(g) (m2) (h) (klgk) (i) (klgm)
(j) None of the above running times is a valid bound.
8 of 15 UML CS 91.404 Final Exam Fall, 2004
(4 points) Can MysteryHash be used as a hash function for a hash table with m slots?
Circle YES or NO & briefly justify your choice.
YES NO
9 of 15 UML CS 91.404 Final Exam Fall, 2004
6: (6 points) Shortest Paths
In order to find the shortest path in the graph below from vertex A to vertex F, which of the algorithms listed below solves the problem using the smallest worst-case asymptotic running time?
4 4 B E A 4 4 4 4 D 4 4 4 F C 4 (Assume an adjacency list representation of the graph.)
Circle one choice below. Briefly justify your answer. a) DIJKSTRA b) MST-PRIM c) BFS d) MST-KRUSKAL e) DFS f) TOPOLOGICAL-SORT g) None of the above algorithms can solve the problem
10 of 15 UML CS 91.404 Final Exam Fall, 2004
7: (6 points) Data Structures
Consider the following task for a collection C of items in which each item has a key value and satellite data. The task is to make many repetitions of a call to an operation DeleteAll. DeleteAll accepts, as inputs, the collection C plus two key values k1 and k2. DeleteAll(C, k1, k2) deletes from C all items whose key values are in between k1 and k2 (inclusive). It returns a list of the items that are deleted.
Assume that: - each key value is an integer; - there is a total ordering defined on the key values; - satellite data for each item consists of 5 integer values.
In the list below, circle the representation of C that allows the entire task (including all repetitions) to be accomplished with the smallest worst-case asymptotic running time, while only using an amount of storage that is linear in the number of elements in the collection.
Circle one choice below. Justify your answer. a) STACK b) QUEUE c) LINKED LIST (singly linked) d) BINARY SEARCH TREE e) RED-BLACK TREE f) LINKED LIST (doubly linked) g) HASH TABLE h) HEAP (using array implementation) i) None of the above data structures can solve the problem.
11 of 15 UML CS 91.404 Final Exam Fall, 2004
8: (40 points) Design an Algorithm:
Given a Binary Search Tree T and a key value k, design an algorithm to efficiently locate and return all the records with key value equal to k. (Note that you will need to select an appropriate data structure for storing the records.)
The next 3 pages ask you to provide pseudocode, correctness justification and an upper bound on the worst-case asymptotic running time.
12 of 15 UML CS 91.404 Final Exam Fall, 2004
a) (13 points) Pseudocode.
13 of 15 UML CS 91.404 Final Exam Fall, 2004 b) (13 points) Correctness.
14 of 15 UML CS 91.404 Final Exam Fall, 2004 c) (14 points) Analysis: Provide as tight an upper bound on the worst- case asymptotic running time as you can.
Hint: You might want to express the running time as a function of the height h of the tree and the number of records found that have key values equal to k.
15 of 15