SlideShare a Scribd company logo
4
Most read
19
Most read
20
Most read
DIVIDE AND CONQUER
PRESENTED BY
M.RAMYA
M.sc[CS&IT]
NADAR SARASWATHI COLLEGE OF ARTS
&SCIENCE,
VADAPUDUPATTI , THENI.
GENERAL METHOD
• A function to compute on n inputs the divide and conquer
strategy suggests splitting the inputs into k distinct subsets , 1
< k ≤ n , yielding k sub problems.
• If the sub problems are still relatively large , then the divide
and conquer strategy can possibly be reapplied
CONTROL ABSTRACTION FOR DIVIDE AND
CONQUER
Algorithm DAndC(P)
{
if Small(P)then return S(P);
else
{
divide P into smaller instances P1,P2,……,Pk , k≥1;
Apply DAndC to each of these sub problems;
return Combine(DAndC(P1),DAndC(P2),…,DAndC(Pk));
}
}
• If the size of P is n and the size of the k sub problems are n1 ,
n2 , ……, nk , respectively then the computing time of DAndC
is described by the recurrence relation
where T(n) is the time for DAndC on any input of size n
and g(n) is the time to compute the answer directly for small
inputs.












)()(....)()(
)()(
21 nfnTnTnT
ngnT
k
n small
otherwise
• The complexity of many divide and conquer algorithms is
given by recurrences of the form
where a and b are known constants. We assume that T(1) is
known and n is a power of b.
• One of the methods for solving any such recurrence relation is
called the substitution method.












)()/(
)1()(
nfbnaT
TnT n=1
n>1
BINARY SEARCH
• Let ai , 1 ≤ i ≤ n , be a list of elements that are sorted in
nondecreasing order. Consider the problem determine where a
given element x is present in the list. If x is present , we are to
determine a value j such that aj = x. If x is not in the list , then j
is to be set to zero.
RECURSIVE BINARY SEARCH
Algorithm BinSrch(a , i , l , x)
{
if(l = i) then
{
if (x = a[i]) then return i;
else return 0;
}
else
{
mid:=[(i+l)/2];
if (x=a[mid]) then return mid;
else if (x < a[mid]) then
return BinSrch(a , i , mid-1 , x );
else return BinSrch(a , mid+1 , l , x);
}
}
ITERATIVE BINARY SEARCH
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;
}
THREE EXAMPLES OF BINARY SEARCH ON 14
ELEMENTS
Example:
Let us consider 14 entries:
-15,-6,0,7,9,23,54,82,101,112,125,131,142,151
• Place them in a[1:14] and simulate the steps that bin search goes through as
it searches for different values of x.
• Only the variables low, high and mid need to be traced as we simulate the
algorithm.
• We try the following values for x: 151,-14,9 for two successful searches
and one unsuccessful search. The traces of these three inputs shows below:
X = 151 low high mid
1 14 7
8 14 11
12 14 13
14 14 14
Found
COND….
X = -14 low high mid
1 14 7
1 6 3
1 2 1
2 2 2
2 1 Not Found
X = 9 low high mid
1 14 7
1 6 3
4 6 5
Found
BINARY DECISION TREE FOR BINARY
SEARCH , n =14
Divide and conquer
BINARY SEARCH USING ONE COMPARSION
PER CYCLE
Algorithm BinSearch1(a , n , x)
{
low:=1;high:=n+1;
while (low<(high-1)) do
{
mid:=[(low+high)/2];
if(x < a[mid]) then high:=mid;
else low:=mid;
}
if (x=a[low]) then return low;
else return 0;
}
ADVANTAGES
1. In this method elements are eliminated by half each time .So it is
very faster than the sequential search.
2. It requires less number of comparisons than sequential search to
locate the search key element.
DISADVANTAGES
1. An insertion and deletion of a record requires many records in the
existing table be physically moved in order to maintain the records
in sequential order.
2. The ratio between insertion/deletion and search time is very high.
FINDING THE MAXIMUM AND MINIMUM
• The divide and conquer technique. The problem is to find the
maximum and minimum items in a set of n elements.
• More importantly , when the elements in a[1:n] are
polynomials , vectors , very large numbers or strings of
characters , the cost of an element comparison is much higher
than the cost of the other operations. Hence the time is
determined mainly by the total cost of the element
comparisons.
• StraightMaxMin 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 contents of the for loop by
if (a[i] > max) then max:=a[i];
else if (a[i] < min) then min:=a[i];
STRAIGHTFORWARD MAXIMUM AND
MINIMUM
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];
}
}
If the T(n) represents the number , then the resulting
recurrence relation is,
RECURSIVELY FINDING THE MAXIMUM AND
MINIMUM
TREES OF RRECURSIVE CALLS OF MAXMIN
Divide and conquer

