SlideShare a Scribd company logo
Recurrences
(CLRS 4.1-4.2)
• Last time we discussed divide-and-conquer algorithms
Divide and Conquer
To Solve P:
1. Divide P into smaller problems P1, P2, P3.....Pk.
2. Conquer by solving the (smaller) subproblems recursively.
3. Combine solutions to P1, P2, ...Pk into solution for P.
• Analysis of divide-and-conquer algorithms and in general of recursive algorithms leads to
recurrences.
• Merge-sort lead to the recurrence T(n) = 2T(n/2) + n
– or rather, T(n) =
Θ(1) If n = 1
T( n
2 ) + T( n
2 ) + Θ(n) If n > 1
– but we will often cheat and just solve the simple formula (equivalent to assuming that
n = 2k for some constant k, and leaving out base case and constant in Θ).
Methods for solving recurrences
1. Substitution method
2. Iteration method
• Recursion-tree method
• (Master method)
1 Solving Recurrences with the Substitution Method
• Idea: Make a guess for the form of the solution and prove by induction.
• Can be used to prove both upper bounds O() and lower bounds Ω().
• Let’s solve T(n) = 2T(n/2) + n using substitution
– Guess T(n) ≤ cn log n for some constant c (that is, T(n) = O(n log n))
– Proof:
∗ Base case: we need to show that our guess holds for some base case (not necessarily
n = 1, some small n is ok). Ok, since function constant for small constant n.
∗ Assume holds for n/2: T(n/2) ≤ cn
2 log n
2 (Question: Why not n − 1?)
Prove that holds for n: T(n) ≤ cn log n
T(n) = 2T(n/2) + n
≤ 2(c
n
2
log
n
2
) + n
= cn log
n
2
+ n
= cn log n − cn log 2 + n
= cn log n − cn + n
So ok if c ≥ 1
• Similarly it can be shown that T(n) = Ω(n log n)
Exercise!
• Similarly it can be shown that T(n) = T( n
2 ) + T( n
2 ) + n is Θ(n lg n).
Exercise!
• The hard part of the substitution method is often to make a good guess. How do we make
a good (i.e. tight) guess??? Unfortunately, there’s no “recipe” for this one. Try iteratively
O(n3), Ω(n3), O(n2), Ω(n2) and so on. Try solving by iteration to get a feeling of the growth.
2 Solving Recurrences with the Iteration/Recursion-tree Method
• In the iteration method we iteratively “unfold” the recurrence until we “see the pattern”.
• The iteration method does not require making a good guess like the substitution method (but
it is often more involved than using induction).
• Example: Solve T(n) = 8T(n/2) + n2 (T(1) = 1)
T(n) = n2
+ 8T(n/2)
= n2
+ 8(8T(
n
22
) + (
n
2
)2
)
= n2
+ 82
T(
n
22
) + 8(
n2
4
))
= n2
+ 2n2
+ 82
T(
n
22
)
= n2
+ 2n2
+ 82
(8T(
n
23
) + (
n
22
)2
)
= n2
+ 2n2
+ 83
T(
n
23
) + 82
(
n2
42
))
= n2
+ 2n2
+ 22
n2
+ 83
T(
n
23
)
= . . .
= n2
+ 2n2
+ 22
n2
+ 23
n2
+ 24
n2
+ . . .
– Recursion depth: How long (how many iterations) it takes until the subproblem has
constant size? i times where n
2i = 1 ⇒ i = log n
– What is the last term? 8iT(1) = 8log n
T(n) = n2
+ 2n2
+ 22
n2
+ 23
n2
+ 24
n2
+ . . . + 2log n−1
n2
+ 8log n
=
log n−1
k=0
2k
n2
+ 8log n
= n2
log n−1
k=0
2k
+ (23
)log n
• Now log n−1
k=0 2k is a geometric sum so we have log n−1
k=0 2k = Θ(2log n−1) = Θ(n)
• (23)log n = (2log n)3 = n3
T(n) = n2
· Θ(n) + n3
= Θ(n3
)
2.1 Recursion tree
A different way to look at the iteration method: is the recursion-tree, discussed in the book (4.2).
• we draw out the recursion tree with cost of single call in each node—running time is sum of
costs in all nodes
• if you are careful drawing the recursion tree and summing up the costs, the recursion tree is
a direct proof for the solution of the recurrence, just like iteration and substitution
• Example: T(n) = 8T(n/2) + n2 (T(1) = 1)
(n/2)2 (n/2)2
(n/4)2 (n/4)2
n2 n2
(n/2)2 (n/2)2
T(n/4)
n2
T(n/4)
T(n)
1)
T(n/2) T(n/2)
n2
2)
T(1) T(1) T(1)
8(n/2) = 2n
8 (n/4) = 2 n
2 2
2 2 22
log n
8 T(1)
+
+
+
+
3)
log n)
T(n) = n2 + 2n2 + 22n2 + 23n2 + 24n2 + . . . + 2log n−1n2 + 8log n
3 Matrix Multiplication
• Let X and Y be n × n matrices
X =



x11 x12 · · · x1n
x21 x22 · · · x1n
x31 x32 · · · x1n
· · · · · · · · · · · ·
xn1 xn2 · · · xnn



