SlideShare ist ein Scribd-Unternehmen logo
1 von 32
Analysis of Algorithms
CS 477/677
Recurrences
Instructor: George Bebis
(Appendix A, Chapter 4)
Recurrences and Running Time
• An equation or inequality that describes a function in
terms of its value on smaller inputs.
T(n) = T(n-1) + n
• Recurrences arise when an algorithm contains recursive
calls to itself
• What is the actual running time of the algorithm?
• Need to solve the recurrence
– Find an explicit formula of the expression
– Bound the recurrence by an expression that involves n
2
Example Recurrences
• T(n) = T(n-1) + n

Θ(n2)

– Recursive algorithm that loops through the input to
eliminate one item

• T(n) = T(n/2) + c

Θ(lgn)

– Recursive algorithm that halves the input in one step

• T(n) = T(n/2) + n

Θ(n)

– Recursive algorithm that halves the input but must
examine every item in the input

• T(n) = 2T(n/2) + 1

Θ(n)

– Recursive algorithm that splits the input into 2 halves
and does a constant amount of other work
3
Recurrent Algorithms
BINARY-SEARCH
•

for an ordered array A, finds if x is in the array A[lo…hi]

Alg.: BINARY-SEARCH (A, lo, hi,1 x)

2

3

4

5

if (lo > hi)
2 3 5 7
return FALSE
mid ← (lo+hi)/2
lo
if x = A[mid]
return TRUE
if ( x < A[mid] )
BINARY-SEARCH (A, lo, mid-1, x)
if ( x > A[mid] )
BINARY-SEARCH (A, mid+1, hi, x)

6

7

8

9 10 11 12
mid

4

hi
Example
• A[8] = {1, 2, 3, 4, 5, 7, 9, 11}
– lo = 1 hi = 8 x = 7
1

2

3

4

5

6

1

2

3

4

5

7

5

6

5

7

1

2

3

4

7

8

9 11
7

mid = 4, lo = 5, hi = 8

8

9 11

mid = 6, A[mid] = x
Found!

5
Another Example
• A[8] = {1, 2, 3, 4, 5, 7, 9, 11}
– lo = 1

hi = 8

x=6

1

2

3

4

5

6

1

2

3

4

5

7

7

9 11

mid = 4, lo = 5, hi = 8

high

low

1

8

2

3

4

5

7

9 11

mid = 6, A[6] = 7, lo = 5, hi = 5

high

low

1

2

3

4

5

7

9 11

1

2

3

4

5

7

9 11

high

mid = 5, A[5] = 5, lo = 6, hi = 5
NOT FOUND!

low
6
Analysis of BINARY-SEARCH
Alg.: BINARY-SEARCH (A, lo, hi, x)
if (lo > hi)
return FALSE
mid ← (lo+hi)/2
if x = A[mid]
return TRUE
if ( x < A[mid] )
BINARY-SEARCH (A, lo, mid-1, x)
if ( x > A[mid] )
BINARY-SEARCH (A, mid+1, hi, x)

constant time: c1
constant time: c2
constant time: c3

same problem of size n/2
same problem of size n/2

• T(n) = c + T(n/2)
–

T(n) – running time for an array of size n

7
Methods for Solving Recurrences
• Iteration method
• Substitution method
• Recursion tree method
• Master method

8
The Iteration Method
• Convert the recurrence into a summation and try
to bound it using known series
– Iterate the recurrence until the initial condition is
reached.
– Use back-substitution to express the recurrence in
terms of n and the initial (boundary) condition.

9
The Iteration Method
T(n) = c + T(n/2)
T(n) = c + T(n/2)
T(n/2) = c + T(n/4)
T(n/4) = c + T(n/8)
= c + c + T(n/4)
= c + c + c + T(n/8)
Assume n = 2k
T(n) = c + c + … + c + T(1)
k times

