0% found this document useful (0 votes)
9 views32 pages

Lecture 12 13

The document discusses the design and analysis of algorithms, focusing on solving recurrence equations using recursion tree methods. It outlines the importance of recurrence relations, methods for solving them, and provides examples to illustrate the recursion tree method. The document emphasizes the use of asymptotic notation in algorithm analysis and the significance of boundary conditions.

Uploaded by

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

Lecture 12 13

The document discusses the design and analysis of algorithms, focusing on solving recurrence equations using recursion tree methods. It outlines the importance of recurrence relations, methods for solving them, and provides examples to illustrate the recursion tree method. The document emphasizes the use of asymptotic notation in algorithm analysis and the significance of boundary conditions.

Uploaded by

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

Design and Analysis of Algorithm

Recurrence Equation
(Solving Recurrence using
Recursion Tree Methods)

Lecture – 12 and 13
Overview
• A recurrence is a function is defined in terms of
– one or more base cases, and
– itself, with smaller arguments.
Examples:

Linear Decay Division

Changing Variable Decision Tree


Overview
• Many technical issues:
• Floors and ceilings
[Floors and ceilings can easily be removed and don’t affect
the solution to the recurrence. They are better left to a
discrete math course.]
• Exact vs. asymptotic functions
• Boundary conditions
Overview

In algorithm analysis, the recurrence and it’s solution are


expressed by the help of asymptotic notation.
• Example: 𝑇 (𝑛) = 2𝑇 (𝑛/2) + 𝛩(𝑛), with solution
𝑇 (𝑛) = 𝛩(𝑛 lg 𝑛).
• The boundary conditions are usually expressed as
𝑇 (𝑛) = 𝛰(1) for sufficiently small n..
• But when there is a desire of an exact, rather than
an asymptotic, solution, the need is to deal with
boundary conditions.
• In practice, just use asymptotics most of the time,
and ignore boundary conditions.
Recursive Function
• Example
𝐴(𝑛)
{
𝐼𝑓(𝑛 > 1)
𝑛
𝑅𝑒𝑡𝑢𝑟𝑛 𝐴
2
}
The relation is called recurrence relation
The Recurrence relation of given function is written as follows.
𝑛
𝑇(𝑛) = 𝑇 +1
2
Recursive Function
• To solve the Recurrence relation the following methods
are used:
1. Iteration method
2. Recursion-Tree method
3. Master Method
4. Substitution Method
Recursion Tree Method
• Recursion Tree is another method for solving recurrence relations.
This method work on two steps. These are
• First, A set of pre level costs are obtained by sum the cost of each
level of the tree and the height of the tree.
• Second, to determine the total cost of all level of recursion, we sum
all the pre level cost.
• This method is best used for good guess.
• For generating good guess, we can ignore floors( 𝑥 ) and ceiling
( 𝑥 ) when solving the recurrences. Because they usually do not
affect the final guess.
Recursion Tree Method (Example 1)
Example 1

𝑛
Solve the recurrence 𝑇 𝑛 = 3𝑇 + Θ 𝑛2 by
4
using recursion tree method.
Recursion Tree Method (Example 1)
Answer:
We start by focusing on finding an upper bound for the
solution by using good guess. As we know that floors
and ceilings usually do not matter when solving the
recurrences, we drop the floor and write the recurrence
equation as follows:
𝑛
𝑇 𝑛 = 3𝑇 + 𝑐𝑛2 , 𝑐 > 0
4
The term 𝑐𝑛2 , at the root represent the costs incurred by
𝑛
the subproblems of size .
4
Recursion Tree Method (Example 1)
𝑛
𝑇 𝑛 = 3𝑇 + 𝑐𝑛2 , 𝑐 > 0 𝑖𝑠 𝑎 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡
4
𝑇(𝑛)
Fig –(a)
Figure (a) shows T(n), which progressively expands in (b) and (d) to form recursion
tree.
𝑐 𝑛 2

𝑛 𝑛 𝑛
𝑇 𝑇 𝑇
4 4 4
Fig –(b)

L1.10
Recursion Tree Method (Example 1)
𝑛
𝑇 𝑛 = 3𝑇 + 𝑐𝑛2 , 𝑐 > 0 𝑖𝑠 𝑎 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡
4
𝑐 𝑛 2

𝑐(𝑛/4)2 𝑐(𝑛/4)2 𝑐(𝑛/4)2

𝑛 𝑛 𝑛 𝑛 𝑛 𝑛 𝑛 𝑛 𝑛
𝑇 𝑇 𝑇 𝑇 𝑇 𝑇 𝑇 𝑇 𝑇
16 16 16 16 16 16 16 16 16

