calgorithmdivide-and-conquer

Time complexity in Divide and Conquer algorithms


I am wanting to understand the Time Complexity for Divide and Conquer algorithm.

Let's take example of this one:

http://www.geeksforgeeks.org/archives/4583 Method 2:
It gave T(n) = 3/2n -2 and I don't understand why.

I am sorry if I gave you an extra page to open too but I really want to understand at least to a good high level so that I can find the complexity of such algorithms on my own.


Solution

  • Can't open this link due to some reason. I'll still give it a try. When you use the divide and conquer strategy, what you do is you break up the problem into many smaller problems and then you combine the solutions for the small problems to get the solution for the main problem. How to solve the smaller problems: By breaking them up further. This process of breaking up continues until you reach a level where the problem is small enough to be handled directly.

    How to compute time complexity: Assume the time taken by your algo is T(n). Notice that time taken is a function of the problem size i.e. n.

    Now, notice what you are doing. You break up the problems into let's say k parts each of size n/k (they may not be equal in size, in which case you'll have to add the time taken by them individually). Now, you'll solve these k parts. Time taken by each part would be T(n/k) because the problem size is reduced to n/k now. And you are solving k of these. So, it takes k * T(n/k) time.

    After solving these smaller problems, you'll combine their solutions. This will also take some time. And that time will be a function of your problem size again. (It could also be constant). Let that time be O(f(n)).

    So, total time taken by your algorithm will be: T(n) = (k * T(n/k)) + O(f(n))

    You've got a recurrence relation now which you can solve to get T(n).