Вы находитесь на странице: 1из 3

CS 4120 Analysis of Algorithms A term 2010

Solutions for Homework 4


READING: Chapters 15-17, 22-26. 1. Problem 23-1 a. on page 638. Solution: Let x be a node with two children. Since x has a right child the successor s of x is in the right subtree of x. Then s cannot have a left child y because this element would come between x and s, and thus s wouldnt be the successor. (20 points) 2. Suppose we perform a sequence of n operations on a data structure in which the i-th operation costs i if i is an exact power of 2, and 1 otherwise. Use the potential method to determine the amortized cost per operation and use this to get an upper bound on the actual cost of the sequence of n operations. Solution: Let ci = cost of the i-th operation, i.e. ci = i if i is an exact power of 2, 1 otherwise.

We dene the potential function as twice the distance of i from the largest power of 2 that is at most i, i.e. (Di ) = 2 i 2
log2 i

.
n i=1 ci

Then we have (Di ) 0 = (D0 ), and thus have to estimate ci . If i = 2k , then

n i=1 ci .

We just

ci = ci + (Di ) (Di1 ) = 1 + 2 = 3. If i = 2k , then ci = ci + (Di ) (Di1 ) = i + 0 2(2k 1 2k1 ) = 2k 2k + 2 3. Thus


n i=1 ci

n i=1 ci

3n. (20 points) 1

3. Describe an algorithm for nding a spanning tree with minimal weight containing a specied acyclic (cycle-free) set of edges A in a connected weighted undirected graph. Solution: We just run Kruskals algorithm, but instead of starting with an empty set of edges, we start with the edge set A given. (20 points) 4. Problem 23-1 a. on page 638. Solution: To see that the minimum spanning tree (MST) is unique, observe that since the graph is connected and all edge weights are distinct, then there is a unique light edge crossing every cut. But then if we consider two MSTs T and T , we will show that every edge of T is also an edge of T (and vice versa) and thus T is the same as T . Consider an edge (u, v) T . If we remove (u, v) from T , then T becomes disconnected, resulting in a cut (S, V \ S). The edge (u, v) is a light edge crossing this cut. T also has a light edge (x, y) crossing this cut. Because the light edge crossing this cut is unique, the edges (u, v) and (x, y) are the same, and thus (u, v) is an edge of T . To see that the second-best MST need not be unique, here is a weighted, undirected graph G on 4 vertices {v1 , v2 , v3 , v4 } with a unique MST T of weight 7 and two second-best MSTs T1 and T2 of weight 8: E(G) = {(v1 , v2 ), (v1 , v3 ), (v2 , v3 ), (v2 , v4 ), (v3 , v4 )}, where w(v1 , v2 ) = 3, w(v1 , v3 ) = 2, w(v2 , v3 ) = 1, w(v2 , v4 ) = 4, w(v3 , v4 ) = 5. Then T = {(v1 , v3 ), (v2 , v3 ), (v2 , v4 )}, T1 = {(v1 , v2 ), (v2 , v3 ), (v2 , v4 )} and T2 = {(v1 , v3 ), (v2 , v3 ), (v3 , v4 )}. (20 points) 5. Exercise 24.3-5 on page 663. Solution: Here is a simple example for a directed graph for which Dijkstras algorithm could relax the edges of a shortest path out of order. The graph has vertices s, x, y, z and edges (s, x), (x, y), (y, z), (s, y) and let every edge have weight 0. Dijkstras algorithm could relax edges in the order (s, y), (s, x), (y, z), (x, y). The graph has two shortest paths 2

from s to z: (s, x, y, z) and (s, y, z), both with weight 0. The edges on the shortest path (s, x, y, z) are relaxed out of order, because (x, y) is relaxed after (y, z). (20 points)

Вам также может понравиться