0

I'm given the pseudocode of an algorithm:

1     for i=1 to n          //n times
2         j=2^i             //n times
3         while j>=2        //2+3+....? times
4               j=j/2       //1+2+....? times

Where 'to' means less than or equal to, and where '^' means power of.

I'm quite the sure that the first two lines run n times, however I'm not quite sure how to compute the other two lines.

I know that line 3's first two values would be 2... then 3, but I'm not sure what to do next there.

Same goes along with line 4, the first two values being 1.. then 2, but then I have no idea how to continue.

I'm told that the answer is O(n^2), where I have to add up all the lines to get the actual result.

Any ideas?

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
TTEd
  • 309
  • 1
  • 10

3 Answers3

1

The inner loop will run log2(j) times as j is halved each iteration. While j is 2^i, then log2(j) = i. I.e. the inner loop running i times. As i is doing from 1 to n, the total iterations will be 1+2+3+4+..+n, while it is a simple arithmetic series, whose sum is (n+1)n/2, which is O(n^2) as stated. (remark: I might have missed some +/-1 constants somewhere, but it won't affect the resulting complexity).

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
1

Well, this one is a bit weird.

Let's begin by pretending that every arithmetic operation takes the same amount of time. In the case, look at what this code does:

2         j=2^i             //n times
3         while j>=2        //2+3+....? times
4               j=j/2       //1+2+....? times

If you think about how loop (3) executes, it will keep dividing j by two on each iteration. If you think of j as a power of two, it decreases the exponent by one on each iteration. This can only happen O(i) times before j drops down to two. Consequently, this part code runs in time O(i), assuming that all arithmetic operations take the same amount of time.

The outer loop will run n times, so the work done here will be proportional to 1 + 2 + 3 + 4 + ... + n = Θ(n2).

The problem is that the assumption that all arithmetic operations take the same amount of time isn't really realistic. The number 2n grows really fast and will quickly overflow any fixed-width integer type. A more realistic assumption of the time bounds here is that each operation will take time proportional to the number of bits in the numbers. I'm going to ignore this case because I suspect this is for a class assignment, but it's worth keeping that in mind going forward.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
0

Break this into smaller steps

3         while j>=2        //2+3+....? times
4               j=j/2       //1+2+....? times

Above loop for some j=N runs for ~log(N) times

Since i varies from 1 to N, j varies from 2^1 to 2^N. Each while loop now executes ~log(2^i) times, which equals ~i times

Hope this helps

Rozuur
  • 4,115
  • 25
  • 31