Unit 2 Divide and Conquer Approach: Structure Page Nos
Unit 2 Divide and Conquer Approach: Structure Page Nos
Unit 2 Divide and Conquer Approach: Structure Page Nos
2.0 Introduction 42
2.1 Objective 42
2.2 General Issues in Divide and Conquer 43
2.3 Binary Search 45
2.4 Sorting 49
2.4.1: Merge sort
2.4.2: Quick sort
2.5 Integer multiplication 67
2.6 Matrix multiplication 70
2.7 Summary 75
2.8 Solution/Answers 76
2.9 Further Readings 81
2.0 INTRODUCTION
We have already mentioned in unit-1 of Block-1 that there are five fundamental
techniques which are used to design the Algorithm efficiently. These are: Divide and
Conquer, Greedy Method, Dynamic Programming, Backtracking and Branch and
Bound. Out of these techniques Divide & Conquer is probably the most well-known.
Many useful algorithms are recursive in nature. To solve a given problem, they call
themselves recursively one or more times. These algorithms typically follow a divide
& Conquer approach. A divide & Conquer method works by recursively breaking
down a problem into two or more sub-problems of the same type, until these become
simple enough (i.e. smaller in size w.r.t. original problem) to be solved directly. The
solutions to the sub-problems are then combined to give a solution to the original
problem.
Subsolutions-1
Sub-problem 1
Subsolutions-2
Any problem (such Sub-problem 2
Solution
as Quick sort,
Merge sort, etc.)
Subsolutions-n
Sub-problem n
Thus, in general, a divide and Conquer technique involves 3 Steps at each level of
recursion:
42
Step 1: Divide the given big problem into a number of sub-problems that are similar Divide and Conquer
to the original problem but smaller in size. A sub-problem may be further Approach
divided into its sub-problems. A Boundary stage arrives when either a direct
solution of a sub-problem at some stage is available or it is not further sub-
divided. When no further sub-division is possible, we have a direct solution
for the sub-problem.
In this unit we will solve the problems such as Binary Search, Searching - QuickSort,
MergeSort, integer multiplication etc by using Divide and Conquer method;
2.1 OBJECTIVES
Many useful algorithms are recursive in structure, they make a recursive call to itself
until a base (or boundary) condition of a problem is not reached. These algorithms
closely follow the Divide and Conquer approach.
Thus any algorithms which follow the divide-and-conquer strategy have the following
recurrence form:
43
Design Techniques Where
If the problem size is small enough (say, n ≤ c for some constant c), we have a
base case. The brute-force (or direct) solution takes constant time: Θ(1)
Otherwise, suppose that we divide into a sub-problems, each 1/b of the size of
the original problem of size n.
Suppose each sub-problem of size n/b takes time to solve and since
there are a sub-problems so we spend total time to solve sub-
problems.
is the cost(or time) of dividing the problem of size n.
is the cost (or time) to combine the sub-solutions.
Thus in general, an algorithm which follow the divide and conquer strategy have the
following recurrence:
Where
T(n) = running time of a problem of size n
a means “In how many part the problem is divided”
means “Time required to solve a sub-problem each of size (n/b)”
D(n) + C(n) = f(n) is the summation of the time requires to divide the
problem and combine the sub-solutions.
MERGE_SORT (A, p, r)
1. if (p < r)
2. then q ← *(p + r)/2] /* Divide
3. MERGE_SORT (A, r, q) /* Conquer
4. MERGE_SORT (A, q + 1, r) /* Conquer
5. MERGE (A, p, q, r) /* Combine
To set up a recurrence T(n) for MERGE SORT algorithm, we can note down the
following points:
Base Case: MERGE SORT on just one element (n=1) takes constant time i.e.
44
Divide and Conquer
When we have n > 1 elements, we can find a running time as follows: Approach
(1) Divide: Just compute q as the middle of p and r, which takes constant
time. Thus
(2) Conquer: We recursively solve two sub-problems, each of size n/2, which
contributes
(3) Combine: Merging two sorted subarrays (for which we use MERGE (A,
p, r) of an n-element array) takes time , so .
Thus , which is a linear
function of n.
Thus from all the above 3 steps, a recurrence relation for MERGE_SORT (A, 1, n) in
the worst case can be written as:
Now after solving this recurrence by using any method such as Recursion-tree or
Master Method (as given in UNIT-1), we have .
This algorithms will be explained in detailed in section 2.4.2
Search is the process of finding the position (or location) of a given element (say x) in
the linear array. The search is said to be successful if the given element is found in the
array otherwise it is considered unsuccessful.
A Binary search algorithm is a technique for finding a position of specified value (say
x) within a sorted array A. the best example of binary search is ―dictionary‖, which
we are using in our daily life to find the meaning of any word. The Binary search
algorithm proceeds as follow:
(1) Begin with the interval covering the whole array; binary search repeatedly divides
the search interval by half.
(2) At each step, the algorithm compares the input key (or search) value x with the key
value of the middle element of the array A.
(3) If it matches, then a searching element x has been found, so then its index, or
position, is returned. Otherwise, if the value of the search element x is less than the
item in the middle of the interval; then the algorithm repeats its action on the sub-
array to the left of the middle element or, if the search element x is greater than the
middle element‘s key, then on the sub-array to the right.
45
Design Techniques (4) We repeatedly check until the searched element is found or the interval is empty,
which indicates x is ―not found‖.
BinarySearch_Iterative(A[1…n],n,x)
Output: This algorithm find the location of the search element x in linear array A. If
search ends in success, it returns the index of the searched element x, otherwise returns
-1 indicating x is “not found”. Here variable low and high is used to keep track of the
first element and last element of the array to be searched, and variable mid is used as
index of the middle element of the array under consideration. */
{
low=1
high=n
while(low<=high)
{
mid= (low+high)/2
if(A[mid]==x]
return mid; // x is found
else if(x<A[mid])
high=mid-1;
else low=mid+1;
}
return -1 // x is not found
}
Method1:
Let us assume for the moment that the size of the array is a power of 2, say . Each
time in the while loop, when we examine the middle element, we cut the size of the
sub-array into half. So before the 1st iteration size of the array is 2k.
After the 1st iteration size of the sub-array of our interest is: 2k-1
After the 2nd iteration size of the sub-array of our interest is:
……
…….
After the .iteration size of the sub-array of our interest is :
So we stop after the next iteration. Thus we have at most
.iterations.
Since with each iteration, we perform a constant amount of work: Computing a mid
point and few comparisons. So overall, for an array of size n, we perform
comparisions. Thus
Method 2:
46
We know that any problem, which is solved by using Divide-and-Conquer having a Divide and Conquer
Approach
recurrence of the form:
Since at each iteration, the array is divided into two sub-arrays but we are solving only
one sub-array in the next iteration. So value of a=1 and b=2 and f(n)=k where k is a
constant less than n.
Thus a recurrence for a binary search can be written as
11 22 30 33 40 44 55 60 66 77 80 88 99
Illustrate the working of binary search technique, while searching an element (say
ITEM)
(i) 40 (ii) 85
Solution
(a) Suppose ITEM = 40. The search for ITEM in the array DATA is pictured
in Fig.1, where the values of DATA[Low] and DATA[High] in each stage
of the algorithm are indicated by circles and the value of DATA[MID] by
a square. Specifically, Low, High and MID will have the following
successive values:
2. Since 40 < 55, High has its value changed by High = MID – 1 = 6.
Hence MID = so DATA
3. Since 40 < 30, Low has its value changed by Low = MID – 1 = 4.
Hence MID = so DATA
47
Design Techniques
We have found ITEM in location LOC = MID = 5.
(1) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99,
(2) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99
(3) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99 [Successful]
(b) Suppose ITEM = 85. The binary search for ITEM is pictured in Figure 2.
Here Low, High and MID will have the following successive values:
2. Since 85 > 55, Low has its value changed by Low = MID + 1 = 8. Hence
MID = so DATA
3. Since 85 > 77, Low has its value changed by Low = MID + 1 = 11. Hence
MID = so DATA
4. Since 85 > 88, High has its value changed by High = MID – 1 = 11.
Hence
MID = so
DATA
Since 85 > 80, Low has its value changed by Low = MID + 1 = 12. But now
Low > High, Hence ITEM does not belong to DATA.
(1) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99,
(2) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99
(3) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80 88, 99 [unsuccessful]
48
Using the binary search algorithm, one requires only about 20 comparisons to find Divide and Conquer
the location of an ITEM in an array DATA with elements, since Approach
7 14 17 25 30 48 56 75 87 94 98 115 200
Illustrate the working of binary search algorithm, while searching for ITEM
(i) 17 (ii) 118
6. Analyze the running time of binary search algorithm in best average and
worst cases.
2.4 SORTING
Sorting is the process of arranging the given array of elements in either increasing or
decreasing order.
2.4.1 MERGE-SORT
Merge Sort algorithm closely follows the Divide-and-conquer strategy. Merge sort on
an input array A [1… n] with n-elements (n > 1) consists of (3) Steps:
49
Design Techniques
Divide: Divide the n-element sequence into two sequences of length n/2 and n/2
(say A1 = A(1), (2), …..A[ n/2 ] and A2 A[ n/2 +1], ….A[n])
Conquer: Sort these two subsequences A1 and A2 recursively using MERGE SORT;
and then
Combine: Merge the two sorted subsequences A1 and A2 to produce a single sorted
subsequence.
n
Divide Array A into two halves of size
A *1+, A*2+ ……………………, Av*n+ n/2 and n/2
Where is a …operator and
Is a floor operator
Recursively Sort A1
A*1+, A*2+, ……..A* n/2 ] A[ n/2 +1+, ….., A*n+
and A2 using MERGE-
SORT
(A1) (A2)
Merge
Figure 6: Illustrate the operation of two- way merge sort algorithm. We assume to
sort the given array A [1…n] into ascending order. We divide the given array A [1..n]
into 2 subarrays: A[1, … n/2 ] and [ n/2 +1, …. n]. Each subarray is individually
sorted, and the resulting sorted subarrays are merged to produce a single sorted array
of n- elements.
For example, consider an array of 9 elements :{ 80, 45 15, 95, 55, 98, 60, 20, 70}
The MERGE-SORT algorithm divides the array into subarrays and merges them into
sorted subarrays by MERGE () algorithm as illustrated in arrows the (dashed line
arrows indicate the process of splitting and regular arrows the merging process).
50
Divide and Conquer
Approach
[1] [2] [3] [4] [5] [6] [7] [8] [9]
80 45 15 95 55 98 60 20 70
divide
merge
[1] [2] [6] [7] [8] [9]
45 80 20 60 70 98
1st, left half of the array with 5-elements is being split and merge; and next
second half of the array with 4-elements is processed.
Since, here we are always dealing with sub-problems, we state each sub-problem as
sorting a subarray A [p…r]. Initially p = 1 and r = n, but these values changes as we
recurse through sub-problems.
51
Design Techniques Thus, to sort the subarray A[p…r]
1) Divide: Partition A [p…r] into two subarrays A [p…q] and A [q+1…r], where q is
the half way point of A [p…r].
3) Combine: Merge the two sorted subarray A [p..q] and A[q+1..r] to produce a single
sorted subarray A[p..r]. To accomplish this step, we will define a procedure MERGE
(A, p, q, r).
Note that the recursion stops, when the subarray has just 1 element, so that it is
trivially sorted.
_____________________________________________________________________
Algorithm:
{
1. if (p < r) /* Check for base case
{
2. q ← (p + r)/2 /* Divide step
3. Merge-Sort (A,p,q) /* Conquer step
4. MERGE-SORT (A,q+1,r) /* Conquer step
5. MERGE (A,p,q,r) /* Combine step
}
}
_____________________________________________________________________
Intial Call is MERGE-SORT (A,1,n)
Next, we define Merge (A, p, q, r), which is called by the Algorithm MERGE-SORT
(A, p, q).
Merging
Output: The two subarrays are merged into a single sorted subarray in A [p..r]
52
Divide and Conquer
Approach
The following Algorithm merge the two sorted subarray A [p.. r] and A [q+1..r] into
one sorted output subarray A [p..r].
Merge (A, p, q, r)
1. n1 ← q – p + 1 // No. of elements in sorted subarray A [p..q]
2. n2 ← r – q // No. of elements in sorted subarray A[q+1 .. r]
Create arrays L [1.. n1 +1] and R [1..n1 +1]
3. for i ← 1 to n1
4. do L[i] ← A [p + I -1] // copy all the elements of A [p..r] into L [1 .. n1]
5. for j ← 1 to n1
6. do R [j] ← A [q + j] // copy all the elements of A [q + 1, .. r] into
R[1..n2]
7. L [n1 +1] ← ∞
8. R [n2 + 1] ← ∞
9. i ← 1
10. j ← 1
11. for k ← p to r
12. do if L [i] ≤ R [j]
13. then A [k] ← L [i]
14. i ← i +1
15. else A [k] ← R [j]
16. j←j+1
53
Design Techniques To understand both the algorithm Merge-Sort (A, p, r) and MERGE (A, p, q, r);
consider a list of (7) elements:
1 2 3 4 5 6 7
A 70 20 30 40 10 50 60
p q r
Figure 9: Merging algorithms
1 2 3 4 5 6 7
70 20 30 40 10 50 60
p q q+1 r
(1) This sub array is (4) this array
further sub-divided can be
subdivided as
1 2
70 20 30 40 10 50 60
(2)This subarray (3) This subarray (5) Again
is a again divided can be subdivided
subdivided
1 2 3 4 5 6
70 20 30 40 10 50
(6)Combine two (7) combine back
sorted subaaray to original array
back to original A[p..r]
(9)combine
array A[p…r]
1 2 \3 4 5 6
20 70 30 40 10 50
(10)
merge
20 30 40 50 60 10 50 60
54
Lets us see the MERGE operation more closely with the help of some example. Divide and Conquer
Consider that at some instance we have got two sorted subarray in A, which we have Approach
to merge in one sorted subarray.
Ex: 1 2 3 4 5 6 7
A 20 30 40 70 10 50 60
1 2 3 4 5 6 7
A 20 30 40 70 10 50 60
k
1 2 3 4 5 1 2 3 4
L 20 30 40 70 R 10 50 60
i J
Figure 12 (a) : Illustration of merging – II using line 1 to 10
In figure (a), we just copy the A[p…q] into L[1..n1] and A[q+1,…r] into R[1…n2]
Variable I and j both are pointing to 1st element of an array L & R, respectively.
Now line 11-16 of MERGE (A,p,q,r) is used to merge the two sorted subarray
L[1,…4] and R[1…4] into one sorted array [1…7]; (see figure b-h)
A 10
1 2 3 4 5 1 2 3 4
L 20 30 40 70 R 10 50 60
i j
(b)
55
Design Techniques
1
10 20
1 2 3 4 5 1 2 3 4
L 20 30 40 70 R 10 50 60
i j
(c)
1 2 3
A 10 20 30
1 2 3 4 5 1 2 3 4
L 20 30 40 70 R 10 50 60
i j
(d)
1 2 3 4
10 20 30 40
1 2 3 4 5 1 2 3 4
L 20 30 40 70 R 10 50 60
i j
(e)
1 2 3 4 5 6 7
10 20 30 40 50
1 2 3 4 5
L 20 30 40 70 R 10 50 60
i j
(f)
56
1 2 3 4 5 7
Divide and Conquer
10 20 30 40 50 60 Approach
1 2 3 4 5
L 20 30 40 70 R 10 50 60
i J
(g)
1 2 3 4 5 6 7
10 20 30 40 50 60 70
1 2 3 4 5
L 20 30 40 70 R 10 50 60
i j
(h)
For simplicity, assume that n is a power of 2 each divide step yields two sub-
problem, both of size exactly n/2.
- (1)
57
Design Techniques
1) Master Method:
-- (1)
We have: a = 2
b=2
f (n) = n
nlogba = nlog22 = n ; Now compare f(n) with nlog22 i-e (n log22 = n)
Since f(n) = n = O (nlog22) Case 2 of Master Method
T (n) = (nlogba. logn)
= (n. logn)
Recursion tree:
C. n C.n
C. n
C. n
C. C. C.n
C. C.
Log2n
C. C. C. C. C.n
T T T T
Figure A
C C C C C C C C C.n
Figure B
2.4.2 QUICK-SORT
Quick-Sort, as its name implies, is the fastest known sorting algorithm in practice.
The running time of Quick-Sort depends on the nature of its input data it receives for
sorting. If the input data is already sorted, then this is the worst case for quick sort. In
58
this case, its running time is O (n2). Inspite of this slow worst case running time, Divide and Conquer
Quick sort is often the best practical of this choice for sorting because it is remarkably Approach
efficient on the average; its expected running time in (nlogn).
3) Average Case (when input data is not sorted & Partition of array is not
unbalance as worst case) :
Quick Sort
The Quick sort algorithm (like merge sort) closely follows the Divide-and Conquer
strategy. Here Divide-and-Conquer Strategy involves 3 steps to sort a given subarray
A[p..r].
2) Conquer: These two subarray A [p…q-1] and A [q+1..r] are sorted by recursive
calls to QUICKSORT.
3) Combine: Since the subarrays are sorted in place, so there is no need to combine
the subarrays.
P r
A[1] A[2] - ----------------------------------------- A[n]
1 2 3 n
q
(Sub array of size ) (subarray of size )
The element of A[1…q - 1] The element of A [q+1..r]
is ≤ A[q] is ≥ A[q]
59
Design Techniques These two subarray A [p…q-1] and A[q+1..r] is further divided by recursive call to
QUICK-SORT and the process is repeated till we are left with only one-element in
each sub-array (or no further division is possible).
p
……. …… ………
…………………………………...1 1 1 1 1…1
QUICKSORT (A, p, r) uses a procedure Partition (), which always select a last
element A[r], and set this A[r] as a Pivot element at some index (say q) in the
array A[p..r].
The PARTITION () always return some index (or value), say q, where the array
A[p..r] partitioned into two subarray A[p..q-1] and A[q+1…r] such that A[p..q-1]
≤ A[q] and A[q] ≤ A[q+1…r].
60
Divide and Conquer
Pseudo code for PARTITION: Approach
PARTITION (A, p, r)
4: if A[j] ≤ r A [r]
5: i←i+1
8: return ( i+ 1)
}
The running time of PARTITION procedure is (n), since for an array A[a…n], the
loop at line 3 is running 0(n) time and other lines at code take constant time i.e. 0(1)
so overall time is 0(n).
A 2 8 7 1 3 5 6 4
1 2 3 4 5 6 7 8
x ← A*r+ = 4
I ← p-1 = 0
p r
2 8 7 1 3 5 6 4
i 1 2 3 4 5 6 7 8
2 8 7 1 3 5 6 4
i
61
Design Techniques
b)
2) j = 2; if A*2+ ≤ 4 i.e 8 ≤ 4 No
So line 5 – 6, will not be executed
Thus:
p r
2 8 7 1 3 5 6 4
i
c)
3) j = 3; if A*3+ ≤ 4 i.e 7 ≤ 4 No; so line 5-6 will not execute
4) j = 4; if A*4+ ≤ 4 i.e 1 ≤ 4 YES
So i ← i + 1 = 1 + 1 = 2
exchange (A*2+ ↔ A*4+)
1 2 3 4 5 6 7 8
2 81 7 1 8 3 5 6 4
i
d)
1 2 3 4 5 6 7 8
2 1 73 8 37 5 6 4
i
e)
6) j = 6; A*6+ ≤ 4 i.e 5≤ 4 NO
7) j = 7: A *7+ ≤ 4 i.e 6 ≤ 4 NO
Now for loop is now finished; so finally line 7 is execute i.e
exchange (A*4+ ↔ A*8+), so finally we get:
1 2 3 4 5 6 7 8
2 1 3 4 7 5 6 8
i
Finally
we return (i + 1) i.e (3 + 1) = 4; by this partition procedure;
Now we can easily see that all the elements of A*1, … 3+ ≤ A*4+; and all
the elements of A*5, ..8+ ≥ A*4+. Thus
i
2 1 3 4 7 5 6 8
To sort the entire Array A{1..8}; there is a Recursive calls to QuickSort on both the subarray
A*..3+ and A*5…8+.
62
Divide and Conquer
Approach
Performance of Quick Sort
The running time of QUICK SORT depends on whether the partitioning is balanced or
unbalanced. Partitioning of the subarrays depends on the input data we receive for
sorting.
Best Case: If the input data is not sorted, then the partitioning of subarray is balanced;
in this case the algorithm runs asymptotically as fast as merge-sort (i.e. 0(nlogn)).
Worst Case: If the given input array is already sorted or almost sorted, then the
partitioning of the subarray is unbalancing in this case the algorithm runs
asymptotically as slow as Insertion sort (i.e. (n2)).
n- elements
≈ elements ≈ elements
≈ ≈ ≈ ≈
n- elements
(n-1) elements
(n-2) elements
(n-3) elements
1
(b) Worst Case
63
Design Techniques n- elements
The best case behaviour of Quicksort algorithm occurs when the partitioning
Method 1: Using Master Method; we have a=2; b=2, f(n)=n and nlogba = nlog22 = n
C.n
Log2n
C.n
C C C C C C C C C.n
The worst case behaviour for QuickSort occurs, when the partitioning procedures one
region with (n-1) elements and one with 0-elements → completely unbalanced
partition.
In this case:
Recursion Tree:
C.n C.n
C.(n-1) o C.(C-1)
n C.(n-2) o C.(C-2)
C.2 C.2
C.1 o C.1
= = 0(n2)
Average Case
Quick sort average running time is much closer to the best case.
Suppose the PARTITION procedure always produces a 9-to-1 split so recurrence can
be:
65
Design Techniques
Recursion Tree:
C.n C.n
Log10/9n C.n
Log10n
C.n
(Objective questions)
1) Which of the following algorithm have same time complexity in Best, average and
worst case:
a) Quick sort b) Merge sort c) Binary search d) all of these
5) Suppose the input array A[1…n] is already in sorted order (increasing or decreasing)
then it is _______ case situation for QUICKSORT algorithm
a) Best b) worst c) average d) may be best or worst
70 35 5 85 45 88 50 10 60
66
Divide and Conquer
7) Show that the running time of MERGESORT algorithm is Approach
10) Show that the running time of QUICKSORT algorithm in the best case is
11) Show that the running time of QUICKSORT algorithm is when all
elements of array A have the same value.
12) Find the running time of QUICKSORT algorithm when the array A is sorted in
non increasing order.
Note: The algorithm, which we are discussing here, works for any number base, e.g.,
binary, decimal, hexadecimal etc. For simplicity matter, we use decimal
number.
The straight forward method (Broute force method) requires 0(n2) time to multiply two
n-bit numbers. But by using divide and conquer, it requires only 0(nlog23) i.e. 0(n1.59)
time.
A Divide & Conquer based algorithm splits the number X and Y into ②equal parts as:
n/2
X= a b = xn-1 xn-2.------------X X ┌n/2┐------------- x1 = a × 10 +
┌n/2┐ x0 b
n/2 n/2
X ┌n/2┐ n/2
Y= c d = yn-1 yn-2.------------X 1------------- = c × 10 +
┌n/2┐ y1 y0 d
n/2 n/2
67
Design Techniques Note: Both number X and Y should have same number of digits; if any number has
less number of digits then add zero‘s at most-significant bit position. So that we can
If X = 1026732
Y = 743914
Then X = 1026732 = 1026 x 103 + 732
Y = 0743914 = 0743 x 103 + 914
Now we can compute the product as:
Z = X.Y
After solving this recurrence using master method, we have: T(n) = θ(n2); So direct
(or Brute force) method requires O (n2) time.
if n 1
0(1)
T (n) =
3T (n / 2) 0(n)
Otherwise
Where 0(n) is the cost of addition, subtraction and digit shift (multiplications by
power of 10‘s), all these takes time proportional to ‗n‘.
68
Method 1: - (Master Method) Divide and Conquer
Approach
a=3
b=2
f(n) = n
nlogba = nlog23
f(n)=n=0(nlog23-є) case 1 of Master Method
T(n) = θ(nlog23)
θ(n1.59)
=
Now
69
Design Techniques
=
=
=
=
The product matrix C = A.B. = (Cij)i, j =1…n is also an (n x n) matrix, whose (i,j)th
elements is defined as:
The divide and conquer strategy is yet another way to compute the product of two (n x
n) matrices. Assuming that n is an exact power of 2 (i.e. n=2k). We divide each of A,
B and C into four
n n
matrices. i.e.
2 2
and B = where each Aij and Bij are sub matrices of size
n n
,
2 2
70
Divide and Conquer
Approach
Algorithm Divide and Conquer Multiplication (A,B)
1. n ← no. of rows of A
2. if n = 1 then return (a11 b11)
3. else
n n
4. Let Aij, Bij (for i,j = 1,2, be submatrices)
2 2
S.t and
7. Return
matrices).
2
Line 6 requires 4 = )
So the overall computing time, T(n), for the resulting Divide and conquer Matrix
Multiplication
2
T(n) = 8T + )
Now we can see by using V. Stressen‘s Method, we improve the time complexity of
matrix multiplication from O(n3) to O (n2.81).
71
Design Techniques
3) Strassen’s Method
Volker Strassen had discovered a way to compute the Cij of Eg. (2) using only (7)
multiplication and (18) additions / subtractions.
1) Let
P1 = (A11+A22) (B11+B22)
P2 = (A21+A22).B11
P3 = A11 (B12-B22)
P4 = A22 (B21-B11) (I)
P5 = (A11+A12).B22
P6 = (A21-A11) (B11+B12)
P7 = (A12-A22) (B21+B22)
C1 = P1+ P4 – P5 + P7
C12 = P3+ P5
(II)
C21 = P2+ P4
C22 = P1+ P3 - P2 + P6
(1) n 2
T(n) =
7T n2 Otherwise
72
Divide and Conquer
Approach
Strassen showed how the matrix C can be computed using only 7 block
multiplications and 18 block additions or subtractions (12 additions and 6
subtractions):
= x =
C11 = P21 + P4 – P5 + P7 =
C12 = P3 + P5 =
C21 = P2 + P4 =
C22 = P1 + P3 – P2 + P6 =
73
Design Techniques
C =
0(1) if n 1
T ( n)
7T (n / 2) 0(n 2 )
Otherwise
a = 7; b = 2; f(n) = n2
n b = nlog27 = n2.81
log a
T (n) = Q (nlog27)
= Q (n2.81).
(Objective questions)
4) Strassen‘s matrix multiplication algorithm (C = AB),if the matrices A and B are not
of type 2n 2n , the missing rows and columns are filled with ___________.
(a) (b) 1‘s (c) -1‘s (d) 2‘s
A= B=
2.7 SUMMARY
Many useful algorithms are recursive in structure as they make a recursive call to
itself until a base (or boundary) condition of a problem is not reached. These
algorithms closely follow the Divide and Conquer approach.
Divide and Conquer is a top-down approach, which directly attack the complete
instance of a given problem and break down into smaller parts.
Where
T(n) = running time of a problem of size n
a means ―In how many part the problem is divided‖
means ―Time required to solve a sub-problem each of size (n/b)‖
D(n) + C(n) = f(n) is the summation of the time requires to divide the problem
and combine the sub-solutions.
75
Design Techniques The following table summarizes the recurrence relations and time complexity
of the various problems solved using Divide-and-conquer.
Quick Sort
Best case:
Worst Case:
Average:
Merge sort
Multiplication
of two n-bits Best case or worst:
numbers
Worst case:
2.8 SOLUTIONS/ANSWERS
Worst case:
Since at each iteration, the array is divided into two sub-arrays but we are solving only
one sub-array in the next iteration. So value of a=1 and b=2 and f(n)=k where k is a
constant less than n.
76
Divide and Conquer
; by solving this recurrence using substitution method, Approach
we have:
Using any method such as Recursion-tree or Master Method (as given in UNIT-1), we
have .
Solution 8:
Let
1 2 3 4 5 6 7 8 9 10
A[1…10] = 35 10 40 5 60 25 55 30 50 25
x
Here p = 1 r = 10
(1)
(2)
i.e.,
p r
1 2 3 4 5 6 7 8 9 10
10 35 40 5 60 25 55 30 50 25
i
77
Design Techniques
(3)
(4)
i.e.,
1 2 3 4 5 6 7 8 9 10
10 5 40 35 60 25 55 30 50 25
i
(5)
(6) j
i.e.,
1 2 3 4 5 6 7 8 9 10
10 5 25 35 60 40 55 30 50 25
i
(7)
(8)
78
Divide and Conquer
Approach
(9)
i.e.,
1 2 3 4 5 6 7 8 9 10
10 5 25 20 60 40 50 30 50 35
i
Here PARTITION procedure finally return index (i+1)=4, where the array gets
partitioned. Now the two sub-arrays are
15 1 25 60 40 50 25 50 36
Solution 10: A recurrence relation for QUICKSORT algorithm for best case can be
written as:
Using any method such as Recursion-tree or Master Method (as given in UNIT-1), we
have .
Solution 11: when all elements of array A have the same value, then we have a worst
case for QUICKSORT and in worst case QUICKSORT algorithm requires
time.
Solution 12: when the array A is sorted in non increasing order, then we have a worst
case for QUICKSORT and in worst case QUICKSORT algorithm requires
time.
Solution 6
1026732 × 732912
Though, the above may be simplified in another simpler way, yet we want to
explain Karatsuba‘s method, therefore, next, we compute the products.
U = 1026 × 732
V = 732 × 912
P = 1758 × 1644
Let us consider only the product 1026 × 732 and other involved products may
be computed similarly and substituted in (A).
Let us write
2 2
U = 1026 × 732 = (10 × 10 + 26) (07 × 10 + 32)
4
= (10 × 7) 10 + 26 × 32 + [(10 + 7) (26 + 32)
2
─ 10 × 7 ─ 26 × 32)] 10
4 2
= 17 × 10 + 26 × 32 + (17 × 58 ─ 70 ─ 26 × 32) 10
At this stage, we do not apply Karatsuba‘s algorithm and compute the products of 2-
digit numbers by conventional method.
Solution7:
Accordingly,
, , ,
80
Divide and Conquer
From the above products, we can compute C as follows: Approach
C1 = P1+ P4 – P5 + P7 =[120]+[35]-[72]-[60]=[23]
C12 = P3+ P5 = [-20]+[72]=[52]
C21 = P2+ P4 = [13]+[35]=[48]
C22 = P1+ P3 - P2 + P6 =[120]+[-20]-[13]+[6]=[93]
81