= clgn + T(1)
= Θ(lgn)
10
Iteration Method – Example
Assume: n = 2k
T(n) = n + 2T(n/2)
T(n) = n + 2T(n/2)
T(n/2) = n/2 + 2T(n/4)
= n + 2(n/2 + 2T(n/4))
= n + n + 4T(n/4)
= n + n + 4(n/4 + 2T(n/8))
= n + n + n + 8T(n/8)
… = in + 2iT(n/2i)
= kn + 2kT(1)
= nlgn + nT(1) = Θ(nlgn)
11
The substitution method

1. Guess a solution
2. Use induction to prove that the
solution works

12
Substitution method
•

Guess a solution
–

T(n) = O(g(n))

–

Induction goal: apply the definition of the asymptotic notation
•

–

•

T(n) ≤ d g(n), for some d > 0 and n ≥ n0

Induction hypothesis: T(k) ≤ d g(k) for all k < n

(strong induction)

Prove the induction goal
–

Use the induction hypothesis to find some values of the
constants d and n0 for which the induction goal holds

13
Example: Binary Search
T(n) = c + T(n/2)
•

Guess: T(n) = O(lgn)
– Induction goal: T(n) ≤ d lgn, for some d and n ≥ n0
– Induction hypothesis: T(n/2) ≤ d lg(n/2)

•

Proof of induction goal:
T(n) = T(n/2) + c ≤ d lg(n/2) + c
= d lgn – d + c ≤ d lgn
if: – d + c ≤ 0, d ≥ c

•

Base case?

14
Example 2
T(n) = T(n-1) + n
•

Guess: T(n) = O(n2)
–
–

•

Induction goal: T(n) ≤ c n2, for some c and n ≥ n0
Induction hypothesis: T(n-1) ≤ c(n-1)2 for all k < n

Proof of induction goal:
T(n) = T(n-1) + n ≤ c (n-1)2 + n
= cn2 – (2cn – c - n) ≤ cn2
if: 2cn – c – n ≥ 0 ⇔ c ≥ n/(2n-1) ⇔ c ≥ 1/(2 – 1/n)
–

For n ≥ 1 ⇒ 2 – 1/n ≥ 1 ⇒ any c ≥ 1 will work

15
Example 3
T(n) = 2T(n/2) + n
•

Guess: T(n) = O(nlgn)
– Induction goal: T(n) ≤ cn lgn, for some c and n ≥ n0
– Induction hypothesis: T(n/2) ≤ cn/2 lg(n/2)

•

Proof of induction goal:
T(n) = 2T(n/2) + n ≤ 2c (n/2)lg(n/2) + n
= cn lgn – cn + n ≤ cn lgn
if: - cn + n ≤ 0 ⇒ c ≥ 1

•

Base case?

16
Changing variables
T(n) = 2T( n ) + lgn
– Rename: m = lgn ⇒ n = 2m
T (2m) = 2T(2m/2) + m
– Rename: S(m) = T(2m)
S(m) = 2S(m/2) + m ⇒ S(m) = O(mlgm)
(demonstrated before)
T(n) = T(2m) = S(m) = O(mlgm)=O(lgnlglgn)
Idea: transform the recurrence to one that you
have seen before
17
The recursion-tree method

Convert the recurrence into a tree:
–

Each node represents the cost incurred at various
levels of recursion

–

Sum up the costs of all levels

Used to “guess” a solution for the recurrence

18
Example 1
W(n) = 2W(n/2) + n2

•
•
•
•

Subproblem size at level i is: n/2i
Subproblem size hits 1 when 1 = n/2i ⇒ i = lgn
Cost of the problem at level i = (n/2i)2
No. of nodes at level i = 2i
i
i
lg n −1 2
lg n −1
∞
Total cost:
n
1
1
1
lg n
2
2
2
W ( n) =

∑2
i =0

⇒ W(n) = O(n2)

i

+ 2 W (1) = n

∑ 2 
 
i =0

+n≤n

∑ 2 
 

+ O ( n) =n

i =0

19

1− 1

+ O ( n) = 2n 2

2
Example 2
E.g.: T(n) = 3T(n/4) + cn2

•
•

Subproblem size at level i is: n/4i
Subproblem size hits 1 when 1 = n/4i ⇒ i = log4n

