HW3

advertisement
CS 583: Data Structures and Analysis of Algorithms: Fall 2006: D. Kaznachey
Home Work #3
1. (5 points) Suppose there is no way to represent the key -. Rewrite the BinomialHeap-Delete procedure to work correctly in this situation (see exercise 19.2-6) Ensure
and prove that the algorithm still runs in O(lg n) time.
Solution:
Binomial-Heap-Delete(H, x)
1 y = x
2 z = p(y)
// Bubble up to the top of the tree
3 while z <> NIL
4
<exchange key[y] with key[z]>
5
<exchange satellite info y with z>
6
y = z
7
z = p[y]
// Delete y from H
8 H2 = Make-Binomial-Heap()
// Remove y from the tree, and
// prepare y’s children as the linked list B0, B1, ... , Bk-1
9 <reverse the order of y’s children>
10 head[H2] = <head of y’s children>
11 H = Binomial-Heap-Union(H,H2)
Steps 1-7 will take at most O(lg n) to iterate through the tree’s height. Steps 8, 10 runs in
(1) time. Step 9 runs in O(lg n) as the number of children is k < lg n. Finally, step 11
runs in O(lg n) time as the overall number of nodes < n. Hence, we have the running time
of the above algorithm O(lg n).
2. (5 points) The square of a directed graph G=(V,E) is the graph G2 = (V, E2) such that
(u,w)  E2 if and only if for some v  V, both (u,v)  E, and (v,w)  E (see exercise
22.1-5) Write an efficient algorithm for computing G2 from G for adjacency matrix
representation. Analyze the running time of your algorithm.
Solution:
Make-Square-Graph(G, n)
// Allocate adjacency matrix for G2
1 for i=1 to n
2
for j=1 to n
3
G2[i][j] = 0
// Test all edges in G to create E2 (note that it is a directed
// graph, hence (u,v) <> (v,u))
5 for i = 1 to n
6
for j = 1 to n
7
if (G[i][j] = 1) // edge (i,j) in E
8
for k = 1 to n // include all (i,k) into E2
9
if (G[j][k] = 1)
10
G2[i][k] = 1
CS 583: Data Structures and Analysis of Algorithms: Fall 2006: D. Kaznachey
11 return G2
Assume |V| = n. Steps 1-3 take (n2) time. There are n2 iterations in 5-6. For iteration, in
the worst case, we will need n more iterations to test other edges. Hence, the overall time
is (n2) + O(n3) = O(n3).
3. (Bonus 1 point) Let e be a maximum-weight edge on some cycle of G = (V, E). Prove
that there exists a minimum spanning tree of G that does not include e. (See exercise
23.1-5).
Solution:
For simplicity, assume a cycle (C) in G contains three edges: e1 = (1,2), e2=(2,3),
e3=(1,3), so that we1 = max{we1, we2, we3}. A minimum spanning tree on G cannot
include all e1, e2, e3 as it would form a cycle and violate the tree property.
During the course of Generic-MST algorithm an edge from C will be considered for
inclusion in A if it contains a safe edge. According to Theorem 23.1, it can be a light edge
crossing a cut (S, V-S) that respects A.
The cut (S, V-S) therefore does not respect C. (Otherwise, no edge in C crosses the cut).
If e1 crosses the cut, it is easy to see that at least one more edge in C crosses the cut.
Since e1 has the maximum weight, it will not be chosen to be included in A. Note that,
this will be true even when some edges from C are already included in A. Hence, we can
select safe edges in such a way that they will never include e1. The above proof can be
readily generalized to a cycle of any size.
Download