algorithm-D-A-C
algorithm-D-A-C
algorithm-D-A-C
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)
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.
swap(A[ i ], A[ j ]) ;
} Until(i>j) ;
return(i) ;
}
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).
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