SlideShare a Scribd company logo
1 of 7
Download to read offline
Chapter 9
Divide and Conquer
Divide and conquer: the
algorithmic version of recursion
Divide and conquer is closely related to recursion—you could even say that it is
nothing more. It has the same structure as recursion. We often implement divide
and conquer algorithms in terms of recursive functions, but we do not need to. The
defining characteristic of these algorithms is that we reduce a problem to smaller
versions of the same problem
Example: Merge sort
For example, if we want to sort a sequence, then we can split it into two sub-
sequences that we sort and then we can merge the two. The step where we sort
the two sub-sequences is the recursive step. It might feel circular to sort in terms
of sorting, but we are saved from circularity by base cases in the recursion.
The merge sort algorithm works like this:

1) Split the input in two

2) Sort the two halves

3) Combine then by merging them
Since we cut the input in half every time we call for a recursive solution, we never
get more than O(log n) function calls deep. Since the number of calls at each level
grows by powers of two as the size of the problems shrink by powers of two, we
end up with a running time of O(n log n).

Notice that this is faster than the other sorting algorithms we have seen. It is
optimal in the sense that no comparison-based sorting algorithms can run faster
than O(n log n).
Examples
Some are just a rephrasing of
algorithms you have already seen…
Divide and conquer is a powerful approach to designing new algorithms, but we
can also rephrase some we have already seen as divide and conquer.
DnC formulation of linear search:

1) Check the first element in our sequence and report if it matches

2) Do a linear sort on the rest of the sequence
Selection sort:

1) move the smallest element to the first location in the sequence,

2) sort the rest of the sequence

The "first location in the sequence" is the first location in the sequence we consider
in the recursion. Not the full sequence.
Binary search:

1) check the middle element. Report it found if it is a match.

2) Do a binary search in the left or right half
Sum (less likely to lose significant bits):

1) Compute the sum of both halves of the input

2) Add the sums together
Sum (less likely to lose significant bits):

1) Add all pairs of the input

2) Compute the sum of these
Common running times
Honestly, I find it easier just to memorise
these than redo the math each time…
They keep popping up…
There are a few recurrence equations that show up a lot for divide and conquer
running times. There are some general techniques when the recurrence doesn’t
match this but sometimes you just have to work out the math. If you can recognise
one of these recurrences for your algorithm, then just use these equations.
We have already seen why T(n) = O(1) + 2T(n/2) is in O(n log n)
It isn’t hard to see that T(n) = T(n - 1) + O(1) must be O(n) either. The argument is
not different from the one we had for the running time of linear search.
For selection sort we can argue that we recurse to depth n and at each level we
perform a search that costs O(n). The running time must then be O(n²) — as we
also argued first time we saw selection sort.
Binary search was already a divide and conquer algorithm (although we didn’t
implement it using recursion). We now it can reach call depth O(log n) but it only
does constant time at each level.
It might be less obvious what the running time of T(n) = 2T(n/2) + O(1) is, but we
can argue as follows

1) We cut the problem size in half each time we solve a recursive problem,
therefore we go to call depth log n

2) We double the number of operations we must do at each level

3) You have to know this sum, but it is easy (see next slide)
This sketch doesn’t give us the exact sum, but we can see that if we double the
number we add at each step up to n, then the sum is less than 2n
We can also consider summing in the other direct, starting with n. If we half the
number each time we add a new one, then regardless of where we stop, we will
have a sum that is less than 2n.
In this algorithm we spend linear time per level and cut the problem size in two
when going from one level to the next. The sum is the same as before (more or
less) and we have a linear time algorithm.
Exercises!
Time to put divide-and-
conquer into practice

More Related Content

What's hot

What's hot (20)

maXbox Blix the Programmer
maXbox Blix the ProgrammermaXbox Blix the Programmer
maXbox Blix the Programmer
 
Database structure Structures Link list and trees and Recurison complete
Database structure Structures Link list and trees and Recurison complete  Database structure Structures Link list and trees and Recurison complete
Database structure Structures Link list and trees and Recurison complete
 