Fig –(c)

L1.11
Recursion Tree Method (Example 1)
𝑛
𝑇 𝑛 = 3𝑇 + 𝑐𝑛2 , 𝑐 > 0 𝑖𝑠 𝑎 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡
4
2 2
𝑐 𝑛 𝑐 𝑛

3 2
𝑐(𝑛/4)2 𝑐(𝑛/4)2 𝑐(𝑛/4)2 𝑐 𝑛
16
log 4 𝑛

2
𝑛 2
𝑛 2 𝑛 2 𝑛 2 𝑛 2 𝑛 2
𝑛 2 𝑛 2 𝑛 2 3 2
𝑐
16
𝑐
16
𝑐
16
𝑐
16
𝑐
16
𝑐
16
𝑐
16
𝑐
16
𝑐
16 𝑐 𝑛
16

𝑖
3 2
𝑐 𝑛
16
𝑖
𝑇 1 𝑇 1 𝑇 1 ………………………..𝑇 1 3
Fig –(d)

L1.12
Recursion Tree Method (Example 1)

Analysis
First, we find the height of the recursion tree
𝑛
Observe that a node at depth ′𝑖′ reflects a subproblem of size .
4 𝑖
𝑛
i.e. the subproblem size hits 𝑛 = 1, when =1
4 𝑖
𝑛
So, if 4 𝑖
=1
⟹𝑛= 4 𝑖 𝐴𝑝𝑝𝑙𝑦 𝐿𝑜𝑔 𝑏𝑜𝑡ℎ 𝑠𝑖𝑑𝑒
⟹ log 𝑛 = log 4 𝑖
⟹ 𝑖 = log 4 𝑛
So the height of the tree is log 4 𝑛.
Recursion Tree Method (Example 1)
Second, we determine the cost of each level of the tree.
The number of nodes at depth ′𝑖′ is 3 𝑖 . It was observed that
2 3 𝑖
3 3 3 3
𝑇 𝑛 = 𝑐𝑛2 + 𝑐𝑛2 + 𝑐𝑛2 + 𝑐𝑛2 + ⋯ + 𝑐𝑛2 + (3)𝑖
16 16 16 16
𝑛 2
So, each node at depth ′𝑖 ′ 𝑖. 𝑒. 𝑖 = 0,1,2,3,4, … , log 4 𝑛 − 1 has cost 𝑐 .
4𝑖
𝑛 2
Hence the total cost at level ′𝑖′ is 3𝑖 𝑐 4𝑖
𝑖
𝑖
𝑛 2
𝑖
𝑛2 3𝑖 2
3
⟹ 3 . 𝑐. 𝑖 ⟹ 3 . 𝑐. 𝑖 ⟹ 𝑖 . 𝑐. 𝑛 ⟹ . 𝑐. 𝑛2
4 16 16 16
Recursion Tree Method (Example 1)

However, the bottom level is special. Each of the bottom node has
contribute cost = 𝑇(1)
Hence the cost of the bottom level is = 3𝑖
⟹ 3log4 𝑛 𝑎𝑠 𝑖 = log 4 𝑛 𝑡ℎ𝑒 ℎ𝑒𝑖𝑔ℎ𝑡 𝑜𝑓 𝑡ℎ𝑒 𝑡𝑟𝑒𝑒
⟹ 𝑛log4 3
So, the total cost of entire tree is
𝑇 𝑛
2 3 𝑖
3 3 3 3
= 𝑐𝑛2 + 𝑐𝑛2 + 𝑐𝑛2 + 𝑐𝑛2 + ⋯ + ⋯ + 𝑐𝑛2 + Θ(𝑛log4 3 )
16 16 16 16
log4 𝑛 𝑖
3
𝑇 𝑛 = ෍ 𝑐𝑛2 + Θ(𝑛log4 3 )
16
𝑖=0
Recursion Tree Method (Example 1)
The left term is just a sum of Geometric series. So the value of 𝑇 𝑛 is as follows.

