Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Homework 4

November 3, 2010

Question 1 - Exercise 17.4-3, 17-3.6


Exercise 17.4-3:
Notice that the i-th operation cannot cause the table to contract since contract only occurs when i < 1/3. We need to consider cases for the load factor i . For both cases numi = numi1 1. And sizei = 2/3 sizei1 . There fore, sizei /2 = sizei1 /3 = numi1 = numi + 1. Assume i 1/3. Then ci = 1 ci = ci + i i1 = 1 + |(2 numI sizei )| |(2 numi1 sizei1 )| = 1 + |(2 numI sizei )| |(2 (numi + 1) sizei )| = 1 + |(2 numI sizei )| |(2 numi + 2 sizei )| 1 + |(2 numI sizei )| |(2 numi sizei )| + 2 =3 Then consider i < 1/3, Then ci = numi + 1, ci = ci + i i1 = numi + 1 + |2 numi sizei | |2 numi1 sizei1 | = numi + 1 2 numi + sizei + 2 numi1 sizei1 = numi + 1 2 numi1 + 2 + sizei 2 numi1 sizei1 = numi + 3 + sizei sizei1 = numi + 3 sizei1 /3 = sizei1 /3 1 + 3 sizei1 /3 =2

Exercise 17.3-6:
Dene two stacks of size n each. Lets call them E and D. ENQUEUE is implemented as a push onto the E stack. DEQUEUE is implemented as a pop from the D stack. the queue items are moved from E to D whenever D is empty and DEQUEUE is called. Function:Move-From-E-to-D(E, D) while TRUE do x = POP(E) if NOT STACK-EMPTY(E) then PUSH(D,x) else return x end if end while

Function:ENQUEUE(E, x) PUSH(E,x)

Function:DEQUEUE(E, D) if STACK-EMPTY(D) then return Move-From-E-to-D(E, D) else return POP(D) end if Analysis using accounting method: Enqueue is assigned an amortized cost of 4, Dequeue is assigned amortized cost of 0. A sequence of k enqueue operations leaves a credit of 3 k. The rst Dequeue uses 2 k credits. The subsequent dequeues use 1 credit each. So amortized cost of 4 per operation, or O(1).

Question 2 - Exercise 22.1-6, 22.2-7


Exercise 22.1-6:
Notice that if edge is present from vertex v to vertex u, then v cannot be a sink and if the edge is not present then u cannot be a sink. Searching the adjacency-list in a linear fashion enables us to exclude one vertex at a time.

Exercise 22.2-7:
Create a graph G where each vertex represents a wrestler and each edge represents a rivalry. The graph will contain n vertices and r edges. Perform as many BFSs as needed to visit all vertices. Assign all wrestlers whose distance is even to be good guys and all wrestlers whose distance is odd to be bad guys. Then check each edge to verify that it goes between a good guy and a bad guy. This solution would take O(n + r) time for the BFS, O(n) time to designate each wrestler as a good guy or bad guy, and O(r) time to check edges, which is O(n + r) time overall.

Question 3 - Problem 23-3


Part a:
We will prove that an MST of a graph is also a bottleneck spanning tree (BNST). Suppose the claim is not true. and assume T is an MST of a graph G and there is a BNST T such that, max{wt(e) : e T } = > = max{wt(e ) : e T } Let em be an edge in T of weight . The above condition implies that em T . / Now if we add em to T then resultant graph will contain a cycle and em is the edge with maximum weight in that cycle. Now, using (exercise 23.1-5), the edge em cannot belong to any MST. [Contradiction]

Part b:
Delete all edges in G with weight > and obtain a subgraph G . Then G has a BNST with maximum weight i G is connected. connectedness of G can be determined by a linear time search algorithm like DF S and BF S.

