Analyzing Statements Executed in Code Segments & Inductive Proofs: Homework 6 Key, Assignments of Data Structures and Algorithms

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

Pre 2010

Uploaded on 07/30/2009

koofers-user-2ba
koofers-user-2ba 🇺🇸

5

(1)

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures and Algorithms
Key to Homework Assignment 6
1. For each of the following code segments, find a function f(n) with the property that the
number of statements executed by the segment is O(f(n)).Your functions f(n) should be as
simple as possible.
(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) = n2.
(c) sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n*n; j++)
sum++;
The inner loop is Θ(n2).So f(n) = n3.
(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
n1
X
i=0
i=n(n1)
2,
and f(n) = n2.
(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++;
1
pf3
pf4

Partial preview of the text

Download Analyzing Statements Executed in Code Segments & Inductive Proofs: Homework 6 Key and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms

Key to Homework Assignment 6

  1. For each of the following code segments, find a function f (n) with the property that the number of statements executed by the segment is O(f (n)). Your functions f (n) should be as simple as possible.

(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.

  1. Problem 5.2, page 161. Define the degree of a node as the number of its nonempty children. Prove by induction that the number of degree 2 nodes in any binary tree is one less than the number of leaves. (You can assume that the tree is nonempty.) We use induction on n, the number of nodes in the binary tree. If T is any binary tree, we use the notation l(T ) and d(T ) to denote the number of leaves of T and the number of degree 2 nodes of T, respectively. So we wish to show that for any nonempty binary tree T l(T ) = d(T ) + 1.

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).