• We want to compute Z = X · Y
– zij = n
k=1 Xik · Ykj
• Naive method uses ⇒ n2 · n = Θ(n3) operations
• Divide-and-conquer solution:
Z =
A B
C D
·
E F
G H
=
(A · E + B · G) (A · F + B · H)
(C · E + D · G) (C · F + D · H)
– The above naturally leads to divide-and-conquer solution:
∗ Divide X and Y into 8 sub-matrices A, B, C, and D.
∗ Do 8 matrix multiplications recursively.
∗ Compute Z by combining results (doing 4 matrix additions).
– Lets assume n = 2c for some constant c and let A, B, C and D be n/2 × n/2 matrices
∗ Running time of algorithm is T(n) = 8T(n/2) + Θ(n2) ⇒ T(n) = Θ(n3)
– But we already discussed a (simpler/naive) O(n3) algorithm! Can we do better?
3.1 Strassen’s Algorithm
• Strassen observed the following:
Z =
A B
C D
·
E F
G H
=
(S1 + S2 − S4 + S6) (S4 + S5)
(S6 + S7) (S2 + S3 + S5 − S7)
where
S1 = (B − D) · (G + H)
S2 = (A + D) · (E + H)
S3 = (A − C) · (E + F)
S4 = (A + B) · H
S5 = A · (F − H)
S6 = D · (G − E)
S7 = (C + D) · E
– Lets test that S6 + S7 is really C · E + D · G
S6 + S7 = D · (G − E) + (C + D) · E
= DG − DE + CE + DE
= DG + CE
• This leads to a divide-and-conquer algorithm with running time T(n) = 7T(n/2) + Θ(n2)
– We only need to perform 7 multiplications recursively.
– Division/Combination can still be performed in Θ(n2) time.
• Lets solve the recurrence using the iteration method
T(n) = 7T(n/2) + n2
= n2
+ 7(7T(
n
22
) + (
n
2
)2
)
= n2
+ (
7
22
)n2
+ 72
T(
n
22
)
= n2
+ (
7
22
)n2
+ 72
(7T(
n
23
) + (
n
22
)2
)
= n2
+ (
7
22
)n2
+ (
7
22
)2
· n2
+ 73
T(
n
23
)
= n2
+ (
7
22
)n2
+ (
7
22
)2
n2
+ (
7
22
)3
n2
.... + (
7
22
)log n−1
n2
+ 7log n
=
log n−1
i=0
(
7
22
)i
n2
+ 7log n
= n2
· Θ((
7
22
)log n−1
) + 7log n
= n2
· Θ(
7log n
(22)log n
) + 7log n
= n2
· Θ(
7log n
n2
) + 7log n
= Θ(7log n
)
– Now we have the following:
7log n
= 7
log7 n
log7 2
= (7log7 n
)(1/ log7 2)
= n(1/ log7 2)
= n
log2 7
log2 2
= nlog 7
– Or in general: alogk n = nlogk a
So the solution is T(n) = Θ(nlog 7) = Θ(n2.81...)
• Note:
– We are ’hiding’ a much bigger constant in Θ() than before.
– Currently best known bound is O(n2.376..) (another method).
– Lower bound is (trivially) Ω(n2).
4 Master Method
• We have solved several recurrences using substitution and iteration.
• we solved several recurrences of the form T(n) = aT(n/b) + nc (T(1) = 1).
– Strassen’s algorithm ⇒ T(n) = 7T(n/2) + n2 (a = 7, b = 2, and c = 2)
– Merge-sort ⇒ T(n) = 2T(n/2) + n (a = 2, b = 2, and c = 1).
• It would be nice to have a general solution to the recurrence T(n) = aT(n/b) + nc.
• We do!
T(n) = aT n
b + nc a ≥ 1, b ≥ 1, c > 0
⇓
T(n) =



Θ(nlogb a) a > bc
Θ(nc logb n) a = bc
Θ(nc) a < bc
Proof (Iteration method)
T(n) = aT n
b + nc
= nc + a n
b
c
+ aT n
b2
= nc + a
bc nc + a2T n
b2
= nc + a
bc nc + a2 n
b2
c
+ aT n
b3
= nc + a
bc nc + a
bc
2
nc + a3T n
b3
= ...
= nc + a
bc nc + a
bc
2
nc + a
bc
3
nc + a
bc
4
nc + ... + a
bc
logb n−1
nc + alogb nT(1)
= nc logb n−1
k=0
a
bc
k
+ alogb n
= nc logb n−1
k=0
a
bc
k
+ nlogb a
Recall geometric sum n
k=0 xk = xn+1−1
x−1 = Θ(xn)
• a < bc
a < bc ⇔ a
bc < 1 ⇒
logb n−1
k=0
a
bc
k
≤ +∞
k=0
a
bc
k
= 1
1−( a
bc )
= Θ(1)
a < bc ⇔ logb a < logb bc = c
T(n) = nc logb n−1
k=0
a
bc
k
+ nlogb a
= nc · Θ(1) + nlogb a
= Θ(nc)
• a = bc
a = bc ⇔ a
bc = 1 ⇒
logb n−1
k=0
a
bc
k
=
logb n−1
k=0 1 = Θ(logb n)
a = bc ⇔ logb a = logb bc = c
T(n) =
logb n−1
k=0
a
bc
k
+ nlogb a
= ncΘ(logb n) + nlogb a
= Θ(nc logb n)
• a > bc
a > bc ⇔ a
bc > 1 ⇒
logb n−1
k=0
a
bc
k
= Θ a
bc
logb n
= Θ alogb n
(bc)logb n = Θ alogb n
nc
T(n) = nc · Θ alogb n
nc + nlogb a
= Θ(nlogb a) + nlogb a
= Θ(nlogb a)
• Note: Book states and proves the result slightly differently (don’t read it).
5 Changing variables
Sometimes reucurrences can be reduced to simpler ones by changing variables
• Example: Solve T(n) = 2T(
√
n) + log n
Let m = log n ⇒ 2m = n ⇒
√
n = 2m/2
T(n) = 2T(
√
n) + log n ⇒ T(2m) = 2T(2m/2) + m
Let S(m) = T(2m)
T(2m) = 2T(2m/2) + m ⇒ S(m) = 2S(m/2) + m
⇒ S(m) = O(m log m)
⇒ T(n) = T(2m) = S(m) = O(m log m) = O(log n log log n)
6 Other recurrences
Some important/typical bounds on recurrences not covered by master method:
• Logarithmic: Θ(log n)
– Recurrence: T(n) = 1 + T(n/2)
– Typical example: Recurse on half the input (and throw half away)
– Variations: T(n) = 1 + T(99n/100)
• Linear: Θ(N)
– Recurrence: T(n) = 1 + T(n − 1)
– Typical example: Single loop
– Variations: T(n) = 1 + 2T(n/2), T(n) = n + T(n/2), T(n) = T(n/5) + T(7n/10 + 6) + n
• Quadratic: Θ(n2)
– Recurrence: T(n) = n + T(n − 1)
– Typical example: Nested loops
• Exponential: Θ(2n)
– Recurrence: T(n) = 2T(n − 1)

