0% found this document useful (0 votes)
15 views

Divide and Conquer Notes

Imp topic in daa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Divide and Conquer Notes

Imp topic in daa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

➢ General Method
➢ Binary search and its complexity
➢ Finding the Maximum and Minimum and its
Complexity
➢ Merge sort and its complexity
➢ Quick sort and its complexity
➢ Selection
➢ Strassen’s matrix multiplication
➢ Convex Hull problem

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 1
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

GENERAL METHOD

Divide and conquer strategy:

• Given a function to compute on n inputs the divide-and –conquer strategy


suggests that spitting the input into k distinct subsets, yield 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 original problem if necessary.

• If sub problems are still relatively large, then the divide-and-conquer strategy can
possibly reapplied to sub problems.

• Sodivide-and-conquer strategy is naturally expressed by a recursion algorithm.


We apply divide-and-conquer strategy recursively on sub problems until sub
problems are convert into small problems.

Every Divide-And-Conquer strategy involves 3 steps:

I. Divide: Divide the problem into k number of sub problems

II. Conquer: solve the individual sub problems, if it is small problems otherwise apply
divide process

III. Combine: combine the solutions of sub problems into a solution to original problem (if
necessary)

Control Abstraction for divide-and-conquer

Algorithm:

Algorithm DAndC(P)
{
if small(P) then return s(P);
else
{
divide P into smaller instaances P1,P2,…..Pk.;
apply DAC to each of these sub problems;
return combine(DAndC(P1),DAndC(P2)…..DAndC(Pk));
}
}

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 2
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

Analysis of Divide and Conquer:

We can compute the runtime of DAC as follows:

STEP 1:

Given problem size is n and if n is small then directly we compute the solution without applying
DAC. So that the time required isT(1).

STEP 2:

If the size of ‘p’ is ‘n’ and if it is not smaller then divide the problem into k sub problems
n1,n2…..nk.Each of size is 1/b where b is a constant.

Hence the time required to compute the given problem is

g(n) if n is small
T(n) =
T(n1/b)+T(n2/b)+……….+T(nk/b)+f(n); otherwise

Where T(n) is the time for DAndC on any input size n.


g(n) is the time to compute the answer directly for small inputs.
Where f(n) represents the time required to combine the sub-problems.

The Time complexity of many divide-and-conquer algorithms is given by recurrences of the


form

T(1) if n=1
T(n ) =
aT(n/b) + f(n) if n>1

where a , b are constants and assume that n = b k is called general divide-and-conquer


recurrence relation.

we solve this recurrence relation using substitution method

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 3
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

Substitution method:

Recurrence Relation T(n) = aT(n/b)+f(n)


Consider a=2,b=2, T(1) = 2 and f(n) = n

T(n) = 2 T(n/2)+n;

Assume that n = 2 k that is k = log2 n

By substitution method,
T(n) = 2 T(n/2)+n
T(n) = 2{ 2 T(n/4) +(n/2)}+n = 4 T(n/4) +n+n = 22 T( n/22)+ 2n

= 4 { 2T(n/8) +(n/4)} + 2n = 8T(n/8) + 3n = 23T( n/23)+ 3n


Substitute k times ..

= 2k T( n/ 2k ) + kn
= n T(1) + nlog2n

= 2 n+ nlog2n

 T(n) = O(n logn)

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 4
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

BINARY SEARCH

• Let a[i], 1<=i<=n be a list of elements that are sorted in non-descending order.

• Suppose if we have 'n' number of elements and x is the element to be searched.

• If n=1,small(p) be true then there is no requirement of applying divide and conquer,if n>1
then it can be divided into new sub problems.

• Pick an index 'q' = (i+l)/2 in the range[i,l] and compare x with a[q] then the following 3
possibilities will occur,

I. If x=a[q], then search element found,

II. If x<a[q],then is searched in left sublist a[i], a[i+1],…,a[q-1],

III. If x>a[q], then x is searched in right sub list a[q+1], a[q+2], …., a[l]