Shell sort
Shell sortShell sort
Shell sort
 
Recursion and looping
Recursion and loopingRecursion and looping
Recursion and looping
 
Aes
AesAes
Aes
 
2Bytesprog2 course_2014_c7_double_lists
2Bytesprog2 course_2014_c7_double_lists2Bytesprog2 course_2014_c7_double_lists
2Bytesprog2 course_2014_c7_double_lists
 
Ch05
Ch05Ch05
Ch05
 
Distributed System by Pratik Tambekar
Distributed System by Pratik TambekarDistributed System by Pratik Tambekar
Distributed System by Pratik Tambekar
 
Operating System Engineering
Operating System EngineeringOperating System Engineering
Operating System Engineering
 
Iteration, induction, and recursion
Iteration, induction, and recursionIteration, induction, and recursion
Iteration, induction, and recursion
 
Recursion and Sorting Algorithms
Recursion and Sorting AlgorithmsRecursion and Sorting Algorithms
Recursion and Sorting Algorithms
 
Programmable PN Sequence Generators
Programmable PN Sequence GeneratorsProgrammable PN Sequence Generators
Programmable PN Sequence Generators
 
Bp150513(compiler)
Bp150513(compiler)Bp150513(compiler)
Bp150513(compiler)
 
Divide And Conquer.pptx
Divide And Conquer.pptxDivide And Conquer.pptx
Divide And Conquer.pptx
 
Parallel searching
Parallel searchingParallel searching
Parallel searching
 
3 recursion
3 recursion3 recursion
3 recursion
 
Distributed System by Pratik Tambekar
Distributed System by Pratik TambekarDistributed System by Pratik Tambekar
Distributed System by Pratik Tambekar
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
 
Analyzing algorithms
Analyzing algorithmsAnalyzing algorithms
Analyzing algorithms
 
Master of Computer Application (MCA) – Semester 4 MC0080
Master of Computer Application (MCA) – Semester 4  MC0080Master of Computer Application (MCA) – Semester 4  MC0080
Master of Computer Application (MCA) – Semester 4 MC0080
 

Similar to Chapter 9 divide and conquer handouts with notes

Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
chidabdu
 
Sure interview algorithm-1103
Sure interview algorithm-1103Sure interview algorithm-1103
Sure interview algorithm-1103
Sure Interview
 
lecture 9
lecture 9lecture 9
lecture 9
sajinsc
 
Counting Sort Lowerbound
Counting Sort LowerboundCounting Sort Lowerbound
Counting Sort Lowerbound
despicable me
 
Data Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study NotesData Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study Notes
Haitham El-Ghareeb
 

Similar to Chapter 9 divide and conquer handouts with notes (20)

Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
 
Ada notes
Ada notesAda notes
Ada notes
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTES
 
Sure interview algorithm-1103
Sure interview algorithm-1103Sure interview algorithm-1103
Sure interview algorithm-1103
 
lecture 9
lecture 9lecture 9
lecture 9
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV Syllabus
 
Design & Analysis Of Algorithm
Design & Analysis Of AlgorithmDesign & Analysis Of Algorithm
Design & Analysis Of Algorithm
 
01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms Notes01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms Notes
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
Counting Sort Lowerbound
Counting Sort LowerboundCounting Sort Lowerbound
Counting Sort Lowerbound
 
Parallel Computing 2007: Bring your own parallel application
Parallel Computing 2007: Bring your own parallel applicationParallel Computing 2007: Bring your own parallel application
Parallel Computing 2007: Bring your own parallel application
 
recursion.ppt
recursion.pptrecursion.ppt
recursion.ppt
 
Data Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study NotesData Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study Notes
 
Module 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxModule 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptx
 
Tower of Hanoi.ppt
Tower of Hanoi.pptTower of Hanoi.ppt
Tower of Hanoi.ppt
 
Recurrence relationships
Recurrence relationshipsRecurrence relationships
Recurrence relationships
 
Data analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxData analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptx
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
 

More from mailund