More Related Content

What's hot

Master method
Master methodMaster method
Master method
arun jacob
 
Master theorem
Master theoremMaster theorem
Master theorem
fika sweety
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
Rajendran
 
Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3
Traian Rebedea
 
Analysis Of Algorithms Ii
Analysis Of Algorithms IiAnalysis Of Algorithms Ii
Analysis Of Algorithms Ii
Sri Prasanna
 
recurrence relations
 recurrence relations recurrence relations
recurrence relations
Anurag Cheela
 
Master method
Master method Master method
Master method
Rajendran
 
Dynamical systems solved ex
Dynamical systems solved exDynamical systems solved ex
Dynamical systems solved ex
Maths Tutoring
 
Asymptotic Growth of Functions
Asymptotic Growth of FunctionsAsymptotic Growth of Functions
Asymptotic Growth of Functions
DEVTYPE
 
02 asymp
02 asymp02 asymp
02 asymp
aparnabk7
 
A Proof of Twin primes and Golbach's Conjecture
A Proof of Twin primes and Golbach's ConjectureA Proof of Twin primes and Golbach's Conjecture
A Proof of Twin primes and Golbach's Conjecture
nikos mantzakouras
 
L2
L2L2
lecture 3
lecture 3lecture 3
lecture 3
sajinsc
 
L2
L2L2
Proof master theorem
Proof master theoremProof master theorem
Proof master theorem
Rajendran
 
Recurrence theorem
Recurrence theoremRecurrence theorem
Recurrence theorem
Rajendran
 
Proof of Beal's conjecture
Proof of Beal's conjecture Proof of Beal's conjecture
Proof of Beal's conjecture
nikos mantzakouras
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
Rajendran
 
Solve Equations
Solve EquationsSolve Equations
Solve Equations
nikos mantzakouras
 
Recurrence Relation
Recurrence RelationRecurrence Relation
Recurrence Relation
Papu Kumar
 

What's hot (20)

Master method
Master methodMaster method
Master method
 
Master theorem
Master theoremMaster theorem
Master theorem
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3
 
Analysis Of Algorithms Ii
Analysis Of Algorithms IiAnalysis Of Algorithms Ii
Analysis Of Algorithms Ii
 
recurrence relations
 recurrence relations recurrence relations
recurrence relations
 
Master method
Master method Master method
Master method
 
Dynamical systems solved ex
Dynamical systems solved exDynamical systems solved ex
Dynamical systems solved ex
 
Asymptotic Growth of Functions
Asymptotic Growth of FunctionsAsymptotic Growth of Functions
Asymptotic Growth of Functions
 
02 asymp
02 asymp02 asymp
02 asymp
 
A Proof of Twin primes and Golbach's Conjecture
A Proof of Twin primes and Golbach's ConjectureA Proof of Twin primes and Golbach's Conjecture
A Proof of Twin primes and Golbach's Conjecture
 
L2
L2L2
L2
 
lecture 3
lecture 3lecture 3
lecture 3
 
L2
L2L2
L2
 
Proof master theorem
Proof master theoremProof master theorem
Proof master theorem
 
Recurrence theorem
Recurrence theoremRecurrence theorem
Recurrence theorem
 
Proof of Beal's conjecture
Proof of Beal's conjecture Proof of Beal's conjecture
Proof of Beal's conjecture
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
 
Solve Equations
Solve EquationsSolve Equations
Solve Equations
 
Recurrence Relation
Recurrence RelationRecurrence Relation
Recurrence Relation
 

Viewers also liked

Рукописные лекции по линейной алгебре
Рукописные лекции по линейной алгебреРукописные лекции по линейной алгебре
Рукописные лекции по линейной алгебре
DEVTYPE
 
Continuity and Uniform Continuity
Continuity and Uniform ContinuityContinuity and Uniform Continuity
Continuity and Uniform Continuity
DEVTYPE
 
Разбор задач по дискретной вероятности
Разбор задач по дискретной вероятностиРазбор задач по дискретной вероятности
Разбор задач по дискретной вероятности
DEVTYPE
 
Разбор задач пятого модуля
Разбор задач пятого модуляРазбор задач пятого модуля
Разбор задач пятого модуля
DEVTYPE
 
Разбор задач модуля "Теория графов ll"
Разбор задач модуля "Теория графов ll"Разбор задач модуля "Теория графов ll"
Разбор задач модуля "Теория графов ll"
DEVTYPE
 
ЖАДНЫЕ АЛГОРИТМЫ
ЖАДНЫЕ АЛГОРИТМЫ ЖАДНЫЕ АЛГОРИТМЫ
ЖАДНЫЕ АЛГОРИТМЫ
DEVTYPE
 
Скорость роста функций
Скорость роста функцийСкорость роста функций
Скорость роста функций
DEVTYPE
 
Задачи №2. Работа со звуком.
Задачи №2. Работа со звуком.Задачи №2. Работа со звуком.
Задачи №2. Работа со звуком.
DEVTYPE
 
Тестовое задание для веб-программиста
Тестовое задание для веб-программистаТестовое задание для веб-программиста
Тестовое задание для веб-программиста
DEVTYPE
 
Задача №1. Работа с видео.
Задача №1. Работа с видео.Задача №1. Работа с видео.
Задача №1. Работа с видео.
DEVTYPE
 
Логарифм и экспонента
Логарифм и экспонентаЛогарифм и экспонента
Логарифм и экспонента
DEVTYPE
 
С. Дасгупта, Х. Пападимитриу, У. Вазирани. Алгоритмы
С. Дасгупта, Х. Пападимитриу, У. Вазирани. АлгоритмыС. Дасгупта, Х. Пападимитриу, У. Вазирани. Алгоритмы
С. Дасгупта, Х. Пападимитриу, У. Вазирани. Алгоритмы
DEVTYPE
 
