0% found this document useful (0 votes)
11 views46 pages

CH 3

CH 3 OF ADA

Uploaded by

Eva Watts'
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views46 pages

CH 3

CH 3 OF ADA

Uploaded by

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

Unit 3 Divide & Conquer

Algorithm
Divide and Conquer Technique
• In algorithmic methods, the design is to take a dispute on a huge input,
break the input into minor pieces, decide the problem on each of the small
pieces, and then merge the piecewise solutions into a global solution.
• This mechanism of solving the problem is called the Divide & Conquer
Strategy.
• Divide: Break the original problem into a set of sub-problems.
• Conquer: Solve every sub-problem individually, recursively.
• Combine: Put together the solutions of the sub-problems to get the solution
to the whole problem.
Divide and Conquer Technique
Algorithm DC(P)
{
if P is too small then
return solution of P
else
{ Divide(P) and obtain P1, P2, P3, …. Pn
where n >= 1
Apply DC to each subproblem
return combine (DC(P1), DC(P2)...(DC(Pn));
}
}
Divide and Conquer Technique
Recurrence relation:
T(n) = g(n) if n is small
T(n1) + T(n2) + …….T(nr) + F(n) when n is sufficiently large

The total time t(n) taken by this divide and conquer algorithm is something like
T(n) = a T(n / b) + g(n)
So Time complexity of Divide and Conquer can be identified by Master Theory.
Divide and Conquer Technique

Problem of size n

Subproblem-1 of size Subproblem-2 of size


n/2 n/2

Solution to sub Solution to sub


problem-1 problem-2

Solution to original
problem
Problem solving using Divide and Conquer
1. Binary Search
2. Quick Sort
3. Merge Sort
4. Min max problem
5. Matrix Multiplication
6. Exponential
Multiplying large integers problems
• General formula for multiplying large integer problems
c=a*b
c=+
Where n is total number of digits in the integer
=
=
= (+ (
Example
• Multiply 2101 and 1130
c=a*b
=2101*1130
Divide the numbers in equal halves.
=21, =01, =11, =30
= = 21*11 = 231
= = 01*30 = 30
= (+(
= (01+21) * (11+30) – (231+30) = (22*41) – 261 =641

a*b = +
= 231* + 641*+ 30
= 2374130
Binary search
• Binary search is an efficient method. While searching the elements using
this method the most essential thing is that the elements in the array should
be sorted one.
• An element which is to be searched from the list of elements stored in array
A[0…n-1] is called KEY element.
• Let A[m] be the mid element of array A. then there are three conditions that
needs to be tested while searching the array using this method.
1. If KEY= A[m] then desired element is present in the list.
2. Otherwise if KEY<A[m] then search the left sub list.
3. Otherwise if KEY>A[m] then search the right sub list.
Binary search
• This can be represented as,
A[0]… A[m-1] A[m] A[m+1]…A[n-1]

Search here if Search here if


KEY ?
KEY < A[m] KEY > A[m]
Example
• Apply binary search method to search 60 from the list 10,20,30,40,50,60,70
0 1 2 3 4 5 6
10 20 30 40 50 60 70

Low High

• m=(low+high)/2
= (0+6)/2 = 3
Check A[m]=KEY
A[3]=60 ??
A[3]=40 and 40<60
Now search right sub list.
Example
• Right sub list is
4 5 6
50 60 70

Low High

• m=(low+high)/2
= (4+6)/2 = 5
Check A[m]=KEY
A[5]=60 ??
A[5]=60
The number is present in the list.
Algorithm
Searching the element using binary search.
low0
highn-1
while(low<high) do
{
m(low+high)/2
if(KEY=A[m])then
return m
elseif(KEY<A[m])then
highm-1 // search the left sub list
else
lowm+1 // search the right sub list

}
return -1 // if element is not present in the list
Analysis
• Time complexity of binary search.
Best case Average case Worst case
Θ(1) Θ( Θ(

• Advantages:
• Optimal searching algorithm using which we can search the desired element very
efficiently.
• Disadvantages:
• Algorithm requires the list to be sorted.
• Applications:
• Search records from the database.
• For solving non linear equation with one unknown.
Sequential search vs. Binary Search
Sr Sequential Search Binary Search
no.
1 Simple technique of searching an Efficient technique of searching an
element. element.
2 This technique does not require the This technique requires the list to be
list to be sorted. sorted.

3 Every element of the list may get Only the mid element of the list is
compared with the key element. compared with the key element.

O(n).
4 Worst case time complexity is Worst case time complexity is O(
Max-Min Problem
• Problem : Find Minimum and Maximum number from the given list.
• Example
50 40 -5 -9 45 90 65 25 75

n-1 comparisons to find min value


n-1 comparisons to find max value
So 2n-2 comparisons in Classes method.
Max-Min Problem
Algorithm
Algorithm Max_Min ( A[1…n], Max,Min)
{
//problem description: the algorithm obtains min and max values
Max  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]
}
}
Example
• Find max and min from the given list
22,13,-5,-8,15,60,17,31,47
0 1 2 3 4 5 6 7 8
22 13 -5 -8 15 60 17 31 47

• Step-1: divide the list into sub list.


0 1 2 3 4 5 6 7 8
22 13 -5 -8 15 60 17 31 47

• Step-2: further divide the sub lists.


0 1 2 3 4 5 6 7 8
22 13 -5 -8 15 60 17 31 47

• Step-3: divide the list(0…2) further into sublists


0 1 2 3 4 5 6 7 8
22 13 -5 -8 15 60 17 31 47
Example
• Step-4: we will combine list(0…1) and(2).
the min=-5, max=22 combine the list(0…2) and (3,4).
min=-8 and max=22
• Step-5: combine (5,6) and (7,8)
min= 17 and max=60
• Step-6: combine (0…4) and (5…8)
min=-8 and max=60
Merge Sort
• The merge sort is a sorting algorithm that uses the divide and conquer strategy.
• Merge sort on an input array with n elements consists of three steps:
• Divide: partition array into two sub lists s1 and s2 with n/2 elements each.
• Conquer: sort sub lists s1 and sub lists s2.
• Combine: merge s1 and s2 into a unique sorted group.
• Example: consider the elements as
38,27,43,3,9,82,10
Merge Sort
Merge Sort
Algorithm Mergesort(A[0..n-1] , low , high)
{
if (low < high) then
{ mid  (low + high)/2
Mergesort(A , low , mid)
Mergesort(A , mid+1, high)
Combine(A , low , mid , high)
}
}
Merge Sort
Algorithm Combine (A[0..n-1] , low , mid , high) k  k+1
{ }
k  low; }
i  low; while (i <= mid) do
j  mid+1; {
while (i<= mid and j<= high) do temp[k]  A[i]
{ if(A[i] <= A[j]) then i i+1
{ k  k+1
temp[k]  A[i] }
i  i+1 while (j<=high) do
k  k+1 { temp[k]  A[j]
} j  j+1
else k  k+1
{ }
temp[k]  A[j]
j  j++
Merge Sort analysis
Here,
T(n) = T(n/2) + T(n/2) + cn
Time taken by Time taken by Time taken by
left sub list right sub list combining two
sub lists

=2T(n/2) + cn

So,
a=2, b=2, k=1, p=0
so, as per master theorem
T(n) = Ɵ(nlogb a logp+1 n)
= Ɵ(n logn)
Quick Sort
Quick sort algorithm is a sorting algorithm that uses the divide and conquer
strategy.

In this method division is dynamically carried out.

• First divide using Pivot element.


• Then recursively sort the sub parts.
• Finally combine all the elements in a group to form a list of
sorted elements. At the time of combining of sub parts,
comparison is not required.

Let’s understand using example . . .


Quick Sort example
Quick Sort example
i<j

i<j

Here i>j
Quick Sort example

i<j

Now
condition
false
i>j
Quick Sort example

Now
condition
false
i>j
Quick Sort algorithm
Algorithm Quick(A[0 . . .n-1] , low , high)
{
if (low < high)
then m partition(A[low. . . high])

Quick(A[low . . .m-1])
Quick(A[m+1 . . .high])
}
Quick Sort algorithm
Algorithm Partition (A[low . . . high])
{
pivot  A[low]
i  low
j  high
while(i < j) do
{ while(A[i] < pivot) do // Move left to right
ii+1
while(A[j] >= pivot) do // Move right to left
jj–1
if( i < j) then
swap(A[i] , A[j])
}
swap(A[low] , A[j])
return j
}
Quick Sort analysis
• As per master method
T(n) = aT(n/b) + f(n)
in Quick sort every time there will be two parts so a=2, b=2.

T(n) = 2T(n/2) + n
So, a = 2, b=2, k=1, p=0
Hence,
T(n) = Ɵ(nlogb a logp+1 n)
= Ɵ(n logn)
Matrix Multiplication
• Suppose we want to multiply two matrices of size N x N: for example
C= A x B

A11 A12 B11 B12


A = A21 A22 and B= B21 B22

C11 = A11B11 + A12B21


C12 = A11B12 + A12B22
C21 = A21B11 + A22B21
C22 = A21B12 + A22B22

2x2 matrix multiplication can be accomplished in 8 multiplication.


Strassen’s matrix multiplication
A11 B11 + A12B21 A11B12 + A12B22
C =
A21B11 + A22B21 A21B12 + A22B22
• S1=(A11+A22)(B11+B22)
• S2=(A21+A22)B11
• S3=A11(B12-B22)
• S4=A22(B21-B11)
• S5=(A11+A12)B22
• S6=(A21-A11)(B11+B12)
• S7=(A12-A22)(B21+B22)

• C11=S1+S4-S5+S7
• C12=S3+S5
• C21=S2+S4
• C22=S1+S3-S2+S6
Continue . . .
• Now

C11= S1+ S4 - S5 + S7 C12 = S3 + S5


C=
C21 = S2 + S4 C22 = S1+S3-S2+S6

For example
C11 = S1+ S4 - S5 + S7
C11 = (A11 + A22)(B11+B22) + A22 (B21 – B11) – (A11 + A12) B22 + (A12 – A22) (B21 + B22)
C11=A11B11 + A11B22 + A22B11 + A22B22 + A22B21 – A22B11 – A11B22 – A12B22 + A12B21
+ A12B22 – A22B21 – A22B22

C11=A11B11 + A12B21
Continue . . .
• It is therefore possible to multiply two 2x2 matrices using only seven scalar
multiplications.
• Let, t(n) be the time needed to multiply two nxn matrices.
• Here matrices can be added and subtracted in a time in Ө(n2).
So, t(n) = 7 t(n/2) + g(n)
When g(n) Є Ө(n2)
a= 7 , b= 2, P =0, k=2
Case-1 is true. So,
T(n) = Ө(nlog27)
= Ө(n2.81).
Exponential
• Suppose : an
• Here a is any constant value and n is power of a.
Example :
a5 = a * a * a * a *a
Here total 4 multiplications are required.

It means that for an total n-1 multiplications are required.

Now how to reduce number of multiplications.


Exponential
• Recurrence equation for exponential

• Note : Definition – A recurrence is an equation that describes a function in terms of


its value on smaller inputs

• Which involves only three multiplications and four squaring instead of the 28
multiplications required. The above recurrence gives rise to the following algorithm.

• Find number of multiplication required for a 15.


• Find number of multiplication required for a 36.
Continue…
• Find number of multiplication required for a15.
a15 = a a14 = a (a7)2 = a (a a6) 2 = a (a(a3)2)2 = a(a(a a2)2)2 = a(a(a(a)2)2)2 (no of
multiplication =6)

• Find number of multiplication required for a36.


a36 = ((a18)2) = ((a9)2)2 = ((a a8)2)2 = ((a (a4)2)2)2 = ((a ((a2)2)2)2)2 = ((a
((a2)2)2)2)2 (no of multiplication =6)

• ALGORITHM
To find number of multiplication required

Example : a15
So, N(15) = ?

Amit G. Maru Gyanmanjari


Institute of Technology, Bhavnagar
To find number of multiplication required

Example:

N(36) = N(18)+1 = N(9)+2 = N(8)+3 = N(4)+4 = N(2)+5 = N(1)+6 =


0+6 = 6
To find number of multiplication required

Example:

N(36) = N(18)+1 = N(9)+2 = N(8)+3 = N(4)+4 = N(2)+5 = N(1)+6 =


0+6 = 6
Exponentiation
• Suppose : an
• Here a is any constant value and n is power of a.
Example :
a5 = a * a * a * a *a
Here total 4 multiplications are required.
It means that for an total n-1 multiplications are required.
Now how to reduce number of multiplications.
• Recurrence equation for exponential
Exponentiation
• Analysis: The Algorithm take O(logn) time to compute exponentiation.

Algorithm Exponentiation(x,n)
{
if(n=0) then
return 1;
else
r= Exponentiation(x,n/2);
if(n%2==0) then
return (r*r) //when exponentiation value is even
else
return (r*r*x) //when exponentiation value is odd
}
Thank You

You might also like