Ku 05 08 2009
Ku 05 08 2009Ku 05 08 2009
Ku 05 08 2009
mailund
 
Association mapping using local genealogies
Association mapping using local genealogiesAssociation mapping using local genealogies
Association mapping using local genealogies
mailund
 
Neural Networks
Neural NetworksNeural Networks
Neural Networks
mailund
 
Probability And Stats Intro
Probability And Stats IntroProbability And Stats Intro
Probability And Stats Intro
mailund
 
Probability And Stats Intro2
Probability And Stats Intro2Probability And Stats Intro2
Probability And Stats Intro2
mailund
 
Linear Regression Ex
Linear Regression ExLinear Regression Ex
Linear Regression Ex
mailund
 

More from mailund (20)

Chapter 9 divide and conquer handouts
Chapter 9   divide and conquer handoutsChapter 9   divide and conquer handouts
Chapter 9 divide and conquer handouts
 
Chapter 9 divide and conquer
Chapter 9   divide and conquerChapter 9   divide and conquer
Chapter 9 divide and conquer
 
Chapter 7 recursion handouts with notes
Chapter 7   recursion handouts with notesChapter 7   recursion handouts with notes
Chapter 7 recursion handouts with notes
 
Chapter 7 recursion handouts
Chapter 7   recursion handoutsChapter 7   recursion handouts
Chapter 7 recursion handouts
 
Chapter 7 recursion
Chapter 7   recursionChapter 7   recursion
Chapter 7 recursion
 
Chapter 5 searching and sorting handouts with notes
Chapter 5   searching and sorting handouts with notesChapter 5   searching and sorting handouts with notes
Chapter 5 searching and sorting handouts with notes
 
Chapter 5 searching and sorting handouts
Chapter 5   searching and sorting handoutsChapter 5   searching and sorting handouts
Chapter 5 searching and sorting handouts
 
Chapter 5 searching and sorting
Chapter 5   searching and sortingChapter 5   searching and sorting
Chapter 5 searching and sorting
 
Chapter 4 algorithmic efficiency handouts (with notes)
Chapter 4   algorithmic efficiency handouts (with notes)Chapter 4   algorithmic efficiency handouts (with notes)
Chapter 4 algorithmic efficiency handouts (with notes)
 
Chapter 4 algorithmic efficiency handouts
Chapter 4   algorithmic efficiency handoutsChapter 4   algorithmic efficiency handouts
Chapter 4 algorithmic efficiency handouts
 
Chapter 4 algorithmic efficiency
Chapter 4   algorithmic efficiencyChapter 4   algorithmic efficiency
Chapter 4 algorithmic efficiency
 
Chapter 3 introduction to algorithms slides
Chapter 3 introduction to algorithms slidesChapter 3 introduction to algorithms slides
Chapter 3 introduction to algorithms slides
 
Chapter 3 introduction to algorithms handouts (with notes)
Chapter 3 introduction to algorithms handouts (with notes)Chapter 3 introduction to algorithms handouts (with notes)
Chapter 3 introduction to algorithms handouts (with notes)
 
Chapter 3 introduction to algorithms handouts
Chapter 3 introduction to algorithms handoutsChapter 3 introduction to algorithms handouts
Chapter 3 introduction to algorithms handouts
 
Ku 05 08 2009
Ku 05 08 2009Ku 05 08 2009
Ku 05 08 2009
 
Association mapping using local genealogies
Association mapping using local genealogiesAssociation mapping using local genealogies
Association mapping using local genealogies
 
Neural Networks
Neural NetworksNeural Networks
Neural Networks
 
Probability And Stats Intro
Probability And Stats IntroProbability And Stats Intro
Probability And Stats Intro
 
Probability And Stats Intro2
Probability And Stats Intro2Probability And Stats Intro2
Probability And Stats Intro2
 
Linear Regression Ex
Linear Regression ExLinear Regression Ex
Linear Regression Ex
 

Recently uploaded

