CS 360 Exam 3 Fall 2011 Solution 1. Given disjoint sets {1), {2), {3

CS 360 Exam 3
Fall 2011
Solution
1. Given disjoint sets {1), {2), {3), {4), {5), {6), {7), {8), write a sequence of Merge operations
using union-by-size that will produce a tree with height 3 (so the tree has 4 levels), and
draw the resulting tree. Next perform a Find operation using path-compression that finds
the key value at the deepest node, and again draw the resulting tree.
Merge(1,2); Merge(3,4); Merge(2,4); Merge(5,6); Merge(7,8); Merge(6,8); Merge(4,8); Find(1)
8
8
7
6
4
5
3
7
2
6
4
5
3
2
1
1
2. For each recurrence below, write its solution T(n) as the simplest Θ function of n.
a. T(n) = 4 T(n/2) + n3
Θ(n3)
b. T(n) = 8 T(n/2) + n3
Θ(n3 lg n)
c. T(n) = 16 T(n/2) + n4
Θ(n4 lg n)
d. T(n) = 32 T(n/2) + n4
Θ(n5)
e. T(n) = 64 T(n/2) + n5
Θ(n6)
3. Trace the O(nlg 3)-time divide-and-conquer algorithm for polynomial multiplication when
n=2, P(x)= 2+3x, and Q(x)= 4+5x. Be sure to show precisely which three scalar products are
computed, and how to use these scalar products to obtain the product of the polynomials.
P(x) = R + Sx where R=2, S=3
Q(x) = T + Ux where T=4, U=5
Three scalar products:
R⋅T=2⋅4=8
S ⋅ U = 3 ⋅ 5 = 15
(R+S) ⋅ (T+U) = 5 ⋅ 9 = 45
P(x) ⋅ Q(x) = 8 + (45 – 8 – 15)x + 15x2 = 8 + 22x + 15x2
4. Complete the weighted adjacency matrix and the weighted adjacency lists data structures
for the graph below. Arrange each adjacency list by neighbors in alphabetical order.
9
8
D
E
7
4
A
F
16
10
B
C
6
A
0
6
∞
7
∞
∞
A
B
C
D
E
F
3
5
B
6
0
5
4
16
∞
C
∞
5
0
∞
10
3
D
7
4
∞
0
9
∞
E
∞
16
10
9
0
8
F
∞
∞
3
∞
8
0
A
B
C
D
E
F
List
→ (B,6) → (D,7)
→ (A,6) → (C,5) → (D,4) → (E,16)
→ (B,5) → (E,10) → (F,3)
→ (A,7) → (B,4) → (E,9)
→ (B,16) → (C,10) → (D,9) → (F,8)
→ (C,3) → (E,8)
5. Draw both a breadth-first search tree and a depth-first search tree for the graph in problem 4,
starting at root vertex E, and assuming that each adjacency list is arranged alphabetically. Also
write the list of vertices both in BFS order and in DFS order.
BFS:
E, B, C, D, F, A
DFS:
E, B, A, D, C, F
D
E
F
D
E
F
A
B
C
A
B
C
6. Draw a minimum spanning tree for the graph in problem 4. Also show the order that edges
are added to the tree both when using Kruskal’s algorithm and when using Prim’s algorithm,
starting at root vertex E.
8
D
E
F
4
A
Kruskal:
3, 4, 5, 6, 8
3
B
6
C
Prim:
8, 3, 5, 4, 6
5
7. Draw a shortest paths tree for the graph in problem 4, starting at root vertex E, and show
the distance from E to each destination node. Also show the order that edges are added to
this tree when using Dijkstra’s algorithm.
9
D, 9
8
E, 0
7
A,16
4
B,13
F, 8
10
C,10
Dijkstra:
8, 9, 10, 4, 7
8. Complete the first recursive formula below so that it computes the length Dk (i, j) of a
shortest path from i to j that does not include any intermediate vertex greater than k, as in
Floyd’s algorithm. Also complete the second recursive formula so that it computes the
Boolean value Pk (i, j) = true if there exists a path from i to j that does not include any
intermediate vertex greater than k, and otherwise Pk (i, j) = false.
D0 (i, j) = 0
if i = j
D0 (i, j) = weight (i, j)
if i ≠ j and (i, j) ∈ E
D0 (i, j) = ∞
if i ≠ j and (i, j) ∉ E
Dk (i, j) = min [Dk–1 (i, j), Dk–1 (i, k) + Dk–1 (k, j)]
if k>0
P0 (i, j) = true
if i = j or (i, j) ∈ E
P0 (i, j) = false
if i ≠ j and (i, j) ∉ E
Pk (i, j) = Pk–1 (i, j) or [Pk–1 (i, k) and Pk–1 (k, j)]
if k>0
9. Write a pseudo-code O(n lg n)-time greedy algorithm for this Wedding Party problem:
Given n groomsmen with heights G[1…n] and n bridesmaids with heights B[1…n], pair
each groomsman j with a distinct bridesmaid k such that the following sum is minimized:
∑pairs (j,k) |G[j] − B[k]|. You may use any algorithms that we’ve discussed in class as
helper functions.
heap sort or merge sort the groomsmen by their heights G[1…n];
heap sort or merge sort the bridesmaids by their heights B[1…n];
for i = 1 to n
pair the groomsman with height G[i] and the bridesmaid with height B[i];
// that is, pair the ith shortest groomsman with the ith shortest bridesmaid
10. Trace the dynamic programming algorithm that computes the length of the longest
common subsequence of strings S = ”cadb” and T = ”abcd”. Also show how to build each
string that is a longest common subsequence.
0
1
2
3
4
0
0
0
0
0
0
T:
1
0
0
1
1
1
a
2
0
0
1
1
2
b
3
0
1
1
1
2
c
4
0
1
1
2
2
d
S:
c
a
d
b
Follow each yellow highlighted path from
the bottom-right cell to the top-left cell to
build these strings: “ab”, “ad”, “cd”.