ai,…………………………..aq-1 aq+1 , ……….al

Binary Search (Iterative Algorithm)

Algorithm binsearch( a, n, x )
{
low := 1; high := n;
while( low<= high ) do
{
mid := (low + high)/2;
if ( x< a[mid] ) then high := mid-1;
else if ( x> a[mid] ) then low := mid + 1;
else return mid;
}
return 0;
}

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 5
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

Binary Search (recursive Algorithm)

Algorithm Recbinsearch( a, i, l, x )
{
if ( i = l ) then //small(p)
{
if ( a[i] = x ) then return i;
else return 0;
}
else
{
mid = ( i+l )/2;
if (x < a[mid] ) then return Recbinsearch ( a, i, mid-1, x );
else if ( x> a[mid] ) then return Recbinsearch ( a, mid+1, l, x );
else return mid;
}
}
EXAMPLE:

Consider the list n=14 where a={-15, -6, 0, 7, 9, 23, 54, 82, 101, 112, 125, 131, 142, 151}

index 1 2 3 4 5 6 7 8 9 10 11 12 13 14

a[index] -15 -6 0 7 9 23 54 82 101 112 125 131 142 151

Using binsearch iterative algorithm

X=151(key element which has to be find)

Low high mid

1 14 (1+14)/2 = 7

8 14 (8+14)/2 = 11

12 14 (12+14)/2 = 13

14 14 (14+14)/2 = 14 found

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 6
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

X= -14 (key element which has to be find)

Low high mid

1 14 (1+14)/2 = 7

1 6 (1+6)/2 = 3

1 2 (1+2)/2 = 1

2 2 (2+2)/2 = 2

2 1 not found

X=9 (key element which has to be find)

Low high mid

1 14 (1+14)/2 = 7

1 6 (1+6)/2 = 3

4 6 (4+6)/2 = 5 found

Using Recbinsearch algorithm

1) X=151(key element which has to be find)

Call Recbinsearch( a, 1, 14, 151)

Call Recbinsearch( a, 8, 14, 151)

Call Recbinsearch( a, 12, 14, 151)

Call Recbinsearch( a, 14, 14, 151)

return 14

2) X= -14 (key element which has to be find)

Call Recbinsearch( a, 1, 14, -14)

Call Recbinsearch( a, 1, 6, -14)


SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR
ENGINEERING COLLEGE Page 7
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

Call Recbinsearch( a, 1, 2, -14)

Call Recbinsearch( a, 2, 2, -14)

return 0

3) X=9 (key element which has to be find)

Call Recbinsearch( a, 1, 14, 9)

Call Recbinsearch( a, 1, 6, 9)

Call Recbinsearch( a, 4, 6, 9)

Call Recbinsearch( a, 5, 5, 9)

return 5

Binary Decision Tree for Binary Search, n=14

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 8
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

Analysis
The basic operation of the binary search algorithm is element comparison. So, for
calculate time complexity it is enough to calculate number of element comparisons
require for binary search algorithm.
Time Complexity for binary search is

1 if n = 1
T(n) =
1 + T(n/2) if n> 1

Worst Case:

Assume that n = 2k So, k = log2n


T(n) = 1+T(n/2)
= 1+[ 1+T(n/22) ] = 2+T(n/22)
= 3+T(n/23)
K times
= k+T(n/2k) = logn+T(1) = 1+logn =O(logn)

Worst case average case best case

Successful search O(logn) O(logn) O(1)

Unsuccessful search O(logn) O(logn) O(logn)

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 9
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

FINDING THE MAXIMUM AND MINIMUM

The problem is to find the maximum and minimum elements in an array of n elements.
The straight forward algorithm is

Algorithm StraightMaxMin( a, n, max, min )


