
In this step, we start solving the sub-problems by replacing the smallest block over the middle block in Tower B as given below: Similar to the above step, we further divided the sub-problem from N=2 to N=1 at the source point. Then move the middle block to Tower B as given in the figure below.

Therefore, now we can assume our sub-problem to be moving 2 blocks from Tower A to Tower C. This step shows the division of the problem by dividing the number of blocks from N=3 to N=2. Suppose we take some blocks let, N = 3, the process of moving the blocks from Tower A to Tower C would be as given below:įirst, move the smallest block to Tower C. Remember that you can move only one block at a time and block can never be on top of a smaller block. Problem Statement: In this problem, you are given N blocks in decreasing order of size on Tower A and you want to move all these blocks to Tower C with the help of Tower B. Below we have mentioned 2 such examples which are most important for any programmer to learn. Divide and Conquer Algorithm Examplesĭivide and conquer approach is widely used to solve many problem statements like merge Sort, quick sort, finding closest pair of points, etc. We can say that f(n) is the work done outside the recursive call. Where 'n' is the input size, 'a' is the number of sub-problems in the recursion, and ‘n/b’ is the size of each sub-problem where all sub-problems are assumed to have the same size. We call adhoc a basic sub-algorithm such that it can be efficient in solving small instances, but its performance on large instances is of no concern.
Divide and conquer code#

The problem with more recursive steps looks as follows: Combine the solutions of the sub-problems as one complete solution for the main problem.ĭiagrammatic Representation of General procedure of Divide-and-Conquer Method:.If they are small enough, solve the sub-problems as base cases. Conquer the sub-problems by solving them recursively.Divide the problem into a number of sub-problems that are small instances of the main problem.Divide-and-Conquer algorithms have three parts as follows:.Divide-and-Conquer solve sub-problems recursively, each sub-problem must be smaller than the original problem, and there must be a base case for sub-problems.Some divide-and-conquer algorithms create more than two sub-problems also.Divide-and-Conquer creates at least two sub-problems, a divide-and-conquer algorithm makes multiple recursive calls.Then solve that sub-problem independently, and at last combine the solutions of small sub-problems as a solution for the main problem.

In simple words, Divide-and-Conquer break down the main problem into small sub-problems.General Procedure of Divide-and-Conquer Method