More Related Content

PPT
Dinive conquer algorithm
PPTX
Stressen's matrix multiplication
PPTX
Data Wrangling
PPTX
daa-unit-3-greedy method
PPTX
sum of subset problem using Backtracking
PPT
Divide and conquer
PPTX
Advanced topics in artificial neural networks
PPTX
Means End Analysis (MEA) in Artificial.pptx
Dinive conquer algorithm
Stressen's matrix multiplication
Data Wrangling
daa-unit-3-greedy method
sum of subset problem using Backtracking
Divide and conquer
Advanced topics in artificial neural networks
Means End Analysis (MEA) in Artificial.pptx

What's hot (20)

PDF
Code optimization in compiler design
PDF
Run time storage
PPTX
Sorting Algorithms
PDF
Algorithms Lecture 2: Analysis of Algorithms I
PPT
Sum of subsets problem by backtracking 
PPTX
N queen problem
PPTX
Greedy method
PPTX
PPT
16. Concurrency Control in DBMS
PDF
I. Alpha-Beta Pruning in ai
PDF
I.BEST FIRST SEARCH IN AI
PPTX
Strassen's matrix multiplication
PPTX
Dynamic programming
PPTX
Breadth First Search & Depth First Search
DOC
CS8391 Data Structures Part B Questions Anna University
PPTX
Knowledge representation
PPT
Red black tree
PPT
Divide and Conquer
PPTX
Sum of subset problem.pptx
PPTX
05 Clustering in Data Mining
Code optimization in compiler design
Run time storage
Sorting Algorithms
Algorithms Lecture 2: Analysis of Algorithms I
Sum of subsets problem by backtracking 
N queen problem
Greedy method
16. Concurrency Control in DBMS
I. Alpha-Beta Pruning in ai
I.BEST FIRST SEARCH IN AI
Strassen's matrix multiplication
Dynamic programming
Breadth First Search & Depth First Search
CS8391 Data Structures Part B Questions Anna University
Knowledge representation
Red black tree
Divide and Conquer
Sum of subset problem.pptx
05 Clustering in Data Mining
Ad

Similar to Divide and conquer (20)

