Unit 3 For ADA
Unit 3 For ADA
Unit 3 For ADA
1. Introduction:
• Divide the problem into a number of sub-problems. There must be base case (to stop
recursion).
• Conquer (solve) each sub-problem recursively
• Combine (merge) solutions to sub-problems into a solution to the original problem
Algorithm DandC(P)
{
else{
Types of methods:
-Substitution Method
-Iteration Method
-Recursion Tree Method
-Master Method
2.1 Substitution Method: This is the method of solving recurrence in two steps:
- guess a bound (upper) or solution of problem(equation).
- use mathematical induction to prove our guess is correct.
Example 1:
Guess that solution is T(n)=O(n*log(n)). This is upper bound of equation thus we have to prove
that
Hence, T(n)=O(n*log(n))
Using mathematical induction, we need to prove that our assumption holds true for base case
also
Example 2:
Example:
T(n)=2T(n/3)+n _____(1)
Expanding above terms
Iteration 1:
Assume n=n/3
T(n/3)=2T(n/9)+n/3 ____(2)
T(n)=2[2T(n/9)+n/3]+n
= n+2/3n+4T(n/9) _________(3)
Iteration 2:
T(n)=n+2/3n+4/9n+8T(n/27) _________(4)
log(n)=log(3i)
i=logn3
T(n)=n+2/3n+(2/3)2n+8T(n/27)+……….+2logn3 T(1)
<= 3n + O(n)
Example 1:
Example 2:
Thus, case 2:
wx=1234=102w+x _________(1)
yz=0981=102y+z ___________(2)
Now, n1*n2=wx*yz
= (102w+x)* (102y+z)
= 104wy+102(wz+xy)+xz
=104(12*9)+ 102((12*81)+(34*9))+(34*81)
=1080000+127800+2754
=1210554
wy, wz, xy, xz total 4 multiplication required of n/2 size and 3 addition required to get
final answer
4. Binary Search:
The binary search algorithm begins by comparing the target value to value of the middle
element of the sorted array. If the target value is equal to the middle element's value, the
position is returned. If the target value is smaller, the search continues on the lower half of
the array, or if the target value is larger, the search continues on the upper half of the array.
Example :
Algorithm in c:
#include <stdio.h>
int main()
{
int i, first, last, middle, n, x, a[100];
first = 0;
last = n - 1;
mid = (first+last)/2;
return 0;
}
Time Complexity:
From example we analyze that each iteration problem is divided into n/2i where i>1
Iteration 1: n elements
……………………….
………………………..
Thus, T(n)=T(n/2) +1
T(n)=O(Log(n))
5. Min-Max Problem:
Normal strategy (Needs 2n - 3 compares)
-Find MAX (n - 1 compares)
- Find MIN of the remaining elements (n - 2)
Divide and Conquer strategy
– Split the array in half
– Find the MAX and MIN of both halves
– Compare the 2 MAXes and compare the 2 MINs to get overall MAX and MIN.
• In this example, we only consider the number of comparisons and ignore all other
operations.
Example:
array: A = [8, 7, 2, 1, 5, 6, 3, 4] size of array is 8
Iteration 1:
Divide Array into [8, 7, 2, 1] and [5, 6, 3, 4]
From iteration 2 we get values max1=8 and max2=6 so final max=8
min1=1 and min2=3 so final min=1
Iteration 2:
Divide Array into [8,7] and [2,1]
Thus, max1=8, min1=7 and max2=2 and min2=1
So, final max=maximum(8,2)=8 and min=minimum(7,1)=1
Algorithm:
#include<stdio.h>
#include<conio.h>
maxmin(int,int,int,int);
int a[20],max,min,max1,min1;
main()
{ int n,i;
printf("enter number of elements in the array\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the number\n");
scanf("%d",&a[i]);
}
maxmin(0,n-1,max,min);
printf("the maximum element is- %d\n",max);
printf("the minimum element is- %d\n",min);
getch();
else if(j=i+1)
{
if(a[i]<a[j])
{
max=a[j]; min=a[i];
}
else
{
max=a[i]; min=a[j];
}
}
else
{
int mid=(i+j)/2;
maxmin(i,mid,max1,min1);
maxmin(mid+1,j,max2,min2);
min=minimum(min1,min2);
max= maximum(max1,max2);
}
}
int minimum(int m1,int m2)
{
if(m1<m2)
{
return m1 ;
}
else
{
return m2;
}
}
int miximum(int m1,int m2)
{
if(m1>m2)
{
return m1 ;
}
T(n)= 0 if n=1;
T(n)={ 1
2T(n/2) + 2
if n=2;
if n>2;
6. Merge sort:
A merge sort works as follows:
1. Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is
considered sorted).
2. Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist
remaining. This will be the sorted list.
• Merge
[2, 3, 4, 5, 6, 7, 8, 10 ]
Algorithm in C:
#include<stdio.h>
int main(){
int a[50],i,n;
partition(a,0,n-1);
return 0;
}
if(low<high){
mid=(low+high)/2;
partition(a,low,mid);
partition(a,mid+1,high);
mergeSort(a,low,mid,high);
}
}
int i,j,c[MAX];
i=low;
j=mid+1;
while((i<=mid)&&(j<=high)){
if(a[i]<=a[j]){
c[k]=a[i];
i++;
k++;
}
else{
c[k]=a[j];
j++;
k++;
}
}
while(i<=mid)
{
c[k]=a[i];
i++;
k++;
}
while(j<=high)
{
c[k]=a[j];
j++;
k++;
}
Sample output:
7. Quick Sort:
Given an array of n elements (e.g., integers):
• If array only contains one element, return
• Else
– pick one element to use as pivot.
– Partition elements into two sub-arrays:
• Elements less than or equal to pivot
• Elements greater than pivot
– Quicksort two sub-arrays
– Return results
Example:
Given a pivot, partition the elements of the array such that the resulting array consists of:
1. One sub-array that contains elements >= pivot
2. Another sub-array that contains elements < pivot
while(a[i]<pivot){
i++;}
while(a[j]>=pivot)
j—;
Interchange(pivot, a[j])
#include<stdio.h>
int main(){
int a[20],n,i;
quicksort(a,0,n-1);
return 0;
}
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(a[i]<=x[pivot]&&i<last)
i++;
while(a[j]>x[pivot])
j--;
if(i<j){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[pivot];
}
}
Output:
Enter size of the array: 5
Enter 5 elements: 3 8 0 1 2
Sorted elements: 0 1 2 3 8
Time Complexity:
• Best case running time: O(n log2n)
• Worst case running time:
Recursion:
Partition splits array in two sub-arrays:
• one sub-array of size 0
• the other sub-array of size n-1
• Quicksort each sub-array
• Depth of recursion tree is O(n)
• Number of accesses per partition is O(n) so, total time T(n)=O(n2)
So, T(n)=O(n3)
A= A11 A12
A21 A22
B= B11 B12
B21 B22
C= C11 C12
C21 C22
C11 = P + S – T + V
C12 = R + T
C21 = Q + S
C22 = P – Q + R + U
Time Complexity:
Here, 7- Multiplication takes 7T(n/2) and 18- Addition takes 18n time then we can write
T(N)=7T(n/2)+18n
Solve using master method
T(n)≈O(n2.81)
9. Exponential:
void power(a,n)
{
res=1
while(n>0)
{
res=res*a;
n--;
}
}
While loop iterate n times so time complexity of algorithm is T(n)=O(n)
*************THE END**************