


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Solutions to problem 1 of homework 6 in a data structures and algorithms course. It includes the analysis of the number of statements executed by different code segments using big o notation, and inductive proofs for binary tree properties. Topics such as big o notation, binary trees, and inductive reasoning.
Typology: Assignments
1 / 4
This page cannot be seen from the preview
Don't miss anything!



(a) sum = 0; for (int i = 0; i < n; i++) sum++;
f (n) = n. (b) sum = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) sum++;
The inner loop is Θ(n). So f (n) = n^2. (c) sum = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n*n; j++) sum++;
The inner loop is Θ(n^2 ). So f (n) = n^3. (d) sum = 0; for (int i = 0; i < n; i++) for (int j = 0; j < i; j++) sum++;
The inner loop is Θ(i). So the entire code segment has runtime proportional to n∑− 1
i=
i = n(n − 1) 2
and f (n) = n^2. (e) sum = 0; for (int i = 0; i < n; i++) for (int j = 0; j < i*i; j++) for (int k = 0; k < j; k++) sum++;
The innermost loop is Θ(j). So the for j loop is
i∑^2 − 1
j=
j
(^) = Θ
( i^2 (i^2 − 1) 2
) ,
which is the same as Θ(i^4 ). So by the fact that ∑^ n t=
tk^ is O(tk+1),
we can conclude that f (n) = n^5. (f) sum = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= i*i; j++) if (j % i == 0) for (int k = 0; k < j; k++) sum++;
The innermost loop will only be executed when j is is a multiple of i, which will occur precisely i times. The remaining passes through the for j loop will simply execute the test and increment j. So we can rewrite the code so that it has the same runtime (up to big-Θ) as follows. sum = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= i; j++) { j_prime = i*j; for (int k = 0; k < j_prime; k++) sum++; Code that has runtime i-1; }
The runtime of the body of the for j loop is Θ(i + j prime) = Θ(i + ij). So the runtime of the for j loop is
∑^ i
j=
(i + ij)
(^) = Θ
( i^2 + i^2 (i + 1) 2
) ,
which is Θ(i^3 ). So by the result on sums of powers of i, we see that f (n) = n^4.
Base case. If a binary tree T has no internal nodes, it can either be empty, or it can have a single node, the root. In either case, E(T ) = I(T ) = 0, and we see that for this case E(T ) = I(T ) + 2n.
Induction hypothesis. Suppose then that n 0 is a nonegative integer with the property that any full binary tree T with n = n 0 (≥ 0) internal nodes satisfies the formula E(T ) = I(T ) + 2n.
Induction step. Suppose now that T is a full binary tree with n = n 0 + 1 ≥ 1 internal nodes. As discussed in class, T contains at least one internal node P, both of whose children, L and L′, are leaves. Consider the tree T ′^ obtained from T by deleting L and L′^ from T. Then, in T ′^ the node P is a leaf. So T ′^ contains n = n 0 internal nodes. Furthermore, if Q is any node of T ′^ different from P, the children of Q in T ′^ are the same as the children of Q in T. Hence T ′^ is full, and we can apply the induction hypothesis to conclude that E(T ′) = I(T ′) + 2n 0.
If d(Q) denotes the depth of a node of T, we know that d(L) = d(L′) = d(P ) + 1. Thus, we have that
E(T ) = E(T ′) − d(P ) + 2d(L) and I(T ) = I(T ′) + d(P ).
So we have that
E(T ) = I(T ′) + 2n 0 − d(P ) + 2d(L) = I(T ) − d(P ) + 2n 0 − d(P ) + 2d(L) = I(T ) + 2n 0 − 2 d(P ) + 2d(P ) + 2 = I(T ) + 2(n 0 + 1).