•

Cost of a node at level i = c(n/4i)2
Number of nodes at level i = 3 i ⇒ last level has 3log4n = nlog43 nodes

•

Total cost:

•

log 4 n −1

i

(

)

i

(

)

(

)

∞
1
3 2
3
log 4 3
T (n) = ∑   cn + Θ n
≤ ∑   cn 2 + Θ n log 4 3 =
cn 2 + Θ n log 4 3 = O(n 2 )
3
i = 0  16 
i = 0  16 
1−
16 20
⇒ T(n) = O(n2)
Example 2 - Substitution
T(n) = 3T(n/4) + cn2
•

Guess: T(n) = O(n2)
– Induction goal: T(n) ≤ dn2, for some d and n ≥ n0
– Induction hypothesis: T(n/4) ≤ d (n/4)2

•

Proof of induction goal:
T(n) = 3T(n/4) + cn2
≤ 3d (n/4)2 + cn2
= (3/16) d n2 + cn2
≤ d n2

•

if: d ≥ (16/13)c

Therefore: T(n) = O(n2)
21
Example 3 (simpler proof)
W(n) = W(n/3) + W(2n/3) + n
•

The longest path from the root to a
leaf is:

n→

(2/3)n → (2/3)2 n → … → 1
•

Subproblem size hits 1 when

1=

(2/3)in ⇔ i=log3/2n
•

Cost of the problem at level i = n

•

Total cost:

lg n
W (n) < n + n + ... = n(log 3/ 2 n) = n
= O (n lg n)
3
lg
2
⇒ W(n) = O(nlgn)

22
Example 3
W(n) = W(n/3) + W(2n/3) + n
•

The longest path from the root to a
n→

leaf is:

(2/3)n → (2/3)2 n → … → 1
•

Subproblem size hits 1 when

1=

(2/3)in ⇔ i=log3/2n
•

Cost of the problem at level i = n

•

Total cost:

W (n) < n + n + ... =

(log3 / 2 n ) −1

∑

n + 2(log3 / 2 n ) W (1) <

i =0

<n

log3 / 2 n

∑

1 + n log3 / 2 2 = n log 3/ 2 n + O (n) = n

i =0
⇒ W(n) = O(nlgn)

lg n
1
+ O (n ) =
n lg n + O (n)
lg 3 / 2
lg 3 / 2

23
Example 3 - Substitution
W(n) = W(n/3) + W(2n/3) + O(n)
• Guess: W(n) = O(nlgn)
– Induction goal: W(n) ≤ dnlgn, for some d and n ≥ n0
– Induction hypothesis: W(k) ≤ d klgk
(n/3, 2n/3)

for any K < n

• Proof of induction goal:
Try it out as an exercise!!
• T(n) = O(nlgn)

24
Master’s method
• “Cookbook” for solving recurrences of the form:

n
T (n) = aT   + f (n)
b
where, a ≥ 1, b > 1, and f(n) > 0

Idea: compare f(n) with nlogba
• f(n) is asymptotically smaller or larger than nlogba by a
polynomial factor nε
• f(n) is asymptotically equal with nlogba

25
Master’s method
•

“Cookbook” for solving recurrences of the form:

n
T (n) = aT   + f (n)
b
where, a ≥ 1, b > 1, and f(n) > 0

Case 1: if f(n) = O(nlogba -ε) for some ε > 0, then: T(n) = Θ(nlogba)
Case 2: if f(n) = Θ(nlogba), then: T(n) = Θ(nlogba lgn)
Case 3: if f(n) = Ω(nlogba +ε) for some ε > 0, and if
af(n/b) ≤ cf(n) for some c < 1 and all sufficiently large n, then:
regularity condition

