algorithm-D-A-C

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Department of Computer Science 3rd yr: Bule Hora University

DIVIDE – AND - CONQUER


The Divide and Conquer strategy suggests splitting the ‘n’ inputs into k distinct subsets, 1<k≤n,
yielding k sub problems. These sub problems must be solved, and then a method must be found
to combine sub solutions into a solution of the whole. If the sub problems are still relatively
large, then the divide and conquer strategy can possibly be reapplied. Often the sub problems
resulting from a divide-and-conquer design are of the same type as the original problem. For
those cases the reapplication of the divide-and-conquer principle is naturally expressed by a
recursive algorithm. Now smaller and smaller sub problems of the same kind are generated until
eventually sub problems that are small enough to be solved without splitting are produced.
Abstract view of Divide-And-Conquer algorithm:
Algorithm D-A-C (N)
{
If small (N) then return S (N)
Else
{
Divide N into smaller instances N1, N2, N3,….Nk, k≥1;
Apply D-A-C to each of these sub problems;
return Combine[D-A-C(N1), D-A-C(N2), D-A-C(N3),…. D-A-C(Nk)];
}
}
Computing time of the above algorithm can be expressed in terms of a recurrence relation T(n)
= g(N) N is small
T(N1)+T(N2)+…..+T(Nk)+f(N) otherwise
T(1) if N=1
= aT(N/b)+f(N) if N>1

1. Binary Search:
1.1). Algorithm BSearch(A, f, l, e) // RECURSIVE VERSION
// Given an array A[f..l] of elements in increasing order. ‘ e’ s the element to be
// searched for. If ‘e’ is present, return ‘i’ such that e=A[i]; else return 0.
{
If (f = l) then
{
If (e = A[f]) then return(f)
Else return(0)
}
Else // reduce list in to smaller sub list.
{
mid = (f+l)/2
if (e=A[mid]) then return(mid)
else if (e<A[mid]) then return(Bsearch(A, f, mid-1, e) )
else return(Bsearch(A, mid+1, l ,e) )
1 Algorithm analysis – CoSc3131: Divide – And - Conquer
Department of Computer Science 3rd yr: Bule Hora University

}
}
1.2). Algorithm BSearch(A, n, e) // ITERATIVE VERSION
// Given an array A[l..n] of elements in increasing order. ‘ e’ s the element to be
// searched for. If ‘e’ is present, return ‘i’ such that e=A[i]; else return 0.
{
f = 1 and l = n ;
while(f<=l) do
{
mid = (f+l)/2 ;
if (e=A[mid]) then return(mid) ;
else if (e<A[mid]) then l = mid -1 ;
else f = mid + 1 ;
}
return(0);
}
Complexity: If ‘n’ is in the range [ 2k-1,2k ], then Binary Search makes at most ‘k’ element
comparisons for successful search and either k-1 or k comparisons for an unsuccessful search.
Let n = 2k
Take ‘Log’ for both sides. i.e log2(n) = log2(2k)
 log2(n) = k. log2(2)
 log2(n) = k, because log2(2) = 1
So, for Successful search:- For Unsuccessful search:-
Best case complexity = Ө(1) Best case = O(log n)
Average case = Ө(log n) Average case = O(log n)
Worst case = Ө(log n) Worst case = O(log n)

2. Finding Minimum and Maximum:


The problem is to find the ‘maximum’ and ‘minimum’ items in a set of ‘n’ elements.
2.1). Algorithm MaxMin(A, n, max, min) // DIRECT APPROACH
// Set max to the maximum and min to the minimum of A[1..n]
{
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];
}
}
 The above algorithm requires 2(n-1) element comparisons in the best, average and worst
cases. An immediate improvement is possible by realizing that the comparison A[i]<min is
necessary only when A[i]>max is false. Hence we can replace the body of the for loop by
for( i = 2 to n ) do
{

2 Algorithm analysis – CoSc3131: Divide – And - Conquer


Department of Computer Science 3rd yr: Bule Hora University

if (A[i]>max) then max = A[i];


else if (A[i]<min) then min = A[i];
}
Now the best case occurs when the elements are in increasing order. In this case, 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).
So, the average number of comparisons will be less than 2(n-1).

2.2). Algorithm MaxMin(i, j, max, min) // DIVIDE & CONQUER


