1. 程式人生 > >ADA演算法知識(四)Divide-and-conquer algorithm

ADA演算法知識(四)Divide-and-conquer algorithm

分治演算法

Divide the problem into smaller sub-problems.
Governance - breaking down these smaller sub-problems one by one;
Combination - merge the solved sub-problems and finally get the solution of the "mother" problem

Like this question:

[The Coin Counterfeiter]

Give a divide-and-conquer algorithm to find the counterfeit coin in O(logn) weighings. You may assume that n is a power of 2.

So

Like give n as 16

16     8      4      2        1

four progress

Through O (1) operation, the problem of size n into a n/2 problem 

so T(n)=T(n/2)+O(1)

and divide and Conquer: by O (1) operation, the problem of size n into 2 n/2 problems.

so T(n)=2T(n/2)+O(1)

it is obvious that it is a binary search question

When solving the main problems, I will divide the main problems into two sub-problems, for example, dividing n piles of coins into two piles of 2/n piles. If the pile is lighter, the bad coins will be in that pile. After repeated operation, the light piles of coins will be picked out and divided into two piles of the same number of coins until the bad coins are found