T(n) = Θ(f(n))
26
Examples
T(n) = 2T(n/2) + n
a = 2, b = 2, log22 = 1
Compare nlog22 with f(n) = n
⇒ f(n) = Θ(n) ⇒ Case 2
⇒ T(n) = Θ(nlgn)
28
Examples
T(n) = 2T(n/2) + n2
a = 2, b = 2, log22 = 1
Compare n with f(n) = n2
⇒ f(n) = Ω(n1+ε) Case 3 ⇒ verify regularity cond.
a f(n/b) ≤ c f(n)
⇔ 2 n2/4 ≤ c n2 ⇒ c = ½ is a solution (c<1)
⇒ T(n) = Θ(n2)
29
Examples (cont.)
T(n) = 2T(n/2) +

n

a = 2, b = 2, log22 = 1
Compare n with f(n) = n1/2
⇒ f(n) = O(n1-ε)

Case 1

⇒ T(n) = Θ(n)
30
Examples
T(n) = 3T(n/4) + nlgn
a = 3, b = 4, log43 = 0.793
Compare n0.793 with f(n) = nlgn
f(n) = Ω(nlog43+ε) Case 3
Check regularity condition:
3∗(n/4)lg(n/4) ≤ (3/4)nlgn = c ∗f(n), c=3/4
⇒T(n) = Θ(nlgn)
31
Examples
T(n) = 2T(n/2) + nlgn
a = 2, b = 2, log22 = 1
• Compare n with f(n) = nlgn
– seems like case 3 should apply

• f(n) must be polynomially larger by a factor of n ε
• In this case it is only larger by a factor of lgn

32
Readings

33

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationAmrinder Arora
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsGanesh Solanke
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsNikhil Sharma
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsEhtisham Ali
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquerKrish_ver2
 
Coin change Problem (DP & GREEDY)
Coin change Problem (DP & GREEDY)Coin change Problem (DP & GREEDY)
Coin change Problem (DP & GREEDY)Ridhima Chowdhury
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSGayathri Gaayu
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihmSajid Marwat
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IMohamed Loey
 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithmtaimurkhan803
 
Basics & asymptotic notations
Basics & asymptotic notationsBasics & asymptotic notations
Basics & asymptotic notationsRajendran
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsRishabh Soni
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAmrinder Arora
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back trackingTech_MX
 
CMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of FunctionsCMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of Functionsallyn joy calcaben
 

Was ist angesagt? (20)

Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Recurrences
RecurrencesRecurrences
Recurrences
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
Coin change Problem (DP & GREEDY)
Coin change Problem (DP & GREEDY)Coin change Problem (DP & GREEDY)
Coin change Problem (DP & GREEDY)
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithm
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Basics & asymptotic notations
Basics & asymptotic notationsBasics & asymptotic notations
Basics & asymptotic notations
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Big o notation
Big o notationBig o notation
Big o notation
 
Chap4
Chap4Chap4
Chap4
 
CMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of FunctionsCMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of Functions
 

Andere mochten auch

Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrencesWaqas Akram
 
recurence solutions
recurence solutionsrecurence solutions
recurence solutionssoumya8396
 
Recurrence relations
Recurrence relationsRecurrence relations
Recurrence relationsIIUM
 
recurrence relations
 recurrence relations recurrence relations
recurrence relationsAnurag Cheela
 
Application Note: Maintenance Decision Support System (MDSS) for Winter Road ...
Application Note: Maintenance Decision Support System (MDSS) for Winter Road ...Application Note: Maintenance Decision Support System (MDSS) for Winter Road ...
Application Note: Maintenance Decision Support System (MDSS) for Winter Road ...Schneider Electric
 
Upgrading from NetWare to Novell Open Enterprise Server on Linux: The Novell ...
Upgrading from NetWare to Novell Open Enterprise Server on Linux: The Novell ...Upgrading from NetWare to Novell Open Enterprise Server on Linux: The Novell ...
Upgrading from NetWare to Novell Open Enterprise Server on Linux: The Novell ...Novell
 
Novell Netware Protocol suite
Novell Netware Protocol suiteNovell Netware Protocol suite
Novell Netware Protocol suiteOmar Isaid
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1Kumar
 
Recurrences
RecurrencesRecurrences
RecurrencesDEVTYPE
 
Lec2 Algorth
Lec2 AlgorthLec2 Algorth
Lec2 Algorthhumanist3
 