// A[1..n] is a global array. Parameters i and j are integers.
// 1 ≤ i ≤ j ≤ n. This algorithm is to set max and min to the largest and smallest
// values in A[i..j] respectively.
{
If ( i = j ) then max=min=A[i]; // There is only one element in the list.
Else if ( i = j-1 ) then // List contains TWO elements
{
If ( A[i] < A[j] ) then
max = A[ j ] and min = A[ i ] ;
else max = A[ i ] and min = A[ j ] ;
}
Else // If list is not small, divide the list into sub lists.
{
mid = (i + j ) /2 ; // Find where to split the list
// Solve the sub problems
MaxMin(i, mid, max, min) ;
MaxMin(mid+1, j, max1, min1) ;
// Combining the solutions
If (max < max1) then max = max1;
If (min > min1) then min = min1;
}
}

3. MERGE SORT (DIVIDE & CONQUER)


Given a sequence of elements A[1],….,A[n], the general idea is to imagine them split into two
sets A[1],….,A[n/2] and A[n/2 + 1],…,A[n]. Each set is individually sorted, and the resulting
sorted sequences are merged to produce a single sorted sequence of ‘n’ elements.
Example:
3.1). Algorithm Mergesort(low, high)
// A[low..high] is a global array to be sorted. If there is only one element, itself can be
// treated as sorted list.
{
If (low < high) then // If there are more than one element.
{
// divide list into sub lists

3 Algorithm analysis – CoSc3131: Divide – And - Conquer


Department of Computer Science 3rd yr: Bule Hora University

mid = (low + high)/2 ; // Find where to split


// Solve the sub problems
Mergesort(low, mid) ;
Mergesort(mid+1, high) ;
// Combine the solutions
Merge(low, mid, high) ;
}
}

3.1.1) SubAlgorithm Merge(low, mid, high)


// A[low..high] is a global array containing two sorted subsets A[low..mid] and A[mid+1 // ..
high]. The goal is to merge these two sets into a single set residing in A[low..high].
// B[low..high] is a temporary global array.
{
i = low ; j = mid + 1 ; k = low ;
while ((i ≤ mid ) and ( j ≤ high ))
{
If ( A[i] ≤ A[j] ) then
{
B[k] = A[i] ;
k ++ ;
i ++ ;
}
Else
{
B[k] = A[j] ;
k ++ ;
j ++ ;
}
}
While( i ≤ mid )
{
B[k] = A[i];
k ++ ;
i ++ ;
}
While( j ≤ high )
{
B[k] = A[j] ;
k ++ ;
j ++ ;
}
for ( t = low to high ) do
A[ t ] = B[ t ] ;
}
Note: The time complexity of the above algorithm is proportional to ‘n’.

4 Algorithm analysis – CoSc3131: Divide – And - Conquer


Department of Computer Science 3rd yr: Bule Hora University

T(n) = O(n*log n)
 One drawback concerning mergesort is its use of temporary array B[low..high]. It requires
additional ‘n’ locations, because we could not reasonably merge two sorted sets in the same
place.

4. QUICK SORT
In merge sort, the list A[1..n] was divided at its midpoint into sub arrays which were
independently sorted and later merged. In quick sort, the division into two sub arrays is made so
that the sorted sub arrays do not need to be merged later. This is accomplished by rearranging the
elements in a[1..n] such that A[i] ≤ A[j] for all ‘i' between 1 and ‘mid’ and all j between
mid+1 and n for some ‘mid’, 1≤ mid ≤ n. Thus, the elements in A[1..mid] and A[mid+1,n]
can be independently sorted. No merge is needed. The rearrangement of the elements is
accomplished by picking some element of A[1..n], say t = A[p], and then reordering the other
elements so that all elements appearing before ‘t’ in A[1..n] are less than or equal to ‘t’ and
all elements appearing after ‘t’ are greater than or equal to ‘t’. This rearranging is referred to as
partitioning.

a) Sub Algorithm Findpivot(A, f, l)


// This algorithm returns the value(Pivot), based on which the list will be divided into
// sublists. Pivot is the big of first two distinct terms from A[f]..A[l].
{
p = A[f];
for (i = f+1 to l ) do
{
if (A[i] > p ) then return(A[i]) ;
else if (A[i]<p) then return(p) ;
}
return(0) ;
}

