Answers to Self-Study Questions for Compleixty

Test Yourself #1

Test Yourself #2

Question 1: The problem size is the number of people in the room.

Question 2: Assume there are N people in the room. In algorithm 1 you always ask 1 question. In algorithm 2, the worst case is if no one has your birthday. Here you have to ask every person to figure this out. This is N questions. In algorithm 3, the worst case is the same as algorithm 2. The number of questions is 1 + 2 + 3 + ... + N-1 + N. We showed before that this sum is N(N+1)/2.

Question 3: Given the number of questions you can see that algorithm 1 is constant time, algorithm 2 is linear time, and algorithm 3 is quadratic time in the problem size.

Test Yourself #3

  1. The first loop is O(N) and the second loop is O(M). Since you don't know which is bigger, you say this is O(N+M). This can also be written as O(max(N,M)). In the case where the second loop goes to N instead of M the complexity is O(N). You can see this from either expression above. O(N+M) becomes O(2N) and when you drop the constant it is O(N). O(max(N,M)) becomes O(max(N,N)) which is O(N).
  2. The first set of nested loops is O(N2) and the second loop is O(N). This is O(max(N2,N)) which is O(N2).
  3. When i is 0 the inner loop executes N times. When i is 1 the inner loop executes N-1 times. In the last iteration of the outer loop when i is N-1 the inner loop executes 1 time. The number of times the inner loop statements execute is N + N-1 + ... + 2 + 1. This sum is N(N+1)/2 and gives O(N2).

Test Yourself #4

  1. Each call to f(j) is O(1). The loop executes N times so it is N x O(1) or O(N).
  2. The first time the loop executes j is 0 and g(0) takes "no operations". The next time j is 1 and g(1) takes 1 operations. The last time the loop executes j is N-1 and g(N-1) takes N-1 operations. The total work is the sum of the first N-1 numbers and is O(N2).
  3. Each time through the loop g(k) takes k operations and the loop executes N times. Since you don't know the relative size of k and N, the overall complexity is O(N x k).

Test Yourself #5

operation

Complexity

best

worst

average

size

O(1)

O(1)

O(1)

addAfter

 O(1): add after last item because there is no current item or current is last item. Array is not full.

 O(N): add after first item and/or array is full

O(N): assuming each item is equally likely to be current.

addBefore

O(1): last item is current and array is not full

O(N): first item is current and/or array is full

O(N): assuming each item equally likely to be current

removeCurrent

 O(1): last item is current or thre is no current item

O(N): current is first item

O(N): assuming each item is equally likely to be current

start

O(1)

O(1)

O(1)

getCurrent

O(1): whether or not there is a current item

O(1): whether or not there is a current item

O(1): whether or not there is a current item

advance

 O(1): whether or not there is a current item

 O(1): whether or not there is a current item

 O(1): whether or not there is a current item

isCurrent

 O(1)

O(1)

O(1)