Andere mochten auch (15)

Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
 
Recurrence relation
Recurrence relationRecurrence relation
Recurrence relation
 
recurence solutions
recurence solutionsrecurence solutions
recurence solutions
 
Consumer behaviour
Consumer behaviourConsumer behaviour
Consumer behaviour
 
Inferno
InfernoInferno
Inferno
 
Novell
NovellNovell
Novell
 
Recurrence relations
Recurrence relationsRecurrence relations
Recurrence relations
 
recurrence relations
 recurrence relations recurrence relations
recurrence relations
 
Application Note: Maintenance Decision Support System (MDSS) for Winter Road ...
Application Note: Maintenance Decision Support System (MDSS) for Winter Road ...Application Note: Maintenance Decision Support System (MDSS) for Winter Road ...
Application Note: Maintenance Decision Support System (MDSS) for Winter Road ...
 
Upgrading from NetWare to Novell Open Enterprise Server on Linux: The Novell ...
Upgrading from NetWare to Novell Open Enterprise Server on Linux: The Novell ...Upgrading from NetWare to Novell Open Enterprise Server on Linux: The Novell ...
Upgrading from NetWare to Novell Open Enterprise Server on Linux: The Novell ...
 
Novell Netware Protocol suite
Novell Netware Protocol suiteNovell Netware Protocol suite
Novell Netware Protocol suite
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1
 
Recurrences
RecurrencesRecurrences
Recurrences
 
Lec2 Algorth
Lec2 AlgorthLec2 Algorth
Lec2 Algorth
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 

Ähnlich wie Recurrences

Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree methodRajendran
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxGadaFarhan
 
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probabilitybryan111472
 
Master method theorem
Master method theoremMaster method theorem
Master method theoremRajendran
 
Master method theorem
Master method theoremMaster method theorem
Master method theoremRajendran
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03Krish_ver2
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03Krish_ver2
 
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
 
RecurrenceAndMasterTheorem.ppt
RecurrenceAndMasterTheorem.pptRecurrenceAndMasterTheorem.ppt
RecurrenceAndMasterTheorem.pptISHAN194169
 
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
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfAmayJaiswal4
 
lecture 3
lecture 3lecture 3
lecture 3sajinsc
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysisSoujanya V
 

Ähnlich wie Recurrences (20)

Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
 
2.pptx
2.pptx2.pptx
2.pptx
 
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
 
L2
L2L2
L2
 
L2
L2L2
L2
 
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
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
 
RecurrenceAndMasterTheorem.ppt
RecurrenceAndMasterTheorem.pptRecurrenceAndMasterTheorem.ppt
RecurrenceAndMasterTheorem.ppt
 
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
 
03 dc
03 dc03 dc
03 dc
 
3.pdf
3.pdf3.pdf
3.pdf
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
Computer science-formulas
Computer science-formulasComputer science-formulas
Computer science-formulas
 
lecture 3
lecture 3lecture 3
lecture 3
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysis
 

Kürzlich hochgeladen

The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 

Kürzlich hochgeladen (20)

The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 

