Tutorial Exercise (Analysis)

advertisement
Tutorial Exercise (Analysis)
CS3381 Design and Analysis of Algorithms
Helena Wong, 2001
1. Do you understand each of the followings:
 An algorithm
 A correct algorithm
 An instance of a problem
An instance of a problem: An input case needed to be solved.
 Asymptotic Performance
 Random Access Machine
2. What is the best measure to describe the input size of each of the following problems:
a. To search for all files named “index.html” inside a hard disk.
b. To compute the product of 2 matrices.
c.
Width1 , Height 1 , Width2
To replace all the strings “apple” with “orange” in a text document.
d. Given a bag of infinitely many coins in 3 different values ($1.0, $0.5, $0.1), find out the
minimal group of coins to make up the exact fare for a bus.
3. What is the order of growth of the running time of an algorithm, if its running time is:
a. T(n) = 10-6 ×n2 + 106 × n + 106 / n3
b. T(n) = 3.14n + cos(n*60)
c.
T(n) = [n(n log n + n2 + 3)+log(0.5n)] / n2 + 3.14
n2
3.14n
n
4. It makes no sense to say “The running time of algorithm A is at least O(n)”. Why?
Consider the function f0(n)=0
 f0(n)  O(n)
 “f0(n) is at least O(n) is true
Consider any function f*(n)>f0(n)
 f*(n) is at least O(n) is also true
 “f(n) is at least O(n)” is true for any positive function f(n)
 “All algorithm’s running times are at least O(n)”
 “The running time of algorithm A is at least O(n)” is meaningless.


5. Prove that n2=O(n3)
To prove that n2=O(n3), we need to find out positive c,n0 such that n2  c*n3, for all n >= n0.
Dividing by n2 yields 1 c*n.
(1 c*n) is true when c=1 and n>=1.
Hence, “n2  c*n3, for all n>=n0” is true given n0=1 and c=1.
ie., we can find out positive c,n0 such that n2  c*n3, for all n >= n0.
ie. n2=O(n3) is true;
Tutorial Exercise (Analysis)
CS3381 Design and Analysis of Algorithms
Helena Wong, 2001
6. Prove that 2n2-5n=(n2)
To prove 2n2-5n=(n2), we need to find out positive c1,c2,n0 such that c1*n2  2n2-5n  c2*n2, for all n
>= n0.
Dividing by n2 yields: c1  2-5/n  c2
Take c1 = 0.1, c2=100, n0=30
For 2-5/n  c2
For c1  2-5/n
Consider that 5/n is positive whenever n>=1.
=> 2-5/n  2 for all n1
=> 2-5/n  c2 for all nn0
=> 2n2-5n  c2*n2 for all nn0
Consider that (2-5/n) is an increasing function,
and (2-5/n0)=1.833
=> 2-5/n1.833 for all nn0
=> 2-5/nc1 for all nn0
=> 2n2-5nc1*n2 for all nn0
=> c1*n2  2n2-5n for all nn0
Conclusion, “c1*n2  2n2-5n  c2*n2, for all n  n0” is true given c1=0.1, c2=100, n0=30. (Indeed, it is
also true for any c1, c2 and n0 such that they are  1/3, 2, and  3 respectively. See the proof in the
course web page). ie., we can find out positive c1,c2,n0 such that c1*n2  2n2-5n  c2*n2, for all n  n0.
Therefore, 2n2-5n=(n2).
7. Write an algorithm (in pseudocode) to compute the product of all the odd integers in an nonempty array. Using a loop invariant, prove that your algorithm is correct.
function ProductOdd(A,n)
1
result=1;
2
for i=1 to n
3
4
5
if A[i] is odd
result = result * A[i]
return result;
Loop Invariant: At the start of each iteration of the for loop, if i=1, then result contains 1, otherwise,
result contains the product of all the odd integers in A[1..i-1].
Initialization: Before the first iteration, i=1 and result contains 1 => Loop invariant holds
Maintenance: The for loop checks that if A[i] is odd, then result is multiplied with A[i], otherwise result
is not changed. Hence, if the loop invariant is true before an iteration, it remains true before next
iteration.
Termination: The for loop ends with i=n+1, and i<>1 since A is not empty. Substituting n+1for i in the
loop invariant, we get result contains the product of all odd integers in A[1..n].
Tutorial Exercise (Analysis)
CS3381 Design and Analysis of Algorithms
Helena Wong, 2001
8. The following algorithm counts the number of male students enrolled in both of 2 courses (A
and B). Assume that the class sizes of both courses are n, n>50.
function AlgorithmA(A,B)
1
result = 0;
2
for i=1 to n
3
4
5
if A[i] is male
for j=1 to n
if A[i]=B[j]
6
result = result + 1;
7
break; //stop iterating the inner for-loop.
8
return result;
What are the best, average, and worst cases of this algorithm [Assume that the courses are
equally welcomed by male and female students, and all students enrolled in A will enroll or
not enroll in B with equal possibility]?
Best case: all course A students are female
Average case: half of course A students are male and half of these male students are found in course B’s
list, evenly distributed.
Worst case: all course A students are male and none of them study course B.
By direct observation, state the order of growth of the running time of each of the 3 cases.
Best case: n
Worst case: n2
Average case: n2
Formulate the running times with abstract coefficients to prove your observation.
Let t3a = number of male students in course A.
Let t4 = number of times line 4 is executed.
Let t5 = number of times line 5 is executed.
Let t6 = number of times line 6 is executed.
T(n)=C1+C2(n+1)+C3(n)+C4(t4)+C5(t5)+C6(t6)+C7(t6)+C8
Best case: t3a=0, t4=t5=t6=0 => T(n)= C1+C2(n+1)+C3(n)+C8=An+B
Worst case: t3a=n, t4=n(n+1),t5=n2, t6=0=> T(n)= C1+C2(n+1)+C3(n)+C4(n*(n+1))+C5(n2)+C6(0)+C7(0)+C8=An2+Bn+C
Average case: t3a=n/2, t6=n/4,t5=n/4*(n)+n/4*n/2=(3/8)n2 , t4= n/4*(n+1)+n/4*n/2=(3/8)n2+n/4,
T(n)=C1+C2(n+1)+C3(n)+C4((3/8)n2+n/4)+C5* ((3/8)n2) +C6(n/4)+C7(n/4)+C8=An2+Bn+C
Describe the running times in terms of
a. Asymptotic Upper Bound,
b. Asymptotic Lower Bound, and/or
c.
Asymptotic Tight Bound
Best case: (c) (n)
Worst case: (c) (n2)
Average case: (c) (n2)
AlgorithmA: (a) O(n2), (b) (n)
Download