DOC
algorithm Unit 2
DOC
Unit 2 in daa
PDF
Divide and Conquer Case Study
PPTX
data_structure_Chapter two_computer.pptx
PPTX
Algorithm in computer science
PDF
BINARY SEARCH - A - DATA STRUCTURE AND ALGORITHM.pdf
PPT
Divide and conquer algorithm
PPTX
Analysis of Algorithm II Unit version .pptx
PPTX
Algorithm Using Divide And Conquer
PPTX
Daa unit 2
PPTX
Daa unit 2
PPTX
12.03.Divide-and-conquer_algorithms.pptx
PPTX
12.03.Divide-and-conquer_algorithms.pptx
DOCX
Report 02(Binary Search)
PPTX
Module 2_ Divide and Conquer Approach.pptx
PPTX
Divide and Conquer - Part 1
PPT
5.2 divede and conquer 03
PPT
5.2 divede and conquer 03
PPTX
Data Structure and Algorithm - Divide and Conquer
PDF
Divide and conquer
algorithm Unit 2
Unit 2 in daa
Divide and Conquer Case Study
data_structure_Chapter two_computer.pptx
Algorithm in computer science
BINARY SEARCH - A - DATA STRUCTURE AND ALGORITHM.pdf
Divide and conquer algorithm
Analysis of Algorithm II Unit version .pptx
Algorithm Using Divide And Conquer
Daa unit 2
Daa unit 2
12.03.Divide-and-conquer_algorithms.pptx
12.03.Divide-and-conquer_algorithms.pptx
Report 02(Binary Search)
Module 2_ Divide and Conquer Approach.pptx
Divide and Conquer - Part 1
5.2 divede and conquer 03
5.2 divede and conquer 03
Data Structure and Algorithm - Divide and Conquer
Divide and conquer
Ad

More from ramya marichamy (19)

PPTX
NETWORK DEVICE SECURITY NETWORK HARDENING
PPTX
DIGITAL VIDEO DATA SIZING AND OBJECT BASED ANIMATION
PPTX
Image processing
PPTX
Classical encryption techniques
PPTX
Servlets api overview
PPTX
Region based segmentation
PPTX
Design notation
PPTX
Mining single dimensional boolean association rules from transactional
PPTX
Architecture of data mining system
PPTX
segmentation
PPTX
File Management
PPTX
Arithmetic & Logic Unit
PPTX
SHADOW PAGING and BUFFER MANAGEMENT
PPTX
PPTX
pointer, virtual function and polymorphism
PPTX
Managing console i/o operation,working with files
PPTX
Operator overloading
PPTX
microcomputer architecture - Arithmetic instruction
PPTX
High speed lan
NETWORK DEVICE SECURITY NETWORK HARDENING
DIGITAL VIDEO DATA SIZING AND OBJECT BASED ANIMATION
Image processing
Classical encryption techniques
Servlets api overview
Region based segmentation
Design notation
Mining single dimensional boolean association rules from transactional
Architecture of data mining system
segmentation
File Management
Arithmetic & Logic Unit
SHADOW PAGING and BUFFER MANAGEMENT
pointer, virtual function and polymorphism
Managing console i/o operation,working with files
Operator overloading
microcomputer architecture - Arithmetic instruction
High speed lan

Recently uploaded (20)

PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Insiders guide to clinical Medicine.pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Pharma ospi slides which help in ospi learning
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Lesson notes of climatology university.
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Institutional Correction lecture only . . .
PPTX
GDM (1) (1).pptx small presentation for students
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Renaissance Architecture: A Journey from Faith to Humanism
O7-L3 Supply Chain Operations - ICLT Program
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Insiders guide to clinical Medicine.pdf
Computing-Curriculum for Schools in Ghana
Pharma ospi slides which help in ospi learning
TR - Agricultural Crops Production NC III.pdf
Lesson notes of climatology university.
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Cell Structure & Organelles in detailed.
PPH.pptx obstetrics and gynecology in nursing
Institutional Correction lecture only . . .
GDM (1) (1).pptx small presentation for students

