0% found this document useful (0 votes)
24 views7 pages

Module 2 AAD

The document outlines the divide-and-conquer strategy, which involves breaking a problem into smaller instances, solving them recursively, and combining their solutions. It discusses control abstraction for the strategy, specifically the DANDC procedure, and provides examples such as finding maximum and minimum values and binary search. The document also compares the efficiency of different algorithms and highlights the importance of careful design in applying the divide-and-conquer technique.

Uploaded by

Gautham J K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views7 pages

Module 2 AAD

The document outlines the divide-and-conquer strategy, which involves breaking a problem into smaller instances, solving them recursively, and combining their solutions. It discusses control abstraction for the strategy, specifically the DANDC procedure, and provides examples such as finding maximum and minimum values and binary search. The document also compares the efficiency of different algorithms and highlights the importance of careful design in applying the divide-and-conquer technique.

Uploaded by

Gautham J K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Module 2

Divide and Conquer


Divide and Conquer
Divide and conquer strategy is as follows: divide the problem instance into two
or more smaller instances of the same problem, solve the smaller instances
recursively, and assemble the solutions to form a solution of the original
instance. The recursion stops when an instance is reached which is too small to
divide.
The divide-and-conquer design strategy involves the following steps:
1. Divide an instance of a problem into one or more smaller instances.
2. Conquer (solve) each of the smaller instances. Unless a smaller instance is
sufficiently small, use recursion to do this.
3. If necessary, combine the solutions to the smaller instances to obtain the
solution to the original instance.
Divide and Conquer : Control Abstraction
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.
If the sizes of the two sub problems are approximately equal then the
computing time of DANDC is:  g (n) T (n) =  2 T(n/2)+ f (n) n small otherwise
Where, T (n) is the time for DANDC on ‘n’ inputs g (n) is the time to complete
the answer directly for small inputs and f (n) is the time for Divide and Combine.
(examples : check recursive solutions in pdf given by Anoop sir)

Divide and Conquer : Finding Maximum and Minimum


Straightforward maximum and minimum

StraightMaxMin requires2(n - 1) element comparisons in the best, average, and


worst case. An immediate improvement is possible by realizing that the
comparisona [i] < min is necessary only when a[i] > max is false. Hence we can
replace the contents of the for loop by

Now the best case occurs when the elements are in increasing order. The
number of element comparisons is n – 1. The worst case occurs when the
elements are in decreasing order. In this case the number of element
comparisons is 2(n- 1). The average number of element comparisons is less than
2(n - 1).
Recursively finding the maximum and minimum
Note that 3n/2-2 is the best, average, and worst-case number of comparisons
when n is a power of two.
Compared with the 2n - 2 comparisons for the straightforward method, this is a
saving of 25% in comparison.
But ,in terms of storage, MaxMin is worse than the straightforward algorithm
because it requires stack space for i,j,max,min,max1, and min1.Given n
elements, there will be ⌊ log2n ⌋ +1 levels of recursion and we need to save
seven values for each recursive call.
Thus, MaxMin will be slower than StraightMaxMin because of the overhead of
stacking i,j,max,and min for the recursion
In divide and conquer technique if the assumption is not true, the
technique yields a less-efficient algorithm.Thus the divide-and-conquer
strategy is seen to be only a guide to better algorithm design which
may not always succeed.
BINARY SEARCH
In divide-and-conquer terminology, Binary Search locates a key x in a sorted
(nondecreasing order) array by first comparing x with the middle item of the
array.
If x equals the middle item, quit. Otherwise:
1. Divide the array into two subarrays about half as large. If x is smaller than the
middle item, choose the left subarray. If x is larger than the middle item, choose
the right subarray.
2. Conquer (solve) the subarray by determining whether x is in that subarray.
Unless the subarray is sufficiently small, use recursion to do this.
3. Obtain the solution to the array from the solution to the subarray.

When developing a recursive algorithm for a problem, we need to


• Develop a way to obtain the solution to an instance from the solution to one
or more smaller instances.
• Determine the terminal condition(s) that the smaller instance(s) is (are)
approaching.
• Determine the solution in the case of the terminal condition(s).
Both will have the same time complexity O(log(n)), but they will different in term of
space usage.
Recursive May reach to "log(n)" space (because of the stack), in iterative BS it should
be "O(1)" space complexity.

(For Strassen’s Matrix Multiplication ,Quick Sort, Merge Sort refer Anoop sir’s
pdf)

You might also like