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

 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. 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 ×n 2 + 10 6 × n + 10 6 / n 3 b. T(n) = 3.14

n + cos(n*60) c. T(n) = [n(n log n + n 2 + 3)+log(0.5n)] / n 2 + 3.14

4. It makes no sense to say “The running time of algorithm A is at least O(n)”. Why?

5. Prove that n 2 =O(n 3 )

Tutorial Exercise (Analysis) CS3381 Design and Analysis of Algorithms

Helena Wong, 2001

6. Prove that 2n 2 -5n=

(n 2 )

7. Write an algorithm (in pseudocode) to compute the product of all the odd integers in a non-empty array. Using a loop invariant, prove that your algorithm is correct.

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 if A[i] is male

4 for j=1 to n

5 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]?

By direct observation, state the order of growth of the running time of each of the 3 cases.

Formulate the running times with abstract coefficients to prove your observation.

Describe the running times in terms of a. Asymptotic Upper Bound, b. Asymptotic Lower Bound, and/or c. Asymptotic Tight Bound

Download