Name _________________________

CS60

Computational Complexity Worksheet

Submit this for up to 10 points

Suppose p and q are two procedures, functions, or methods as appropriate, with run-time complexity P and Q respectively. That is, the runtime of p(x) is O(P(n)) and the runtime of q(x) is O(Q(n)) where n is a variable indicating the "size" of the input data x.

1. Suppose P(n) = n2 and Q(n) = n3. Give the tightest possible bound on the run-time of the sequence

p(x); q(x); 

(p and q done in sequence) assuming that p has no side-effects on the input data x.

 

2. Give the tightest possible bound on the run-time of the sequence

p(x); q(x); 

in terms of P and Q, again assuming that p has no side-effects on the input data.

 

3. Consider the for loop:


for( int i = 0; i < n; i++)
  {
  r(i, x);
  }

where r does not modify its argument and has a runtime bound of R(n), independent of i.

Give the tightest possible bound on the run-time of this loop.

 

4. Consider the for loops:


for( int i = 0; i < n; i++)
  for( int j = 0; j < n; j++ )
    {
    s(i, j, x);
    }

where s does not modify its argument and has a runtime of S(n), independent of i and j.

Give the tightest possible bound on the run-time of this loop.

 

5. Repeat the previous question, for the following program, noting that the inner index is a function of the outer one:


for( int i = 0; i < n; i++)
  for( int j = 0; j < i; j++ )
    {
    s(i, j, x);
    }

 

6. Repeat the previous question, for the following program:


for( int i = 0; i < n; i++)
  for( int j = 0; j < 10000; j++ )
    {
    s(i, j, x);
    }

 

7. Repeat the previous question, for the following program:


for( int i = 1; i <= n; i *= 2)
  for( int j = 0; j < i; j++ )
    {
    s(i, j, x);
    }

 

8. Repeat the previous question, for the following program:


for( int i = n; i > 0; i /= 2)
  for( int j = 0; j < i; j++ )
    {
    s(i, j, x);
    }

 

9. Repeat the previous question, for the following program:


for( int i = 0; i < n; i++ )
  for( int j = 1; j < i; j*= 2 )
    {
    s(i, j, x);
    }

 

10. What if p and q are functions, with P and Q as bounds on the run-time of p and q as before. How would you give a bound on the run-time of:

q(p(x));