Part c:
The key step is to call the algorithm from part (b) passing it the median edge weight, wm , of G. Recall that median nding can be done in linear time, so this does not require sorting the edges of G by weight. Let E> denote the set of edges with weight > wm . If G happens to have a BNST of weight wm , we can safely delete all edges in E> , note that this gets rid of about half the edges of G. On the other hand, if G does not have such a BNST, then we can use DFS to nd a spanning forest of G?E> and, in linear time, contract all such components to single vertices. This also gets rid of about half the edges of G. Therefore, in either case, we may safely recurse on the remaining graph. Running time of the whole algorithm is : O(m + m/2 + m/4 + ) = O(m). Also keep track of all spanning forest edges found during this algorithm and return the union of these edges as the BNST.

Question 4 - Problem 23-4


Part a:
The algorithm clearly produces a spanning tree T . Consider two vertices u and v such that the edge (u, v) is in G but not in T . Then w(u, v) is at least as large as any weight of an edge e on the path from u to v since otherwise e would have been removed earlier. By exercise 23.1 5 this implies that the algorithm produces a minimum spanning tree. The sort on the edges can be done in O(E log E) = O(E log V ) time. Using a DFS to determine connectivity in line 5 the total running time is O(E log V + E(V + E)) = O(E 2 + EV ). The running time can be reduced using results on the ?decremental connectivity problem?.

Part b:
This algorithm simply produces a spanning tree, but clearly does not guarantee that the tree is of minimum weight. Line 3 and 4 in the algorithm can be implemented using disjoint-set operations yielding a total running time of O(E(V )).

Part c:
The algorithm produces a spanning tree T . Observe that on completion any edge (u, v) in G but not in T will be a maximum weight edge on the path from u to v. Again by exercise 23.1 5 the algorithm produces a minimum spanning tree. We can implement the algorithm as follows: Line 3 can be done in O(1) time by appending the edge to the proper adjacency list. Line 4 can be done in O(V ) time using a DFS as described in exercise 22.4 3. Since the number of edges in T is at most O(V ) line 5 can be done using a linear search on the edges in O(V ) time. Line 6 can be done using a search on the adjacency list taking at most O(V ) time. The for loop iterates at most O(E) times and the total running time is thus O(EV )

Question 5 - Problem 24-2


Part a:
Consider boxes with dimensions x = (x1 , , xd ), y = (y1 , , yd ), and z = (z1 , , zd ). Suppose there exists a permutation such that x(i) < yi for i = 4

1, , d and there exists a permutation such that y(i) < zi for i = 1, , d, so that x nests inside y and y nests inside z. Construct a permutation , where (i) = ((i)) Then for i = 1, , d, we have x(i) = x((i)) < y(i) < zi and so x nests inside z.

Part b:
Sort the dimensions of each box from longest to shortest. A box X with sorted dimensions (x1 , x2 , , xd ) nests inside a box Y with sorted dimensions (y1 , y2 , , yd ) if and only if xi < yi for i = 1, 2, , d. The sorting can be done in O(d log d) time, and the test for nesting can be done in O(d) time, and so the algorithm runs in O(d log d) time. This algorithm works because a ddimensional box can be oriented so that every permutation of its dimensions is possible.

Part c:
Construct a dag G = (V, E), where each vertex vi corresponds to box Bi , and (vi , vj ) E if and only if box Bi nests inside box Bj . Graph G is indeed a dag, because nesting is transitive and anti-reexive. The time to construct the dag is O(dn2 + dn log d), from comparing each of the n pairs of boxes after sorting r the dimensions of each. Add a super-source vertex s and a super-sink vertex t to G, and add edges (s, vi ) for all vertices vi with in-degree 0 and (vj , t) for all vertices vj with out-degree 0. Call the resulting dag G. The time to do so is O(n). Find a longest path from s to t in G. (Section 24.2 discusses how to nd a longest path in a dag.) This path corresponds to a longest sequence of nesting boxes. The time to nd a longest path is O(n2 ), since G has n + 2 vertices and O(n2 ) edges. Overall, this algorithm runs in O(dn2 + dn log d) time

You might also like