CS 141 Mid-Term 2002

Question 2

(a)    True

(b)   True

(c)    True

(d)   False

(e)    True

(f)     False

(g)    True

(h)    False

(i)      True

Question 4:

(A)   :

r - children

….

Each node has r -children

…..                                             ….

(B)   The depth of the tree D(n) will be k-1 if n = 2^k. The other way of saying it will be that the depth of the tree will be lg(n) - 1. We can prove this by induction on k.

1. Base Case : For k = 1, the function call will terminate at the first stage itself (because n=2) and the depth of the tree will be zero.

D(n) = k-1 = 0

2. Induction hypothesis : Let the depth of the tree be k-1 for n = 2^k.

In other words:

D(n) = k-1 for n = 2^k …….(I)

3. To Prove D(n ) = k  for n = 2^(k+1)

Observe that the call Stupid(2^(k+1)) will generate r children, each a call to Stupid(2^(k+1)/2) which is same as Stupid(2^k). Therefore the depth of the tree will be one more than the depth of the tree generated by Stupid(2^k). In other words:

D(2^(k+1)) = 1+ D(2^k) ……..(II).

Now putting the value of D(2^k) from (I) in (II) we have

D(2^(k+1)) = 1+(k-1) = k . Hence proved.

(C). Each non-leaf function Stupid() creates r children. The number of procedure calls at depth 1 are r. At level 2, the number of function calls are r*r ( because each of the r nodes has r children. ) This continues till we reach the depth of the tree.

Thus, the total number of function calls C(n) is

C(n) = 1+r+r^2+r^3+…..+r^(k-1) where n = 2^k.

Optional : The above is a geometric series which can be summed up. The Summations is

C(n) = (r^k – 1)/(r-1).

(D). Observe that each function takes constant time, say c, to execute. ( It prints Cool!). As we saw above , the total number of function calls are O(r^k). Hence the time complexity is O(c*r^k) = O(r^lg(n)) because k = lg(n).

Optional : Given the relationship between k and n, r^k can be shown to be equal to n^lg(r). Hence the time complexity is O(n^lg(r))

Question 5 :

(a)    f(n) = O(g(n)) if \$ c>0 and N0 such that 0<f(n) <= c.g(n) "n>N0. We assume that the statement is true and we shall try to find c and N0.

Taking f(n) = 2^(n+1) and g(n) = 2^n we will have

2^(n+1) <= c.2^n

(dividing both sides by 2^n gives us)

ð     2<=c.

Hence if we take c = 3 and N0 = 1This inequality is satisfied for all n >= N0

Hence the above statement is true.

(b). Again assuming that the statement is true , than we will have

2^2n <= c.2^n

dividing both sides by 2^n gives us :

ð     2^n <= c  ( We are using the fact that 2^2n = (2^n)*(2^n).

Taking any constant c, we can always find an n such that 2^n >c. Hence it is not possible to find a pair c and N0 for which above inequality is satisfied for all n >= N0. Hence the above assumption is not true.

Question 6 :

Let G = (V,E) be the graph and u be any node and (u,v) be the minimum weight edge with weight w(u,v) going out of u. We claim that (u,v) belongs to every minimums spanning tree.

Let us assume that there is a Minimum Spanning Tree T which does not have (u,v). As T is a spanning tree, there is a unique simple path between any two nodes. In particular, there is a simple path, say ( u,v1,v2,v3,….vk,v) between u and v. Let us add the edge (u,v) to this path. This will create a simple cycle which can be removed by removing edge (u,v1). Thus the resulting tree will also be the spanning tree, say T’. Let the weight of T and T’ be w(T) and w(T’) respectively and weight opf (u,v1) be w(u,v1)

Then we have

w(T) = w(T’)+w(u,v)-w(u,v1)

As w(u,v) < w(u,v1), we have w(T’) < w( T).  This contradicts the original hypothesis that T is an MST. Hence the edge (u,v) belongs to every minimum spanning tree.