b) Sub Algorithm Partition(A, f, l, p)


// This algorithm divides the list in to two sublists based on Pivot. Here A[f..l] is the
// given unordered list and ‘p’ is the pivot element. It will return the index of the array for //
splitting.
{
i = f and j = l ;
Repeat
{
While(A[i] ≤ p) do
i++ ;
While(A[j]>p) do
j-- ;

5 Algorithm analysis – CoSc3131: Divide – And - Conquer


Department of Computer Science 3rd yr: Bule Hora University

swap(A[ i ], A[ j ]) ;
} Until(i>j) ;
return(i) ;
}

c) Algorithm QUICKSORT(A, f, l) // Main Algorithm


// This algorithm will sort the given unordered list A[f..l] using ‘Divide and Conquer’
// Strategy.
{
k = Findpivot(A,f,l) ;
if(k≠0) then
{
p = Partition(A, f, l, k) ;
QUICKSORT(A, f, p-1) ;
QUICKSORT(A, p, l) ;
}
return(0);
}

Complexity: The worst case complexity is O(n2). Since, the above algorithm was designed
using ‘divide and conquer’ method, its average case complexity is O(nlogn).

5. Strassen’s Matrix Multiplication:


Let A and B be two n X n matrices. The product matrix C = AB is also an n X n matrix whose
(i,j)th element is formed by taking the elements in the ith row of A and jth column of B and
multiplying them to get
C(i,j) = ∑ A(i,k)B(k,j) for all i and j between 1 and n.
1≤k≤n
To compute C(i,j) using this formula, we need ‘n’ multiplications. As the matrix C has n 2
elements, the time for the resulting matrix multiplication algorithm will be O(n 3).
The Divide-and-Conquer strategy suggests another way to compute the product of two
n X n matrices. For simplicity, we assume that ‘n’ is power of 2. In case n is not a power of 2,
then enough rows and columns of zeros can be added to both A & B so that the resulting
dimensions are a power of 2. Imagine that A and B are each partitioned into four square sub-
matrices, each sub-matrix having dimensions (n/2 X n/2). Then the product AB can be
computed by using the above formula for the product of 2 X 2 matrices: if AB is

A11 A12 B11 B12 C11 C12


A21 A22 B21 B22 = C21 C22
Then
C11 = A11B11 + A12B21
C12 = A11B12 + A12B22
C21 = A21B11 + A22B21
C22 = A21B12 + A22B22

6 Algorithm analysis – CoSc3131: Divide – And - Conquer


Department of Computer Science 3rd yr: Bule Hora University

To compute AB using the above method, we need to perform 8 multiplications of n/2 X n/2
matrices and four additions of n/2 X n/2 matrices. Since two n/2 X n/2 matrices can be added in
time cn2 for some constant c , the overall computing time can be expressed in terms of
following recurrence relation:
a if n ≤ 2, ‘a’ is a constant
2
T(n) = 8T(n/2) + cn if n > 2 , ‘c’ is a constant
By solving the above recurrence relation, we can obtain O(n3) as the time complexity of
Divide-and-Conquer matrix multiplication algorithm.
Since matrix multiplications are more expensive than matrix additions (O(n3)>O(n2)), we
can attempt to reformulate with fewer matrix multiplications and possibly more additions.
Strassen’s Method:
P = (A11 + A22)(B11 + B22)
Q = (A21 + A22)B11
R = A11(B12 – B22)
S = A22(B21 – B11)
T = (A11 + A12)B22
U = (A21 – A11)(B11+ B12)
V = (A12 – A22)(B21 + B22)
C11 = P + S – T + V
C12 = R + T
C21 = Q + S
C22 = P + R – Q + U
**Please verify our class running notes for detailed solution of above recurrence relation.
6. CONVEX HULL
A Convex Hull is an important structure in geometry that can be used in the construction of
many other geometric structures. The Convex Hull of a set S of points in the plane is defined to
be the smallest convex polygon containing all the points of S(A polygon is said to be convex if
for any two points p1 and p2 inside the polygon, the directed line segment from p1 to p2 is fully
contained in the polygon.). The vertices of the convex hull of a set S of points form a subset of
S.
Note: Kindly verify our class running notes for construction method and it’s algorithm

7 Algorithm analysis – CoSc3131: Divide – And - Conquer

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy