7

I'm studying about algorithm complexity analysis. I have problem with unconformity or C(n, k).

int C(int n, int k){
   if(n==k || k==0)
     return 1;
   return C(n-1, k) + C(n-1, k-1);
}

How can I determine its execution complexity or T(n)?

AbcAeffchen
  • 12,535
  • 15
  • 46
  • 62
Andiana
  • 1,634
  • 3
  • 27
  • 65

1 Answers1

14

The recurrence you are looking for is

T(n,k) = T(n-1,k) + T(n-1,k-1) + O(1)       with        T(n,n) = T(n,0) = O(1)

Obviously n is decreased by one every step. If we ignore (just for the moment) that there is a parameter k, basically the number of calls doubles every step. This happens n times, until n = 1. Now C(1,k) returns 1. So you call C(n,k) at most 2n times. So C(n,k) is in O(2n).

Now we remember the k. What would be a worst case for k? maybe k = n/2 (for an even n). You can see that you need at least n/2 steps until k reaches 1 or n reaches n/2 in any recursive call. So the number of calls doubles at least 2n/2 times. But there are many more calls. Writing it down is quite a bit of work.

Edit 1 So lets take a look at this picture

pascal's triangle with number of calls and arrows

You start calling C(n,n/2) (with n=6). The grey triangle is the part that is contained in the n/2 "steps" until you reach the corner (C(n,0) or C(n,n)) first. But as you can see, there are more calls. I marked the calls, where the recursion stops with a blue box and wrote the number a special C(n,k) is called, with a green number.

Edit 2 The value of C(n,k) and the number of calls of C(n,k), that return the value 1, are the same, since the function return only the value 1 (or a result of a recursive call). In the example you see that the sum of the green numbers written at the blue boxes, which are the number of calls, sums up to 20 which is also the value of C(6,3).

Edit 3 Since all operations in one C(n,k) call run in O(1) (constant time), we only have to count the calls to get the complexity. Notice: If C(n,k) would contain an operation that runs in O(n), we would have to multiply the number of call by O(n) to get the complexity.

You will also notice the connection to Pascal's triangle.

A little trick

The algorithm C(n,k) computes the Binomial coefficient by adding 1's. By using Stirling's approximation you can see, that C(n,n/2) ≈ 2n/sqrt(n) (left out some constants for simplification). So the algorithm has to add that many 1's and so it has a complexity of O(2n/sqrt(n)).

AbcAeffchen
  • 12,535
  • 15
  • 46
  • 62
  • I understood until "need at least n/2 steps until k reaches 1 or n reaches n/2", but I think, after n/2 step (at most), k must reach 1 or n much reach k, so the max steps number must be n/2 (instead of at least n/2)? Can you tell me more clearly? And is 1's is 1 step? And why we have C(n, n/2) in complexity? – Andiana Oct 07 '14 at 14:55
  • 1
    @HyNguyen I improved my answer a little bit. – AbcAeffchen Oct 07 '14 at 15:35
  • It's took me a time to digest it :) So why we calc the complexity of C(n, k) through C(n, n/2), is it the max complexity? – Andiana Oct 08 '14 at 00:50
  • yes, C(n,n/2) is the worst case. In complexity theory and specialy in big O you are interested in worst case complexity. Since C(n,k) is symmetric, C(n,n/2) is the worst case. – AbcAeffchen Oct 08 '14 at 00:55
  • C(n,n/2) ≈ 2n/sqrt(n), but why it make the complexity to be O(2n/sqrt(n)), I can't link the before part with it, sorry about this :3 – Andiana Oct 08 '14 at 01:21
  • The thing is: If you call C(n,k) there are only two cases: 1. it returns 1. 2. it calls itself recursivly (and retuns something). But this way you can see, that the number you get at the end is build as a sum like 1+1+1+1+1+....+1. and every 1 comes from a call of C(n,k), so C(n,k) is called at least that many times. – AbcAeffchen Oct 08 '14 at 01:27
  • Oh! So If the C(n,k) = A, so when we call, it will be the summary of 1+1+..+1 (A times). But, I think the worst operator is if(n==k || k==0), so it will be execute more time? – Andiana Oct 08 '14 at 01:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62640/discussion-between-abcaeffchen-and-hy-nguyen). – AbcAeffchen Oct 08 '14 at 01:36
  • AbcAeffchen, Thank you very much, I am stucked with this for days, I am out of message number limit, thank again – Andiana Oct 08 '14 at 02:46
  • @AbcAeffchen Nice answer ! Have in mind that there is an arrow missing from c(4,3) -> C(3,2). What software did you use for the graph? – Panos Kal. Aug 11 '17 at 07:45
  • 1
    @PanosK. I used Illustrator. – AbcAeffchen Aug 11 '17 at 07:48