3ൗ log4 𝑛 − 1
𝑇 𝑛 = 16 𝑐𝑛2 + Θ(𝑛log4 3 )
3
−1
16
The above equation looks very complicated. So, we use an infinite geometric series as an upper
bound. Hence the new form of the equation is given below:
log4 𝑛−1 𝑖
3
𝑇 𝑛 = ෍ 𝑐𝑛2 + Θ(𝑛log4 3 )
𝑖=0
16 𝑆𝑢𝑚 𝑜𝑓 𝑓𝑖𝑛𝑖𝑡𝑒 𝐺𝑒𝑜𝑚𝑒𝑡𝑟𝑖𝑐 𝑃𝑟𝑜𝑔𝑟𝑒𝑠𝑠𝑖𝑜𝑛
∞ 𝑛
3
𝑖 2
𝑆𝑛 = 𝑎 + 𝑎𝑟 + 𝑎𝑟 + . . . + 𝑎𝑟
𝑇 𝑛 ≤෍ 𝑐𝑛2 + Θ(𝑛log4 3 )
16
𝑖=0 𝑛
1 𝑟 𝑛+1 −1
2 log4 3
𝑇 𝑛 ≤ 𝑐𝑛 + Θ(𝑛 )
1−
3 𝑆𝑛 = ෍ 𝑎𝑟 𝑖 = 𝑎
16 𝑟−1
𝑇 𝑛 ≤
16 2
𝑐𝑛 + Θ(𝑛log4 3 )
𝑖=0
13
𝑇 𝑛 = Ο(𝑛2 )
Recursion Tree Method (Example 2)
Example 2
𝑛
Solve the recurrence 𝑇 𝑛 = 4𝑇 + 𝑛 by using recursion tree
2
method.
𝑛
𝑇 𝑛 = 4𝑇 + 𝑐𝑛 , 𝑐 > 0
2
The term 𝑐𝑛, at the root represent the costs incurred by the
𝑛
subproblems of size .
2

Construction of Recursion tree

𝑇(𝑛)

Fig –(a)

Figure (a) shows T(n), which progressively expands in (b) to form


recursion tree.
Recursion Tree Method (Example 2)
𝑛
𝑇 𝑛 = 4𝑇 + 𝑐𝑛 , 𝑐 > 0
2
𝑐𝑛 𝑐𝑛

𝑛 4
𝑛 𝑐 𝑐𝑛
𝑛 𝑐 2
𝑐
𝑛
2 2
𝑐
2 2
log 2 𝑛

2
4
𝑛 𝑛 𝑇
𝑛
𝑛 𝑛 𝑇
𝑛
𝑐𝑛
𝑇
4 𝑇
𝑛
𝑇
𝑛
4
𝑇
4 𝑇
𝑛
4 𝑇
𝑛
𝑇
𝑛 𝑇
𝑛
4 𝑇
𝑛
𝑛 𝑛 𝑇
𝑛
4 𝑇
4
𝑇
4
4
2
4 4 4 𝑇 4
4 𝑇
4 4 3
4
𝑐𝑛
2
𝑖
4
𝑐𝑛
2

𝑇 1 𝑇 1 𝑇 1 ………………………..𝑇 1 4 𝑖

Fig –(b)
Recursion Tree Method (Example 2)

Analysis
First, we find the height of the recursion tree
𝑛
Observe that a node at depth ′𝑖′ reflects a subproblem of size .
2 𝑖
𝑛
i.e. the subproblem size hits 𝑛 = 1, when =1
2 𝑖
𝑛
So, if =1
2 𝑖
⟹𝑛= 2 𝑖 𝐴𝑝𝑝𝑙𝑦 𝐿𝑜𝑔 𝑏𝑜𝑡ℎ 𝑠𝑖𝑑𝑒
⟹ log 𝑛 = log 2 𝑖
⟹ 𝑖 = log 2 𝑛
So the height of the tree is log 2 𝑛.
Recursion Tree Method (Example 2)
Second, we determine the cost of each level of the tree.
The number of nodes at depth ′𝑖′ is 4 𝑖 .
So, each node at depth ′𝑖 ′ 𝑖. 𝑒. 𝑖 = 0,1,2,3,4, … … , log 2 𝑛 − 1 has cost
𝑛
𝑐 2𝑖 .
𝑛
Hence the total cost at level ′𝑖′ is 4𝑖 𝑐 2𝑖
𝑖
𝑛
⟹ 4 . 𝑐. 𝑖
2
𝑖
𝑛
⟹ 4 . 𝑐. 𝑖
2
𝑖
4
⟹ 𝑖 . 𝑐𝑛
2
𝑖
4
⟹ . 𝑐𝑛
2
Recursion Tree Method (Example 2)
However, the bottom level is special. Each of the bottom node has contribute
cost = 𝑇(1)
Hence the cost of the bottom level is = 4𝑖
⟹ 4log2 𝑛 𝑎𝑠 𝑖 = log 2 𝑛 𝑡ℎ𝑒 ℎ𝑒𝑖𝑔ℎ𝑡 𝑜𝑓 𝑡ℎ𝑒 𝑡𝑟𝑒𝑒
⟹ 𝑛log2 4