Recurrences

  • 1. Analysis of Algorithms CS 477/677 Recurrences Instructor: George Bebis (Appendix A, Chapter 4)
  • 2. Recurrences and Running Time • An equation or inequality that describes a function in terms of its value on smaller inputs. T(n) = T(n-1) + n • Recurrences arise when an algorithm contains recursive calls to itself • What is the actual running time of the algorithm? • Need to solve the recurrence – Find an explicit formula of the expression – Bound the recurrence by an expression that involves n 2
  • 3. Example Recurrences • T(n) = T(n-1) + n Θ(n2) – Recursive algorithm that loops through the input to eliminate one item • T(n) = T(n/2) + c Θ(lgn) – Recursive algorithm that halves the input in one step • T(n) = T(n/2) + n Θ(n) – Recursive algorithm that halves the input but must examine every item in the input • T(n) = 2T(n/2) + 1 Θ(n) – Recursive algorithm that splits the input into 2 halves and does a constant amount of other work 3
  • 4. Recurrent Algorithms BINARY-SEARCH • for an ordered array A, finds if x is in the array A[lo…hi] Alg.: BINARY-SEARCH (A, lo, hi,1 x) 2 3 4 5 if (lo > hi) 2 3 5 7 return FALSE mid ← (lo+hi)/2 lo if x = A[mid] return TRUE if ( x < A[mid] ) BINARY-SEARCH (A, lo, mid-1, x) if ( x > A[mid] ) BINARY-SEARCH (A, mid+1, hi, x) 6 7 8 9 10 11 12 mid 4 hi
  • 5. Example • A[8] = {1, 2, 3, 4, 5, 7, 9, 11} – lo = 1 hi = 8 x = 7 1 2 3 4 5 6 1 2 3 4 5 7 5 6 5 7 1 2 3 4 7 8 9 11 7 mid = 4, lo = 5, hi = 8 8 9 11 mid = 6, A[mid] = x Found! 5
  • 6. Another Example • A[8] = {1, 2, 3, 4, 5, 7, 9, 11} – lo = 1 hi = 8 x=6 1 2 3 4 5 6 1 2 3 4 5 7 7 9 11 mid = 4, lo = 5, hi = 8 high low 1 8 2 3 4 5 7 9 11 mid = 6, A[6] = 7, lo = 5, hi = 5 high low 1 2 3 4 5 7 9 11 1 2 3 4 5 7 9 11 high mid = 5, A[5] = 5, lo = 6, hi = 5 NOT FOUND! low 6
  • 7. Analysis of BINARY-SEARCH Alg.: BINARY-SEARCH (A, lo, hi, x) if (lo > hi) return FALSE mid ← (lo+hi)/2 if x = A[mid] return TRUE if ( x < A[mid] ) BINARY-SEARCH (A, lo, mid-1, x) if ( x > A[mid] ) BINARY-SEARCH (A, mid+1, hi, x) constant time: c1 constant time: c2 constant time: c3 same problem of size n/2 same problem of size n/2 • T(n) = c + T(n/2) – T(n) – running time for an array of size n 7
  • 8. Methods for Solving Recurrences • Iteration method • Substitution method • Recursion tree method • Master method 8
  • 9. The Iteration Method • Convert the recurrence into a summation and try to bound it using known series – Iterate the recurrence until the initial condition is reached. – Use back-substitution to express the recurrence in terms of n and the initial (boundary) condition. 9
  • 10. The Iteration Method T(n) = c + T(n/2) T(n) = c + T(n/2) T(n/2) = c + T(n/4) T(n/4) = c + T(n/8) = c + c + T(n/4) = c + c + c + T(n/8) Assume n = 2k T(n) = c + c + … + c + T(1) k times = clgn + T(1) = Θ(lgn) 10
  • 11. Iteration Method – Example Assume: n = 2k T(n) = n + 2T(n/2) T(n) = n + 2T(n/2) T(n/2) = n/2 + 2T(n/4) = n + 2(n/2 + 2T(n/4)) = n + n + 4T(n/4) = n + n + 4(n/4 + 2T(n/8)) = n + n + n + 8T(n/8) … = in + 2iT(n/2i) = kn + 2kT(1) = nlgn + nT(1) = Θ(nlgn) 11
  • 12. The substitution method 1. Guess a solution 2. Use induction to prove that the solution works 12
  • 13. Substitution method • Guess a solution – T(n) = O(g(n)) – Induction goal: apply the definition of the asymptotic notation • – • T(n) ≤ d g(n), for some d > 0 and n ≥ n0 Induction hypothesis: T(k) ≤ d g(k) for all k < n (strong induction) Prove the induction goal – Use the induction hypothesis to find some values of the constants d and n0 for which the induction goal holds 13
  • 14. Example: Binary Search T(n) = c + T(n/2) • Guess: T(n) = O(lgn) – Induction goal: T(n) ≤ d lgn, for some d and n ≥ n0 – Induction hypothesis: T(n/2) ≤ d lg(n/2) • Proof of induction goal: T(n) = T(n/2) + c ≤ d lg(n/2) + c = d lgn – d + c ≤ d lgn if: – d + c ≤ 0, d ≥ c • Base case? 14
  • 15. Example 2 T(n) = T(n-1) + n • Guess: T(n) = O(n2) – – • Induction goal: T(n) ≤ c n2, for some c and n ≥ n0 Induction hypothesis: T(n-1) ≤ c(n-1)2 for all k < n Proof of induction goal: T(n) = T(n-1) + n ≤ c (n-1)2 + n = cn2 – (2cn – c - n) ≤ cn2 if: 2cn – c – n ≥ 0 ⇔ c ≥ n/(2n-1) ⇔ c ≥ 1/(2 – 1/n) – For n ≥ 1 ⇒ 2 – 1/n ≥ 1 ⇒ any c ≥ 1 will work 15
  • 16. Example 3 T(n) = 2T(n/2) + n • Guess: T(n) = O(nlgn) – Induction goal: T(n) ≤ cn lgn, for some c and n ≥ n0 – Induction hypothesis: T(n/2) ≤ cn/2 lg(n/2) • Proof of induction goal: T(n) = 2T(n/2) + n ≤ 2c (n/2)lg(n/2) + n = cn lgn – cn + n ≤ cn lgn if: - cn + n ≤ 0 ⇒ c ≥ 1 • Base case? 16
  • 17. Changing variables T(n) = 2T( n ) + lgn – Rename: m = lgn ⇒ n = 2m T (2m) = 2T(2m/2) + m – Rename: S(m) = T(2m) S(m) = 2S(m/2) + m ⇒ S(m) = O(mlgm) (demonstrated before) T(n) = T(2m) = S(m) = O(mlgm)=O(lgnlglgn) Idea: transform the recurrence to one that you have seen before 17
  • 18. The recursion-tree method Convert the recurrence into a tree: – Each node represents the cost incurred at various levels of recursion – Sum up the costs of all levels Used to “guess” a solution for the recurrence 18
  • 19. Example 1 W(n) = 2W(n/2) + n2 • • • • Subproblem size at level i is: n/2i Subproblem size hits 1 when 1 = n/2i ⇒ i = lgn Cost of the problem at level i = (n/2i)2 No. of nodes at level i = 2i i i lg n −1 2 lg n −1 ∞ Total cost: n 1 1 1 lg n 2 2 2 W ( n) = ∑2 i =0 ⇒ W(n) = O(n2) i + 2 W (1) = n ∑ 2    i =0 +n≤n ∑ 2    + O ( n) =n i =0 19 1− 1 + O ( n) = 2n 2 2
  • 20. Example 2 E.g.: T(n) = 3T(n/4) + cn2 • • Subproblem size at level i is: n/4i Subproblem size hits 1 when 1 = n/4i ⇒ i = log4n • Cost of a node at level i = c(n/4i)2 Number of nodes at level i = 3 i ⇒ last level has 3log4n = nlog43 nodes • Total cost: • log 4 n −1 i ( ) i ( ) ( ) ∞ 1 3 2 3 log 4 3 T (n) = ∑   cn + Θ n ≤ ∑   cn 2 + Θ n log 4 3 = cn 2 + Θ n log 4 3 = O(n 2 ) 3 i = 0  16  i = 0  16  1− 16 20 ⇒ T(n) = O(n2)
  • 21. Example 2 - Substitution T(n) = 3T(n/4) + cn2 • Guess: T(n) = O(n2) – Induction goal: T(n) ≤ dn2, for some d and n ≥ n0 – Induction hypothesis: T(n/4) ≤ d (n/4)2 • Proof of induction goal: T(n) = 3T(n/4) + cn2 ≤ 3d (n/4)2 + cn2 = (3/16) d n2 + cn2 ≤ d n2 • if: d ≥ (16/13)c Therefore: T(n) = O(n2) 21
  • 22. Example 3 (simpler proof) W(n) = W(n/3) + W(2n/3) + n • The longest path from the root to a leaf is: n→ (2/3)n → (2/3)2 n → … → 1 • Subproblem size hits 1 when 1= (2/3)in ⇔ i=log3/2n • Cost of the problem at level i = n • Total cost: lg n W (n) < n + n + ... = n(log 3/ 2 n) = n = O (n lg n) 3 lg 2 ⇒ W(n) = O(nlgn) 22
  • 23. Example 3 W(n) = W(n/3) + W(2n/3) + n • The longest path from the root to a n→ leaf is: (2/3)n → (2/3)2 n → … → 1 • Subproblem size hits 1 when 1= (2/3)in ⇔ i=log3/2n • Cost of the problem at level i = n • Total cost: W (n) < n + n + ... = (log3 / 2 n ) −1 ∑ n + 2(log3 / 2 n ) W (1) < i =0 <n log3 / 2 n ∑ 1 + n log3 / 2 2 = n log 3/ 2 n + O (n) = n i =0 ⇒ W(n) = O(nlgn) lg n 1 + O (n ) = n lg n + O (n) lg 3 / 2 lg 3 / 2 23
  • 24. Example 3 - Substitution W(n) = W(n/3) + W(2n/3) + O(n) • Guess: W(n) = O(nlgn) – Induction goal: W(n) ≤ dnlgn, for some d and n ≥ n0 – Induction hypothesis: W(k) ≤ d klgk (n/3, 2n/3) for any K < n • Proof of induction goal: Try it out as an exercise!! • T(n) = O(nlgn) 24
  • 25. Master’s method • “Cookbook” for solving recurrences of the form: n T (n) = aT   + f (n) b where, a ≥ 1, b > 1, and f(n) > 0 Idea: compare f(n) with nlogba • f(n) is asymptotically smaller or larger than nlogba by a polynomial factor nε • f(n) is asymptotically equal with nlogba 25
  • 26. Master’s method • “Cookbook” for solving recurrences of the form: n T (n) = aT   + f (n) b where, a ≥ 1, b > 1, and f(n) > 0 Case 1: if f(n) = O(nlogba -ε) for some ε > 0, then: T(n) = Θ(nlogba) Case 2: if f(n) = Θ(nlogba), then: T(n) = Θ(nlogba lgn) Case 3: if f(n) = Ω(nlogba +ε) for some ε > 0, and if af(n/b) ≤ cf(n) for some c < 1 and all sufficiently large n, then: regularity condition T(n) = Θ(f(n)) 26
  • 27. Examples T(n) = 2T(n/2) + n a = 2, b = 2, log22 = 1 Compare nlog22 with f(n) = n ⇒ f(n) = Θ(n) ⇒ Case 2 ⇒ T(n) = Θ(nlgn) 28
  • 28. Examples T(n) = 2T(n/2) + n2 a = 2, b = 2, log22 = 1 Compare n with f(n) = n2 ⇒ f(n) = Ω(n1+ε) Case 3 ⇒ verify regularity cond. a f(n/b) ≤ c f(n) ⇔ 2 n2/4 ≤ c n2 ⇒ c = ½ is a solution (c<1) ⇒ T(n) = Θ(n2) 29
  • 29. Examples (cont.) T(n) = 2T(n/2) + n a = 2, b = 2, log22 = 1 Compare n with f(n) = n1/2 ⇒ f(n) = O(n1-ε) Case 1 ⇒ T(n) = Θ(n) 30
  • 30. Examples T(n) = 3T(n/4) + nlgn a = 3, b = 4, log43 = 0.793 Compare n0.793 with f(n) = nlgn f(n) = Ω(nlog43+ε) Case 3 Check regularity condition: 3∗(n/4)lg(n/4) ≤ (3/4)nlgn = c ∗f(n), c=3/4 ⇒T(n) = Θ(nlgn) 31
  • 31. Examples T(n) = 2T(n/2) + nlgn a = 2, b = 2, log22 = 1 • Compare n with f(n) = nlgn – seems like case 3 should apply • f(n) must be polynomially larger by a factor of n ε • In this case it is only larger by a factor of lgn 32