Digital Dentistry.Digital Dentistryvv.pptx
Digital Dentistry.Digital Dentistryvv.pptxDigital Dentistry.Digital Dentistryvv.pptx
Digital Dentistry.Digital Dentistryvv.pptx
MohamedFarag457087
 
Lipids: types, structure and important functions.
Lipids: types, structure and important functions.Lipids: types, structure and important functions.
Lipids: types, structure and important functions.
Cherry
 
Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.
Cherry
 
Porella : features, morphology, anatomy, reproduction etc.
Porella : features, morphology, anatomy, reproduction etc.Porella : features, morphology, anatomy, reproduction etc.
Porella : features, morphology, anatomy, reproduction etc.
Cherry
 
CYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptxCYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptx
Cherry
 

Recently uploaded (20)

Energy is the beat of life irrespective of the domains. ATP- the energy curre...
Energy is the beat of life irrespective of the domains. ATP- the energy curre...Energy is the beat of life irrespective of the domains. ATP- the energy curre...
Energy is the beat of life irrespective of the domains. ATP- the energy curre...
 
Digital Dentistry.Digital Dentistryvv.pptx
Digital Dentistry.Digital Dentistryvv.pptxDigital Dentistry.Digital Dentistryvv.pptx
Digital Dentistry.Digital Dentistryvv.pptx
 
FAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical ScienceFAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical Science
 
Plasmid: types, structure and functions.
Plasmid: types, structure and functions.Plasmid: types, structure and functions.
Plasmid: types, structure and functions.
 
Cyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptxCyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptx
 
Site specific recombination and transposition.........pdf
Site specific recombination and transposition.........pdfSite specific recombination and transposition.........pdf
Site specific recombination and transposition.........pdf
 
FAIRSpectra - Enabling the FAIRification of Spectroscopy and Spectrometry
FAIRSpectra - Enabling the FAIRification of Spectroscopy and SpectrometryFAIRSpectra - Enabling the FAIRification of Spectroscopy and Spectrometry
FAIRSpectra - Enabling the FAIRification of Spectroscopy and Spectrometry
 
Lipids: types, structure and important functions.
Lipids: types, structure and important functions.Lipids: types, structure and important functions.
Lipids: types, structure and important functions.
 
Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.
 
Porella : features, morphology, anatomy, reproduction etc.
Porella : features, morphology, anatomy, reproduction etc.Porella : features, morphology, anatomy, reproduction etc.
Porella : features, morphology, anatomy, reproduction etc.
 
FS P2 COMBO MSTA LAST PUSH past exam papers.
FS P2 COMBO MSTA LAST PUSH past exam papers.FS P2 COMBO MSTA LAST PUSH past exam papers.
FS P2 COMBO MSTA LAST PUSH past exam papers.
 
Selaginella: features, morphology ,anatomy and reproduction.
Selaginella: features, morphology ,anatomy and reproduction.Selaginella: features, morphology ,anatomy and reproduction.
Selaginella: features, morphology ,anatomy and reproduction.
 
Genome sequencing,shotgun sequencing.pptx
Genome sequencing,shotgun sequencing.pptxGenome sequencing,shotgun sequencing.pptx
Genome sequencing,shotgun sequencing.pptx
 
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry
 
X-rays from a Central “Exhaust Vent” of the Galactic Center Chimney
X-rays from a Central “Exhaust Vent” of the Galactic Center ChimneyX-rays from a Central “Exhaust Vent” of the Galactic Center Chimney
X-rays from a Central “Exhaust Vent” of the Galactic Center Chimney
 
Genome Projects : Human, Rice,Wheat,E coli and Arabidopsis.
Genome Projects : Human, Rice,Wheat,E coli and Arabidopsis.Genome Projects : Human, Rice,Wheat,E coli and Arabidopsis.
Genome Projects : Human, Rice,Wheat,E coli and Arabidopsis.
 
CYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptxCYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptx
 
GBSN - Microbiology (Unit 4) Concept of Asepsis
GBSN - Microbiology (Unit 4) Concept of AsepsisGBSN - Microbiology (Unit 4) Concept of Asepsis
GBSN - Microbiology (Unit 4) Concept of Asepsis
 