Divide and conquer

  • 1. DIVIDE AND CONQUER PRESENTED BY M.RAMYA M.sc[CS&IT] NADAR SARASWATHI COLLEGE OF ARTS &SCIENCE, VADAPUDUPATTI , THENI.
  • 2. GENERAL METHOD • A function to compute on n inputs the divide and conquer strategy suggests splitting the inputs into k distinct subsets , 1 < k ≤ n , yielding k sub problems. • If the sub problems are still relatively large , then the divide and conquer strategy can possibly be reapplied
  • 3. CONTROL ABSTRACTION FOR DIVIDE AND CONQUER Algorithm DAndC(P) { if Small(P)then return S(P); else { divide P into smaller instances P1,P2,……,Pk , k≥1; Apply DAndC to each of these sub problems; return Combine(DAndC(P1),DAndC(P2),…,DAndC(Pk)); } }
  • 4. • If the size of P is n and the size of the k sub problems are n1 , n2 , ……, nk , respectively then the computing time of DAndC is described by the recurrence relation where T(n) is the time for DAndC on any input of size n and g(n) is the time to compute the answer directly for small inputs.             )()(....)()( )()( 21 nfnTnTnT ngnT k n small otherwise
  • 5. • The complexity of many divide and conquer algorithms is given by recurrences of the form where a and b are known constants. We assume that T(1) is known and n is a power of b. • One of the methods for solving any such recurrence relation is called the substitution method.             )()/( )1()( nfbnaT TnT n=1 n>1
  • 6. BINARY SEARCH • Let ai , 1 ≤ i ≤ n , be a list of elements that are sorted in nondecreasing order. Consider the problem determine where a given element x is present in the list. If x is present , we are to determine a value j such that aj = x. If x is not in the list , then j is to be set to zero.
  • 7. RECURSIVE BINARY SEARCH Algorithm BinSrch(a , i , l , x) { if(l = i) then { if (x = a[i]) then return i; else return 0; } else { mid:=[(i+l)/2]; if (x=a[mid]) then return mid; else if (x < a[mid]) then return BinSrch(a , i , mid-1 , x ); else return BinSrch(a , mid+1 , l , x); } }
  • 8. ITERATIVE BINARY SEARCH 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; }
  • 9. THREE EXAMPLES OF BINARY SEARCH ON 14 ELEMENTS Example: Let us consider 14 entries: -15,-6,0,7,9,23,54,82,101,112,125,131,142,151 • Place them in a[1:14] and simulate the steps that bin search goes through as it searches for different values of x. • Only the variables low, high and mid need to be traced as we simulate the algorithm. • We try the following values for x: 151,-14,9 for two successful searches and one unsuccessful search. The traces of these three inputs shows below: X = 151 low high mid 1 14 7 8 14 11 12 14 13 14 14 14 Found
  • 10. COND…. X = -14 low high mid 1 14 7 1 6 3 1 2 1 2 2 2 2 1 Not Found X = 9 low high mid 1 14 7 1 6 3 4 6 5 Found
  • 11. BINARY DECISION TREE FOR BINARY SEARCH , n =14
  • 13. BINARY SEARCH USING ONE COMPARSION PER CYCLE Algorithm BinSearch1(a , n , x) { low:=1;high:=n+1; while (low<(high-1)) do { mid:=[(low+high)/2]; if(x < a[mid]) then high:=mid; else low:=mid; } if (x=a[low]) then return low; else return 0; }
  • 14. ADVANTAGES 1. In this method elements are eliminated by half each time .So it is very faster than the sequential search. 2. It requires less number of comparisons than sequential search to locate the search key element. DISADVANTAGES 1. An insertion and deletion of a record requires many records in the existing table be physically moved in order to maintain the records in sequential order. 2. The ratio between insertion/deletion and search time is very high.
  • 15. FINDING THE MAXIMUM AND MINIMUM • The divide and conquer technique. The problem is to find the maximum and minimum items in a set of n elements. • More importantly , when the elements in a[1:n] are polynomials , vectors , very large numbers or strings of characters , the cost of an element comparison is much higher than the cost of the other operations. Hence the time is determined mainly by the total cost of the element comparisons.
  • 16. • StraightMaxMin 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 contents of the for loop by if (a[i] > max) then max:=a[i]; else if (a[i] < min) then min:=a[i];
  • 17. STRAIGHTFORWARD MAXIMUM AND MINIMUM 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]; } }
  • 18. If the T(n) represents the number , then the resulting recurrence relation is,
  • 19. RECURSIVELY FINDING THE MAXIMUM AND MINIMUM
  • 20. TREES OF RRECURSIVE CALLS OF MAXMIN