# 2003 F

*221,950*pages on

this wiki

In the following six problems, circle all of the choices that correctly answer the question. Cross out the remaining (incorrect) choices, and briefly explain why the item is wrong. (You need not explain correct answers.) Note that: If you circle or cross out incorrectly, you will be penalized 2 points each.

1. (4%) Which of the following algorithms are greedy?

- (a) Prim's algorithm for finding a minimum spanning tree
- (b) finding a longest common subsequence of two strings
- (c) Dijkstra's algorithm for solving the single-source shortest paths problem

2. (4%) Which of the following statements about trees are correct?

- (a) Given a set S of n real keys chosen at random from a uniform distribution over [a:b), a binary search tree can be constructed on S in O(n) expected time.
- (b) Given a connected, weighted undirected graph G in which the edge with minimum weight is unique, that edge belongs to every minimunm spanning tree of G.
- (c) Deleting a node from a binary search tree on n nodes take O(lgn) time in the worst case.

3. (4%) It is known that if a solution to Problem A exists, then a solution to Problem B exists also. Which of the following statements are correct?

- (a) Professor Lee has just produced a 1,000-page proof that Problem A is unsolvable. If his proof turns out to be valid, can we conclude that Problem B is also unsolvable?
- (b) Professor Chang has just produced a 10,000-page proof that Problem B is unsolvable. If the proof turns out to be valid, can we conclude that problem A is unsolvable as well?

4. (4%) Which of the following statements are correct?

- (a) 2-SATISFIABILITY problem is NP-complete.
- (b) The time complexity of any algorithm that solves the problem is an upper bound on the time complexity of the problem.
- (c) The lower bound of NP-complete problem is exponential if and only if NP≠P is proved.

5. (4%) On inputs for which both are valid, in the worst case:

- (a) breadth-first search asymptotically beats depth-first search.
- (b) insertion into an AVL tree asymptotically beats insertion into a 2-3-4- tree.
- (c) Dijkstra's algorithm asymptotically beats the Bellman-Ford algorithm at solving the single-source shortest paths problem.

6. (4%) Which of the following statements are correct?

- (a) If each operation on a data structure runs in O(1) amortized time, then n consecutive operations run in O(n) time in the worst case.
- (b) A graph algorithm with O(|E|log|V|) running time is asymptotically better than an algorithm with a O(|E|log|E|) running time for a connected, undirected graph G=(V,E).
- (c) For hashing an item into a hash table in which collisions are resolved by chaining, the worst-case time is proportional to the load factor of the table.

7. (4%) Prove that sorting 6 elements with a comparison sort requires at least 10 comparisons in the worst case.

8. (20%) A d-ary heap is like a binary heap, but (with no possible exception) nonleaf nodes have d children instead of 2 children.

- (a) (4%) How would you represent a d-ary heap in an array?
- (b) (4%) What is the height of a d-ary heap of n elements in terms of n ad d?
- (c) (4%) Give an efficient implementation of EXTRA-MAX in a d-ary max-heap. Analyze its running time in terms of n and d.
- (d) (4%) Give an efficient implementation of INSERT in a d-ary max-heap. Analyze its running time in terms of n adn d.
- (e) (4%) When might be better to use a d-ary heap instead of a binary heap?

9. (10%) Let E be a sequence of integers . The multiplicity of x in E is the number of times x appears in E. A number z is a majority in E if its multiplicity is greater than n/2. Given a sequence of numbers design an O(n)-time algorithm to find the majority in the sequence or determine that non exists.

10. (10%) The input is a list of n sorted integers and a number y. Design an O(n)-time algorithm to determine whether there are two elements of the list whose sum is exactly y.

11. (12%) Let A= and B= be two strings of characters. We assume that the characters come from a finite set (English characters, for example). We would like to change A character by character such that it becomes equal to B. We allow three types of changes (or edit steps), and we assign a cost of 1 to each: (i) insert -- insert a character into the string, (ii) delete --- delete a character from the string, and (iii) replace --- replace one character with a different character.

- (a) Design a dynamic-programming algorithm to find the minimum number of edit steps required to change A into B. (Please clearly define your notation.)
- (b) Analyze the running ime and space requirements of your algorithm (interms of big-O notation).

12. (10%) Write an O(V)-time algorithm to determine if a given undirected graph G=(V,E) contains a cycle.

13. (10%) A boolean array M[1..n,1..n] represents a square maze. If M[i,j]=TRUE, it is allowable to pass through the point (i,j); otherwise, the point (i,j) can not be included in any traversal from any starting point to be destination point. Write an algorithm to find a path, if one exists, from point (1,1) to point (n,n), and discuss the reasoning behind your design.