# CMPSCI 311: Theory of Algorithms

### from Wednesday 10 Sept 2003

Questions are in black, answers in blue.

• Question 1: The family of undirected graphs {K_n: n≥ 1} is defined as follows. K1 is a single node with no edges. Kn+1 consists of a copy of Kn plus one new node, with an edge from the new node to each of the old nodes. Prove by induction on all n≥1 that there are exactly n(n-1)/2 edges in Kn.

Base Case: The statement P(1) says that a K1 has 1(1-1)/2=0 edges. This is true because the definition says it has no edges.

Inductive Hypothesis: A Kn has n(n-1)/2 edges.

Inductive Goal: A Kn+1 has (n+1)(n+1-1)/2 = n(n+1)/2 edges.

Proof of Inductive Goal: By the definition the Kn+1 has exactly n more edges than does the Kn. So it has n + n(n-1)/2, which equals n(n+1)/2 by arithmetic.

• Question 2: A forest is an undirected graph with no cycles. For all n, prove by induction on all e from 0 through n-1 that a forest with n nodes and e edges has exactly n-e connected components.

Let n be an arbitrary positive integer. We prove the desired statement by induction on e.

Base Case: If e=0 then each of the n nodes forms its own connected component, so there are n connected components, which equals the claimed n-e.

Inductive Hypothesis: A forest with n nodes and e edges has n-e connected components.

Inductive Goal: A forest with n nodes and e+1 edges has n-(e+1) = n-e-1 connected components, assuming e+1 &le n-1.

Proof of Inductive Goal: Consider such a forest and remove one of its edges. Since it now has e edges, by the inductive hypothesis it has n-e connected components. Now put the edge back. Since the new edge does not create a cycle, it must connect two points that were in different components, reducing the number of components by one. (Here is where we use the assumption about e+1, to be sure that there are at least two connected components.) So now there are (n-e)-1 connected components, as desired.

This completes the induction proof, so the statement is true for this n and for all e in the indicated range. Then, since n was arbitrary, we may conclude that the statement is true for all n.

• Question 3: A full rooted binary tree or FBRT of depth d, with d≥0, is defined as follows. An FRBT of depth 0 is a single node (its root) with no edges. An FBRT of depth d+1 consists of two FBRT's of depth d plus a new node, its root, and new directed edges from the root to the roots of the depth-d trees. Prove by induction on d that a depth-d FBRT has exactly 2d+1-1 nodes and exactly 2d+1-2 edges.

Base Case: When d=0, the definition tells us that there is 20+1-1=1 node and that there are 20+1-2 = 0 edges.

Inductive Hypothesis: An FRBT of depth d has 2d+1-1 nodes and 2d+1-2 edges.

Inductive Goal: An FRBT of depth d+1 has 2d+2-1 nodes and 2d+2-2 edges.

Proof of Inductive Goal: The FRBT of depth d+1 consists of two FRBT's of depth d, plus one more node and two more edges. So it has 2(2d+1-1)+1 = 2d+2-1 nodes and 2(2d+1-2)+2 = 2d+2-2 edges, as desired.

• Question 4: You are proving on HW#1 that if a and b are non-negative integers with a > b, and c is the remainder when a is divided by b, then gcd(a,b) = gcd(b,c). Using this fact, prove by strong induction on the number a+b that the Euclidean algorithm terminates with the correct greatest common divisor when started on inputs a and b. Assume the following recursive Java-like code is being used for gcd:

int gcd (int a, int b)
{  //assumes a > b >= 0
if (b == 0) return a;
else return gcd (b, a%b);}

The statement "P(n)" is "For any a and b with a+b=n, gcd(a,b) terminates with the correct answer".

Base Case: Since a > b ≥ 0 is given, the smallest possible value of a+b is 1. In this case, the call gcd(1,0) terminates without recursing and returns 1, which is the correct answer.

Strong Inductive Hypothesis: For any m with m≤n, and for any a and b with a>b≥0 and a+b=m, gcd(a,b) terminates with the correct answer.

Inductive Goal: For any a and b with a>b≥0 and a+b=n+1, gcd(a,b) terminates with the correct answer.

Proof of Inductive Goal: Consider the operation of gcd(a,b) with these assumptions. If b=0, then gcd(n+1,0) terminates without recursing and returns n+1, which is the correct answer. If b>0, then gcd(a,b) makes a recursive call to gcd(b,c), where c is a%b. Since b+c is less than a+b (we know that a%b is less than a as long as a>b), the inductive hypothesis tells us that gcd(b,c) terminates with the correct answer. Thus gcd(a,b) terminates with the correct answer for gcd(b,c), and the homework problem tells us that this is the correct answer for gcd(a,b) as well.