Математическая индукция
Математическая индукцияМатематическая индукция
Математическая индукция
DEVTYPE
 
Cis435 week02
Cis435 week02Cis435 week02
Cis435 week02
ashish bansal
 
lecture 8
lecture 8lecture 8
lecture 8
sajinsc
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
Waqas Akram
 
Recurrence relations
Recurrence relationsRecurrence relations
Recurrence relations
IIUM
 
1.2 Выборка. Выборочное пространство
1.2 Выборка. Выборочное пространство1.2 Выборка. Выборочное пространство
1.2 Выборка. Выборочное пространство
DEVTYPE
 
Reccurrence relations
Reccurrence relationsReccurrence relations
Reccurrence relations
SharingIsCaring1000
 
1.4 Точечные оценки и их свойства
1.4 Точечные оценки и их свойства1.4 Точечные оценки и их свойства
1.4 Точечные оценки и их свойства
DEVTYPE
 

Viewers also liked (20)

Рукописные лекции по линейной алгебре
Рукописные лекции по линейной алгебреРукописные лекции по линейной алгебре
Рукописные лекции по линейной алгебре
 
Continuity and Uniform Continuity
Continuity and Uniform ContinuityContinuity and Uniform Continuity
Continuity and Uniform Continuity
 
Разбор задач по дискретной вероятности
Разбор задач по дискретной вероятностиРазбор задач по дискретной вероятности
Разбор задач по дискретной вероятности
 
Разбор задач пятого модуля
Разбор задач пятого модуляРазбор задач пятого модуля
Разбор задач пятого модуля
 
Разбор задач модуля "Теория графов ll"
Разбор задач модуля "Теория графов ll"Разбор задач модуля "Теория графов ll"
Разбор задач модуля "Теория графов ll"
 
ЖАДНЫЕ АЛГОРИТМЫ
ЖАДНЫЕ АЛГОРИТМЫ ЖАДНЫЕ АЛГОРИТМЫ
ЖАДНЫЕ АЛГОРИТМЫ
 
Скорость роста функций
Скорость роста функцийСкорость роста функций
Скорость роста функций
 
Задачи №2. Работа со звуком.
Задачи №2. Работа со звуком.Задачи №2. Работа со звуком.
Задачи №2. Работа со звуком.
 
Тестовое задание для веб-программиста
Тестовое задание для веб-программистаТестовое задание для веб-программиста
Тестовое задание для веб-программиста
 
Задача №1. Работа с видео.
Задача №1. Работа с видео.Задача №1. Работа с видео.
Задача №1. Работа с видео.
 
Логарифм и экспонента
Логарифм и экспонентаЛогарифм и экспонента
Логарифм и экспонента
 
С. Дасгупта, Х. Пападимитриу, У. Вазирани. Алгоритмы
С. Дасгупта, Х. Пападимитриу, У. Вазирани. АлгоритмыС. Дасгупта, Х. Пападимитриу, У. Вазирани. Алгоритмы
С. Дасгупта, Х. Пападимитриу, У. Вазирани. Алгоритмы
 
Математическая индукция
Математическая индукцияМатематическая индукция
Математическая индукция
 
Cis435 week02
Cis435 week02Cis435 week02
Cis435 week02
 
lecture 8
lecture 8lecture 8
lecture 8
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
 
Recurrence relations
Recurrence relationsRecurrence relations
Recurrence relations
 
1.2 Выборка. Выборочное пространство
1.2 Выборка. Выборочное пространство1.2 Выборка. Выборочное пространство
1.2 Выборка. Выборочное пространство
 
Reccurrence relations
Reccurrence relationsReccurrence relations
Reccurrence relations
 
1.4 Точечные оценки и их свойства
1.4 Точечные оценки и их свойства1.4 Точечные оценки и их свойства
1.4 Точечные оценки и их свойства
 

Similar to Recurrences

Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutions
subhashchandra197
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
GadaFarhan
 
Computer science-formulas
Computer science-formulasComputer science-formulas
Computer science-formulas
RAJIV GANDHI INSTITUTE OF TECHNOLOGY
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
Megha V
 
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
bryan111472
 
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
Joepang2015
 
04_Recurrences_1.ppt
04_Recurrences_1.ppt04_Recurrences_1.ppt
04_Recurrences_1.ppt
MouDhara1
 
3.pdf
3.pdf3.pdf
3.pdf
AlaaOdeh18
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
sohelranasweet
 
2.pptx
2.pptx2.pptx
2.pptx
MohAlyasin1
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
pallavidhade2
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Vikas Sharma
 
Introduction
IntroductionIntroduction
Introduction
pilavare
 
03 dc
03 dc03 dc
03 dc
Hira Gul
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
Krish_ver2
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
Krish_ver2
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
Krish_ver2
 
Improvised Master's Theorem
Improvised Master's TheoremImprovised Master's Theorem
Improvised Master's Theorem
Laiba Younas
 
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
Seshu Chakravarthy
 
Daa divide-n-conquer
Daa divide-n-conquerDaa divide-n-conquer
Daa divide-n-conquer
sankara_rao
 

Similar to Recurrences (20)

Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutions
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
 
Computer science-formulas
Computer science-formulasComputer science-formulas
Computer science-formulas
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
 
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
 
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
 
04_Recurrences_1.ppt
04_Recurrences_1.ppt04_Recurrences_1.ppt
04_Recurrences_1.ppt
 
3.pdf
3.pdf3.pdf
3.pdf
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
2.pptx
2.pptx2.pptx
2.pptx
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Introduction
IntroductionIntroduction
Introduction
 
03 dc
03 dc03 dc
03 dc
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
Improvised Master's Theorem
Improvised Master's TheoremImprovised Master's Theorem
Improvised Master's Theorem
 
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
 
Daa divide-n-conquer
Daa divide-n-conquerDaa divide-n-conquer
Daa divide-n-conquer
 

More from DEVTYPE

1.3 Описательная статистика
1.3 Описательная статистика1.3 Описательная статистика
1.3 Описательная статистика
DEVTYPE
 
