Professional Documents
Culture Documents
Homework 4: Question 1 - Exercise 17.4-3, 17-3.6
Homework 4: Question 1 - Exercise 17.4-3, 17-3.6
November 3, 2010
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).
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.
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.
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 )
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