Concept of gene and Complementation test.pdf
Concept of gene and Complementation test.pdfConcept of gene and Complementation test.pdf
Concept of gene and Complementation test.pdf
 
GBSN - Biochemistry (Unit 3) Metabolism
GBSN - Biochemistry (Unit 3) MetabolismGBSN - Biochemistry (Unit 3) Metabolism
GBSN - Biochemistry (Unit 3) Metabolism
 

Chapter 9 divide and conquer handouts with notes

  • 1. Chapter 9 Divide and Conquer Divide and conquer: the algorithmic version of recursion Divide and conquer is closely related to recursion—you could even say that it is nothing more. It has the same structure as recursion. We often implement divide and conquer algorithms in terms of recursive functions, but we do not need to. The defining characteristic of these algorithms is that we reduce a problem to smaller versions of the same problem Example: Merge sort For example, if we want to sort a sequence, then we can split it into two sub- sequences that we sort and then we can merge the two. The step where we sort the two sub-sequences is the recursive step. It might feel circular to sort in terms of sorting, but we are saved from circularity by base cases in the recursion.
  • 2. The merge sort algorithm works like this: 1) Split the input in two 2) Sort the two halves 3) Combine then by merging them Since we cut the input in half every time we call for a recursive solution, we never get more than O(log n) function calls deep. Since the number of calls at each level grows by powers of two as the size of the problems shrink by powers of two, we end up with a running time of O(n log n). Notice that this is faster than the other sorting algorithms we have seen. It is optimal in the sense that no comparison-based sorting algorithms can run faster than O(n log n). Examples Some are just a rephrasing of algorithms you have already seen… Divide and conquer is a powerful approach to designing new algorithms, but we can also rephrase some we have already seen as divide and conquer.
  • 3. DnC formulation of linear search: 1) Check the first element in our sequence and report if it matches 2) Do a linear sort on the rest of the sequence Selection sort: 1) move the smallest element to the first location in the sequence, 2) sort the rest of the sequence The "first location in the sequence" is the first location in the sequence we consider in the recursion. Not the full sequence. Binary search: 1) check the middle element. Report it found if it is a match. 2) Do a binary search in the left or right half
  • 4. Sum (less likely to lose significant bits): 1) Compute the sum of both halves of the input 2) Add the sums together Sum (less likely to lose significant bits): 1) Add all pairs of the input 2) Compute the sum of these Common running times Honestly, I find it easier just to memorise these than redo the math each time… They keep popping up… There are a few recurrence equations that show up a lot for divide and conquer running times. There are some general techniques when the recurrence doesn’t match this but sometimes you just have to work out the math. If you can recognise one of these recurrences for your algorithm, then just use these equations.
  • 5. We have already seen why T(n) = O(1) + 2T(n/2) is in O(n log n) It isn’t hard to see that T(n) = T(n - 1) + O(1) must be O(n) either. The argument is not different from the one we had for the running time of linear search. For selection sort we can argue that we recurse to depth n and at each level we perform a search that costs O(n). The running time must then be O(n²) — as we also argued first time we saw selection sort.
  • 6. Binary search was already a divide and conquer algorithm (although we didn’t implement it using recursion). We now it can reach call depth O(log n) but it only does constant time at each level. It might be less obvious what the running time of T(n) = 2T(n/2) + O(1) is, but we can argue as follows 1) We cut the problem size in half each time we solve a recursive problem, therefore we go to call depth log n 2) We double the number of operations we must do at each level 3) You have to know this sum, but it is easy (see next slide) This sketch doesn’t give us the exact sum, but we can see that if we double the number we add at each step up to n, then the sum is less than 2n
  • 7. We can also consider summing in the other direct, starting with n. If we half the number each time we add a new one, then regardless of where we stop, we will have a sum that is less than 2n. In this algorithm we spend linear time per level and cut the problem size in two when going from one level to the next. The sum is the same as before (more or less) and we have a linear time algorithm. Exercises! Time to put divide-and- conquer into practice