SlideShare a Scribd company logo
CSE 421
Algorithms
Richard Anderson
Lecture 11
Recurrences
Divide and Conquer
• Recurrences, Sections 5.1 and 5.2
• Algorithms
– Counting Inversions (5.3)
– Closest Pair (5.4)
– Multiplication (5.5)
– FFT (5.6)
Divide and Conquer
Array Mergesort(Array a){
n = a.Length;
if (n <= 1)
return a;
b = Mergesort(a[0 .. n/2]);
c = Mergesort(a[n/2+1 .. n-1]);
return Merge(b, c);
}
Algorithm Analysis
• Cost of Merge
• Cost of Mergesort
T(n) <= 2T(n/2) + cn; T(2) <= c;
Recurrence Analysis
• Solution methods
– Unrolling recurrence
– Guess and verify
– Plugging in to a “Master Theorem”
Unrolling the recurrence
Substitution
Prove T(n) <= cn log2n for n >= 2
Induction:
Base Case:
Induction Hypothesis:
A better mergesort (?)
• Divide into 3 subarrays and recursively
sort
• Apply 3-way merge
What is the recurrence?
Unroll recurrence for
T(n) = 3T(n/3) + dn
T(n) = aT(n/b) + f(n)
T(n) = T(n/2) + cn
Where does this recurrence arise?
Recurrences
• Three basic behaviors
– Dominated by initial case
– Dominated by base case
– All cases equal – we care about the depth
Divide and Conquer
Recurrence Examples
• T(n) = 2 T(n/2) + cn
– O(n log n)
• T(n) = T(n/2) + cn
– O(n)
• More useful facts:
– logkn = log2n / log2k
– k log n
= n log k
Recursive Matrix Multiplication
Multiply 2 x 2 Matrices:
| r s | | a b| |e g|
| t u| | c d| | f h|
r = ae + bf
s = ag + bh
t = ce + df
u = cg + dh
A N x N matrix can be viewed as
a 2 x 2 matrix with entries that
are (N/2) x (N/2) matrices.
The recursive matrix
multiplication algorithm
recursively multiplies the
(N/2) x (N/2) matrices and
combines them using the
equations for multiplying 2 x 2
matrices
=
Recursive Matrix Multiplication
• How many recursive calls
are made at each level?
• How much work in
combining the results?
• What is the recurrence?
What is the run time for the recursive
Matrix Multiplication Algorithm?
• Recurrence:
T(n) = 4T(n/2) + cn
T(n) = 2T(n/2) + n2
T(n) = 2T(n/2) + n1/2
Recurrences
• Three basic behaviors
– Dominated by initial case
– Dominated by base case
– All cases equal – we care about the depth
Solve by unrolling
T(n) = n + 5T(n/2)
What you really need to know
about recurrences
• Work per level changes geometrically with
the level
• Geometrically increasing (x > 1)
– The bottom level wins
• Geometrically decreasing (x < 1)
– The top level wins
• Balanced (x = 1)
– Equal contribution
Classify the following recurrences
(Increasing, Decreasing, Balanced)
• T(n) = n + 5T(n/8)
• T(n) = n + 9T(n/8)
• T(n) = n2
+ 4T(n/2)
• T(n) = n3
+ 7T(n/2)
• T(n) = n1/2
+ 3T(n/4)
Strassen’s Algorithm
Multiply 2 x 2 Matrices:
| r s | | a b| |e g|
| t u| | c d| | f h|
r = p1 + p4 – p5 + p7
s = p3 + p5
t = p2 + p5
u = p1 + p3 – p2 + p7
Where:
p1 = (b + d)(f + g)
p2= (c + d)e
p3= a(g – h)
p4= d(f – e)
p5= (a – b)h
p6= (c – d)(e + g)
p7= (b – d)(f + h)
=
Recurrence for Strassen’s
Algorithms
• T(n) = 7 T(n/2) + cn2
• What is the runtime?
BFPRT Recurrence
• T(n) <= T(3n/4) + T(n/5) + 20 n
What bound do you expect?

More Related Content

PPT
3-Chapter Three - Divide and Conquer.ppt
PDF
02 Notes Divide and Conquer
PPT
Divide and Conquer
PDF
Daa chapter 2
PPTX
Divided and conqurddddddddddddddfffffe.pptx
PPTX
Divide and Conquer - Part 1
PPT
5. Recursive.ppt
3-Chapter Three - Divide and Conquer.ppt
02 Notes Divide and Conquer
Divide and Conquer
Daa chapter 2
Divided and conqurddddddddddddddfffffe.pptx
Divide and Conquer - Part 1
5. Recursive.ppt

Similar to Lecture11_12COMPILER DESIGN_unit 2COMPILER DESIGN_unit 2.ppt (20)

PDF
Divide and conquer
PDF
Recurrences
PPTX
Divide and conquer strategy
PPTX
T2311 - Ch 4_Part1.pptx
PDF
Lecture 5 6_7 - divide and conquer and method of solving recurrences
PPT
Divide and conquer
PPTX
solving_Recurrence_relations_using_methods1.pptx
PPTX
designs and analysis of algorithmss.pptx
PPT
3. Recursion and Recurrences.ppt detail about recursive learning
PPTX
Ayush Jajoo(MCA2501622) AOA .pptx
PPT
recurrence relations in analysis of algorithm
PPT
recurrence relation is explained in this
PPTX
Algorithm Design and Complexity - Course 3
PPT
data unit notes from department of computer science
PPT
daa_unit THIS IS GNDFJG SDGSGS SFDF .ppt
PDF
Copy of y16 02-2119divide-and-conquer
PPT
07 dc3
PPT
daaadafrhdncxfbfbgdngfmfhmhagshh_unit_i.ppt
PPTX
2.pptx
DOC
pradeepbishtLecture13 div conq
Divide and conquer
Recurrences
Divide and conquer strategy
T2311 - Ch 4_Part1.pptx
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Divide and conquer
solving_Recurrence_relations_using_methods1.pptx
designs and analysis of algorithmss.pptx
3. Recursion and Recurrences.ppt detail about recursive learning
Ayush Jajoo(MCA2501622) AOA .pptx
recurrence relations in analysis of algorithm
recurrence relation is explained in this
Algorithm Design and Complexity - Course 3
data unit notes from department of computer science
daa_unit THIS IS GNDFJG SDGSGS SFDF .ppt
Copy of y16 02-2119divide-and-conquer
07 dc3
daaadafrhdncxfbfbgdngfmfhmhagshh_unit_i.ppt
2.pptx
pradeepbishtLecture13 div conq
Ad

Recently uploaded (20)

PPTX
Cell Types and Its function , kingdom of life
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Complications of Minimal Access Surgery at WLH
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Lesson notes of climatology university.
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
Cell Types and Its function , kingdom of life
O7-L3 Supply Chain Operations - ICLT Program
2.FourierTransform-ShortQuestionswithAnswers.pdf
Microbial disease of the cardiovascular and lymphatic systems
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
RMMM.pdf make it easy to upload and study
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Complications of Minimal Access Surgery at WLH
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Anesthesia in Laparoscopic Surgery in India
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Final Presentation General Medicine 03-08-2024.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Lesson notes of climatology university.
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Chinmaya Tiranga quiz Grand Finale.pdf
Ad

Lecture11_12COMPILER DESIGN_unit 2COMPILER DESIGN_unit 2.ppt