Coin Change Problem
Coin Change ProblemCoin Change Problem
Coin Change Problem
DEVTYPE
 
D-кучи и их применение
D-кучи и их применениеD-кучи и их применение
D-кучи и их применение
DEVTYPE
 
Диаграммы Юнга, плоские разбиения и знакочередующиеся матрицы
Диаграммы Юнга, плоские разбиения и знакочередующиеся матрицыДиаграммы Юнга, плоские разбиения и знакочередующиеся матрицы
Диаграммы Юнга, плоские разбиения и знакочередующиеся матрицы
DEVTYPE
 
Кучи
КучиКучи
Кучи
DEVTYPE
 
Кодирование Хаффмана
Кодирование ХаффманаКодирование Хаффмана
Кодирование Хаффмана
DEVTYPE
 
Жадные алгоритмы: введение
Жадные алгоритмы: введениеЖадные алгоритмы: введение
Жадные алгоритмы: введение
DEVTYPE
 
Наибольший общий делитель
Наибольший общий делительНаибольший общий делитель
Наибольший общий делитель
DEVTYPE
 
Числа Фибоначчи
Числа ФибоначчиЧисла Фибоначчи
Числа Фибоначчи
DEVTYPE
 
О-символика
О-символикаО-символика
О-символика
DEVTYPE
 
Зачем изучать алгоритмы?
Зачем изучать алгоритмы?Зачем изучать алгоритмы?
Зачем изучать алгоритмы?
DEVTYPE
 
Программирование: теоремы и задачи
Программирование: теоремы и задачиПрограммирование: теоремы и задачи
Программирование: теоремы и задачи
DEVTYPE
 
7. Дискретная вероятность
7. Дискретная вероятность7. Дискретная вероятность
7. Дискретная вероятность
DEVTYPE
 
Основы комбинаторики II. Разбор задач
Основы комбинаторики II. Разбор задачОсновы комбинаторики II. Разбор задач
Основы комбинаторики II. Разбор задач
DEVTYPE
 

More from DEVTYPE (14)

1.3 Описательная статистика
1.3 Описательная статистика1.3 Описательная статистика
1.3 Описательная статистика
 
Coin Change Problem
Coin Change ProblemCoin Change Problem
Coin Change Problem
 
D-кучи и их применение
D-кучи и их применениеD-кучи и их применение
D-кучи и их применение
 
Диаграммы Юнга, плоские разбиения и знакочередующиеся матрицы
Диаграммы Юнга, плоские разбиения и знакочередующиеся матрицыДиаграммы Юнга, плоские разбиения и знакочередующиеся матрицы
Диаграммы Юнга, плоские разбиения и знакочередующиеся матрицы
 
Кучи
КучиКучи
Кучи
 
Кодирование Хаффмана
Кодирование ХаффманаКодирование Хаффмана
Кодирование Хаффмана
 
Жадные алгоритмы: введение
Жадные алгоритмы: введениеЖадные алгоритмы: введение
Жадные алгоритмы: введение
 
Наибольший общий делитель
Наибольший общий делительНаибольший общий делитель
Наибольший общий делитель
 
Числа Фибоначчи
Числа ФибоначчиЧисла Фибоначчи
Числа Фибоначчи
 
О-символика
О-символикаО-символика
О-символика
 
Зачем изучать алгоритмы?
Зачем изучать алгоритмы?Зачем изучать алгоритмы?
Зачем изучать алгоритмы?
 
Программирование: теоремы и задачи
Программирование: теоремы и задачиПрограммирование: теоремы и задачи
Программирование: теоремы и задачи
 
7. Дискретная вероятность
7. Дискретная вероятность7. Дискретная вероятность
7. Дискретная вероятность
 
Основы комбинаторики II. Разбор задач
Основы комбинаторики II. Разбор задачОсновы комбинаторики II. Разбор задач
Основы комбинаторики II. Разбор задач
 

Recently uploaded

Applied Science: Thermodynamics, Laws & Methodology.pdf
Applied Science: Thermodynamics, Laws & Methodology.pdfApplied Science: Thermodynamics, Laws & Methodology.pdf
Applied Science: Thermodynamics, Laws & Methodology.pdf
University of Hertfordshire
 
在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样
在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样
在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样
vluwdy49
 
bordetella pertussis.................................ppt
bordetella pertussis.................................pptbordetella pertussis.................................ppt
bordetella pertussis.................................ppt
kejapriya1
 
Basics of crystallography, crystal systems, classes and different forms
Basics of crystallography, crystal systems, classes and different formsBasics of crystallography, crystal systems, classes and different forms
Basics of crystallography, crystal systems, classes and different forms
MaheshaNanjegowda
 
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
yqqaatn0
 
Bob Reedy - Nitrate in Texas Groundwater.pdf
Bob Reedy - Nitrate in Texas Groundwater.pdfBob Reedy - Nitrate in Texas Groundwater.pdf
Bob Reedy - Nitrate in Texas Groundwater.pdf
Texas Alliance of Groundwater Districts
 
SAR of Medicinal Chemistry 1st by dk.pdf
SAR of Medicinal Chemistry 1st by dk.pdfSAR of Medicinal Chemistry 1st by dk.pdf
SAR of Medicinal Chemistry 1st by dk.pdf
KrushnaDarade1
 
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
Travis Hills MN
 
Topic: SICKLE CELL DISEASE IN CHILDREN-3.pdf
Topic: SICKLE CELL DISEASE IN CHILDREN-3.pdfTopic: SICKLE CELL DISEASE IN CHILDREN-3.pdf
Topic: SICKLE CELL DISEASE IN CHILDREN-3.pdf
TinyAnderson
 
The binding of cosmological structures by massless topological defects
The binding of cosmological structures by massless topological defectsThe binding of cosmological structures by massless topological defects
The binding of cosmological structures by massless topological defects
Sérgio Sacani
 
Cytokines and their role in immune regulation.pptx
Cytokines and their role in immune regulation.pptxCytokines and their role in immune regulation.pptx
Cytokines and their role in immune regulation.pptx
Hitesh Sikarwar
 
