next up previous


CSE 2320 Section 501/571 Fall 1999

Homework 1 Solution

1.
Design and analysis of an iterative algorithm called LOGICALAND(A,B,C,n) that stores the logical-and of the binary arrays A and B into binary array C, each of length n.

(a)
Pseudocode for the iterative LOGICALAND(A,B,C,n) algorithm. There are other possible implementations.

LogicalAnd(A,B,C,n)               cost  times
1  for i = 1 to n                   1    n+1
2    if (A[i] = 1) and (B[i] = 1)   1     n
3    then C[i] = 1                  1     t1
4    else C[i] = 0                  1    n-t1

(b)
Line-by-line analysis shown above, where t1 is the number array entries that have 1's in both A and B. Summing the Cost*Times columns yields the precise running time T(n) = 3n + 1.

(c,d)
There are no variables left in the running time expression of part b, which indicates that the best-case and worst-case running times are the same; namely, T(n) = 3n + 1. The asymptotically-tight bound is $T(n) = \Theta(n)$.

Note that a different implementation for LOGICALAND may have a different best-case and worst-case running time. For example, if lines 2-4 above were replaced by

                          cost times
2a   C[i] = 0              1     n
3a   if A[i] = 1           1     n
4a   then if B[i] = 1      1     t2
5a        then C[i] = 1    1     t3

then the precise running time would be T(n) = 3n + 1 + t2 + t3, where t2 is the number of 1's in A, and t3 is the number of array entries having 1's in both A and B. The best-case would be when there are no 1's in A ( t2 = t3 = 0), i.e., $T(n) = 3n + 1 = \Theta(n)$. The worst-case would be when both A and B contained all 1's ( t2 = t3 = n), i.e., $T(n) = 5n + 1 = \Theta(n)$.

2.
We can prove that $3n + 1 = \Theta(n)$ by finding positive constants c1, c2 and n0 such that for all $n \geq n_0$, $c_1 n \leq 3n + 1
\leq c_2 n$ or $c_1 \leq 3 + 1/n \leq c_2$. Since for any value of $n \geq
1$ the value of 3 + 1/n is no less than 3 and no more than 4, the constants c1 = 3, c2 = 4 and n0 = 1 will satisfy the definition of $\Theta$. Thus, $3n + 1 = \Theta(n)$. Of course, there are many other values for c1, c2 and n0 that work.

3.
We can prove that $2n^4 - n^3 + 3 = \Omega(n^2)$ by finding positive constants c and n0 such that for all $n \geq n_0$, $cn^2 \leq 2n^4 -
n^3 + 3$ or $c \leq 2n^2 - n + 3/n^2$. The righthand-side is monotonically increasing with n and has its minimum value of 4 when n=1. Thus, the definition of $\Omega$ is satisfied for $c \leq 4$ and n0 = 1. Of course, there are many other values for c and n0 that work.

4.
We can prove that $2n^3 - 3 \neq O(n^2)$ by showing that no positive constants c and n0 exist such that for all $n \geq n_0$, $2n^3 - 3
\leq cn^2$ or $c \geq 2n - 3/n^2$. Since 2n - 3/n2 is a monotonically increasing function, then no matter how big a positive (>0) value we choose for c, there will be some value for n such that 2n - 3/n2 > c. Therefore, there are no positive constants c and n0that satisfy the above inequality, and $2n^3 - 3 \neq O(n^2)$.

5.
Design and analysis of a recursive version of the LOGICALAND(A,B,C,p,r) algorithm that uses a divide-and-conquer approach in which the algorithm divides the arrays A[p..r] and B[p..r]in half and calls itself recursively on each half.

(a)
Pseudocode for recursive LOGICALAND(A,B,C,p,r) algorithm.

LogicalAnd(A,B,C,p,r)                 cost    times
1  if p < r                             1       1
2  then q = floor((p+r)/2)              1       t1         (t1 = 0 or 1)
3       LogicalAnd(A,B,C,p,q)         T(n/2)    t1
4       LogicalAnd(A,B,C,q+1,r)       T(n/2)    t1
6  else if (A[p] = 1) and (B[p] = 1)    1      1-t1
7       then C[p] = 1                   1     t2(1-t1)     (t2 = 0 or 1)
8       else C[p] = 0                   1    (1-t2)(1-t1)

(b)
Line-by-line analysis shown above, where T(n) is the running time of the algorithm for input size n. t1 determines the stopping condition, and t2 = 1 if both entries in A and B equal 1. I have chosen to derive precise constants for the recurrence, but you may also combine them to be $\Theta(1)$. By adding the products of the cost and times columns,