{
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 basic operation of the algorithm is element comparison. So, we concentrate on the
number of element comparisons required this algorithm.
The algorithm requires 2(n-1) element comparisons in best, worst and average cases.

Suppose we can replace the algorithm is

Algorithm StraightMaxMin( a, n, max, min )


{
max = min = a[1];
for i = 2 to n do
{
if ( a[i] > max ) then max = a[i];
else if ( a[i] < min ) then min = a[i];
}
}

Best case:
Best case occurs when the elements are in increasing order. The number of element
comparisons required is n-1.

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 10
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

Worst case:
Worst case occurs when the elements are in decreasing order. The number of element
comparisons required is 2(n-1).
Average case:
On the average, a[i] is greater than max half the time,
The average number of comparisons is n/2 + 2(n-1)/2 = n/2 + n-1 = 3n/2 – 1

Divide-And- Conquer approach :


Let small(p) is true if n<=2.
if n = 1, max = min = a[i].
if n =2, the problem can be solved by making one comparison.
if n > 2, p divide into two instances p1 and p2 each has equal elements. We solve them by
recursively invoking the same divide-and –conquer algorithm. Combine the solutions of
p1 and p2 to obtain a solution for p.

Algorithm maxmin( i, j, max, min )


{
if (i=j) then max = min = a[i];
if ( i=j-1) then
{
if (a[i]<a[j]) then
{
min=a[i];
max= a[j];
}
else
{
min = a[j];
max = a[i];
}
}
else
{
mid = ( i+j )/2;
maxmin( i, mid, max, min );
maxmin( mid+1, j, max1, min1 );
if ( max< max1 ) then max = max1;
if ( min> min1 ) then min = min1;
}
}
SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR
ENGINEERING COLLEGE Page 11
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

Ex: find the maximum and minimum on the following list


i 1 2 3 4 5 6 7 8 9

a[i] 22 13 -5 -8 15 60 17 31 37

Tree recursive calls of maxmin:

1 , 9, 60, -8

6, 9, 60, 17
1, 5, 22, -8

1, 3, 22, -5 4, 5, 15, -8 6, 7, 60, 17 8, 9, 37, 31

1, 2, 22, 3, 3, -5, -5
13

The basic operation of this algorithm is element comparison.


Let Time complexity T(n) = The number of element comparison required for n elements.

0 if n = 1

T(n) = 1 if n = 2

T(n/2) + T(n/2) + 2 if n > 2

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 12
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

Assume that n = 2 k then k = log2n

T(n) = 2T(n/2) + 2
= 2[ 2T(n/22) + 2] + 2 = 22T(n/22) + 22 + 2
= 2[22T(n/23) +2] + 22 + 2 = 23T(n/23) + 23 + 22 + 2

K-1 times
= 2k-1T(n/2k-1)+ [ 2k-1 + 2k-2 + ……+ 23 + 22 + 2]
= (n/2)T(2) + 2(2k-1 – 1)/(2-1)
= n/2 + 2k – 2 = n/2 + n -2
= (3n/2) -2

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 13
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

MERGE SORT

The elements are to be sorted in non-decreasing order. Given a sequence of n elements. The
general idea is to imagine them split into two equal size sets. Each set is individually sorted and
the resulting sorted sequences are merged to produce a single sorted sequence of n elements.

The divide-and-conquer strategy in which the list is splitting into two equal subsets and sorts
them and combines operation is the merging of two sorted sub lists into a single sorted list.

Let small(p) is true if n = 1. In this case the list is already sorted.

EXAMPLE:

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 14
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

MERGE SORT ALGORITHM

Algorithm mergsort( low,high)


{
if(low<high)
{
mid:=(low+high)/2;
mergesort(low,mid);
mergesort(mid+1,high);
merge( low,mid,high);
}
}

Algorithm merge(low,mid,high)
{
i:=low;
j:=mid+1;
k:= low;
while((i<=mid) and (j<=high)) do
{
if(a[i]<=a[j]) then
{
b[k]:=a[i];
i:=i+1;
}
else
{
b[k]:=a[j];
j:=j+1;
}
k:= k+1;
}
if (i>mid) then
for h = j to high do
{
b[k]:= a[h];
k:= k+1;
}
else
for h = i to mid do
{

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 15
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

b[k]:= a[h];
k:=k+1;
}
for h = low to mid do
a[h]:=b[h];
}

Tree of calls of Merge Sort

1, 10

1, 5 6, 10

1, 3 4, 5 6, 8 9, 10

1, 2 3, 3 4, 4 5, 5 6, 7 8, 8 9, 9 10, 10

1, 1 2, 2 6, 6 7, 7

Tree of calls of Merge


1, 1, 2 6, 6, 7

1, 2, 3 4, 4, 5 6, 7, 8 9, 9, 10

6, 8, 10
1, 3, 5

1, 5, 10

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 16
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

ANALYSIS:
The basic operation of the Merge sort algorithm is element comparison.
Let T(n) = the number of element comparisons required for Merge sort algorithm for n elements.

2T(n/2) + Tmerge(n) if n > 1


T(n) =
0 if n = 1

Assume that n = 2k then k = log2n


Best case: Tmerge(n) = n/2.

T(n) = 2T(n/2) + n/2


= 2[ 2T(n/22) + n/22] + n/2 = 22T(n/22) + n/2 + n/2
=22[ 2T(n/23) + n/23] + n/2 + n/2 = 23T(n/23) + n/2 + n/2 + n/2
K times
= 2kT(n/2k) + kn/2
= nT(1) + (nlogn)/2
=(nlogn)/2 = ( nlogn)

Worst case: Tmerge(n) = n-1

T(n)= 2T(n/2) + n-1

= 2[ 2T(n/22) + (n/2) -1] + n-1 = 22T(n/22) + n-2 + n-1 = 22T(n/22) + 2n-(2 +1)
=22[2T(n/23) + (n/22)-1]+2n –(2+1) =23T(n/23) + n-22+2n –(2+1) =23T(n/23) + 3n-(22 +2+1)
K times
= 2kT(n/2k) + kn - ( 1+2+…..+2k-1)
= nT(1) + nlogn – 2k-1 + 1
= nlogn – (n/2) +1 = ( nlogn)

Best case Average case Worst case

Merge Sort ( nlogn) ( nlogn) ( nlogn)

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 17
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

QUICKSORT

1. Quicksort technique based on divide and conquer strategy,in this technique at every step
each element is placed in the proper position.

2. It performs very well on longer lists,it works recursively by first selecting random pivot
element from the array then it partitions the list into left sub list and right sub list that are
less than the pivot element and greater than pivot element respectively.

3. The problem of sorting a given list is reduced to the problem of sorting two sublists and
process continuous until the list is sorted.

RULES:

1. Add +∞ to end of the list. That is a[n+1] = +∞


2. Pivot element P = a[1]
3. i = 1 and j = n+1.
4. First i is increased by 1, i is increased by 1 until a[i] is greater than or equal to
pivot element. If a[i] is greater than or equal to the pivot element considered
index as ‘i’ and First j is decreased by 1, j is decreased by 1 until a[j] is less than
or equal to pivot element. If a[j] is less than or equal to the pivot element
considered index as ‘j’.
5. if(i<j)then interchange array values a[i] & a[j] and proceed step 3.Otherwise
(i>=j) then interchange pivot and a[j] this procedure divides the list into
twosublists a[1]............a[j-1] and a[j+1]..............a[n].
6. Apply same procedure for sublists which produces the list in sorted order.
Initially

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 18
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

i j
1 2 3 4 5 6 7 8 9
65 75 80 85 60 55 50 45 +∞

i j
1 2 3 4 5 6 7 8 9
65 45 80 85 60 55 50 75 +∞

i j
1 2 3 4 5 6 7 8 9
65 45 50 85 60 55 80 75 +∞

j i
1 2 3 4 5 6 7 8 9
65 45 50 55 60 85 80 75 +∞

j i
1 2 3 4 5 6 7 8 9
65 45 50 55 60 85 80 75 +∞

1 2 3 4 5 6 7 8

60 45 50 55 65 85 80 75

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 19
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

Initially
i j
1 2 3 4 5
55 45 50 60 ∞

j i
1 2 3 4 5
55 45 50 60 ∞

Initially
i j
1 2 3 3 4

50 45 ∞ 55 60

j i
1 2 3

50 45 ∞

1 2

45 50

Initially
i j
6 7 8 9
85 80 75 ∞

j i
6 7 8 9
85 80 75 ∞

Initially
i j

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 20
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

6 7 8 8

75 80 ∞ 85

J i
6 7 8

75 80 ∞

6 7

75 80

So, the list is

1 2 3 4 5 6 7 8

45 50 55 60 65 75 80 85

QUICKSORT ALGORITHM:

Algorithm Quicksort(p, q)
{
if(p<q)then
{
j:=partition(a, p, q+1);
Quicksort(p, j-1);
Quicksort(j+1, q);
}
}

Algorithm partition(a, m, p)

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 21
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

{
pivot:= a[m];
i:=m;
j:=p;
repeat
{
repeat
{
i:=i+1;
} until (a[i] >= pivot);
repeat
{
j:=j-1;
} until (a[j] <= pivot);
if (i<j)thenswap(a[i],a[j]);
} until (i>= j);
swap (a[j], pivot);
return j;

}
Analysis:
The basic operation of the algorithm is element comparison.
Let C(n) = The number of element comparisons required for n elements in quick sort algorithm.

Best case:
In this case the pivot element is fixed in the middle of the list in each partition. If the list contain
same elements (2, 2, 2, 2,….,2) then quick sort algorithm gives the best case.

Cbest(n) = 2Cbest(n/2) + n + 1 if n > 1


0 if n=1

Assume that n = 2 k then k = log2n

Cbest(n) =2Cbest(n/2) + (n + 1)
= 2[2Cbest(n/22) + (n/2 + 1)] + (n+1) = 2 2Cbest(n/22) + 2n +(1+2)
= 23Cbest(n/23) + 3n +(1+2+22)

K times
= 2kCbest(n/2k) + kn +(1+2+…..+2 k-1) = nCbest(1) + nlogn + (2k-1)
SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR
ENGINEERING COLLEGE Page 22
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

=nlogn + n – 1= (nlogn)

Worst case:
In this case the pivot element is fixed in the extreme position (first or last) of the list in each
partition. If the list contain elements are in sorting order then quick sort algorithm gives the
worst case.

Cworst(n) = Cworst(n-1) + n + 1 if n > 1


0 if n=1

Cworst(n) = Cworst(n-1) + (n + 1)
= Cworst(n-2) + n + (n+1)
= Cworst(n-3) + (n-1) + n + (n+1)
= Cworst(1) + 3 + 4 + ….+n+(n+1) = 3+4+….+(n+1)
=( (n+1)(n+2)/2)-3 = O(n2)

Average case:

The partition element ‘v’ has equal probability.


CA(n) = (n+1) + 1/n  [ CA[k-1] + CA[n-k] ] ----- 1
1≤ k ≤ n
Where the no of element comparisons required for partition is n+1 and C A(1) = 0 and CA(0) = 0.

1 *n, we obtain,
n * CA(n) = n(n+1) + [ CA(0) + CA(n-1) + CA(1) + CA(n-2) +…..+CA(n-1) + CA(0) ]
= n(n+1) + 2[ CA(0) + CA(1) + CA(2) +…..+CA(n-1) ] ------ 2
Replace n by n-1 in , we
2 obtain
3
SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR
ENGINEERING COLLEGE Page 23
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

(n-1) * CA(n-1) = n(n-1) + 2[ CA(0) + CA(1) + CA (2) +…..+CA (n-2) ] ---------


2 - 3 , we get
n * CA(n) - (n-1) * CA(n-1) = n(n+1) – n(n-1) +2 CA(n-1)
n * CA(n) = (n-1) * CA(n-1)+ 2n+ 2CA(n-1)
= 2n + (n+1) CA(n-1)
Divided by n(n+1), we get
CA(n)/(n+1) = 2/(n+1) + CA(n-1)/n
Replace n by n-1, we get
CA(n-1)/(n) = 2/(n) + CA(n-2)/n-1

CA(n)/(n+1) = 2/(n+1) + 2/n + CA(n-2)/(n-1)


= 2/(n+1) + 2/n + 2/(n-1) + CA(n-3)/(n-2)

= 2/(n+1) + 2/n + 2/(n-1) + …… + CA(1)/2


𝑛+1
= 2 ∑𝑛+1
𝑘=3 1/𝑘 ≤ 2∫2 1/𝑥 dx
= 2 [ log(n+1) – log2]

CA(n) ≤ 2(n+1) [ log(n+1) -1] =  (nlogn)

SELECTION

The partition algorithm can be used to obtain an efficient algorithm for the selection problem.

The problem is, we are given n elements a[1], a[2], ….,a[n] and are required to determine the
k-th smallest element.

If the partition element ‘v’ is positioned at j.

If k = j then a[j] is the k-th smallest element.

If k < j then k-th smallest element in a[1], a[2], …..,a[j-1].

If k > j then k-th smallest element is (k-j)-th smallest element in a[j+1], a[j+2], …..,a[n].

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 24
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

EXAMPLE:

Find the 7th smallest element in an array elements

65 70 75 80 85 60 55 50 45

i j
1 2 3 4 5 6 7 8 9 10
65 70 75 80 85 60 55 50 45 ∞

i j
1 2 3 4 5 6 7 8 9 10
65 45 75 80 85 60 55 50 70 ∞

i j
1 2 3 4 5 6 7 8 9 10
65 45 50 80 85 60 55 75 70 ∞

i j
1 2 3 4 5 6 7 8 9 10
65 45 50 55 85 60 80 75 70 ∞

i j
1 2 3 4 5 6 7 8 9 10
65 45 50 55 60 85 80 75 70 ∞

j i
1 2 3 4 5 6 7 8 9 10
65 45 50 55 60 85 80 75 70 ∞

1 2 3 4 5 6 7 8 9

60 45 50 55 65 85 80 75 70

So j=5 and k=7 , since k > 7, we find the k= (7-5) = 2nd smallest element in the right sub array

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 25
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

j i
1 2 3 4 5
85 80 75 70 ∞

j i
1 2 3 4 5
70 80 75 85 ∞

1 2 3
4

70 80 75 85

So j=4 and k=2, since k < j, we find the 2 nd smallest element in the left sub array

j i
1 2 3 4
70 80 75 ∞

1 2 3
80 75
70

So j=1 and k = 2, since k > j, we find the k = (k-j) = 1st smallest element in the right sub array

i j
1 2 3
SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR
ENGINEERING COLLEGE Page 26
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

80 75 ∞

j i
1 2 3
80 75 ∞

1 2

75 80

So j = 2 and k = 1, k < j, we find the 1 st smallest element in the left sub array

Therefore the 7th smallest element = 75

Algorithm

Algorithm select (a, n, k)


{
Low:= 1;
up:= n+1;
a[n+1] = ∞;
repeat
{
j = partition (a, low, up);
if (k=j) then return j;

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 27
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

else if (k<j) then up = j;


else low = j+1;
} until(false);
}

STRASSEN’S MULTIPLICATION

Let A and B be two nXn matrices. The product matrix C = A * B is also nXn matrix whose
elements C[i, j] = ∑𝑛𝑘=1 𝐴 [𝑖, 𝑘] ∗ 𝐵[𝑘, 𝑗]
The time complexity for the resulting matrix is  (n3)
The divide and conquer strategy suggest another way to compute the product of two nXn
matrices. For simplicity we assume that n = 2 k. if n ≠ 2k, then enough rows and columns of zeros
can be added to both A and B.
Imagine that A and B are partitioned into four square sub matrices, each sub matrix is
n/2Xn/2.

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 28
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

A11 A12 B11 B12 C11 C12

* =

A21 A22 B21 B22 C21 C21

Then, C11 = A11B11 + A12 B21


C12 = A11B12 + A12 B22
C21 = A21B11 + A22 B21
C22 = A21B12 + A22B22
If n = 2, then the formulas are computed using element multiplication and addition.
If n > 2, then the formulas are computed using matrix multiplication and matrix addition for
n/2xn/2 matrices.
Since n = 2k, there matrix product can be recursively computed by the same algorithm. This
algorithm will continue applying itself until n become small (n = 2). To compute product of two
nXn matrices(A*B), we need 8 multiplications and 4 additions of n/2Xn/2 matrices.

8T(n/2) + cn2 if n>2


T(n) =
b if n≤2
Assume that n = 2 k then k = log2n
T(n) = 8T(n/2) + cn2
= 8[8T(n/22) + c(n/2)2 ]+cn2 = 82T(n/22)+ 8cn2/22 + cn2 = 82T(n/22) + cn2 [1+8/4]
= 83T(n/23) + cn2 [1+8/4+(8/4)2]

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 29
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

K times
= 8kT(n/2k) + cn2 [1+8/4+(8/4)2+….+(8/4)k-1] = 8kT(1) + cn2(2k-1)
= 8log2n + cn2(n-1) = n log28 +cn3 – cn2
= n 3 +cn3 – cn2 =  (n3)
Hence no improvement over divide-and-conquer strategy. Since we use 8 matrix multiplications
and 4 matrix additions.
Volker strassen has discovered a way to compute the formulas using only 7 matrix
multiplications and 18 additions or subtractions for computing C 11, C12, C21, and C22.
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

7T(n/2) + cn2 if n>2


T(n) =
b if n≤2

T(n) = 7T(n/2) + cn2


= 7[7T(n/22) + c(n/2)2 ]+cn2 = 72T(n/22)+ 7cn2/22 + cn2 = 72T(n/22) + cn2 [1+7/4]
= 73T(n/23) + cn2 [1+7/4+(7/4)2]

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 30
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

K times
= 7kT(n/2k) + cn2 [1+7/4+(7/4)2+….+(7/4)k-1] ≤7kT(1) + cn2(7/4)k
= n log27 +cn2 * nlog27/4
= n log27 +cn log27 = (n log27) =  (n 2.81)

CONVEX HULL
Let S be a set of points in the plane.

Convex:

A polygon is defined to be convex if for any two points P1 and P2 inside the polygon, the direct
line segment from P1 to P2 is fully contained in the polygon.

Convex Hull:

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.

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 31
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

Quick Hull Algorithm

First identify the two points (call P1 and P2) of X with the smallest and largest x-coordinate
values, both P1 and P2 are extreme points and part of the convex hull.

The set X is divided into X1 and X2 so that X1 has all points to the left of the line
segment <P1, P2> and X2 has all the points to the right of the line segment <P1, P2>. Both X1
and X2 include the two points P1 and P2. Then the convex hull of the X1 and X2 (called Upper
hull and Lower hull) are computed using divide-and-conquer algorithm (Quick hull algorithm).

The union of these two hulls is the overall convex hull.

Quick hull algorithm:

Identify a point P3 in X1 with the largest area of the triangle. The algorithm identifies all the
points of X1 that are to the left of the line <P1, P3>, along with P1 and P3 will make up the set
X11. The points of X1 that are to the left of the line <P3, P2> along with P2 and P3 will make up
the set X12.

1. P3 is a vertex of the Upper hull.

2. The points inside the triangle P1P2P3 can be eliminate from the further consideration.

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 32
DESIGN AND ANALYSIS OF ALGORITHM

DIVIDE-AND-CONQUER

The algorithm recursively continues for construction of upper hull on X11 and X12. Concatenate
all the points P1, P2, P3, …. And so on, we get the upper hull of X1

The average run time of Quick hull algorithm is (nlogn).

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR


ENGINEERING COLLEGE Page 33

You might also like