SlideShare a Scribd company logo
1 of 29
Divide and Conquer
Pasi Fränti
2.11.2016
2
Divide and Conquer
1. Divide to sub-problems
2. Solve the sub-problems
3. Conquer the solutions
By recursion!
3
StupidExample(N)
IF N=0 THEN RETURN O(1)
ELSE
FOR i←1 TO N DO N
WRITE(‘x’); O(1)
StupidExample(N-1); T(N-1)
END;
1)1()(  NTNNT
Stupid example
    
 
      
   
          
   
   2
1
1
0
1
2
1
1
0
...
4134321
)3(321
31221
)2(21
2111
)1(1)(
NN
NN
Ni
TkiN
NTNNNNN
NTNNN
NTNNN
NTNN
NTNN
NTNNT
N
i
k
i















Analysis by substitution method
)1(1)(  NTNNT
k=N
RepeatuntilT(0)
k+…+3+2+1 → 1+2+3+…+k
Sort(A[i, j])
q  FindMin(A[i, j]);
Swap(A[i],A[q])
Sort(A[i+1, j]);
FindMin(A[i, j])
q  FindMin(A[i+1, j]);
IF A[i]<A[q] THEN RETURN i
ELSE RETURN q;
)(
)1(1)(
2
NO
NTNNT


Sorting algorithm
Recursive “through the bones”


 

otherwise
if)(
)(
a
cNedNcNTb
NT
Master Theorem
Arithmetic case
Time complexity function:
Solution:









1)(
0,1)(
0,1)(
)(
)/(
2
bbO
dbNO
dbNO
NT
cN
    
      
      cNcNcN
bbecbcNbNdcTb
bbecNbcNbNdcNTb
becNbNdcNTb
edNcNbTNT
///
223
2
...1...
12)3(
1)2(
)()(




Master Theorem
Proof for arithmetic case
Substitution:
      cNcNcN
bbecbcNbNdcTb ///
...1... 
Master Theorem
Proof for arithmetic case
Case 1: 0,1  db
a1 0   1cN
   NacNNT 
N/c terms
      cNcNcN
bbecbcNbNdcTb ///
...1... 
Master Theorem
Proof for arithmetic case
Case 2: 0,1  db
a1 cid
cN
i
1
       2
2
2
22
21 N
c
N
c
N
cNcNNT 
  1cN
Arithmetic
sum
      cNcNcN
bbecbcNbNdcTb ///
...1... 
Master Theorem
Proof for arithmetic case
Case 3: 1b
ab cN
 cN
bd 
cN
be
Dominating
term
     cNcN
bbedaNT 
Quicksort(A[i, j])
IF i < j THEN O(1)
k ← Partition(A[i, j]); O(N)
Quicksort(A[i, k]); T(N/2)
Quicksort(A[k+1, j]); T(N/2)
ELSE RETURN;
     221 NTNTNNT 
Quicksort
Partition algorithm
 Choose (any) element as pivot-value p.
 Arrange all x≤p to the left side of list.
 Arrange all x>p to the right side.
 Time complexity O(N).
7 3 112 20 136 1 10 145 8
7 3 11 2 20 13 6 1 10 14 5 8
p=7
x≤7 x>7
Partition(A[i, j])
pivot = A[j];
r = i-1;
FOR k = i TO j-1 DO
IF (A[k] ≤ pivot) THEN
r = r + 1;
SWAP(A[r], A[k]);
SWAP(A[r+1], A[j]);
RETURN r+1;
r∙∙∙ k
x≤p x>p
∙∙∙
unprocessed
FOR increases k
R increases
when needed
Partition algorithm
Selection(A[i, j], k)
IF i=j THEN
RETURN A[i];
ELSE
q ← Partition(A, i, j);
size ← (q-i)+1;
IF k ≤ size THEN Selection(A[i, q], k);
ELSE Selection(A[q+1, j], k-size);
2)
2
()(  dN
N
TNT
Selection in linear time
Find the kth smallest
edNcNTbNT  )()(
Master Theorem
Geometric case









cbNO
cbNNO
cbNO
NT
bc
)(
)log(
),(
)(
log
Time complexity function:
Solution:
Master Theorem
Proof of geometric case
   
    
   
      
        
)1(
...1
...
)1(
)1(
21
2
122
122
2









bbbe
bcbcbcbdTb
bebcbcbdcTb
becbcdcTb
eNdecNdcNTbb
edNcNTbNT
pp
ppp
pppp
ppp
Np
cN
c
p
log

Extract
psteps
Case 1: cb 
Master Theorem
Proof of geometric case
  )1(...1 12
2




























 p
p
pp
bbbe
b
c
b
c
b
c
bdTb 
Np
cN
c
p
log

Nb Nc
log
 N
N
b
dc
cc
b
d
c
b
d
b
c
bd
p
p
p
p








 

1
1
Nb Nc
log
1
11
0 




 a
a
a
nn
i
i
Geometric sum
Geometric sum
a>1
Case 2: cb 
Master Theorem
Proof of geometric case
Nb Nc
log
Nb Nc
log
Log N terms
Np
cN
c
p
log

NN log
=1
N
Geometric sum
   NNNT log
  )1(...1 12
2




























 p
p
pp
bbbe
b
c
b
c
b
c
bdTb 
Case 3: cb 
Master Theorem
Proof of geometric case
p
b
…summation…
p
bp
b
Np
cN
c
p
log

  bNp cc
NbbNT loglog

 
 
b
bN
bN
Nb
NbN
c
cc
cc
cc
ccc
N
c
c
c
cb
log
loglog
loglog
loglog
logloglog







  )1(...1 12
2




























 p
p
pp
bbbe
b
c
b
c
b
c
bdTb 
<1
MergeSort(A[i, j])
IF i<j THEN
k := (i+j)/2; O(1)
MergeSort (A[i, k]); T(N/2)
MergeSort(A[k+1, j]); T(N/2)
Merge(A[i, j], k); c∙N
Merge sort
Main algorithm
Merge(A[i, j], k)
{
l←i; m←k+1; t←i;
WHILE (l ≤ k) OR (m ≤ j)
IF l>k THEN B[t]←A[m]; m←m+1;
ELSEIF m>j THEN B[t]←A[l]; l←l+1;
ELSEIF A[l]<A[m] THEN B[t]←A[m]; m←m+1;
ELSE B[t]←A[l]; l←l+1;
t←t+1;
FOR t ← i TO j DO A[t]←B[t];
}
Merge sort
Merge step
47)2/(21)2/(2)(
)()37()(


NNTTNTNT
NONONT
merge
merge
Since we have b=c  O(N log N)
Merge sort
Time complexity
47)2/(2)(  NNTNT
)log(
49log7
247log2
)221(473)2(2
24244777)2(2
24477)4)2(7)2(2(2
24477)2(22
47)427)2(2(2
log
0
2
log
333
233
232
2
2
2
NNO
NNN
NN
NNT
NNNNT
NNNNT
NNNT
NNNT
N
i
iN









Merge sort
Substitution method
3 6 2 4
 2 3 4 5
1 8 1 2 0
1 4 4 9 6
1 0 8 7 2
7 2 4 8
8 4 9 8 2 8 0
Karatsuba-Ofman multiplication
Multiplication of two n-digit numbers
School book algorithm:
)()( 2
nOnT 
Karatsuba-Ofman multiplication
Straightforward divide-and-conquer
tvktusvksu
tvktuksvksu
vuktskyx
nn
nnn
nn



2
22
22
)(
)()(
vkuy
tksx
n
n


2
2
One n-digit number divided into two n/2-digit
numbers (most and least significant)
3624=36∙102+24
2345=23∙102+45
Multiplication reformulated:
Karatsuba-Ofman multiplication
Time complexity analysis
tvktusvksu nn
 2
)(






2
n
T 





2
n
T 





2
n
T 





2
n
T
2
n
n
     24log2
5.4
2
4 nnn
n
TnT 






n nn
Karatsuba-Ofman multiplication
Divide-and-Conquer revised
     tvktvsuvutsksu
tvktusvksu
nn
nn


2
2
)(






2
n
T 





2
n
T 





2
n
T
     58.13log2
5.7
2
3 nnn
n
TnT 






+ 6 summations and 1.5 multiplications by kn
Divide and Conquer Algorithm Analysis
Divide and Conquer Algorithm Analysis

More Related Content

What's hot

Nyquist and polar plot 118 &amp; 117
Nyquist and polar plot 118 &amp; 117Nyquist and polar plot 118 &amp; 117
Nyquist and polar plot 118 &amp; 117RishabhKashyap2
 
Metodo Monte Carlo -Wang Landau
Metodo Monte Carlo -Wang LandauMetodo Monte Carlo -Wang Landau
Metodo Monte Carlo -Wang Landauangely alcendra
 
Development of Matlab Programme to study nonlinear vibration of a cantilever ...
Development of Matlab Programme to study nonlinear vibration of a cantilever ...Development of Matlab Programme to study nonlinear vibration of a cantilever ...
Development of Matlab Programme to study nonlinear vibration of a cantilever ...ijiert bestjournal
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-SortTareq Hasan
 
Lec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrencesLec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrencesAnkita Karia
 
R. Jimenez - Fundamental Physics from Astronomical Observations
R. Jimenez - Fundamental Physics from Astronomical ObservationsR. Jimenez - Fundamental Physics from Astronomical Observations
R. Jimenez - Fundamental Physics from Astronomical ObservationsSEENET-MTP
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and ComplexityRajandeep Gill
 
Topic modeling with Poisson factorization (2)
Topic modeling with Poisson factorization (2)Topic modeling with Poisson factorization (2)
Topic modeling with Poisson factorization (2)Tomonari Masada
 
Operational research
Operational researchOperational research
Operational researchAlbi Thomas
 
Using blurred images to assess damage in bridge structures?
Using blurred images to assess damage in bridge structures?Using blurred images to assess damage in bridge structures?
Using blurred images to assess damage in bridge structures? Alessandro Palmeri
 
MinFill_Presentation
MinFill_PresentationMinFill_Presentation
MinFill_PresentationAnna Lasota
 

What's hot (20)

Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
ENFPC 2013
ENFPC 2013ENFPC 2013
ENFPC 2013
 
Nyquist and polar plot 118 &amp; 117
Nyquist and polar plot 118 &amp; 117Nyquist and polar plot 118 &amp; 117
Nyquist and polar plot 118 &amp; 117
 
Metodo Monte Carlo -Wang Landau
Metodo Monte Carlo -Wang LandauMetodo Monte Carlo -Wang Landau
Metodo Monte Carlo -Wang Landau
 
Development of Matlab Programme to study nonlinear vibration of a cantilever ...
Development of Matlab Programme to study nonlinear vibration of a cantilever ...Development of Matlab Programme to study nonlinear vibration of a cantilever ...
Development of Matlab Programme to study nonlinear vibration of a cantilever ...
 
Kalman
KalmanKalman
Kalman
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
A Note on TopicRNN
A Note on TopicRNNA Note on TopicRNN
A Note on TopicRNN
 
Poisson factorization
Poisson factorizationPoisson factorization
Poisson factorization
 
Lec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrencesLec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrences
 
R. Jimenez - Fundamental Physics from Astronomical Observations
R. Jimenez - Fundamental Physics from Astronomical ObservationsR. Jimenez - Fundamental Physics from Astronomical Observations
R. Jimenez - Fundamental Physics from Astronomical Observations
 
Graph Kernelpdf
Graph KernelpdfGraph Kernelpdf
Graph Kernelpdf
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
 
Topic modeling with Poisson factorization (2)
Topic modeling with Poisson factorization (2)Topic modeling with Poisson factorization (2)
Topic modeling with Poisson factorization (2)
 
discrete-hmm
discrete-hmmdiscrete-hmm
discrete-hmm
 
Operational research
Operational researchOperational research
Operational research
 
Using blurred images to assess damage in bridge structures?
Using blurred images to assess damage in bridge structures?Using blurred images to assess damage in bridge structures?
Using blurred images to assess damage in bridge structures?
 
Alex1 group2
Alex1 group2Alex1 group2
Alex1 group2
 
MinFill_Presentation
MinFill_PresentationMinFill_Presentation
MinFill_Presentation
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 

Similar to Divide and Conquer Algorithm Analysis

Design and Analysis of Algorithms - Divide and Conquer
Design and Analysis of Algorithms - Divide and ConquerDesign and Analysis of Algorithms - Divide and Conquer
Design and Analysis of Algorithms - Divide and ConquerSeshu Chakravarthy
 
lecture 3
lecture 3lecture 3
lecture 3sajinsc
 
Recurrences
RecurrencesRecurrences
RecurrencesDEVTYPE
 
Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutionssubhashchandra197
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquerVikas Sharma
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptxpallavidhade2
 
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probabilitybryan111472
 
【DL輪読会】Unbiased Gradient Estimation for Marginal Log-likelihood
【DL輪読会】Unbiased Gradient Estimation for Marginal Log-likelihood【DL輪読会】Unbiased Gradient Estimation for Marginal Log-likelihood
【DL輪読会】Unbiased Gradient Estimation for Marginal Log-likelihoodDeep Learning JP
 
07- 22 Jan - Divide and Conquer
07- 22 Jan - Divide and Conquer07- 22 Jan - Divide and Conquer
07- 22 Jan - Divide and ConquerNeeldhara Misra
 
Copy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquerCopy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquerJoepang2015
 
Introduction
IntroductionIntroduction
Introductionpilavare
 

Similar to Divide and Conquer Algorithm Analysis (20)

Design and Analysis of Algorithms - Divide and Conquer
Design and Analysis of Algorithms - Divide and ConquerDesign and Analysis of Algorithms - Divide and Conquer
Design and Analysis of Algorithms - Divide and Conquer
 
lecture 3
lecture 3lecture 3
lecture 3
 
Recurrences
RecurrencesRecurrences
Recurrences
 
Recurrences
RecurrencesRecurrences
Recurrences
 
Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutions
 
Computer science-formulas
Computer science-formulasComputer science-formulas
Computer science-formulas
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
2018 MUMS Fall Course - Statistical and Mathematical Techniques for Sensitivi...
2018 MUMS Fall Course - Statistical and Mathematical Techniques for Sensitivi...2018 MUMS Fall Course - Statistical and Mathematical Techniques for Sensitivi...
2018 MUMS Fall Course - Statistical and Mathematical Techniques for Sensitivi...
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
 
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
 
Lec10
Lec10Lec10
Lec10
 
quick and merge.pptx
quick and merge.pptxquick and merge.pptx
quick and merge.pptx
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
【DL輪読会】Unbiased Gradient Estimation for Marginal Log-likelihood
【DL輪読会】Unbiased Gradient Estimation for Marginal Log-likelihood【DL輪読会】Unbiased Gradient Estimation for Marginal Log-likelihood
【DL輪読会】Unbiased Gradient Estimation for Marginal Log-likelihood
 
07- 22 Jan - Divide and Conquer
07- 22 Jan - Divide and Conquer07- 22 Jan - Divide and Conquer
07- 22 Jan - Divide and Conquer
 
Copy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquerCopy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquer
 
Mergesort
MergesortMergesort
Mergesort
 
Introduction
IntroductionIntroduction
Introduction
 
03 dc
03 dc03 dc
03 dc
 

Recently uploaded

Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxPurva Nikam
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 

Recently uploaded (20)

Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptx
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 

Divide and Conquer Algorithm Analysis

  • 1. Divide and Conquer Pasi Fränti 2.11.2016
  • 2. 2 Divide and Conquer 1. Divide to sub-problems 2. Solve the sub-problems 3. Conquer the solutions By recursion!
  • 3. 3 StupidExample(N) IF N=0 THEN RETURN O(1) ELSE FOR i←1 TO N DO N WRITE(‘x’); O(1) StupidExample(N-1); T(N-1) END; 1)1()(  NTNNT Stupid example
  • 4.                                     2 1 1 0 1 2 1 1 0 ... 4134321 )3(321 31221 )2(21 2111 )1(1)( NN NN Ni TkiN NTNNNNN NTNNN NTNNN NTNN NTNN NTNNT N i k i                Analysis by substitution method )1(1)(  NTNNT k=N RepeatuntilT(0) k+…+3+2+1 → 1+2+3+…+k
  • 5. Sort(A[i, j]) q  FindMin(A[i, j]); Swap(A[i],A[q]) Sort(A[i+1, j]); FindMin(A[i, j]) q  FindMin(A[i+1, j]); IF A[i]<A[q] THEN RETURN i ELSE RETURN q; )( )1(1)( 2 NO NTNNT   Sorting algorithm Recursive “through the bones”
  • 6.      otherwise if)( )( a cNedNcNTb NT Master Theorem Arithmetic case Time complexity function: Solution:          1)( 0,1)( 0,1)( )( )/( 2 bbO dbNO dbNO NT cN
  • 7.                   cNcNcN bbecbcNbNdcTb bbecNbcNbNdcNTb becNbNdcNTb edNcNbTNT /// 223 2 ...1... 12)3( 1)2( )()(     Master Theorem Proof for arithmetic case Substitution:
  • 8.       cNcNcN bbecbcNbNdcTb /// ...1...  Master Theorem Proof for arithmetic case Case 1: 0,1  db a1 0   1cN    NacNNT  N/c terms
  • 9.       cNcNcN bbecbcNbNdcTb /// ...1...  Master Theorem Proof for arithmetic case Case 2: 0,1  db a1 cid cN i 1        2 2 2 22 21 N c N c N cNcNNT    1cN Arithmetic sum
  • 10.       cNcNcN bbecbcNbNdcTb /// ...1...  Master Theorem Proof for arithmetic case Case 3: 1b ab cN  cN bd  cN be Dominating term      cNcN bbedaNT 
  • 11. Quicksort(A[i, j]) IF i < j THEN O(1) k ← Partition(A[i, j]); O(N) Quicksort(A[i, k]); T(N/2) Quicksort(A[k+1, j]); T(N/2) ELSE RETURN;      221 NTNTNNT  Quicksort
  • 12. Partition algorithm  Choose (any) element as pivot-value p.  Arrange all x≤p to the left side of list.  Arrange all x>p to the right side.  Time complexity O(N). 7 3 112 20 136 1 10 145 8 7 3 11 2 20 13 6 1 10 14 5 8 p=7 x≤7 x>7
  • 13. Partition(A[i, j]) pivot = A[j]; r = i-1; FOR k = i TO j-1 DO IF (A[k] ≤ pivot) THEN r = r + 1; SWAP(A[r], A[k]); SWAP(A[r+1], A[j]); RETURN r+1; r∙∙∙ k x≤p x>p ∙∙∙ unprocessed FOR increases k R increases when needed Partition algorithm
  • 14. Selection(A[i, j], k) IF i=j THEN RETURN A[i]; ELSE q ← Partition(A, i, j); size ← (q-i)+1; IF k ≤ size THEN Selection(A[i, q], k); ELSE Selection(A[q+1, j], k-size); 2) 2 ()(  dN N TNT Selection in linear time Find the kth smallest
  • 15. edNcNTbNT  )()( Master Theorem Geometric case          cbNO cbNNO cbNO NT bc )( )log( ),( )( log Time complexity function: Solution:
  • 16. Master Theorem Proof of geometric case                              )1( ...1 ... )1( )1( 21 2 122 122 2          bbbe bcbcbcbdTb bebcbcbdcTb becbcdcTb eNdecNdcNTbb edNcNTbNT pp ppp pppp ppp Np cN c p log  Extract psteps
  • 17. Case 1: cb  Master Theorem Proof of geometric case   )1(...1 12 2                              p p pp bbbe b c b c b c bdTb  Np cN c p log  Nb Nc log  N N b dc cc b d c b d b c bd p p p p            1 1 Nb Nc log 1 11 0       a a a nn i i Geometric sum Geometric sum a>1
  • 18. Case 2: cb  Master Theorem Proof of geometric case Nb Nc log Nb Nc log Log N terms Np cN c p log  NN log =1 N Geometric sum    NNNT log   )1(...1 12 2                              p p pp bbbe b c b c b c bdTb 
  • 19. Case 3: cb  Master Theorem Proof of geometric case p b …summation… p bp b Np cN c p log    bNp cc NbbNT loglog      b bN bN Nb NbN c cc cc cc ccc N c c c cb log loglog loglog loglog logloglog          )1(...1 12 2                              p p pp bbbe b c b c b c bdTb  <1
  • 20. MergeSort(A[i, j]) IF i<j THEN k := (i+j)/2; O(1) MergeSort (A[i, k]); T(N/2) MergeSort(A[k+1, j]); T(N/2) Merge(A[i, j], k); c∙N Merge sort Main algorithm
  • 21. Merge(A[i, j], k) { l←i; m←k+1; t←i; WHILE (l ≤ k) OR (m ≤ j) IF l>k THEN B[t]←A[m]; m←m+1; ELSEIF m>j THEN B[t]←A[l]; l←l+1; ELSEIF A[l]<A[m] THEN B[t]←A[m]; m←m+1; ELSE B[t]←A[l]; l←l+1; t←t+1; FOR t ← i TO j DO A[t]←B[t]; } Merge sort Merge step
  • 24. 3 6 2 4  2 3 4 5 1 8 1 2 0 1 4 4 9 6 1 0 8 7 2 7 2 4 8 8 4 9 8 2 8 0 Karatsuba-Ofman multiplication Multiplication of two n-digit numbers School book algorithm: )()( 2 nOnT 
  • 26. Karatsuba-Ofman multiplication Time complexity analysis tvktusvksu nn  2 )(       2 n T       2 n T       2 n T       2 n T 2 n n      24log2 5.4 2 4 nnn n TnT        n nn
  • 27. Karatsuba-Ofman multiplication Divide-and-Conquer revised      tvktvsuvutsksu tvktusvksu nn nn   2 2 )(       2 n T       2 n T       2 n T      58.13log2 5.7 2 3 nnn n TnT        + 6 summations and 1.5 multiplications by kn