So, the total cost of entire tree is


2 3 𝑖
4 4 4 4
𝑇 𝑛 = 𝑐𝑛 + 𝑐𝑛 + 𝑐𝑛 + 𝑐𝑛 + ⋯ + ⋯ + 𝑐𝑛 + Θ(𝑛log2 4 )
2 2 2 2
log2 𝑛 𝑖
4
𝑇 𝑛 = ෍ 𝑐𝑛 + Θ(𝑛log2 4 )
2
𝑖=0
Recursion Tree Method (Example 2)
The left term is just a sum of Geometric series. So the value of 𝑇 𝑛 is as follows.
log2 𝑛
4
−1
2
𝑇 𝑛 = 𝑐𝑛 + Θ(𝑛log2 4 )
4
−1
2
log2 𝑛
2 −1
𝑇 𝑛 = 𝑐𝑛 + Θ(𝑛2 )
2−1
log2 2
𝑛 −1
𝑛 = 𝑐𝑛 + 𝑐𝑛2
2−1
𝑛−1
𝑇 𝑛 = 𝑐𝑛 + 𝑐𝑛2
1
𝑇 𝑛 = 𝑐𝑛2 − 𝑐𝑛 + 𝑐𝑛2
𝑇 𝑛 = 2𝑐𝑛2 − 𝑐𝑛
𝐻𝑒𝑛𝑐𝑒, 𝑇 𝑛 = Θ(𝑛2 )
Recursion Tree Method (Example 3)
Example 3
𝑛
Solve the recurrence 𝑇 𝑛 = 2𝑇 2 + Θ(𝑛) by using recursion tree
method.
𝑇(𝑛) = 2𝑇(𝑛/2) + 𝑐𝑛 , 𝑐 > 0
The term 𝑐𝑛, at the root represent the costs incurred by the
𝑛
subproblems of size 2 .
Construction of Recursion tree
𝑇(𝑛)
Fig –(a)
Figure (a) shows T(n), which progressively expands in (b) to form
recursion tree.
Recursion Tree Method (Example 3)
𝑇(𝑛) = 2𝑇(𝑛/2) + 𝑐𝑛 , 𝑐 > 0

𝑐𝑛 𝑛

𝑛 𝑛 𝑛
𝑐 𝑐 2 =𝑛
2 2 2
log 2 𝑛

𝑛 𝑛 𝑛 𝑛 𝑛
𝑇 𝑇 𝑇 𝑇 22 =𝑛
4 4 4 4 22

𝑛
2𝑖 =𝑛
2𝑖
𝑇 1 𝑇 1 𝑇 1 ………………………..𝑇 1
2𝑖
Fig –(b)
Recursion Tree Method (Example 3)
Analysis
First, we find the height of the recursion tree
𝑛
Observe that a node at depth ′𝑖′ reflects a subproblem of size .
2 𝑖
𝑛
i.e. the subproblem size hits 𝑛 = 1, when =1
2 𝑖
𝑛
So, if =1
2 𝑖
⟹𝑛= 2 𝑖 𝐴𝑝𝑝𝑙𝑦 𝐿𝑜𝑔 𝑏𝑜𝑡ℎ 𝑠𝑖𝑑𝑒
⟹ log 𝑛 = log 2 𝑖
⟹ 𝑖 = log 2 𝑛
So the height of the tree is log 2 𝑛.
Recursion Tree Method (Example 3)
Second, we determine the cost of each level of the tree.
The number of nodes at depth ′𝑖′ is 4 𝑖 .
𝑛
So, each node at depth ′𝑖 ′ 𝑖. 𝑒. 𝑖 = 0,1,2,3,4, … … , log 2 𝑛 − 1 has cost 𝑐 .
2𝑖
𝑛
Hence the total cost at level ′𝑖′ is 2𝑖 𝑐 2𝑖 = 𝑐𝑛
However, the bottom level is special. Each of the bottom node has contribute
cost = 𝑇(1)
Hence the cost of the bottom level is = 2𝑖
⟹ 2log2 𝑛 𝑎𝑠 𝑖 = log 2 𝑛 𝑡ℎ𝑒 ℎ𝑒𝑖𝑔ℎ𝑡 𝑜𝑓 𝑡ℎ𝑒 𝑡𝑟𝑒𝑒
⟹ 𝑛log2 2
⟹𝑛
Recursion Tree Method (Example 3)