Compexometric titration/Chelatorphy titration/chelating titration
Compexometric titration/Chelatorphy titration/chelating titrationCompexometric titration/Chelatorphy titration/chelating titration
Compexometric titration/Chelatorphy titration/chelating titration
Vandana Devesh Sharma
 
Unlocking the mysteries of reproduction: Exploring fecundity and gonadosomati...
Unlocking the mysteries of reproduction: Exploring fecundity and gonadosomati...Unlocking the mysteries of reproduction: Exploring fecundity and gonadosomati...
Unlocking the mysteries of reproduction: Exploring fecundity and gonadosomati...
AbdullaAlAsif1
 
The use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptx
The use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptxThe use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptx
The use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptx
MAGOTI ERNEST
 
Describing and Interpreting an Immersive Learning Case with the Immersion Cub...
Describing and Interpreting an Immersive Learning Case with the Immersion Cub...Describing and Interpreting an Immersive Learning Case with the Immersion Cub...
Describing and Interpreting an Immersive Learning Case with the Immersion Cub...
Leonel Morgado
 
Phenomics assisted breeding in crop improvement
Phenomics assisted breeding in crop improvementPhenomics assisted breeding in crop improvement
Phenomics assisted breeding in crop improvement
IshaGoswami9
 
Eukaryotic Transcription Presentation.pptx
Eukaryotic Transcription Presentation.pptxEukaryotic Transcription Presentation.pptx
Eukaryotic Transcription Presentation.pptx
RitabrataSarkar3
 
Micronuclei test.M.sc.zoology.fisheries.
Micronuclei test.M.sc.zoology.fisheries.Micronuclei test.M.sc.zoology.fisheries.
Micronuclei test.M.sc.zoology.fisheries.
Aditi Bajpai
 
molar-distalization in orthodontics-seminar.pptx
molar-distalization in orthodontics-seminar.pptxmolar-distalization in orthodontics-seminar.pptx
molar-distalization in orthodontics-seminar.pptx
Anagha Prasad
 
Thornton ESPP slides UK WW Network 4_6_24.pdf
Thornton ESPP slides UK WW Network 4_6_24.pdfThornton ESPP slides UK WW Network 4_6_24.pdf
Thornton ESPP slides UK WW Network 4_6_24.pdf
European Sustainable Phosphorus Platform
 

Recently uploaded (20)

Applied Science: Thermodynamics, Laws & Methodology.pdf
Applied Science: Thermodynamics, Laws & Methodology.pdfApplied Science: Thermodynamics, Laws & Methodology.pdf
Applied Science: Thermodynamics, Laws & Methodology.pdf
 
在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样
在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样
在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样
 
bordetella pertussis.................................ppt
bordetella pertussis.................................pptbordetella pertussis.................................ppt
bordetella pertussis.................................ppt
 
Basics of crystallography, crystal systems, classes and different forms
Basics of crystallography, crystal systems, classes and different formsBasics of crystallography, crystal systems, classes and different forms
Basics of crystallography, crystal systems, classes and different forms
 
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
 
Bob Reedy - Nitrate in Texas Groundwater.pdf
Bob Reedy - Nitrate in Texas Groundwater.pdfBob Reedy - Nitrate in Texas Groundwater.pdf
Bob Reedy - Nitrate in Texas Groundwater.pdf
 
SAR of Medicinal Chemistry 1st by dk.pdf
SAR of Medicinal Chemistry 1st by dk.pdfSAR of Medicinal Chemistry 1st by dk.pdf
SAR of Medicinal Chemistry 1st by dk.pdf
 
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
 
Topic: SICKLE CELL DISEASE IN CHILDREN-3.pdf
Topic: SICKLE CELL DISEASE IN CHILDREN-3.pdfTopic: SICKLE CELL DISEASE IN CHILDREN-3.pdf
Topic: SICKLE CELL DISEASE IN CHILDREN-3.pdf
 
The binding of cosmological structures by massless topological defects
The binding of cosmological structures by massless topological defectsThe binding of cosmological structures by massless topological defects
The binding of cosmological structures by massless topological defects
 
Cytokines and their role in immune regulation.pptx
Cytokines and their role in immune regulation.pptxCytokines and their role in immune regulation.pptx
Cytokines and their role in immune regulation.pptx
 
Compexometric titration/Chelatorphy titration/chelating titration
Compexometric titration/Chelatorphy titration/chelating titrationCompexometric titration/Chelatorphy titration/chelating titration
Compexometric titration/Chelatorphy titration/chelating titration
 
Unlocking the mysteries of reproduction: Exploring fecundity and gonadosomati...
Unlocking the mysteries of reproduction: Exploring fecundity and gonadosomati...Unlocking the mysteries of reproduction: Exploring fecundity and gonadosomati...
Unlocking the mysteries of reproduction: Exploring fecundity and gonadosomati...
 
The use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptx
The use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptxThe use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptx
The use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptx
 
Describing and Interpreting an Immersive Learning Case with the Immersion Cub...
Describing and Interpreting an Immersive Learning Case with the Immersion Cub...Describing and Interpreting an Immersive Learning Case with the Immersion Cub...
Describing and Interpreting an Immersive Learning Case with the Immersion Cub...
 
Phenomics assisted breeding in crop improvement
Phenomics assisted breeding in crop improvementPhenomics assisted breeding in crop improvement
Phenomics assisted breeding in crop improvement
 
Eukaryotic Transcription Presentation.pptx
Eukaryotic Transcription Presentation.pptxEukaryotic Transcription Presentation.pptx
Eukaryotic Transcription Presentation.pptx
 
Micronuclei test.M.sc.zoology.fisheries.
Micronuclei test.M.sc.zoology.fisheries.Micronuclei test.M.sc.zoology.fisheries.
Micronuclei test.M.sc.zoology.fisheries.
 
molar-distalization in orthodontics-seminar.pptx
molar-distalization in orthodontics-seminar.pptxmolar-distalization in orthodontics-seminar.pptx
molar-distalization in orthodontics-seminar.pptx
 
