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
*t*_{1}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*) = 3*n*+ 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*) = 3*n*+ 1. The asymptotically-tight bound is .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*) = 3*n*+ 1 +*t*_{2}+*t*_{3}, where*t*_{2}is the number of 1's in*A*, and*t*_{3}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*(*t*_{2}=*t*_{3}= 0), i.e., . The worst-case would be when both*A*and*B*contained all 1's (*t*_{2}=*t*_{3}=*n*), i.e., .

- 2.
- We can prove that
by finding positive constants
*c*_{1},*c*_{2}and*n*_{0}such that for all , or . Since for any value of the value of 3 + 1/*n*is no less than 3 and no more than 4, the constants*c*_{1}= 3,*c*_{2}= 4 and*n*_{0}= 1 will satisfy the definition of . Thus, . Of course, there are many other values for*c*_{1},*c*_{2}and*n*_{0}that work. - 3.
- We can prove that
by finding positive
constants
*c*and*n*_{0}such that for all , or . The righthand-side is monotonically increasing with*n*and has its minimum value of 4 when*n*=1. Thus, the definition of is satisfied for and*n*_{0}= 1. Of course, there are many other values for*c*and*n*_{0}that work. - 4.
- We can prove that
by showing that no positive
constants
*c*and*n*_{0}exist such that for all , or . Since 2*n*- 3/*n*^{2}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 2*n*- 3/*n*^{2}>*c*. Therefore, there are no positive constants*c*and*n*_{0}that satisfy the above inequality, and . - 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*.*t*_{1}determines the stopping condition, and*t*_{2}= 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 . By adding the products of the cost and times columns,

Since

*t*_{1}depends on*n*(*t*_{1}= 0 if*n*=1, and*t*_{1}= 1 if*n*>1), we arrive at the following recurrence for the running time*T*(*n*).

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

The recursion will terminate when the argument to

*T*() equals 1, i.e.,

Substitution Method:

First, show using the inductive hypothesis .

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, , where*b*is a constant. The inductive hypothesis is now .

given that , or . Since this is a valid constant,

*T*(*n*) =*O*(*n*).Next, show using the inductive hypothesis .

since we can subtract 2 from the righthand side without violating the inequality. Therefore, and .

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

Iteration Method:

The recursion will terminate when the argument to

*T*() equals 1, i.e.,*n*-*i*= 1, or*i*=*n*-1.

Substitution Method:

First, show using the inductive hypothesis , where

*b*is a constant.

if , or . Since this is a valid constant,

*T*(*n*) =*O*(2^{n}).Next, show using the inductive hypothesis .

since we can subtract from the righthand side without violating the inequality. Thus, and .