## CPSC 311: Analysis of Algorithms Information and Review Questions for Exam 2 Spring 2003

### General Information

The second exam will be on Thursday April 3. It will cover material from Chapters 10, 11, 12, 21, 22, and 23 in the text. It will be a closed book exam (no notes, books, or neighbors) except you will be allowed a one page cheat sheet (both sides) which must be turned in with the exam. There will be 5 or 6 questions worth a total of 100 points. Remember to show your work - partial credit will be given.

• Review Session: 7:00pm, Tuesday April 1, Room 113 HR Bright Bldg

### Material

All of the following are considered to be fair game:

• Reading. Chapters 10, 11 (except 11.5), 12 (except 11.4), 21 (except 21.4), 22 (except 22.5), and 23.

• Lectures. You are responsible for things that we went over that were either not covered in the text, or that were done differently than in the text.

• Homeworks. Know how to do all the problems on homeworks #5, #6, and #7. (The solutions to homework #7 will be available on Tuesday April 1.)
• Quizzes. Understand and know the answers to all the questions on Quizzes 6-8.

### Review Questions

Below are some suggested review exercises/problems for the exam. It is strongly recommended that you be able to do all of these problems. Solutions will not be handed out. Finally, be sure to check your email -- clarifications and/or explanations will be mailed to the class if needed.

1. Study Homeworks #5, #6, and #7 and Quizzes 6-8; variations of some of these questions will appear on the exam.

2. Data Structures (Chapt 10, 11, 12): Consider inserting the following keys, in this order, into a hash table of size m=13.

keys to insert (in this order): 9, 1, 22, 14, 27

• Suppose you use chaining with the hash function h(k) = k mod 13. Illustrate the result of inserting the keys above using chaining.
• Suppose you use open addressing with the primary hash function h1(k) = k mod 13. Illustrate the result of inserting the keys above using linear probing, i.e., h(k,i) = (h1(k) + i) mod 13.
• Suppose you use open addressing with hash functions h1(k) = k mod 13 and h2(k) = 1 + (k mod 12). Illustrate the result of inserting the keys above using double hashing, i.e., h(k,i) = (h1(k) + h2(k)*i) mod 13.

3. Graph Representation, DFS, BFS (Chapt 22):
• Given an adjacency list representation of a directed graph, how long does it take to compute the out-degree of every vertex? How long does it take to compute the in-degree of every vertex? (The out-degree of a vertex is the number of edges leaving it, and the in-degree is the number of edges entering it.)

Repeat the above analysis assuming the graph is represented by an adjacency matrix.

• One way to perform a topological sort of a directed acyclic graph (DAG) G=(V,E) is to repeatedly find a vertex of in-degree 0 (i.e., one with no incoming edges), output it, and remove it and all its outgoing edges from the graph.
• Explain how to implement this idea so it runs in O(V+E) time. Give pseudo-code and analyze the worst-case running time of your algorithm in terms of both V (number of vertices) and E (number of edges).
• Prove that your algorithm is correct.

4. Disjoint Sets (Chapt 21): Consider the set of elements U=(1,2, ..., n1). Give a sequence of n1 makeset operations, n1 union operations, and n2 find operations that take Theta(n1n2) time when we use simple unions without path compression (for the disjoint-set forest implementation). Explain. Draw the resulting disjoint-set forest.

How long does this same sequence of n1 + n1 + n2 operations take when we use weighted-unions without path compression (again, with the disjoint-set forest implementation)? Explain. Draw the resulting disjoint-set forest.

How long does this same sequence of n1 + n1 + n2 operations take when we use weighted-unions with path compression (again, with the disjoint-set forest implementation)? Explain. Draw the resulting disjoint-set forest.

5. Minimum Spanning Trees (Chapt 23): Let G=(V,E) be a connected, undirected, weighted graph. Know the analysis of Kruskal's and Prim's algorithms for finding a minimum spanning tree. You should be able to derive the worst-case running times of these algorithms in terms of both V (number of vertices) and E (number of edges).

Know the correctness proof of these algorithms. You should be able to produce these proofs on your own.