So, the total cost of entire tree is

𝑇 𝑛 = 𝑐𝑛 + 𝑐𝑛 + 𝑐𝑛 + 𝑐𝑛 + ⋯ + ⋯ + 𝑐𝑛

log 𝑛
𝑇 𝑛 = 𝑐𝑛 σ𝑖=02 1𝑖 .

𝑇 𝑛 = 𝑐𝑛 (log 2 𝑛 + 1)

𝑇 𝑛 = 𝑐𝑛 log 2 𝑛 + 𝑐𝑛

𝐻𝑒𝑛𝑐𝑒, 𝑇 𝑛 = Θ(𝑛 log 2 𝑛)


Recursion Tree Method (Example 4)
Example 4
𝑛 𝑛 𝑛
Solve the recurrence 𝑇 𝑛 = 𝑇 2 + 𝑇 4 + 𝑇 8 + Θ(𝑛) by using
recursion tree method.
𝑛 𝑛 𝑛
𝑇 𝑛 =𝑇 +𝑇 +𝑇 + 𝑐𝑛 , 𝑐 > 0
2 4 8
The term 𝑐𝑛, at the root represent the costs incurred by the subproblems
𝑛 𝑛 𝑛
of size 2 , 4 , 𝑎𝑛𝑑 8 .
Construction of Recursion tree
𝑇(𝑛)
Fig –(a)
Figure (a) shows T(n), which progressively expands in (b) to form
recursion tree.
Recursion Tree Method (Example 4)
𝑛 𝑛 𝑛
𝑇 𝑛 =𝑇 +𝑇 +𝑇 + 𝑐𝑛 , 𝑐 > 0
2 4 8
𝑐𝑛 𝑐𝑛

𝑛 𝑛
𝑛 𝑐 7
𝑐 𝑐 8
2 4 𝑐𝑛
8
log 2 𝑛

2
𝑇
𝑛
𝑇
𝑛
𝑇
𝑛
𝑇
𝑛
8
𝑇
𝑛
16
𝑇
𝑛
𝑇
𝑛
𝑇
𝑛
𝑇
𝑛
7
2 4 8 32 16 32 64
𝑐𝑛
8

𝑖
7
𝑐𝑛
8

𝑇 1 𝑇 1 𝑇 1 ………………………..𝑇 1 𝑐𝑛
Fig –(b)
Recursion Tree Method (Example 4)
Analysis
First, we find the height of the recursion tree

𝑛 𝑛 𝑛
Hear the problem divide into three subproblem of size 𝑖 , 𝑖 , 𝑎𝑛𝑑 𝑖 .
2 4 8
For calculating the height of the tree, we consider the longest path of the tree. It has
been observed that the node on the left-hand side is the longest path of the tree.
𝑛
Hence the node at depth ′𝑖′ reflects a subproblem of size .
2𝑖

𝑛
i.e. the subproblem size hits 𝑛 = 1, when =1
2𝑖
𝑛
So, if =1
2𝑖
⟹ 𝑛 = 2𝑖 𝐴𝑝𝑝𝑙𝑦 𝐿𝑜𝑔 𝑏𝑜𝑡ℎ 𝑠𝑖𝑑𝑒
⟹ log 𝑛 = log 2 𝑖
⟹ 𝑖 = log 2 𝑛
So the height of the tree is log 2 𝑛.
Recursion Tree Method (Example 4)
𝑛 𝑛 𝑛
Second, we determine the cost of the tree in level ′𝑖′ = + +
2𝑖 4𝑖 8𝑖
So, the total cost of the tree is:
2 3
7 7 7
𝑇 𝑛 = 𝑐𝑛 + 𝑐𝑛 + 𝑐𝑛 + 𝑐𝑛 + ⋯ + ⋯
8 8 8
For simplicity we take ∞ Geometric Series
2 3
7 7 7
𝑇 𝑛 = 𝑐𝑛 + 𝑐𝑛 + 𝑐𝑛 + 𝑐𝑛 + ⋯ + ∞
8 8 8
∞ 𝑖
7
𝑇 𝑛 ≤෍ 𝑐𝑛
8
𝑖=0
1
𝑇 𝑛 ≤ 𝑐𝑛
7
1−
8
8
𝑇 𝑛 ≤ 𝑐𝑛
1
𝑇 𝑛 ≤ 8𝑐𝑛
𝐻𝑒𝑛𝑐𝑒, 𝑇 𝑛 = Ο(𝑛)

You might also like