Thornton ESPP slides UK WW Network 4_6_24.pdf
Thornton ESPP slides UK WW Network 4_6_24.pdfThornton ESPP slides UK WW Network 4_6_24.pdf
Thornton ESPP slides UK WW Network 4_6_24.pdf
 

Recurrences

  • 1. Recurrences (CLRS 4.1-4.2) • Last time we discussed divide-and-conquer algorithms Divide and Conquer To Solve P: 1. Divide P into smaller problems P1, P2, P3.....Pk. 2. Conquer by solving the (smaller) subproblems recursively. 3. Combine solutions to P1, P2, ...Pk into solution for P. • Analysis of divide-and-conquer algorithms and in general of recursive algorithms leads to recurrences. • Merge-sort lead to the recurrence T(n) = 2T(n/2) + n – or rather, T(n) = Θ(1) If n = 1 T( n 2 ) + T( n 2 ) + Θ(n) If n > 1 – but we will often cheat and just solve the simple formula (equivalent to assuming that n = 2k for some constant k, and leaving out base case and constant in Θ). Methods for solving recurrences 1. Substitution method 2. Iteration method • Recursion-tree method • (Master method)
  • 2. 1 Solving Recurrences with the Substitution Method • Idea: Make a guess for the form of the solution and prove by induction. • Can be used to prove both upper bounds O() and lower bounds Ω(). • Let’s solve T(n) = 2T(n/2) + n using substitution – Guess T(n) ≤ cn log n for some constant c (that is, T(n) = O(n log n)) – Proof: ∗ Base case: we need to show that our guess holds for some base case (not necessarily n = 1, some small n is ok). Ok, since function constant for small constant n. ∗ Assume holds for n/2: T(n/2) ≤ cn 2 log n 2 (Question: Why not n − 1?) Prove that holds for n: T(n) ≤ cn log n T(n) = 2T(n/2) + n ≤ 2(c n 2 log n 2 ) + n = cn log n 2 + n = cn log n − cn log 2 + n = cn log n − cn + n So ok if c ≥ 1 • Similarly it can be shown that T(n) = Ω(n log n) Exercise! • Similarly it can be shown that T(n) = T( n 2 ) + T( n 2 ) + n is Θ(n lg n). Exercise! • The hard part of the substitution method is often to make a good guess. How do we make a good (i.e. tight) guess??? Unfortunately, there’s no “recipe” for this one. Try iteratively O(n3), Ω(n3), O(n2), Ω(n2) and so on. Try solving by iteration to get a feeling of the growth.
  • 3. 2 Solving Recurrences with the Iteration/Recursion-tree Method • In the iteration method we iteratively “unfold” the recurrence until we “see the pattern”. • The iteration method does not require making a good guess like the substitution method (but it is often more involved than using induction). • Example: Solve T(n) = 8T(n/2) + n2 (T(1) = 1) T(n) = n2 + 8T(n/2) = n2 + 8(8T( n 22 ) + ( n 2 )2 ) = n2 + 82 T( n 22 ) + 8( n2 4 )) = n2 + 2n2 + 82 T( n 22 ) = n2 + 2n2 + 82 (8T( n 23 ) + ( n 22 )2 ) = n2 + 2n2 + 83 T( n 23 ) + 82 ( n2 42 )) = n2 + 2n2 + 22 n2 + 83 T( n 23 ) = . . . = n2 + 2n2 + 22 n2 + 23 n2 + 24 n2 + . . . – Recursion depth: How long (how many iterations) it takes until the subproblem has constant size? i times where n 2i = 1 ⇒ i = log n – What is the last term? 8iT(1) = 8log n T(n) = n2 + 2n2 + 22 n2 + 23 n2 + 24 n2 + . . . + 2log n−1 n2 + 8log n = log n−1 k=0 2k n2 + 8log n = n2 log n−1 k=0 2k + (23 )log n • Now log n−1 k=0 2k is a geometric sum so we have log n−1 k=0 2k = Θ(2log n−1) = Θ(n) • (23)log n = (2log n)3 = n3 T(n) = n2 · Θ(n) + n3 = Θ(n3 )
  • 4. 2.1 Recursion tree A different way to look at the iteration method: is the recursion-tree, discussed in the book (4.2). • we draw out the recursion tree with cost of single call in each node—running time is sum of costs in all nodes • if you are careful drawing the recursion tree and summing up the costs, the recursion tree is a direct proof for the solution of the recurrence, just like iteration and substitution • Example: T(n) = 8T(n/2) + n2 (T(1) = 1) (n/2)2 (n/2)2 (n/4)2 (n/4)2 n2 n2 (n/2)2 (n/2)2 T(n/4) n2 T(n/4) T(n) 1) T(n/2) T(n/2) n2 2) T(1) T(1) T(1) 8(n/2) = 2n 8 (n/4) = 2 n 2 2 2 2 22 log n 8 T(1) + + + + 3) log n) T(n) = n2 + 2n2 + 22n2 + 23n2 + 24n2 + . . . + 2log n−1n2 + 8log n
  • 5. 3 Matrix Multiplication • Let X and Y be n × n matrices X =    x11 x12 · · · x1n x21 x22 · · · x1n x31 x32 · · · x1n · · · · · · · · · · · · xn1 xn2 · · · xnn    • We want to compute Z = X · Y – zij = n k=1 Xik · Ykj • Naive method uses ⇒ n2 · n = Θ(n3) operations • Divide-and-conquer solution: Z = A B C D · E F G H = (A · E + B · G) (A · F + B · H) (C · E + D · G) (C · F + D · H) – The above naturally leads to divide-and-conquer solution: ∗ Divide X and Y into 8 sub-matrices A, B, C, and D. ∗ Do 8 matrix multiplications recursively. ∗ Compute Z by combining results (doing 4 matrix additions). – Lets assume n = 2c for some constant c and let A, B, C and D be n/2 × n/2 matrices ∗ Running time of algorithm is T(n) = 8T(n/2) + Θ(n2) ⇒ T(n) = Θ(n3) – But we already discussed a (simpler/naive) O(n3) algorithm! Can we do better? 3.1 Strassen’s Algorithm • Strassen observed the following: Z = A B C D · E F G H = (S1 + S2 − S4 + S6) (S4 + S5) (S6 + S7) (S2 + S3 + S5 − S7) where S1 = (B − D) · (G + H) S2 = (A + D) · (E + H) S3 = (A − C) · (E + F) S4 = (A + B) · H S5 = A · (F − H) S6 = D · (G − E) S7 = (C + D) · E
  • 6. – Lets test that S6 + S7 is really C · E + D · G S6 + S7 = D · (G − E) + (C + D) · E = DG − DE + CE + DE = DG + CE • This leads to a divide-and-conquer algorithm with running time T(n) = 7T(n/2) + Θ(n2) – We only need to perform 7 multiplications recursively. – Division/Combination can still be performed in Θ(n2) time. • Lets solve the recurrence using the iteration method T(n) = 7T(n/2) + n2 = n2 + 7(7T( n 22 ) + ( n 2 )2 ) = n2 + ( 7 22 )n2 + 72 T( n 22 ) = n2 + ( 7 22 )n2 + 72 (7T( n 23 ) + ( n 22 )2 ) = n2 + ( 7 22 )n2 + ( 7 22 )2 · n2 + 73 T( n 23 ) = n2 + ( 7 22 )n2 + ( 7 22 )2 n2 + ( 7 22 )3 n2 .... + ( 7 22 )log n−1 n2 + 7log n = log n−1 i=0 ( 7 22 )i n2 + 7log n = n2 · Θ(( 7 22 )log n−1 ) + 7log n = n2 · Θ( 7log n (22)log n ) + 7log n = n2 · Θ( 7log n n2 ) + 7log n = Θ(7log n ) – Now we have the following: 7log n = 7 log7 n log7 2 = (7log7 n )(1/ log7 2) = n(1/ log7 2) = n log2 7 log2 2 = nlog 7 – Or in general: alogk n = nlogk a
  • 7. So the solution is T(n) = Θ(nlog 7) = Θ(n2.81...) • Note: – We are ’hiding’ a much bigger constant in Θ() than before. – Currently best known bound is O(n2.376..) (another method). – Lower bound is (trivially) Ω(n2). 4 Master Method • We have solved several recurrences using substitution and iteration. • we solved several recurrences of the form T(n) = aT(n/b) + nc (T(1) = 1). – Strassen’s algorithm ⇒ T(n) = 7T(n/2) + n2 (a = 7, b = 2, and c = 2) – Merge-sort ⇒ T(n) = 2T(n/2) + n (a = 2, b = 2, and c = 1). • It would be nice to have a general solution to the recurrence T(n) = aT(n/b) + nc. • We do! T(n) = aT n b + nc a ≥ 1, b ≥ 1, c > 0 ⇓ T(n) =    Θ(nlogb a) a > bc Θ(nc logb n) a = bc Θ(nc) a < bc Proof (Iteration method) T(n) = aT n b + nc = nc + a n b c + aT n b2 = nc + a bc nc + a2T n b2 = nc + a bc nc + a2 n b2 c + aT n b3 = nc + a bc nc + a bc 2 nc + a3T n b3 = ... = nc + a bc nc + a bc 2 nc + a bc 3 nc + a bc 4 nc + ... + a bc logb n−1 nc + alogb nT(1) = nc logb n−1 k=0 a bc k + alogb n = nc logb n−1 k=0 a bc k + nlogb a Recall geometric sum n k=0 xk = xn+1−1 x−1 = Θ(xn)
  • 8. • a < bc a < bc ⇔ a bc < 1 ⇒ logb n−1 k=0 a bc k ≤ +∞ k=0 a bc k = 1 1−( a bc ) = Θ(1) a < bc ⇔ logb a < logb bc = c T(n) = nc logb n−1 k=0 a bc k + nlogb a = nc · Θ(1) + nlogb a = Θ(nc) • a = bc a = bc ⇔ a bc = 1 ⇒ logb n−1 k=0 a bc k = logb n−1 k=0 1 = Θ(logb n) a = bc ⇔ logb a = logb bc = c T(n) = logb n−1 k=0 a bc k + nlogb a = ncΘ(logb n) + nlogb a = Θ(nc logb n) • a > bc a > bc ⇔ a bc > 1 ⇒ logb n−1 k=0 a bc k = Θ a bc logb n = Θ alogb n (bc)logb n = Θ alogb n nc T(n) = nc · Θ alogb n nc + nlogb a = Θ(nlogb a) + nlogb a = Θ(nlogb a) • Note: Book states and proves the result slightly differently (don’t read it). 5 Changing variables Sometimes reucurrences can be reduced to simpler ones by changing variables • Example: Solve T(n) = 2T( √ n) + log n Let m = log n ⇒ 2m = n ⇒ √ n = 2m/2 T(n) = 2T( √ n) + log n ⇒ T(2m) = 2T(2m/2) + m Let S(m) = T(2m) T(2m) = 2T(2m/2) + m ⇒ S(m) = 2S(m/2) + m ⇒ S(m) = O(m log m) ⇒ T(n) = T(2m) = S(m) = O(m log m) = O(log n log log n)
  • 9. 6 Other recurrences Some important/typical bounds on recurrences not covered by master method: • Logarithmic: Θ(log n) – Recurrence: T(n) = 1 + T(n/2) – Typical example: Recurse on half the input (and throw half away) – Variations: T(n) = 1 + T(99n/100) • Linear: Θ(N) – Recurrence: T(n) = 1 + T(n − 1) – Typical example: Single loop – Variations: T(n) = 1 + 2T(n/2), T(n) = n + T(n/2), T(n) = T(n/5) + T(7n/10 + 6) + n • Quadratic: Θ(n2) – Recurrence: T(n) = n + T(n − 1) – Typical example: Nested loops • Exponential: Θ(2n) – Recurrence: T(n) = 2T(n − 1)