0% found this document useful (0 votes)
5 views19 pages

Week 03 (Reccurence)

Reccurence

Uploaded by

Surid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views19 pages

Week 03 (Reccurence)

Reccurence

Uploaded by

Surid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Lecture Title: Recursion

Course Code: CSC 2211 Course Title: Algorithms

Dept. of Computer Science


Faculty of Science and Technology

Lecture No: 03 Week No: 03 Semester:

Lecturer:
Recurrences
&
Master Method
Lecture Outline

1. Divide and Conquer


2. Recurrences in Divide and Conquer and methodologies for
recurrence Solutions
3. Repeated Backward Substitution Method
4. Substitution Method
5. Recursion Tree (Next Week)
6. Master Method (Next Week)
The divide- and- conquer
design paradigm

• Divide and conquer is just one of several powerful techniques


for algorithm design.
• Divide- and- conquer algorithms can be analyzed using
recurrences and the master method (so practice this math).
• Can lead to more efficient algorithms
The divide- and- conquer
design paradigm

1. Divide the problem (instance) into


subproblems.
2. Conquer the subproblems by solving them
recursively.
3. Combine subproblem solutions.
The divide- and- conquer
design paradigm
Example: merge sort
1. Divide: Trivial (array is halved).
2. Conquer: Recursively sort 2 subarrays.
3. Combine: Linear- time merge.
Recurrence for merge sort
Let T( n ) = Time required for size n

T( n ) = Sub-
problem
Size (n/2)
2 * T( n / 2) + O( n)
Number Of Time required for Work Dividing
+
Subproblems * each Subproblem and Combining
The divide- and- conquer
design paradigm
Binary search
Find an element in a sorted array:
1. Divide: Check middle element.
2. Conquer: Recursively search 1 subarray.
3. Combine: Trivial.
Recurrence for binary search
T( n) =

1 * T( n / 2) + Θ(1)
Number Of Time required for Work Dividing
+
Subproblems * each Subproblem and Combining
Recurrences
 Running times of algorithms with recursive calls can be
described using recurrences.
 A recurrence is an equation or inequality that describes a
function in terms of its value on smaller inputs.
 For divide and conquer algorithms:
 solving_trivial_problem if n 1
T (n) 
num_pieces T ( n / subproblem_size_factor)  dividing  combining if n  1
 Example: Merge Sort

 (1) if n 1
T (n) 
2T (n / 2)  (n) if n  1
Solving Recurrences

 Repeated (backward) substitution method


 Expanding the recurrence by substitution and
noticing a pattern (this is not a strictly formal
proof).
 Substitution method
 guessing the solutions
 verifying the solution by the mathematical
induction
 Recursion-trees
 Master method
 templates for different classes of recurrences
Repeated Substitution
 Let’s find the running time of the merge sort
 1 if n 1
T (n) 
 2T (n / 2)  n if n  1
T(n) = 2T(n/2) + n substitute
= 2(2T(n/4) + n/2)
n/2 + n expand
= 22T(n/4) + 2n substitute
= 22(2T(n/8) + n/4)
n/4 + 2n expand
= 23T(n/8) + 3n observe pattern
= 23T(n/23) + 3n observe pattern

T( n ) = 2iT(n/2i) + in
= n T(1) + n lg n T(n) = O(nlgn)
= n + n lg n
Repeated Substitution
Method
 The procedure is straightforward:
 Substitute, Expand, Substitute, Expand, …
 Observe a pattern and determine the expression after
the i-th substitution.
 Find out what the highest value of i (number of
iterations, e.g., lg n) should be to get to the base case
of the recurrence (e.g., T(1)).
 Insert the value of T(1) and the expression of i into
your expression.
Another Example…
 2 if n 1
T (n) 
 2T ( n / 2)  2n  3 if n  1
T(n) = 2T(n/2) + 2n + 3 substitute
= 2(2T(n/4) + n + 3)
3 + 2n +3 expand
= 22T(n/4) + 4n + 2x3 + 3 substitute
= 22(2T(n/8) + n/2 + 3)
3 + 4n + 2x3 + 3
expand
= 23T(n/23) + 2x3n + 3x(22+21+20) observe
pattern
T(n) = 2iT(n/2i) + 2ni+ 3
= 2iT(1) + 2ni + 3 (2i-1)
= n + 2n lg n + 3(n - 1) T(n) = O(nlgn)
= n+2n lg n + 3n - 3
Substitution Method

 The substitution method to solve recurrences


entails two steps:
 Guess the solution.
 Use induction to prove the solution.
 Example:
 T(n) = 4T(n/2) + n
…Substitution Method
T(n) = 4T(n/2) + n
1) Guess T(n) = O(n3), i.e., T(n) is of the form cn3
2) Prove T(n)  cn3 by induction

T(n) = 4T(n/2) + n recurrence


 4c(n/2)3 + n induction hypothesis
= 0.5cn3 + n simplify
= cn3 – (0.5cn3 – n) rearrange
 cn3 if c>=2 and n>=1

Thus T(n) = O(n3)


Exercise-1
...Substitution Method
 Tighter bound for T(n) = 4T(n/2) + n:

Try to show T(n) = O(n2)

Prove T(n)  cn2 by induction

T(n) = 4T(n/2) + n
 4c(n/2)2 + n
= cn2 + n
 cn2
...Substitution Method

 What is the problem? Rewriting

T(n) = O(n2) = cn2 - (something positive)

 As T(n)  cn2 does not work with the inductive


proof.
 Solution: Strengthen the hypothesis for the
inductive proof:
 T(n)  (answer you want) - (something > 0)
...Substitution Method

 Fixed proof: strengthen the inductive


hypothesis by subtracting lower-order terms:
Prove T(n)  cn2 - dn by induction

T(n) = 4T(n/2) + n
 4(c(n/2)2 – d(n/2)) + n
= cn2 – 2dn + n
= (cn2 – dn) – (dn – n)
 cn2 – dn if d
 Don’t miss the next
class !!!!!!!!!!!!!!!!!!

You might also like