SlideShare a Scribd company logo
Divide-and-Conquer 1
© 2004 Goodrich, Tamassia
Divide-and-Conquer
7 2  9 4  2 4 7 9
7  2  2 7 9  4  4 9
7  7 2  2 9  9 4  4
Divide-and-Conquer 2
© 2004 Goodrich, Tamassia
Divide-and-Conquer
Divide-and conquer is a
general algorithm design
paradigm:
 Divide: divide the input data S in
two or more disjoint subsets S1,
S2, …
 Recur: solve the subproblems
recursively
 Conquer: combine the solutions
for S1, S2, …, into a solution for S
The base case for the
recursion are subproblems of
constant size
Analysis can be done using
recurrence equations
Divide-and-Conquer 3
© 2004 Goodrich, Tamassia
Merge-Sort Review
Merge-sort on an
input sequence S with
n elements consists of
three steps:
 Divide: partition S into
two sequences S1 and S2
of about n2 elements
each
 Recur: recursively sort
S1 and S2
 Conquer: merge S1 and
S2 into a unique sorted
sequence
Algorithm mergeSort(S, C)
Input sequence S with n
elements, comparator C
Output sequence S sorted
according to C
if S.size() > 1
(S1, S2)  partition(S, n/2)
mergeSort(S1, C)
mergeSort(S2, C)
S  merge(S1, S2)
Divide-and-Conquer 4
© 2004 Goodrich, Tamassia
Recurrence Equation
Analysis
The conquer step of merge-sort consists of merging two
sorted sequences, each with n2 elements and implemented by
means of a doubly linked list, takes at most bn steps, for some
constant b.
Likewise, the basis case (n < 2) will take at b most steps.
Therefore, if we let T(n) denote the running time of merge-sort:
We can therefore analyze the running time of merge-sort by
finding a closed form solution to the above equation.
 That is, a solution that has T(n) only on the left-hand side.