\begin{eqnarray*}T(n) & = & 1 + t_1 + 2 t_1 T(n/2) + (1 - t_1) + t_2(1 - t_1) +
...
...) +
(1 - t_1 - t_2 + t_1 t_2) \\
& = & 2 t_1 T(n/2) - t_1 + 3
\end{eqnarray*}


Since t1 depends on n (t1 = 0 if n=1, and t1 = 1 if n>1), we arrive at the following recurrence for the running time T(n).


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

(c,d)
As in problem 1, there are no variables left in the recurrence to distinguish a best or worst case. So, the above recurrence describes both.

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

Iteration Method:

\begin{eqnarray*}T(n) & = & 2 + 2T(n/2) \\
& = & 2 + 2[2 + 2T(n/4)] \\
& = &...
...T(n/8) \\
& = & 2 + 4 + 8 + \ldots + 2(2^{i-1}) + 2^i T(n/2^i)
\end{eqnarray*}


The recursion will terminate when the argument to T() equals 1, i.e.,

\begin{eqnarray*}n/2^i & = & 1 \\
n & = & 2^i \\
i & = & \lg n
\end{eqnarray*}



\begin{eqnarray*}T(n) & = & \sum_{i=0}^{\lg n - 1} 2(2^i) + 2^{\lg n} T(1) \\
...
...) + \Theta(n) \\
& = & 2n - 2 + \Theta(n) \\
& = & \Theta(n)
\end{eqnarray*}


Substitution Method:

First, show $T(n) = O(n) \leq cn$ using the inductive hypothesis $T(n/2) \leq c(n/2)$.

\begin{eqnarray*}T(n) & \leq & 2c(n/2) + 2 \\
& = & cn + 2
\end{eqnarray*}


Unfortunately, we cannot discard the 2 without decreasing the righthand side and violating the inequality. So, we choose a different member of O(n) for T(n); namely, $T(n) = O(n) \leq cn - b$, where b is a constant. The inductive hypothesis is now $T(n/2) \leq c(n/2) - b$.

\begin{eqnarray*}T(n) & \leq & 2c(n/2) - 2b + 2 \\
& = & cn - 2b + 2 \\
& \leq & cn - b
\end{eqnarray*}


given that $b - 2 \geq 0$, or $b \geq 2$. Since this is a valid constant, T(n) = O(n).

Next, show $T(n) = \Omega(n) \geq cn$ using the inductive hypothesis $T(n/2) \geq c(n/2)$.

\begin{eqnarray*}T(n) & \geq & 2c(n/2) + 2 \\
& = & cn + 2 \\
& \geq & cn
\end{eqnarray*}


since we can subtract 2 from the righthand side without violating the inequality. Therefore, $T(n) = \Omega(n)$ and $T(n) = \Theta(n)$.

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 \\
2T(n-1) + \Theta(1) & n > 1
\end{array} \right. \end{displaymath}

Iteration Method:

\begin{eqnarray*}T(n) & = & \Theta(1) + 2T(n-1) \\
& = & \Theta(1) + 2[\Theta(...
...\Theta(1) + 2\Theta(1) + \ldots + 2^{i-1}\Theta(1) + 2^iT(n - i)
\end{eqnarray*}


The recursion will terminate when the argument to T() equals 1, i.e., n - i = 1, or i = n-1.

\begin{eqnarray*}T(n) & = & \sum_{i=0}^{n-2} 2^i\Theta(1) + 2^{n-1}T(1) \\
& =...
...2^n) \\
& = & \Theta(2^n) + \Theta(2^n) \\
& = & \Theta(2^n)
\end{eqnarray*}


Substitution Method:

First, show $T(n) = O(2^n) \leq c2^n - b$ using the inductive hypothesis $T(n-1) \leq c2^{n-1} - b$, where b is a constant.

\begin{eqnarray*}T(n) & \leq & 2c2^{n-1} - 2b + \Theta(1) \\
& = & c2^n - 2b + \Theta(1) \\
& \leq & c2^n - b
\end{eqnarray*}


if $b - \Theta(1) \geq 0$, or $b \geq \Theta(1)$. Since this is a valid constant, T(n) = O(2n).

Next, show $T(n) = \Omega(2^n) \geq c2^n$ using the inductive hypothesis $T(n-1) \geq c2^{n-1}$.

\begin{eqnarray*}T(n) & \geq & 2c2^{n-1} + \Theta(1) \\
& = & c2^n + \Theta(1) \\
& \geq & c2^n
\end{eqnarray*}


since we can subtract $\Theta(1)$ from the righthand side without violating the inequality. Thus, $T(n) = \Omega(n)$ and $T(n) = \Theta(n)$.


next up previous