MODULE II
DIVIDE AND CONQUER DESIGN
TECHNIQUE
THE GENERAL CONCEPT
Divide and Conquer Algorithm is a problem-solving technique used to solve
problems by dividing the main problem into sub problems, solving them
individually and then merging them to find solution to the original problem.
Divide and Conquer is mainly useful when we divide a problem into
independent sub problems.
Working of Divide and Conquer Algorithm
Divide and Conquer Algorithm can be divided into three steps: Divide,
Conquer and Merge.
1. Divide:
Break down the original problem into smaller subproblems.
Each subproblem should represent a part of the overall problem.
The goal is to divide the problem until no further division is possible.
2. Conquer:
Solve each of the smaller sub problems individually.
If a sub problem is small enough (often referred to as the “base case”),
we solve it directly without further recursion.
The goal is to find solutions for these sub problems independently.
3. Merge:
Combine the sub-problems to get the final solution of the whole problem.
Once the smaller sub problems are solved, we recursively combine their
solutions to get the solution of larger problem.
The goal is to formulate a solution for the original problem by merging
the results from the sub problems.
Examples – Binary Search, Merge Sort,Quick Sort
Control Abstraction of Divide and Conquer
A control abstraction is a procedure whose flow of control is clear but whose
primary operations are specified by other procedures whose precise meanings
are left undefined. The control abstraction for divide and conquer technique is
DANDC(P), where P is the problem to be solved.
DANDC (P)
{
if SMALL (P) then return S (p);
else
{
divide p into smaller instances p1, p2, .... Pk, k >1;
apply DANDC to each of these sub problems;
return (COMBINE (DANDC (p1) , DANDC (p2),...., DANDC (pk));
}
}
SMALL (P) is a Boolean valued function which determines whether the input
size is small enough so that the answer can be computed without splitting. If
this is so function ‘S’ is invoked otherwise, the problem ‘p’ into smaller sub
problems. These sub problems p1, p2, . . . , pk are solved by recursive
application of DANDC.
BINARY SEARCH
Binary search is an efficient searching technique that works with
only sorted lists. So the list must be sorted before using the
binary search method. Binary search is based on divide-and-
conquer technique.
The process of binary search is as follows:
The method starts with looking at the middle element of the list.
If it matches with the key element, then search is complete.
Otherwise, the key element may be in the first half or second half
of the list. If the key element is less than the middle element,
then the search continues with the first half of the list. If the key
element is greater than the middle element, then the search
continues with the second half of the list. This process continues
until the key element is found or the search fails indicating that
the key is not there in the list.
Consider the list of elements:-4, -1, 0, 5, 10, 18, 32, 33, 98, 147,
154, 198, 250, 500.Trace the binary search algorithm searching
for the element -1.
FINDING THE MAXIMUM AND MINIMUM
The problem is to find the maximum and minimum items in a set of n
elements.