next up previous


CSE 2320 Section 501 Fall 1998

Homework 1

Due: September 15, 1998, in class (September 16, 1998, 5:00pm for -10%)

1.
Design and analyze an iterative algorithm called ADD-ODDS(A,n) that returns the sum of the odd numbers found in the positive integer array $A[1 \ldots n]$. Specifically,

(a)
Give pseudocode for your iterative ADD-ODDS(A,n) algorithm.

(b)
Perform a line-by-line analysis of your algorithm and derive a precise expression T(n) for the running time. You may assume that each line of pseudocode takes unit time (i.e., ci = 1).

(c)
Describe the situation resulting in the best-case running time of your algorithm and derive an expression for T(n) in this case. Also, give an asymptotically-tight bound for T(n).

(d)
Describe the situation resulting in the worst-case running time of your algorithm and derive an expression for T(n) in this case. Also, give an asymptotically-tight bound for T(n).

2.
Prove that your asymptotically-tight bound in part 1d is correct by finding the constants c1, c2 and n0 from the definition of $\Theta$.

3.
Prove that 2n is an asymptotic upper bound for n2 by finding the constants c and n0 from the definition of O.

4.
Prove that 2n is not an asymptotic lower bound for n2 by showing that no constants c and n0 exist satisfying the definition of $\Omega$.

5.
Design and analyze a recursive version of the ADD-ODDS(A,p,r) algorithm that uses a divide-and-conquer approach in which the algorithm divides the array $A[p \ldots r]$ in half and calls itself recursively on each half. Specifically,

(a)
Give pseudocode for your recursive ADD-ODDS(A,p,r) algorithm. NOTE: You may not use global variables in your algorithm.

(b)
Perform a line-by-line analysis of your algorithm and derive a recurrence T(n) for the running time, where n = r - p + 1. You may assume that each line of pseudocode takes unit time (i.e., ci = 1), except, of course, those lines containing recursive calls.

(c)
Describe the situation resulting in the best-case running time of your algorithm and derive a precise recurrence for T(n) in this case.

(d)
Describe the situation resulting in the worst-case running time of your algorithm and derive a precise recurrence for T(n) in this case.

(e)
Solve your recurrence from part (d) using the iteration method and substitution method.

6.
Solve the following recurrence using the iteration method and verify your result using the substitution method.


\begin{displaymath}T(n) = \left\{ \begin{array}{ll}
\Theta(1) & n = 1 \\
T(n-2) + \Theta(1) & n > 1
\end{array} \right. \end{displaymath}


next up previous