Divide and Conquer
Algorithm¶
A typical Divide and Conquer algorithm solves a problem using following three steps:
- Divide: Break the given problem into sub-problems of same type.
- Conquer: Recursively solve these sub-problems.
- Combine: Appropriately combine the answers.
Example¶
Given an integer array \(A[1, n]\), write a function calculate(int[] A)
to calculate its sub sequence with max summary, \(MS\) for short.
Divide¶
Assume \(m=\frac{n}{2}\), then divide \(A\) into two parts, \(A[1, m]\) and \(A[m+1,n]\). Compute the \(MS\) of \(A[1,m]\) and \(A[m+1,n]\) separately, as well as \(X\), \(MS\) of sub sequence crossing over \(A[1,m]\) and \(A[m+1,n]\).
Easy to get that
Combine¶
Time Complexity¶
Time complexity of \(X\) is \(O(n)\). So, \(T(n) = 2T(\frac{n}{2}) + O(n)\). Then the total time complexity is \(O(n\lg{n})\).
Notes¶
The best algorithm for the problem has \(O(n)\) time complexity. Assume \(MS(k)\) is \(MS\) of the front k elements of \(A\) and \(MSE(k)\) is \(MS\) of the front \(k\) elements ending with \(A[k]\). Then calculate \(MS(k+1)\) as follows.
Then, \(MS(k+1)\) is the larger one. In this way, \(T(n) = T(n-1) + C = O(n)\).