You are on page 1of 19

Analysis & Design of Algorithms

Divide & • Prepared by:-


Conquer Sagar Virani

Assistant Professor

Computer Engineering
Department

VVP Engineering College


Divide-and-Conquer
• The whole problem we want to solve may be too big to understand or
solve at once.

• We break it up into smaller pieces, solve the pieces separately, and


combine the separate pieces together.
Divide-and-Conquer
• Divide the problem into several sub-problems
– Similar sub-problems but of smaller size

• Conquer the sub-problems


– Solve the sub-problems recursively

– Sub-problem size small enough  solve the problems in straightforward


manner

• Combine the solutions of the sub-problems


– Obtain the solution for the original problem

• The base case for the recursion are subproblems of constant size.
Divide-and-Conquer
• Merge Sort

• Quick Sort

• Binary Search

• Recursive Fibonacci Series

• Multiplication of Large Integers

• Max-Min Problem

• Strassen’s Matrix Multiplication

• Exponential

• Tower of Hanoi
Divide-and-Conquer
When to use Divide and Conquer Approach?
• Both paradigms, Divide & Conquer and Dynamic Programming divide
the given problem into subproblems and solve subproblems.

• Divide and Conquer should be used when same subproblems are not
evaluated many times i.e. subproblems are non-overlapping.

• Otherwise Dynamic Programming or Memorization should be used.

• For example,
 Divide and Conquer – Binary Search algorithm.

 Dynamic Programming – Calculating nth Fibonacci number.


Recursive Fibonacci Series
• Program:
int fib(int n)

if ( n <= 1 )

return n;

return fib(n-1) + fib(n-2);

• There are many subproblems which are solved again and again.
Recursive Fibonacci Series
• Recursive Tree:
Fib 5

Fib 4 Fib 3

Fib 3 Fib 2 Fib 2 Fib 1

Fib 2 Fib 1 Fib 1 Fib 0 Fib 1 Fib 0

Fib 1 Fib 0
Recursive Fibonacci Series
• Recursive Tree:
Fib 5

Fib 4 Fib 3

Fib 3 Fib 2 Fib 2 Fib 1

Fib 2 Fib 1 Fib 1 Fib 0 Fib 1 Fib 0

• Function fib(3) is being called 2 times.


Fib 1 Fib 0
• Instead, store the value of fib(3), and reused the old
stored value when required.
When to use Divide-and-Conquer
Approach?
• The problem lends itself to division into sub-problems of the same
type.

• The sub-problems are relatively independent of one another i.e. non-


overlapping.

• An acceptable solution to the problem can be constructed from


acceptable solutions to sub-problems.
Multiplication of Large Integers
• Consider the problem of multiplying two (large) n-digit integers
represented by arrays of their digits such as:
A = 12345678901357986429 B = 87654321284820912836
• The grade-school algorithm:
a b … an
c d … bn
(d10) d11d12 … d1n
(d20) d21d22 … d2n
…………………
(dn0) dn1dn2 … dnn
• Efficiency: Θ(n2) single-digit multiplications
Multiplication of Large Integers
• Example: A  B where A = 2135 and B = 4014
A = (21  102 + 35)
B = (40  102 + 14)
• So, A  B = (21  102 + 35)  (40  102 + 14)
= 21  40  104 + (21  14 + 35  40)  102 + 35  14

• In general, if A = ab and B = cd (where A and B are n-digit, a, b, c, d are n/2-digit


numbers),
• A  B = a  c  10n + (a  d + b  c)  10n/2 + b  d

• Recurrence for the number of one-digit multiplications M(n):


T(n) = 4T(n/2), M(1) = 1
Solution: T(n) = Θ n2
Multiplication of Large Integers
• A  B = a  c  10n + (a  d + b  c) 10n/2 + b  d

• The idea is to decrease the number of multiplications from 4 to 3:

• (a + b )  (c + d ) = a  c + (a  d + b  c) + b  d,

• i.e., (a  d + b  c) = (a + b )  (c + d ) - a  c - b  d

• Requires only 3 multiplications at the expense of (4 – 1 = 3) extra


add/sub.

• Recurrence for the number of multiplications T(n):


T(n) = 3T(n/2), M(1) = 1
Solution: T(n) = 3log 2n = nlog 23 ≈ n1.585
Multiplication of Large Integers
• Example: A = 2135 and B = 4014
• Let a = 21, b = 35, c = 40 , d = 14
• A  B = a  c  10n + (a  d + b  c)  10n/2 + b  d
• Now putting , (a  d + b  c) = (a + b )  (c + d ) - a  c - b  d
• A  B = a  c  10n + ((a + b )  (c + d ) - a  c - b  d)  10n/2 + b  d
• Let,
p = a  c, q = b  d, r = (a + b )  (c + d )
= 21  40, = 35  14, = (21 + 35 )  (40 + 14 )
= 840, = 490, = (56)  (54)
• A  B = p  10n + (r - p - q)  10n/2 + q
= 840  104 + ((56 )  (54) - 840 - 390) 102 + 490
= 8569890
Max-Min Problem
• The Max-Min Problem - to find the maximum and minimum value in an array.
Naïve Method
• In this method, the maximum and minimum number can be found separately.
Algorithm: Max-Min(A)
max = A[1]
min = A[1]
for i = 2 to n do
if A[ i ] > max then
max = A[ i ]
if A[ i ] < min then
min = A[ i ]
return (max, min)
Analysis
The number of comparison = 2n – 2.
Multiplication of Large Integers
• The number of comparisons can be reduced using the divide and conquer
approach. Following is the technique.
Divide and Conquer Approach
• Divide the array into two halves.
• Using recursive approach maximum and minimum numbers in each halves
are found.
• Finally return the maximum of two maxima of each half and the minimum of
two minima of each half.
• In this given problem, the number of elements in an array is r − p + 1,
where r is greater than or equal to p.
• Max−Min(p, r) will return the maximum and minimum values of an
array A[p...r].
Multiplication of Large Integers
Algorithm: Max–Min(p, r)

if r – p ≤ 1 then

return ((max(A[p], A[r]), min(A[p], A[r]))

else

(max1, min1) = Max-Min(p, ((p + r) / 2) )

(max2, min2) = Max-Min( ((p + r) / 2) + 1) , r)

return (max(max1, max2), min(min1, min2))


Multiplication of Large Integers
Analysis

• Let T(n) be the number of comparisons made by Max−Min(p, r), where the

number of elements n= r − p + 1.

• If T(n) represents the numbers, then the recurrence relation can be

represented as

• Let, n = 2k where k is height of the recursion tree.

• So, T(n) = 2 * T( ) + 2

= 2 * (2 * T( ) + 2) ..... = − 2 = 1.5n – 2.
Multiplication of Large Integers
Analysis

• Compared to Naïve method, in divide and conquer approach, the number

of comparisons is less.

• However, using the asymptotic notation both approaches are represented

by O(n).
Any Questions?

You might also like