SlideShare a Scribd company logo
Analysis of Algorithm
chapter -2
Divide-and-Conquer
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)];
}
1. Binary Search:
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.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 = l 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);
}
2. Finding Minimum and Maximum:
o 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];
}
}
o The above algorithm requires 2(n-1) element comparisons in the best,
average and worst cases.
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.
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
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 ] ;
}
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-- ;
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);
}
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 n2 elements, the time for the resulting matrix multiplication
algorithm will be O(n3).
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
• Key idea. multiply 2-by-2 block matrices with only 7
multiplications.
)()(
)()(
)()(
)(
)(
)(
)(
121121117
222122126
221122115
1121224
1122213
2212112
2212111
BBAAP
BBAAP
BBAAP
BBAP
BAAP
BAAP
BBAP







731522
4321
2112
624511
PPPPC
PPC
PPC
PPPPC




Matrix multiplication


















2221
1211
2221
1211
2221
1211
BB
BB
AA
AA
CC
CC
C 11  A11  B 11   A12  B 21 
C 12  A11  B 12   A12  B 22 
C 21  A 21  B 11   A 22  B 21 
C 22  A 21  B 12   A 22  B 22 
  )()T()(2/8)T( 3
ssubmatriceformadd,
2
callsrecursive
nnnnTn  
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.
Ad

More Related Content

What's hot (20)

Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
University of Science and Technology Chitttagong
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
Protap Mondal
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
Amrinder Arora
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge Sort
ManishPrajapati78
 
Activity selection problem
Activity selection problemActivity selection problem
Activity selection problem
QAU ISLAMABAD,PAKISTAN
 
N queen problem
N queen problemN queen problem
N queen problem
Ridhima Chowdhury
 
Algorithm Using Divide And Conquer
Algorithm Using Divide And ConquerAlgorithm Using Divide And Conquer
Algorithm Using Divide And Conquer
UrviBhalani2
 
Merge sort
Merge sortMerge sort
Merge sort
Vidushi Pathak
 
Merge Sort
Merge SortMerge Sort
Merge Sort
Nikhil Sonkamble
 
Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking 
Hasanain Alshadoodee
 
Recurrences
RecurrencesRecurrences
Recurrences
Dr Sandeep Kumar Poonia
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
Dr. C.V. Suresh Babu
 
Amortized Analysis of Algorithms
Amortized Analysis of Algorithms Amortized Analysis of Algorithms
Amortized Analysis of Algorithms
sathish sak
 
Dijkstra's Algorithm
Dijkstra's Algorithm Dijkstra's Algorithm
Dijkstra's Algorithm
Rashik Ishrak Nahian
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
sandeep54552
 
Recurrences
RecurrencesRecurrences
Recurrences
Ala' Mohammad
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
Intro C# Book
 
Lecture 6 disjoint set
Lecture 6 disjoint setLecture 6 disjoint set
Lecture 6 disjoint set
Abirami A
 
Heap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithmsHeap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithms
samairaakram
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
Rajendran
 

Viewers also liked (18)

Chap05alg
Chap05algChap05alg
Chap05alg
Munkhchimeg
 
Mergesort
MergesortMergesort
Mergesort
Melver May Morales
 
Binary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackBinary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red Black
Amrinder Arora
 
Treatments of Mental Illnesses
Treatments of Mental IllnessesTreatments of Mental Illnesses
Treatments of Mental Illnesses
Avigail Gabaleo Maximo
 
Scan scheduling 50 1
Scan scheduling 50 1Scan scheduling 50 1
Scan scheduling 50 1
myrajendra
 
C look scheduling 51 1
C look scheduling 51 1C look scheduling 51 1
C look scheduling 51 1
myrajendra
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
Mohd Arif
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Amrinder Arora
 
Templates presentation
Templates presentationTemplates presentation
Templates presentation
malaybpramanik
 
Disk scheduling algorithm.52
Disk scheduling algorithm.52Disk scheduling algorithm.52
Disk scheduling algorithm.52
myrajendra
 
Generic Programming
Generic ProgrammingGeneric Programming
Generic Programming
Muhammad Alhalaby
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
Madhu Bala
 
Disk scheduling
Disk schedulingDisk scheduling
Disk scheduling
J.T.A.JONES
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 
File Handling in C++
File Handling in C++File Handling in C++
File Handling in C++
Kulachi Hansraj Model School Ashok Vihar
 
polymorphism
polymorphism polymorphism
polymorphism
Imtiaz Hussain
 
file handling c++
file handling c++file handling c++
file handling c++
Guddu Spy
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
Kulachi Hansraj Model School Ashok Vihar
 
Ad

Similar to Divide and Conquer (20)

DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
RUHULAMINHAZARIKA
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
kalyanineve
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
Shiwani Gupta
 
PPS 7.7 RECURSION, AS A DIFFERENT WAY OF SOLVING PROBLEMS. EXAMPLE PROGRAMS
PPS 7.7 RECURSION, AS A DIFFERENT WAY OF SOLVING PROBLEMS. EXAMPLE PROGRAMSPPS 7.7 RECURSION, AS A DIFFERENT WAY OF SOLVING PROBLEMS. EXAMPLE PROGRAMS
PPS 7.7 RECURSION, AS A DIFFERENT WAY OF SOLVING PROBLEMS. EXAMPLE PROGRAMS
Sitamarhi Institute of Technology
 
Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf
Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdfQuicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf
Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf
anupamfootwear
 
Are there trends, changes in the mi.pptx
Are there trends, changes in the mi.pptxAre there trends, changes in the mi.pptx
Are there trends, changes in the mi.pptx
priyaaajadhav31
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
Monika Choudhery
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
Nv Thejaswini
 
Ada notes
Ada notesAda notes
Ada notes
VIKAS SINGH BHADOURIA
 
Sorting Algorithms and their implementations
Sorting Algorithms and their implementationsSorting Algorithms and their implementations
Sorting Algorithms and their implementations
ChakravarthiMusic1
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptx
SajalFayyaz
 
Merge sort
Merge sortMerge sort
Merge sort
Rojin Khadka
 
Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...
Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...
Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...
ssuserd6b1fd
 
Scilab help book 2 of 2
Scilab help book 2 of 2Scilab help book 2 of 2
Scilab help book 2 of 2
Arun Umrao
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
Krish_ver2
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
ramya marichamy
 
Weak 11-12 Sorting update.pptxbhjiiuuuuu
Weak 11-12 Sorting update.pptxbhjiiuuuuuWeak 11-12 Sorting update.pptxbhjiiuuuuu
Weak 11-12 Sorting update.pptxbhjiiuuuuu
baloch4551701
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
appasami
 
Design and Analysis of Algorithm in Compter Science.pptx
Design and  Analysis of Algorithm in Compter Science.pptxDesign and  Analysis of Algorithm in Compter Science.pptx
Design and Analysis of Algorithm in Compter Science.pptx
rahulshawit2023
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
chidabdu
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
RUHULAMINHAZARIKA
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
Shiwani Gupta
 
PPS 7.7 RECURSION, AS A DIFFERENT WAY OF SOLVING PROBLEMS. EXAMPLE PROGRAMS
PPS 7.7 RECURSION, AS A DIFFERENT WAY OF SOLVING PROBLEMS. EXAMPLE PROGRAMSPPS 7.7 RECURSION, AS A DIFFERENT WAY OF SOLVING PROBLEMS. EXAMPLE PROGRAMS
PPS 7.7 RECURSION, AS A DIFFERENT WAY OF SOLVING PROBLEMS. EXAMPLE PROGRAMS
Sitamarhi Institute of Technology
 
Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf
Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdfQuicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf
Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf
anupamfootwear
 
Are there trends, changes in the mi.pptx
Are there trends, changes in the mi.pptxAre there trends, changes in the mi.pptx
Are there trends, changes in the mi.pptx
priyaaajadhav31
 
Sorting Algorithms and their implementations
Sorting Algorithms and their implementationsSorting Algorithms and their implementations
Sorting Algorithms and their implementations
ChakravarthiMusic1
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptx
SajalFayyaz
 
Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...
Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...
Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...
ssuserd6b1fd
 
Scilab help book 2 of 2
Scilab help book 2 of 2Scilab help book 2 of 2
Scilab help book 2 of 2
Arun Umrao
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
Krish_ver2
 
Weak 11-12 Sorting update.pptxbhjiiuuuuu
Weak 11-12 Sorting update.pptxbhjiiuuuuuWeak 11-12 Sorting update.pptxbhjiiuuuuu
Weak 11-12 Sorting update.pptxbhjiiuuuuu
baloch4551701
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
appasami
 
Design and Analysis of Algorithm in Compter Science.pptx
Design and  Analysis of Algorithm in Compter Science.pptxDesign and  Analysis of Algorithm in Compter Science.pptx
Design and Analysis of Algorithm in Compter Science.pptx
rahulshawit2023
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
chidabdu
 
Ad

More from Melaku Bayih Demessie (10)

Chapter 4 computer network and the internet2
Chapter 4 computer network and the internet2Chapter 4 computer network and the internet2
Chapter 4 computer network and the internet2
Melaku Bayih Demessie
 
Introduction to Cloud computing
Introduction to Cloud computingIntroduction to Cloud computing
Introduction to Cloud computing
Melaku Bayih Demessie
 
Selected topics in Computer Science
Selected topics in Computer Science Selected topics in Computer Science
Selected topics in Computer Science
Melaku Bayih Demessie
 
Turingmachines
Turingmachines Turingmachines
Turingmachines
Melaku Bayih Demessie
 
Greencomputing
GreencomputingGreencomputing
Greencomputing
Melaku Bayih Demessie
 
Reed solomon code
Reed solomon codeReed solomon code
Reed solomon code
Melaku Bayih Demessie
 
C2.0 propositional logic
C2.0 propositional logicC2.0 propositional logic
C2.0 propositional logic
Melaku Bayih Demessie
 
Chapter 5 of 1
Chapter 5 of 1Chapter 5 of 1
Chapter 5 of 1
Melaku Bayih Demessie
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Melaku Bayih Demessie
 
minimum spanning tree
minimum spanning tree minimum spanning tree
minimum spanning tree
Melaku Bayih Demessie
 

Divide and Conquer

  • 1. Analysis of Algorithm chapter -2 Divide-and-Conquer
  • 2. 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)]; }
  • 3. 1. Binary Search: 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) ) } }
  • 4. 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 = l 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); }
  • 5. 2. Finding Minimum and Maximum: o 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]; } } o The above algorithm requires 2(n-1) element comparisons in the best, average and worst cases.
  • 6. 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; } }
  • 7. 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. 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 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) ; } }
  • 8. 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
  • 9. { 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 ] ; }
  • 10. 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.
  • 11. 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) ; }
  • 12. 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-- ; swap(A[ i ], A[ j ]) ; } Until(i>j) ; return(i) ; }
  • 13. 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); }
  • 14. 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 n2 elements, the time for the resulting matrix multiplication algorithm will be O(n3). 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:
  • 15. 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 • Key idea. multiply 2-by-2 block matrices with only 7 multiplications.
  • 17. Matrix multiplication                   2221 1211 2221 1211 2221 1211 BB BB AA AA CC CC C 11  A11  B 11   A12  B 21  C 12  A11  B 12   A12  B 22  C 21  A 21  B 11   A 22  B 21  C 22  A 21  B 12   A 22  B 22    )()T()(2/8)T( 3 ssubmatriceformadd, 2 callsrecursive nnnnTn  
  • 18. 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.