2
if
)
2
/
(
2
2
if
)
(
n
bn
n
T
n
b
n
T
Divide-and-Conquer 5
© 2004 Goodrich, Tamassia
Iterative Substitution
In the iterative substitution, or “plug-and-chug,” technique, we iteratively
apply the recurrence equation to itself and see if we can find a pattern:
Note that base, T(n)=b, case occurs when 2i
=n. That is, i = log n.
So,
Thus, T(n) is O(n log n).
ibn
n
T
bn
n
T
bn
n
T
bn
n
T
bn
n
b
n
T
bn
n
T
n
T
i
i














)
2
/
(
2
...
4
)
2
/
(
2
3
)
2
/
(
2
2
)
2
/
(
2
))
2
/
(
))
2
/
(
2
(
2
)
2
/
(
2
)
(
4
4
3
3
2
2
2
n
bn
bn
n
T log
)
( 

Divide-and-Conquer 6
© 2004 Goodrich, Tamassia
The Recursion Tree
Draw the recursion tree for the recurrence relation and look
for a pattern:
depth T’s size
0 1 n
1 2 n2
i 2i
n2i
… … …







2
if
)
2
/
(
2
2
if
)
(
n
bn
n
T
n
b
n
T
time
bn
bn
bn
…
Total time = bn + bn log n
(last level plus all previous levels)
Divide-and-Conquer 7
© 2004 Goodrich, Tamassia
Guess-and-Test Method
In the guess-and-test method, we guess a closed form solution
and then try to prove it is true by induction:
Guess: T(n) < cn log n.
Wrong: we cannot make this last line be less than cn log n
n
bn
cn
n
cn
n
bn
n
cn
n
bn
n
n
c
n
bn
n
T
n
T
log
log
log
)
2
log
(log
log
))
2
/
log(
)
2
/
(
(
2
log
)
2
/
(
2
)
(

















2
if
log
)
2
/
(
2
2
if
)
(
n
n
bn
n
T
n
b
n
T
Divide-and-Conquer 8
© 2004 Goodrich, Tamassia
Guess-and-Test Method,
Part 2
Recall the recurrence equation:
Guess #2: T(n) < cn log2
n.
 if c > b.
So, T(n) is O(n log2
n).
In general, to use this method, you need to have a good guess
and you need to be good at induction proofs.
n
cn
n
bn
cn
n
cn
n
cn
n
bn
n
cn
n
bn
n
n
c
n
bn
n
T
n
T
2
2
2
2
log
log
log
2
log
log
)
2
log
(log
log
))
2
/
(
log
)
2
/
(
(
2
log
)
2
/
(
2
)
(



















2
if
log
)
2
/
(
2
2
if
)
(
n
n
bn
n
T
n
b
n
T
Divide-and-Conquer 9
© 2004 Goodrich, Tamassia
Master Method
(Appendix)
Many divide-and-conquer recurrence equations
have the form:
The Master Theorem:







d
n
n
f
b
n
aT
d
n
c
n
T
if
)
(
)
/
(
if
)
(
.
1
some
for
)
(
)
/
(
provided
)),
(
(
is
)
(
then
),
(
is
)
(
if
3.
)
log
(
is
)
(
then
),
log
(
is
)
(
if
2.
)
(
is
)
(
then
),
(
is
)
(
if
1.
log
1
log
log
log
log














n
f
b
n
af
n
f
n
T
n
n
f
n
n
n
T
n
n
n
f
n
n
T
n
O
n
f
a
k
a
k
a
a
a
b
b
b
b
b
Divide-and-Conquer 10
© 2004 Goodrich, Tamassia
Master Method, Example
1
The form:
The Master Theorem:
Example:







d
n
n
f
b
n
aT
d
n
c
n
T
if
)
(
)
/
(
if
)
(
.
1
some
for
)
(
)
/
(
provided
)),
(
(
is
)
(
then
),
(
is
)
(
if
3.
)
log
(
is
)
(
then
),
log
(
is
)
(
if
2.
)
(
is
)
(
then
),
(
is
)
(
if
1.
log
1
log
log
log
log














n
f
b
n
af
n
f
n
T
n
n
f
n
n
n
T
n
n
n
f
n
n
T
n
O
n
f
a
k
a
k
a
a
a
b
b
b
b
b
n
n
T
n
T 
 )
2
/
(
4
)
(
Solution: logba=2, so case 1 says T(n) is O(n2
).
Divide-and-Conquer 11
© 2004 Goodrich, Tamassia
Master Method, Example
2
The form:
The Master Theorem:
Example:







d
n
n
f
b
n
aT
d
n
c
n
T
if
)
(
)
/
(
if
)
(
.
1
some
for
)
(
)
/
(
provided
)),
(
(
is
)
(
then
),
(
is
)
(
if
3.
)
log
(
is
)
(
then
),
log
(
is
)
(
if
2.
)
(
is
)
(
then
),
(
is
)
(
if
1.
log
1
log
log
log
log














n
f
b
n
af
n
f
n
T
n
n
f
n
n
n
T
n
n
n
f
n
n
T
n
O
n
f
a
k
a
k
a
a
a
b
b
b
b
b
n
n
n
T
n
T log
)
2
/
(
2
)
( 

Solution: logba=1, so case 2 says T(n) is O(n log2
n).
Divide-and-Conquer 12
© 2004 Goodrich, Tamassia
Master Method, Example
3
The form:
The Master Theorem:
Example:







d
n
n
f
b
n
aT
d
n
c
n
T
if
)
(
)
/
(
if
)
(
.
1
some
for
)
(
)
/
(
provided
)),
(
(
is
)
(
then
),
(
is
)
(
if
3.
)
log
(
is
)
(
then
),
log
(
is
)
(
if
2.
)
(
is
)
(
then
),
(
is
)
(
if
1.
log
1
log
log
log
log














n
f
b
n
af
n
f
n
T
n
n
f
n
n
n
T
n
n
n
f
n
n
T
n
O
n
f
a
k
a
k
a
a
a
b
b
b
b
b
n
n
n
T
n
T log
)
3
/
(
)
( 

Solution: logba=0, so case 3 says T(n) is O(n log n).
Divide-and-Conquer 13
© 2004 Goodrich, Tamassia
Master Method, Example
4
The form:
The Master Theorem:
Example:







d
n
n
f
b
n
aT
d
n
c
n
T
if
)
(
)
/
(
if
)
(
.
1
some
for
)
(
)
/
(
provided
)),
(
(
is
)
(
then
),
(
is
)
(
if
3.
)
log
(
is
)
(
then
),
log
(
is
)
(
if
2.
)
(
is
)
(
then
),
(
is
)
(
if
1.
log
1
log
log
log
log














n
f
b
n
af
n
f
n
T
n
n
f
n
n
n
T
n
n
n
f
n
n
T
n
O
n
f
a
k
a
k
a
a
a
b
b
b
b
b
2
)
2
/
(
8
)
( n
n
T
n
T 

Solution: logba=3, so case 1 says T(n) is O(n3
).
Divide-and-Conquer 14
© 2004 Goodrich, Tamassia
Master Method, Example
5
The form:
The Master Theorem:
Example:







d
n
n
f
b
n
aT
d
n
c
n
T
if
)
(
)
/
(
if
)
(
.
1
some
for
)
(
)
/
(
provided
)),
(
(
is
)
(
then
),
(
is
)
(
if
3.
)
log
(
is
)
(
then
),
log
(
is
)
(
if
2.
)
(
is
)
(
then
),
(
is
)
(
if
1.
log
1
log
log
log
log














n
f
b
n
af
n
f
n
T
n
n
f
n
n
n
T
n
n
n
f
n
n
T
n
O
n
f
a
k
a
k
a
a
a
b
b
b
b
b
3
)
3
/
(
9
)
( n
n
T
n
T 

Solution: logba=2, so case 3 says T(n) is O(n3
).
Divide-and-Conquer 15
© 2004 Goodrich, Tamassia
Master Method, Example
6
The form:
The Master Theorem:
Example:







d
n
n
f
b
n
aT
d
n
c
n
T
if
)
(
)
/
(
if
)
(
.
1
some
for
)
(
)
/
(
provided
)),
(
(
is
)
(
then
),
(
is
)
(
if
3.
)
log
(
is
)
(
then
),
log
(
is
)
(
if
2.
)
(
is
)
(
then
),
(
is
)
(
if
1.
log
1
log
log
log
log














n
f
b
n
af
n
f
n
T
n
n
f
n
n
n
T
n
n
n
f
n
n
T
n
O
n
f
a
k
a
k
a
a
a
b
b
b
b
b
1
)
2
/
(
)
( 
 n
T
n
T
Solution: logba=0, so case 2 says T(n) is O(log n).
(binary search)
Divide-and-Conquer 16
© 2004 Goodrich, Tamassia
Master Method, Example
7
The form:
The Master Theorem:
Example:







d
n
n
f
b
n
aT
d
n
c
n
T
if
)
(
)
/
(
if
)
(
.
1
some
for
)
(
)
/
(
provided
)),
(
(
is
)
(
then
),
(
is
)
(
if
3.
)
log
(
is
)
(
then
),
log
(
is
)
(
if
2.
)
(
is
)
(
then
),
(
is
)
(
if
1.
log
1
log
log
log
log














n
f
b
n
af
n
f
n
T
n
n
f
n
n
n
T
n
n
n
f
n
n
T
n
O
n
f
a
k
a
k
a
a
a
b
b
b
b
b
n
n
T
n
T log
)
2
/
(
2
)
( 

Solution: logba=1, so case 1 says T(n) is O(n).
(heap construction)
Divide-and-Conquer 17
© 2004 Goodrich, Tamassia
Iterative “Proof” of the
Master Theorem























1
)
(log
0
log
1
)
(log
0
log
2
2
3
3
2
2
2
)
/
(
)
1
(
)
/
(
)
1
(
.
.
.
)
(
)
/
(
)
/
(
)
/
(
)
(
)
/
(
)
/
(
))
/
(
))
/
(
(
)
(
)
/
(
)
(
n
i
i
i
a
n
i
i
i
n
b
b
b
b
b
n
f
a
T
n
b
n
f
a
T
a
n
f
b
n
af
b
n
f
a
b
n
T
a
n
f
b
n
af
b
n
T
a
bn
b
n
f
b
n
aT
a
n
f
b
n
aT
n
T
Divide-and-Conquer 18
© 2004 Goodrich, Tamassia
Integer Multiplication
Algorithm: Multiply two n-bit integers I and J.
 Divide step: Split I and J into high-order and low-order bits
 We can then define I*J by multiplying the parts and adding:
 So, T(n) = 4T(n/2) + n, which implies T(n) is O(n2
).
 But that is no better than the algorithm we learned in
grade school.
l
n
h
l
n
h
J
J
J
I
I
I




2
/
2
/
2
2
l
l
n
h
l
n
l
h
n
h
h
l
n
h
l
n
h
J
I
J
I
J
I
J
I
J
J
I
I
J
I







2
/
2
/
2
/
2
/
2
2
2
)
2
(
*
)
2
(
*
Divide-and-Conquer 19
© 2004 Goodrich, Tamassia
An Improved Integer
Multiplication Algorithm
Algorithm: Multiply two n-bit integers I and J.
 Divide step: Split I and J into high-order and low-order bits
 Observe that there is a different way to multiply parts:
 So, T(n) = 3T(n/2) + n, which implies T(n) is O(nlog
2
3
), by the
Master Theorem.
 Thus, T(n) is O(n1.585
).
l
n
h
l
n
h
J
J
J
I
I
I




2
/
2
/
2
2
l
l
n
h
l
l
h
n
h
h
l
l
n
l
l
h
h
h
l
h
h
l
l
l
h
n
h
h
l
l
n
l
l
h
h
h
l
l
h
n
h
h
J
I
J
I
J
I
J
I
J
I
J
I
J
I
J
I
J
I
J
I
J
I
J
I
J
I
J
I
J
I
J
J
I
I
J
I
J
I



















2
/
2
/
2
/
2
)
(
2
2
]
)
[(
2
2
]
)
)(
[(
2
*

More Related Content

PPTX
Divided and conqurddddddddddddddfffffe.pptx
PPTX
solving_recurrence_relations_using_methods.pptx
PPTX
solving_Recurrence_relations_using_methods1.pptx
PPTX
T2311 - Ch 4_Part1.pptx
PPTX
Divide and Conquer - Part 1
PDF
Recurrence relation solutions
Divided and conqurddddddddddddddfffffe.pptx
solving_recurrence_relations_using_methods.pptx
solving_Recurrence_relations_using_methods1.pptx
T2311 - Ch 4_Part1.pptx
Divide and Conquer - Part 1
Recurrence relation solutions

Similar to DivideAndConquer.pptDivideAndConquer.ppt (20)

PDF
My PhD talk "Application of H-matrices for computing partial inverse"
PPTX
Algorithm Assignment Help
PDF
A common unique random fixed point theorem in hilbert space using integral ty...
PDF
Application H-matrices for solving PDEs with multi-scale coefficients, jumpin...
PPT
Recurrences
PDF
Discrete form of the riccati equation
DOC
pradeepbishtLecture13 div conq
PPTX
2.pptx
PDF
CS330-Lectures Statistics And Probability
PDF
Elementary Differential Equations 11th Edition Boyce Solutions Manual
PDF
A common random fixed point theorem for rational ineqality in hilbert space ...
PDF
A0212010109
PDF
Ch 3 math_of_thermodynamics
PDF
Compiler Construction | Lecture 9 | Constraint Resolution
PDF
A common random fixed point theorem for rational inequality in hilbert space
PPTX
Algorithms Exam Help
PPTX
CMSC 56 | Lecture 8: Growth of Functions
PDF
Recurrences
PDF
Divide and Conquer
My PhD talk "Application of H-matrices for computing partial inverse"
Algorithm Assignment Help
A common unique random fixed point theorem in hilbert space using integral ty...
Application H-matrices for solving PDEs with multi-scale coefficients, jumpin...
Recurrences
Discrete form of the riccati equation
pradeepbishtLecture13 div conq
2.pptx
CS330-Lectures Statistics And Probability
Elementary Differential Equations 11th Edition Boyce Solutions Manual
A common random fixed point theorem for rational ineqality in hilbert space ...
A0212010109
Ch 3 math_of_thermodynamics
Compiler Construction | Lecture 9 | Constraint Resolution
A common random fixed point theorem for rational inequality in hilbert space
Algorithms Exam Help
CMSC 56 | Lecture 8: Growth of Functions
Recurrences
Divide and Conquer
Ad

More from SyedAliShahid3 (14)

PPT
lecture3.pptlecture3 data structures pptt
PPT
SecurityPolicy.ppt SecurityPolicy.ppt
PPTX
webapplicationattacks-101005070110-phpapp02.pptx
PPT
144205230-Cross-Site-Scripting-XSS-ppt.ppt
PPT
linkedLists.ppt presentation on the topic
PPTX
Levels_of_Requirements_SRE.pptx presentation
PPTX
BST.pptx data structures presentation ppt
PPT
Unit24_TopologicalSort.ppt data structures
PPTX
AVLTrees.pptx data structures presentation
PPT
16-avl-trees.ppt data structures prestentation
PPT
22-graphs1-dfs-bfs.ppt data structures ppt
PPT
25-graphs4-topological-sort.ppt data structures
PPT
dipfinal1--150927115011-lva1-app6891.ppt
PPT
QualityModelsAndAtttribQualityModels.ppt
lecture3.pptlecture3 data structures pptt
SecurityPolicy.ppt SecurityPolicy.ppt
webapplicationattacks-101005070110-phpapp02.pptx
144205230-Cross-Site-Scripting-XSS-ppt.ppt
linkedLists.ppt presentation on the topic
Levels_of_Requirements_SRE.pptx presentation
BST.pptx data structures presentation ppt
Unit24_TopologicalSort.ppt data structures
AVLTrees.pptx data structures presentation
16-avl-trees.ppt data structures prestentation
22-graphs1-dfs-bfs.ppt data structures ppt
25-graphs4-topological-sort.ppt data structures
dipfinal1--150927115011-lva1-app6891.ppt
QualityModelsAndAtttribQualityModels.ppt
Ad

Recently uploaded (20)

PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Institutional Correction lecture only . . .
PDF
RMMM.pdf make it easy to upload and study
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
01-Introduction-to-Information-Management.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Cell Types and Its function , kingdom of life
Module 4: Burden of Disease Tutorial Slides S2 2025
TR - Agricultural Crops Production NC III.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Computing-Curriculum for Schools in Ghana
Institutional Correction lecture only . . .
RMMM.pdf make it easy to upload and study
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Cell Structure & Organelles in detailed.
Abdominal Access Techniques with Prof. Dr. R K Mishra
102 student loan defaulters named and shamed – Is someone you know on the list?
O5-L3 Freight Transport Ops (International) V1.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pre independence Education in Inndia.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Final Presentation General Medicine 03-08-2024.pptx
Supply Chain Operations Speaking Notes -ICLT Program
01-Introduction-to-Information-Management.pdf
PPH.pptx obstetrics and gynecology in nursing
Cell Types and Its function , kingdom of life

DivideAndConquer.pptDivideAndConquer.ppt

  • 1. Divide-and-Conquer 1 © 2004 Goodrich, Tamassia Divide-and-Conquer 7 2  9 4  2 4 7 9 7  2  2 7 9  4  4 9 7  7 2  2 9  9 4  4
  • 2. Divide-and-Conquer 2 © 2004 Goodrich, Tamassia Divide-and-Conquer Divide-and conquer is a general algorithm design paradigm:  Divide: divide the input data S in two or more disjoint subsets S1, S2, …  Recur: solve the subproblems recursively  Conquer: combine the solutions for S1, S2, …, into a solution for S The base case for the recursion are subproblems of constant size Analysis can be done using recurrence equations
  • 3. Divide-and-Conquer 3 © 2004 Goodrich, Tamassia Merge-Sort Review Merge-sort on an input sequence S with n elements consists of three steps:  Divide: partition S into two sequences S1 and S2 of about n2 elements each  Recur: recursively sort S1 and S2  Conquer: merge S1 and S2 into a unique sorted sequence Algorithm mergeSort(S, C) Input sequence S with n elements, comparator C Output sequence S sorted according to C if S.size() > 1 (S1, S2)  partition(S, n/2) mergeSort(S1, C) mergeSort(S2, C) S  merge(S1, S2)
  • 4. Divide-and-Conquer 4 © 2004 Goodrich, Tamassia Recurrence Equation Analysis The conquer step of merge-sort consists of merging two sorted sequences, each with n2 elements and implemented by means of a doubly linked list, takes at most bn steps, for some constant b. Likewise, the basis case (n < 2) will take at b most steps. Therefore, if we let T(n) denote the running time of merge-sort: We can therefore analyze the running time of merge-sort by finding a closed form solution to the above equation.  That is, a solution that has T(n) only on the left-hand side.        2 if ) 2 / ( 2 2 if ) ( n bn n T n b n T
  • 5. Divide-and-Conquer 5 © 2004 Goodrich, Tamassia Iterative Substitution In the iterative substitution, or “plug-and-chug,” technique, we iteratively apply the recurrence equation to itself and see if we can find a pattern: Note that base, T(n)=b, case occurs when 2i =n. That is, i = log n. So, Thus, T(n) is O(n log n). ibn n T bn n T bn n T bn n T bn n b n T bn n T n T i i               ) 2 / ( 2 ... 4 ) 2 / ( 2 3 ) 2 / ( 2 2 ) 2 / ( 2 )) 2 / ( )) 2 / ( 2 ( 2 ) 2 / ( 2 ) ( 4 4 3 3 2 2 2 n bn bn n T log ) (  
  • 6. Divide-and-Conquer 6 © 2004 Goodrich, Tamassia The Recursion Tree Draw the recursion tree for the recurrence relation and look for a pattern: depth T’s size 0 1 n 1 2 n2 i 2i n2i … … …        2 if ) 2 / ( 2 2 if ) ( n bn n T n b n T time bn bn bn … Total time = bn + bn log n (last level plus all previous levels)
  • 7. Divide-and-Conquer 7 © 2004 Goodrich, Tamassia Guess-and-Test Method In the guess-and-test method, we guess a closed form solution and then try to prove it is true by induction: Guess: T(n) < cn log n. Wrong: we cannot make this last line be less than cn log n n bn cn n cn n bn n cn n bn n n c n bn n T n T log log log ) 2 log (log log )) 2 / log( ) 2 / ( ( 2 log ) 2 / ( 2 ) (                  2 if log ) 2 / ( 2 2 if ) ( n n bn n T n b n T
  • 8. Divide-and-Conquer 8 © 2004 Goodrich, Tamassia Guess-and-Test Method, Part 2 Recall the recurrence equation: Guess #2: T(n) < cn log2 n.  if c > b. So, T(n) is O(n log2 n). In general, to use this method, you need to have a good guess and you need to be good at induction proofs. n cn n bn cn n cn n cn n bn n cn n bn n n c n bn n T n T 2 2 2 2 log log log 2 log log ) 2 log (log log )) 2 / ( log ) 2 / ( ( 2 log ) 2 / ( 2 ) (                    2 if log ) 2 / ( 2 2 if ) ( n n bn n T n b n T
  • 9. Divide-and-Conquer 9 © 2004 Goodrich, Tamassia Master Method (Appendix) Many divide-and-conquer recurrence equations have the form: The Master Theorem:        d n n f b n aT d n c n T if ) ( ) / ( if ) ( . 1 some for ) ( ) / ( provided )), ( ( is ) ( then ), ( is ) ( if 3. ) log ( is ) ( then ), log ( is ) ( if 2. ) ( is ) ( then ), ( is ) ( if 1. log 1 log log log log               n f b n af n f n T n n f n n n T n n n f n n T n O n f a k a k a a a b b b b b
  • 10. Divide-and-Conquer 10 © 2004 Goodrich, Tamassia Master Method, Example 1 The form: The Master Theorem: Example:        d n n f b n aT d n c n T if ) ( ) / ( if ) ( . 1 some for ) ( ) / ( provided )), ( ( is ) ( then ), ( is ) ( if 3. ) log ( is ) ( then ), log ( is ) ( if 2. ) ( is ) ( then ), ( is ) ( if 1. log 1 log log log log               n f b n af n f n T n n f n n n T n n n f n n T n O n f a k a k a a a b b b b b n n T n T   ) 2 / ( 4 ) ( Solution: logba=2, so case 1 says T(n) is O(n2 ).
  • 11. Divide-and-Conquer 11 © 2004 Goodrich, Tamassia Master Method, Example 2 The form: The Master Theorem: Example:        d n n f b n aT d n c n T if ) ( ) / ( if ) ( . 1 some for ) ( ) / ( provided )), ( ( is ) ( then ), ( is ) ( if 3. ) log ( is ) ( then ), log ( is ) ( if 2. ) ( is ) ( then ), ( is ) ( if 1. log 1 log log log log               n f b n af n f n T n n f n n n T n n n f n n T n O n f a k a k a a a b b b b b n n n T n T log ) 2 / ( 2 ) (   Solution: logba=1, so case 2 says T(n) is O(n log2 n).
  • 12. Divide-and-Conquer 12 © 2004 Goodrich, Tamassia Master Method, Example 3 The form: The Master Theorem: Example:        d n n f b n aT d n c n T if ) ( ) / ( if ) ( . 1 some for ) ( ) / ( provided )), ( ( is ) ( then ), ( is ) ( if 3. ) log ( is ) ( then ), log ( is ) ( if 2. ) ( is ) ( then ), ( is ) ( if 1. log 1 log log log log               n f b n af n f n T n n f n n n T n n n f n n T n O n f a k a k a a a b b b b b n n n T n T log ) 3 / ( ) (   Solution: logba=0, so case 3 says T(n) is O(n log n).
  • 13. Divide-and-Conquer 13 © 2004 Goodrich, Tamassia Master Method, Example 4 The form: The Master Theorem: Example:        d n n f b n aT d n c n T if ) ( ) / ( if ) ( . 1 some for ) ( ) / ( provided )), ( ( is ) ( then ), ( is ) ( if 3. ) log ( is ) ( then ), log ( is ) ( if 2. ) ( is ) ( then ), ( is ) ( if 1. log 1 log log log log               n f b n af n f n T n n f n n n T n n n f n n T n O n f a k a k a a a b b b b b 2 ) 2 / ( 8 ) ( n n T n T   Solution: logba=3, so case 1 says T(n) is O(n3 ).
  • 14. Divide-and-Conquer 14 © 2004 Goodrich, Tamassia Master Method, Example 5 The form: The Master Theorem: Example:        d n n f b n aT d n c n T if ) ( ) / ( if ) ( . 1 some for ) ( ) / ( provided )), ( ( is ) ( then ), ( is ) ( if 3. ) log ( is ) ( then ), log ( is ) ( if 2. ) ( is ) ( then ), ( is ) ( if 1. log 1 log log log log               n f b n af n f n T n n f n n n T n n n f n n T n O n f a k a k a a a b b b b b 3 ) 3 / ( 9 ) ( n n T n T   Solution: logba=2, so case 3 says T(n) is O(n3 ).
  • 15. Divide-and-Conquer 15 © 2004 Goodrich, Tamassia Master Method, Example 6 The form: The Master Theorem: Example:        d n n f b n aT d n c n T if ) ( ) / ( if ) ( . 1 some for ) ( ) / ( provided )), ( ( is ) ( then ), ( is ) ( if 3. ) log ( is ) ( then ), log ( is ) ( if 2. ) ( is ) ( then ), ( is ) ( if 1. log 1 log log log log               n f b n af n f n T n n f n n n T n n n f n n T n O n f a k a k a a a b b b b b 1 ) 2 / ( ) (   n T n T Solution: logba=0, so case 2 says T(n) is O(log n). (binary search)
  • 16. Divide-and-Conquer 16 © 2004 Goodrich, Tamassia Master Method, Example 7 The form: The Master Theorem: Example:        d n n f b n aT d n c n T if ) ( ) / ( if ) ( . 1 some for ) ( ) / ( provided )), ( ( is ) ( then ), ( is ) ( if 3. ) log ( is ) ( then ), log ( is ) ( if 2. ) ( is ) ( then ), ( is ) ( if 1. log 1 log log log log               n f b n af n f n T n n f n n n T n n n f n n T n O n f a k a k a a a b b b b b n n T n T log ) 2 / ( 2 ) (   Solution: logba=1, so case 1 says T(n) is O(n). (heap construction)
  • 17. Divide-and-Conquer 17 © 2004 Goodrich, Tamassia Iterative “Proof” of the Master Theorem                        1 ) (log 0 log 1 ) (log 0 log 2 2 3 3 2 2 2 ) / ( ) 1 ( ) / ( ) 1 ( . . . ) ( ) / ( ) / ( ) / ( ) ( ) / ( ) / ( )) / ( )) / ( ( ) ( ) / ( ) ( n i i i a n i i i n b b b b b n f a T n b n f a T a n f b n af b n f a b n T a n f b n af b n T a bn b n f b n aT a n f b n aT n T
  • 18. Divide-and-Conquer 18 © 2004 Goodrich, Tamassia Integer Multiplication Algorithm: Multiply two n-bit integers I and J.  Divide step: Split I and J into high-order and low-order bits  We can then define I*J by multiplying the parts and adding:  So, T(n) = 4T(n/2) + n, which implies T(n) is O(n2 ).  But that is no better than the algorithm we learned in grade school. l n h l n h J J J I I I     2 / 2 / 2 2 l l n h l n l h n h h l n h l n h J I J I J I J I J J I I J I        2 / 2 / 2 / 2 / 2 2 2 ) 2 ( * ) 2 ( *
  • 19. Divide-and-Conquer 19 © 2004 Goodrich, Tamassia An Improved Integer Multiplication Algorithm Algorithm: Multiply two n-bit integers I and J.  Divide step: Split I and J into high-order and low-order bits  Observe that there is a different way to multiply parts:  So, T(n) = 3T(n/2) + n, which implies T(n) is O(nlog 2 3 ), by the Master Theorem.  Thus, T(n) is O(n1.585 ). l n h l n h J J J I I I     2 / 2 / 2 2 l l n h l l h n h h l l n l l h h h l h h l l l h n h h l l n l l h h h l l h n h h J I J I J I J I J I J I J I J I J I J I J I J I J I J I J I J J I I J I J I                    2 / 2 / 2 / 2 ) ( 2 2 ] ) [( 2 2 ] ) )( [( 2 *