Aoa Chapter 1
Aoa Chapter 1
SECOND YEAR
SEM- IV
AOA (ANALYSIS OF
ALGORITHM)
C O U R S E O U T C O M E S
CHAPTER-1
CO1- Apply algorithmic fundamentals to perceive various
algorithmic approaches.
What is an algorithm?
Algorithm is a set of steps to complete a task.
For example,
Task: to make a cup of tea.
Algorithm:
• Add water and milk to the kettle,
• Boil it, add tea leaves,
• Add sugar, and then serve it in cup.
Definition: “An algorithm is a procedure to
solve a particular problem in a finite no. of steps for a
finite size input.”
A n algorithm is thus a sequence of computational
steps that transform the input into the output.
We can also view an algorithm as a tool for solving
a well-specified computational problem
Properties of a good
algorithm
• May take Zero or more input
Input • Input may be scalar, vector, array, tree, graph etc
Space Complexity
Time Complexity
Growth of a
function
PERFORMANCE ANALYSIS
Time Complexity
Example-1: Addition of two scalar variables
The addition of two scalar numbers requires
one addition operation. Thus time complexity
of this algorithm is T(n)=O(1).
PERFORMANCE ANALYSIS
Time Complexity
Example-2: Addition of two arrays
for i 1 to n do
C[i] A[i] + B[i]
Total time of algorithm is measured as
T(n)= 1 Initialization + n (Comparison + Increment +
Addition + Assignment)
= 1+ 4n
Thus time required for given algorithm is
T(n) = O(n)
PERFORMANCE ANALYSIS
Growth of Function
The efficiency of the algorithm is expressed in terms
of
input size n.
The relationship between input size and performance
of the algorithm is called order of growth.
Order of growth indicates how quickly the time
required
by algorithm grows with respect to input size.
1< log n <√ n < n < n log n < n^2 < n^3 < ..
PERFORMANCE ANALYSIS
Growth of
Function
Input Constant Linear Quadratic Cubic Exponent
Size (1) (n) (n2) (n3) (2n )
1 1 1 1 1 1
2 1 2 4 8 4
3 1 3 9 27 8
4 1 4 16 64 16
8 1 8 64 512 256
16 1 16 256 4096 66536
32 1 32 1024 32678 4294967296
PERFORMANCE ANALYSIS
Big- Oh, Omega, Theta notation (Asymptotic Notations)
Asymptotic notations are mathematical tool to find
time or space complexity of an algorithm without
implementing it in a programming language.
Asymptotic notations
Big Oh(O)
Big Omega(Ω)
Big Theta(θ)
PERFORMANCE ANALYSIS
Big Oh(O)
Big
Oh(O)
Let f(n) and are two
nonnegative
g(n) functions
indicating running time of two
algorithms.
We say, g(n) is upper bound of
f(n) if there exist some positive
constant c and no such that ,
f(n) ≤ c. g(n) for all n ≥ no
where, no ≥ 1, c > 0
PERFORMANCE ANALYSIS
B ig Oh(O) for c=1 and n=2
3*2+2 ≤ 1*2 => 8 Fals
Let, ≤2 e
f(n)= 3n+2 for c=2 and n=2 Fals
g(n)=n 3*2+2 ≤ 2*2 => 8 e
Can we say f(n) =O(g(n)) ≤4
Fals
iff
f(n) ≤ c. g(n) for c>0 and n0 for c=3 and n=2 e
≥ 1
Putting f(n) and g(n) in above 3*2+2 ≤ 3*2 => 8
eqn ≤6 True
3n+2
Now ≤ wec. n ……
will (1)check when for c=4 and n=2
this condition is true by 3*2+2 ≤ 4*2 => 8
changing values of c ≤8
PERFORMANCE ANALYSIS
Big
Oh(O)
• 1< log n <√ n < n < n log n < n^2 < n^3 < .. < 2^n..<n^n
PERFORMANCE ANALYSIS
Big Omega(Ω)
Big Theta(θ)
Big Theta(θ)
O
Worst
Ω Bes θ Average
Case t Case
Cas
e
We will take Linear Search
example
8 4 7 2 9 3 1 6 5
0 1 2 3 4 5 6 7… n
𝛴ⅈ=1 = 1 +
𝑛
ⅈ𝑘 2𝑘 + 3𝑘 … 𝑛𝑘
𝑘+ =𝑜
+ 𝑛𝑘 = 1
𝑛
𝑘+1
𝛴ⅈ=1𝑘ⅈ = 𝑘 + 𝑘 2 + 𝑘 3 + ⋯
𝑛 𝑘𝑛+1 = 𝑂
𝑘−
+ 𝑘𝑛 = 𝑘𝑛
1
Complexity class :Definition of P, NP, NP-
Hard,
NP-Complete
COMPLEXITY CLASS
P Class
NP Class
NP-hard
NP-complete
P CLASS
Features:
• The solution to P problems is easy to find.
• P is often a class of computational problems that are
solvable and tractable.
• Tractable means that the problems can be solved in theory
as well as in practice.
• But the problems that can be solved in theory but not in
practice are known as intractable.
N P C L A S S
Features:
All NP-hard problems are not in NP.
It takes a long time to check them. This means if a solution for
an NP-hard problem is given then it takes a long time to check
whether it is right or not.
A problem A is in NP-hard if, for every problem L in NP, there
exists a polynomial-time reduction from L to A.
1.2
Analyzing time and space complexity of
Iterative algorithm-
Insertion Sort
Selection Sort
ANALYSIS OF ALGORITHM
Algorithm
Iterative Recursiv
e
A() A(n)
{ {
for i=1 to n if( )
Max(a,b) A (n/2)
}
}
ANALYSIS OF ALGORITHM
Algorithm
Iterative Recursiv
e
Selection Sort
Recurrences
Insertion Sort
ANALYSIS OF ITERATIVE ALGORITHM
Iterative Algorithm
Analysis
A() {
O(n
for(i=1
int i; to n)
)
print(“comp”);
}
A() {
int i, j
for(i=1 to n) O(n2
for (j=1 to )
n)
ANALYSIS OF SELECTION SORT
for i = 0 to n - 1 do min = i
for j = i+1 to n do
if A[j] < A[min] do
min = j ;
end if
end for
swap (A[min], A[i]);
end for
ANALYSIS OF SELECTION SORT
• for i = 0 to n - 1 do min =
14 33 27 10 35 19
i
• for j = i+1 to n do Pass-1
• if A[j] < A[min] do
•min = j; end if
• end for
• swap (A[min], A[i]);
•end for
ANALYSIS OF SELECTION SORT
0 1 2 3 4 5
•Algorithm SELECTION SORT (A)
14 33 27 10 35 19
•// A is an array of size n
i
• for i = 0 to n - 1 do min =
i
• for j = i+1 to n do Pass-1
• if A[j] < A[min] do
•min = j; end if
• end for
• swap (A[min], A[i]);
•end for
ANALYSIS OF SELECTION SORT
0 1 2 3 4 5
• for i = 0 to n - 1 do min = i
i j
• for j = i+1 to n do
min =0
• if A[j] < A[min] do
•min = j; end if
Pass-1
• end for
• swap (A[min], A[i]);
•end for
ANALYSIS OF SELECTION SORT
0 1 2 3 4 5
•Algorithm SELECTION SORT (A) 14 33 27 10 35 19
•// A is an array of size n
i j
• for i = 0 to n - 1 do min = i
• for j = i+1 to n do
min =0
• if A[j] < A[min] do Pass-1
•min = j; end if 33 <
• end for 14
• swap (A[min], A[i]);
•end for
0 1 2 3 4 5
14 33 27 10 35 19
•Algorithm SELECTION SORT (A)
•// A is an array of size n i j
min =0
• for i = 0 to n - 1 do min = i
• for j = i+1 to n do Pass-1
• if A[j] < A[min] do
•min = j; end if
• end for
• swap (A[min], A[i]);
•end for
0 1 2 3 4 5
14 33 27 10 35 19
•Algorithm SELECTION SORT (A)
•// A is an array of size n i j
min =0
• for i = 0 to n - 1 do min =
i Pass-1
27 <
• for j = i+1 to n do
14
• if A[j] < A[min] do
•min = j; end if
• end for
• swap (A[min], A[i]);
0 1 2 3 4 5
•Algorithm SELECTION SORT (A)
14 33 27 10 35 19
•// A is an array of size n
• for i = 0 to n - 1 do min = i i j
• for j = i+1 to n do min =0
• if A[j] < A[min] do
•min = j; end if Pass-1
• end for
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A) 0 1 2 3 4 5
•// A is an array of size n 14 33 27 10 35 19
• for i = 0 to n - 1 do min = i
i j
• for j = i+1 to n do
• if A[j] < A[min] do min =0
•min = j; end if
Pass-1
• end for 10 <
• swap (A[min], A[i]); 14
•end for
•Algorithm SELECTION SORT (A)
•// A is an array of size n 0 1 2 3 4 5
14 33 27 10 35 19
• for i = 0 to n - 1 do min =
i
i j
• for j = i+1 to n do
min =3
• if A[j] < A[min] do
•min = j; end if Pass-1
10 <
• end for
14
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n
14 33 27 10 35 19
• for i = 0 to n - 1 do min = i
• for j = i+1 to n do i j
• if A[j] < A[min] do
min =3
•min = j; end if
• end for Pass-1
• swap (A[min], A[i]);
10 <
14
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n
14 33 27 10 35 19
• for i = 0 to n - 1 do min =
i i j
• for j = i+1 to n do min =3
• if A[j] < A[min] do
Pass-1
•min = j; end if
• end for
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A)
•// A is an array of size n 0 1 2 3 4 5
14 33 27 10 35 19
• for i = 0 to n - 1 do min = i
• for j = i+1 to n do
• if A[j] < A[min] do
i j
•min = j; end if min =3
• end for
Pass-1
• swap (A[min], A[i]); 35 <
•end for 10
•Algorithm SELECTION SORT (A)
•// A is an array of size n 0 1 2 3 4 5
14 33 27 10 35 19
• for i = 0 to n - 1 do min = i
• for j = i+1 to n do
• if A[j] < A[min] do
i j
•min = j; end if min =3
• end for
Pass-1
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n
14 33 27 10 35 19
• for i = 1 to n - 1 do min = i
• for j = i+1 to n do i j
• if A[j] < A[min] do
min =3
•min = j; end if
• end for Pass-1
• swap (A[min], A[i]);
19 <
10
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n
14 33 27 10 35 19
• for i = 1 to n - 1 do min = i
• for j = i+1 to n do i j
• if A[j] < A[min] do min =3
•min = j; end if
• end for Pass-1
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n
14 33 27 10 35 19
• for i = 1 to n - 1 do min = i
• for j = i+1 to n do i j
• if A[j] < A[min] do min =3
•min = j; end if
• end for Pass-1
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A) 0 1 2 3 4 5
•// A is an array of size n 10 33 27 14 35 19
• for i = 1 to n - 1 do min = i
i j
• for j = i+1 to n do
• if A[j] < A[min] do min =3
•min = j; end if
Pass-1
• end for
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n 10 33 27 14 35 19
• for i = 1 to n - 1 do min =
i i
• for j = i+1 to n do
• if A[j] < A[min] do Pass-2
•min = j; end if
• end for
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n
10 33 27 14 35 19
• for i = 1 to n - 1 do min = i
• for j = i+1 to n do
i
• if A[j] < A[min] do
•min = j; end if min =1
• end for Pass-2
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A)0 1 2 3 4 5
•// A is an array of size n
10 33 27 14 35 19
• for i = 1 to n - 1 do min =
i i j
• for j = i+1 to n do min =1
• if A[j] < A[min] do
Pass-2
•min = j; end if
• end for
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A) 0 1 2 3 4 5
•// A is an array of size n 10 33 27 14 35 19
• for i = 1 to n - 1 do min = i
• for j = i+1 to n do
i j
• if A[j] < A[min] do min =1
•min = j; end if
Pass-2
• end for 27 <
• swap (A[min], A[i]); 33
•end for
0 1 2 3 4 5
•Algorithm SELECTION SORT (A)
10 33 27 14 35 19
•// A is an array of size n
• for i = 1 to n - 1 do min = i i j
• for j = i+1 to n do min =2
• if A[j] < A[min] do
•min = j; end if Pass-2
27 <
• end for 33
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n
10 33 27 14 35 19
• for i = 1 to n - 1 do min =
i i j
• for j = i+1 to n do
min =2
• if A[j] < A[min] do
•min = j; end if Pass-2
27 <
• end for 33
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n
10 33 27 14 35 19
• for i = 1 to n - 1 do min = i
• for j = i+1 to n do i j
• if A[j] < A[min] do
min =2
•min = j; end if
• end for Pass-2
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n
10 33 27 14 35 19
• for i = 1 to n - 1 do min = i
• for j = i+1 to n do i j
• if A[j] < A[min] do min =2
•min = j; end if
• end for Pass-2
14< 27
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A)
•// A is an array of size n 0 1 2 3 4 5
10 33 27 14 35 19
• for i = 1 to n - 1 do min =
i
i j
• for j = i+1 to n do
min =3
• if A[j] < A[min] do
•min = j; end if Pass-2
14< 27
• end for
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n
10 33 27 14 35 19
• for i = 1 to n - 1 do min = i
• for j = i+1 to n do i j
• if A[j] < A[min] do
•min = j; end if
min =3
• end for Pass-2
• swap (A[min], A[i]);
14< 27
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n
10 33 27 14 35 19
• for i = 1 to n - 1 do min = i
• for j = i+1 to n do i j
• if A[j] < A[min] do
•min = j; end if
min =3
• end for Pass-2
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n
10 33 27 14 35 19
• for i = 1 to n - 1 do min = i
• for j = i+1 to n do i j
• if A[j] < A[min] do
min =3
•min = j; end if
• end for Pass-2
• swap (A[min], A[i]);
35< 14
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n
10 33 27 14 35 19
• for i = 1 to n - 1 do min = i
• for j = i+1 to n do i j
• if A[j] < A[min] do min =3
•min = j; end if
• end for Pass-2
35< 14
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n
10 33 27 14 35 19
• for i = 1 to n - 1 do min = i
• for j = i+1 to n do i j
• if A[j] < A[min] do min =3
•min = j; end if
• end for Pass-2
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A)
•// A is an array of size n 0 1 2 3 4 5
10 33 27 14 35 19
• for i = 1 to n - 1 do min = i
• for j = i+1 to n do
• if A[j] < A[min] do
i j
•min = j; end if min =3
• end for
Pass-2
• swap (A[min], A[i]); 19< 14
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n
10 33 27 14 35 19
• for i = 1 to n - 1 do min = i
• for j = i+1 to n do i j
• if A[j] < A[min] do
•min = j; end if
min =3
• end for Pass-2
• swap (A[min], A[i]);
19< 14
•end for
•Algorithm SELECTION SORT (A) 0 1 2 3 4 5
•// A is an array of size n 10 33 27 14 35 19
• for i = 1 to n - 1 do min = i
• for j = i+1 to n do i j
• if A[j] < A[min] do min =3
•min = j; end if
Pass-2
• end for
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n
10 33 27 14 35 19
• for i = 1 to n - 1 do min = i
• for j = i+1 to n do i j
• if A[j] < A[min] do min =3
•min = j; end if
• end for Pass-2
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n
10 14 27 33 35 19
• for i = 1 to n - 1 do min = i
• for j = i+1 to n do
• if A[j] < A[min] do
i j
•min = j; end if min =3
• end for
• swap (A[min], A[i]); Pass-2
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n
10 14 27 33 35 19
• for i = 1 to n - 1 do min = i
• for j = i+1 to n do
i
• if A[j] < A[min] do
•min = j; end if min =3
• end for Pass-3
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n
10 14 27 33 35 19
• for i = 1 to n - 1 do min =
i i
• for j = i+1 to n do
min =2
• if A[j] < A[min] do
•min = j; end if Pass-3
• end for
• swap (A[min], A[i]);
•end for
•Algorithm SELECTION SORT (A)
0 1 2 3 4 5
•// A is an array of size n
10 14 27 33 35 19
• for i = 1 to n - 1 do min = i
• for j = i+1 to n do i j
• if A[j] < A[min] do
•min = j; end if
min =2
• end for Pass-3
• swap (A[min], A[i]);
•end for
0 1 2 3 4 5
10 14 27 33 35 19
10 14 27 33 35 19
10 14 27 33 35 19
10 14 27 33 35 19
0 1 2 3 4 5
10 14 19 27 33 35
0 1 2 3 4 5
10 14 19 27 33 35
0 1 2 3 4 5
10 14 19 27 33 35
0 1 2 3 4 5
10 14 19 27 33 35
0 1 2 3 4 5
10 14 19 27 33 35
0 1 2 3 4 5
10 14 19 27 33 35
0 1 2 3 4 5
10 14 19 27 33 35
0 1 2 3 4 5
10 14 19 27 33 35
0 1 2 3 4 5
10 14 19 27 33 35
0 1 2 3 4 5
10 14 19 27 33 35
0 1 2 3 4 5
10 14 19 27 33 35
i Value Number of No of
•Algorithm SELECTION SORT (A) Comparison Swap
•// A is an array of size n
s s
i= 1 n-1 1
0 1 2 3 4 5
10 14 19 27 33 35
i Value Number of No of
•Algorithm SELECTION SORT (A) Comparison Swap
•// A is an array of size n
s s
i= 1 n-1 1
0 1 2 3 4 5
10 14 19 27 33 35
i Value Number of
•Algorithm SELECTION SORT (A) Comparison
•// A is an array of size n
s
i= 1 n-1
Space Complexity
a[j]>Temp 1 1 1 1+1=2
SET ARR[j+ 1] = ARR[j] 2 2 2 2+2=4
SET j = j- 1 3 3 3 3+3=6
[END OF INNER LOOP] … … … ----
[END OF LOOP]
Step 6: EXIT
ANALYSIS OF INSERTION SORT
Time Complexity: Worst
INSERTION-SORT (ARR, N) Case
0 1 2 3 4 5
Step 1: Repeat Steps 2 to 5 for i = 1 to N – 1
Step 2: SET TEMP = ARR[i] 2 3 4 6 8 9
Step 3: SET j= i- 1
i Compar Swap Total
Step 4: Repeat while j>=0 && Value i son
a[j]>Temp 1 1 1 1+1=2
SET ARR[j+ 1] = ARR[j] 2 2 2 2+2=4
SET j = j- 1 3 3 3 3+3=6 2(3)
[END OF INNER LOOP] ---- --- --- ---- ---
[END OF LOOP]
Step 6: EXIT
ANALYSIS OF INSERTION SORT
Time Complexity: Worst
INSERTION-SORT (ARR, N) Case
0 1 2 3 4 5
Step 1: Repeat Steps 2 to 5 for i = 1 to N – 1
Step 2: SET TEMP = ARR[i] 2 3 4 6 8 9
Step 3: SET j= i- 1
i Compar Swap Total
Step 4: Repeat while j>=0 && Value i son
a[j]>Temp 1 1 1 1+1=2
SET ARR[j+ 1] = ARR[j] 2 2 2 2+2=4 2(2)
SET j = j- 1 3 3 3 3+3=6 2(3)
[END OF INNER LOOP] ---- --- --- ---- ---
[END OF LOOP]
Step 6: EXIT
ANALYSIS OF INSERTION SORT
Time Complexity: Worst
INSERTION-SORT (ARR, N) Case
0 1 2 3 4 5
Step 1: Repeat Steps 2 to 5 for i = 1 to N – 1
Step 2: SET TEMP = ARR[i] 2 3 4 6 8 9
Step 3: SET j= i- 1
i Compar Swap Total
Step 4: Repeat while j>=0 && Value i son
[END OF LOOP]
Step 6: EXIT
ANALYSIS OF INSERTION SORT
Time Complexity: Worst
INSERTION-SORT (ARR, N) Case
0 1 2 3 4 5
Step 1: Repeat Steps 2 to 5 for i = 1 to N – 1
Step 2: SET TEMP = ARR[i] 2 3 4 6 8 9
Step 3: SET j= i- 1
i Comp Swap Total
Step 4: Repeat while j>=0 && Va lue a
rison
a[j]>Temp
1 1 1 1+1=2 2(1)
SET ARR[j+ 1] = ARR[j]
2 2 2 2+2=4 2(2)
SET j = j- 1
3 3 3 3+3=6 2(3)
[END OF INNER LOOP]
---- --- --- ---- ---
Step 5: SET ARR[j + 1] = TEMP n-1 n-1 n-1 2n-2 2(n-1)
[END OF LOOP]
= 2(1)+2(2)+2(3)+…
Step 6: EXIT +2(n-1)
= 2 (1+2+3+…+n)
=2 �
= n(n-1) =n2 - n
ANALYSIS OF INSERTION SORT
Time Complexity: Best Case
INSERTION-SORT (ARR, N)
0 1 2 3 4 5
Step 1: Repeat Steps 2 to 5 for i = 1 to N – 1
Step 2: SET TEMP = ARR[i] 2 3 4 6 8 9
Step 3: SET j= i- 1 i Value Comparison Swap
Recursive Program
When function calls itself
A(n)
{
if( )
A(n/2) + A(n/2)
}
Additio
n
RECURRENCES
Recursive
function
A(n) T(n) =
{ C
if( ) Compariso
A(n/2) + n
A(n/2)
Require Constant
}
Time
Additio
n
RECURRENCES
Recursive
function
A(n) T(n) =
{ C
if( )
A(n/2) +
A(n/2)
T( )
}
𝑛
T( )
𝑛
2
2
RECURRENCES
Recursive
function
2
)
A(n) T(n) = C+ 2T
{ 𝑛
(
if( )
A(n/2) +
A(n/2)
}
RECURRENCES
Recursive
function
2
)
A(n) T(n) = C+ 2T Recursive
{ 𝑛 Function
(
if( )
A(n/2) +
A(n/2)
}
RECURRENCES
Recursive
Function
Substitution M ethod
Recursion tree
M ethod M as te r
Method
TOPICS TO BE
COVERED
1. Recurrences
Substitution Method
Recursion Tree
Method
Master Method
SUBSTITUTION METHOD
A(n) First find out recursive
{ function
if(n > 1 )
{
A(n-1)
}
}
SUBSTITUTION METHOD
A(n) T(n) = C +
{ T(n-1)
if(n > 1 )
{
A(n-1)
}
}
SUBSTITUTION METHOD
A(n) T(n) = C +
{ T(n-1)
if(n > 1 ) Back substitution method
{
A(n-1)
}
}
SUBSTITUTION METHOD
A(n) T(n) = C +
{ T(n-1)
if(n > 1 ) Back substitution method
{
A(n-1)
} It can be applied to all type
} of problems
Slo
w
SUBSTITUTION METHOD
A(n) T(n) = C +
{ T(n-1)
if(n > 1 ) Replace n by (n-
{
1)
A(n-1)
}
}
SUBSTITUTION METHOD
A(n) T(n) = C +
{ T(n-1)
if( )
T(n-1) = C + T(n- Replace n by (n-
{
1-1) 1)
A(n-1)
}
}
SUBSTITUTION METHOD
A(n) T(n) = C +
{ T(n-1)
if(n > 1 )
T(n-1) = C + Replace n by (n-
{
T(n-2) 1)
A(n-1)
}
}
SUBSTITUTION METHOD
A(n) T(n) = C +
{ T(n-1)
if(n > 1 )
T(n-1) = C + Replace n by (n-
{
T(n-2) 1)
A(n-1)
} T(n-2) = C + Replace n by (n-
} T(n-3) 2)
SUBSTITUTION METHOD
A(n) T(n) = C + T(n-1)
{ --- (1)
if(n > 1 ) T(n-1) = C + T(n-2)
{ --- (2)
A(n-1) T(n-2) = C + T(n-3) ---
} (3)
}
SUBSTITUTION METHOD
A(n) T(n) = C + T(n-1)
{ --- (1)
if(n > 1 ) T(n-1) = C + T(n-2)
{ --- (2)
A(n-1) T(n-2) = C + T(n-3) ---
} (3)
}
Now substitute eqn2 in
eqn1
SUBSTITUTION METHOD
A(n) T(n) = C T(n- ---
{ + 1) (1)
if(n > 1 )
{
A(n-1)
T(n-1) = C + T(n-2)
--- (2)
}
} T(n-2) = C + T(n-3) ---
(3)
Now substitute eqn2 in
eqn1
SUBSTITUTION METHOD
A(n) T(n) = C + C + T(n-
{ 2)--- (1)
if(n > 1 )
{
A(n-1)
T(n-1) = C + T(n-2)
--- (2)
}
} T(n-2) = C + T(n-3) ---
(3)
Now substitute eqn2 in
eqn1
SUBSTITUTION METHOD
A(n) T(n) = 2C+ T(n-2)---
{ (1)
if(n > 1 )
{
A(n-1)
T(n-1) = C + T(n-2)
--- (2)
}
} T(n-2) = C + T(n-3) ---
(3)
Now substitute eqn3 in
eqn1
SUBSTITUTION METHOD
A(n) T(n) = 2C+ T(n-2)---
{ (1)
if(n > 1 )
{
A(n-1)
T(n-1) = C + T(n-2)
--- (2)
}
} T(n-2) = C + T(n-3) ---
(3)
Now substitute eqn3 in
eqn1
SUBSTITUTION METHOD
A(n) T(n) = 2C+ C + T(n-
{ 3)--- (1)
if(n > 1 )
{
A(n-1)
T(n-1) = C + T(n-2)
--- (2)
}
} T(n-2) = C + T(n-3) ---
(3)
Now substitute eqn3 in
eqn1
SUBSTITUTION METHOD
A(n) T(n) = 3C + T(n-
{ 3)--- (1)
if(n > 1 )
{ Like this it will go till k
A(n-1) number
}
}
SUBSTITUTION METHOD
A(n) T(n) = 3C +
{ T(n-3)
if(n > 1 )
{ Like this it will go till k
A(n-1) number
}
} T(n) = kC +
T(n-k)
SUBSTITUTION METHOD
A(n) T(n) = 3C +
{ T(n-3)
.
if(n > 1 ) .
.
{
T(n) = kC +
A(n-1) T(n-k)
} Now the next point is where to stop
So for this we have to find base
}
condition
SUBSTITUTION METHOD
A(n) T(n) = 3C +
{ T(n-3)
.
if(n > 1 ) .
.
{
T(n) = kC +
A(n-1) T(n-k)
} The algorithm will execute when n is greater
than 1 It will stop when n becomes 1
}
SUBSTITUTION METHOD
A(n) T(n) = 3C +
{ T(n-3)
.
if(n > 1 ) .
.
{
T(n) = kC +
A(n-1) T(n-k)
} T(n) = C + T(n-1)
for n>1
}
=1 for n=1
SUBSTITUTION METHOD
A(n) T(n) = 3C +
{ T(n-3)
.
if(n > 1 ) .
.
{
T(n) = kC +
A(n-1) T(n-k)
} So this algorithm will stop when n-k =
1
}
SUBSTITUTION METHOD
A(n) T(n) = 3C +
{ T(n-3)
.
if(n > 1 ) .
.
{
T(n) = kC +
A(n-1) T(n-k)
} So this algorithm will stop when n-k =
1
}
n-k =
1
SUBSTITUTION METHOD
A(n) T(n) = 3C +
{ T(n-3)
.
if(n > 1 ) .
.
{
T(n) = kC +
A(n-1) T(n-k)
} So this algorithm will stop when n-k =
1
}
n-k =
1 k= n
-1
SUBSTITUTION METHOD
A(n) T(n) = 3C +
{ T(n-3)
.
if(n > 1 ) .
.
{
T(n) = kC +
A(n-1) T(n-k)
} So this algorithm will stop when n-k =
1
}
n-k =
1 k= n
-1
Substitute k in equation
SUBSTITUTION METHOD
A(n) T(n) = 3C +
{ T(n-3)
.
if(n > 1 ) ..
T(n) = kC +
{
T(n-k)
A(n-1)
}
}
SUBSTITUTION METHOD
A(n) T(n) = 3C +
{ T(n-3)
.
if(n > 1 ) ..
T(n) = (n-1)C + T(n-
{
n-1)
A(n-1)
}
}
SUBSTITUTION METHOD
A(n) T(n) = 3C + T(n-3)
.
{ .
.
if(n > 1 )
T(n) = (n-1)C + T(n-
{
n-1)
A(n-1)
} T(n) = (n-1)C + T(n-
}
n-1)
SUBSTITUTION METHOD
A(n) T(n) = 3C + T(n-3)
.
{ .
.
if(n > 1 )
T(n) = (n-1)C + T(n-
{
n-1)
A(n-1)
} T(n) = (n-1)C + T(n-
}
n-1)
T(n) = (n-1)C + T(1)
SUBSTITUTION METHOD
A(n) T(n) = 3C + T(n-3)
.
{ .
.
if(n > 1 )
T(n) = (n-1)C + T(n-
{
n-1)
A(n-1)
} T(n) = (n-1)C + T(n-
}
n-1)
T(n) = (n-1)C + T(1)
T(n) = (n-1)
SUBSTITUTION METHOD
A(n) T(n) = 3C + T(n-3)
.
{ .
.
if(n > 1 )
T(n) = (n-1)C + T(n-
{
n-1)
A(n-1)
} T(n) = (n-1)C + T(n-
}
n-1)
T(n) = (n-1)C + T(1)
T(n) = (n-1)
T(n) = O(n)
SUBSTITUTION METHOD
Example
T(n) = n + ;n>
T(n-1) 1
=1 ;n=
1
SUBSTITUTION METHOD
T(n) = n + ;n> ----
T(n-1) 1 (1)
=1 ;n=
1
SUBSTITUTION METHOD
T(n) = n + ;n> ----
T(n-1) 1 (1)
=1 ;n=
1
Substitute n by (n-
1)
SUBSTITUTION METHOD
T(n) = n + ;n> ----
T(n-1) 1 (1)
=1 ;n=
1
T(n-1) = (n-1) + ----(2)Substitute n by
T(n-2) (n-1)
SUBSTITUTION METHOD
T(n) = n + ;n> ----
T(n-1) 1 (1)
=1 ;n=
1
T(n-1) = (n-1) + ----(2)Substitute n by
T(n-2) (n-1)
T(n-2) = (n-2) + ----(3)Substitute n by
T(n-3) (n-2)
SUBSTITUTION METHOD
T(n) = n + ;n> ---- T(n) = n +
T(n-1) 1 (1) T(n-1)
=1 ;n=
1
T(n-1) = (n-1) + ----
T(n-2) (2)
T(n/2) T(n/2)
RECURSION TREE METHOD
𝑛
2
T(n) = 2T( ;n>
= ) 1
+c c ;n=
1 c
c c
RECURSION TREE METHOD
𝑛
2
T(n) = 2T( ;n>
= ) 1
+c c ;n=
1
c
c c
c c
T(1
)
RECURSION TREE METHOD
𝑛
T(n) = 2T( ;n> Total amount of work done at each
)
2
+c 1 level
;n=
=c
1 c
c c
T(1
)
RECURSION TREE METHOD
𝑛
T(n) = 2T( ;n> Total amount of work done at each
)
2
+c 1 level
;n=
=c
1 c C
c c
T(1
)
RECURSION TREE METHOD
𝑛
T(n) = 2T( ;n> Total amount of work done at each
)
2
+c 1 level
;n=
=c
1 c C
c c 2C
T(1
)
RECURSION TREE METHOD
𝑛
T(n) = 2T( ;n> Total amount of work done at each
)
2
+c 1 level
;n=
=c
1 c C
c c 2C
T(1
)
RECURSION TREE METHOD
𝑛
T(n) = 2T( ;n> Total amount of work done at each
)
2
+c 1 level
;n=
=c
1 c C
c c 2C
nC
T(1
)
𝑛 RECURSION TREE METHOD
T(n) = 2T( ;n>
) Then add the total work done at each
2
+c 1 level
;n=
=c
1 c C
c c 2C
nC
T(1
)
RECURSION TREE METHOD
𝑛
T(n) = 2T( ;n>
) Then add the total work done at each
2
+c 1 level
;n=
=c
c 1 C
= c + 2c + 4c +
nc
c c 2C
nC
T(1
)
RECURSION TREE METHOD
𝑛
T(n) = 2T( ;n>
) Then add the total work done at each
2
+c 1 level
;n=
=c
c 1 C
= c + 2c + 4c + …
+ nc
c c 2C
= c (1 + 2 + 4 + ...
+ n)
(n/ (n/ (n/4 (n/ 4C
4) 4) 4)
nC
T(1
)
RECURSION TREE METHOD
𝑛
T(n) = 2T( ;n>
) Then add the total work done at each
2
+c 1 level
;n=
=c
c 1 C
= c + 2c + 4c + …
+ nc
c c 2C
= c (1 + 2 + 4 + ...
+ n)
(n/ (n/ (n/4 (n/ 4C
= c (1+ 2 + 4 + ... +
4) 4) 4)
n)
nC
T(1
)
RECURSION TREE METHOD
𝑛
T(n) = 2T( ;n>
) Then add the total work done at each
2
+c 1 level
;n=
=c
c 1 C
= c + 2c + 4c + … +
nc
c c 2C
= c (1 + 2 + 4 + ...
+ n)
(n/ (n/ (n/4 (n/ 4C
= c (1+ 2 + 4 + ... +
4) 4) 4)
n)
= 𝐜( 20 +21 + 22 + ⋯
+
nC
T(1 2𝑘 )
)
RECURSION TREE METHOD
𝑛
T(n) = 2T( ;n>
) Then add the total work done at each
2
+c 1 level
;n=
=c
c 1 C
= c + 2c + 4c + … +
nc
c c 2C
= c (1 + 2 + 4 + ...
+ n)
(n/ (n/ (n/4 (n/ 4C
= c (1+ 2 + 4 + ... +
4) 4) 4)
n)
= 𝐜( 20 +21 + 22 + ⋯
+
nC
2𝑘 )
=c( 2𝑘+1 − 1)
T(1
)
RECURSION TREE METHOD
𝑛
T(n) = 2T( ;n>
) Then add the total work done at each
2
+c 1 level
;n=
=c
c 1 C
= c + 2c + 4c + … +
nc
c c 2C
= c (1 + 2 + 4 + ...
+ n)
(n/ (n/ (n/4 (n/ 4C
= c (1+ 2 + 4 + ... +
4) 4) 4)
n)
= 𝐜( 20 +21 + 22 + ⋯
+
nC
2𝑘 )
=c( 2𝑘+1 − 1)
T(1
)
RECURSION TREE METHOD
𝑛
T(n) = 2T( ;n>
) Then add the total work done at each
2
+c 1 level
;n=
=c
c 1 C
= c + 2c + 4c + … +
nc
c c 2C
= c (1 + 2 + 4 + ...
+ n)
(n/ (n/ (n/4 (n/ 4C
= c (1+ 2 + 4 + ... +
4) 4) 4)
n)
= 𝐜( 20 +21 + 22 + ⋯
+
nC
2𝑘 )
=c( 2𝑘+1 − 1)
T(1
)
RECURSION TREE METHOD
𝑛
T(n) = 2T( ;n>
) Then add the total work done at each
2
+c 1 level
;n=
=c
c 1 C
= c + 2c + 4c + … +
nc
c c 2C
= c (1 + 2 + 4 + ...
+ n)
(n/ (n/ (n/4 (n/ 4C
= c (1+ 2 + 4 + ... +
4) 4) 4)
n)
= 𝐜( 20 +21 + 22 + ⋯
+
nC
2𝑘 )
=c( 2𝑘+1 − 1)
T(1
)
RECURSION TREE METHOD
𝑛
T(n) = 2T( ;n>
) Then add the total work done at each
2
+c 1 level
;n=
=c
c 1 C
= c + 2c + 4c + … +
nc
c c 2C
= c (1 + 2 + 4 + ...
+ n)
(n/ (n/ (n/4 (n/ 4C
= c (1+ 2 + 4 + ... +
4) 4) 4)
n)
= 𝐜( 20 +21 + 22 + ⋯
+
nC
2𝑘 )
=c( 2𝑘+1 − 1)
T(1
)
TOPICS TO BE
1. Recurrences
COVERED
Substitution Method
Recursion Tree
Method
Master Method
TOPICS TO BE
COVERED
1. Recurrences
Substitution Method
Recursion Tree
Method
Master Method
MASTER METHOD
The master method provides a "cookbook" method for solving
recurrences of the form
𝑛
𝑇 𝑛 = 𝑎𝑇 +
𝑓 𝑛
�
�
where a ≥ 1 an d b > 1 are constants and f (n) is an asymptotically
positive
function
Master Theorem:
1, the function 𝑛 lo g 𝑏 𝑎
solution is 𝑇
If, as in case is the larger th an f(n) , then
= 𝜃
𝑛
the
If, as in case 𝑛 log
3, the𝑏 𝑎 function f (n) is the larger th an 𝑛 lo g 𝑏 𝑎 ,
then the
solution is T (n) = θ(f (n)).
Master Theorem:
(example-1)
𝐓 𝐧 = 𝟗𝐓 𝐧Τ𝟑+ 𝐧
MASTER METHOD
Master Theorem:
(example-1)
𝐓 𝐧 = 𝟗𝐓 𝐧Τ𝟑+ 𝐧
𝐓 𝐧 = 𝟗 𝐓 𝐧Τ𝟑 +𝐧
𝐓 𝐧 = 𝟗 𝐓 𝐧Τ𝟑 +𝐧
T(n) = a T(n/b) +
f(n), a = 9 , b =
3, f(n) = n
MASTER METHOD
Master Theorem:
for some constant ϵ>0, then 𝑇
𝐓 𝐧 = 𝟗 𝐓 𝐧Τ𝟑
(example-1)
= O 𝑛 log 𝑏
𝑓 𝑛 𝑛 =
1. If
+𝐧
𝑎−𝛜
𝜃
𝑎𝑓 𝑛Τ𝑏≤ 𝑐𝑓 𝑛
f(n) = n
a n d if 𝑎+𝜖
+𝐧 𝑇 𝑛
= 𝜃 𝐚−𝝐
large n, then
𝑇 𝑛 = 𝜃𝑓 𝑛
MASTER METHOD
Master Theorem:
+𝐧 𝑇 𝑛
= 𝜃 𝐚−𝝐
large n, then
𝐧𝐥𝐨𝐠𝐛 𝐚 𝑇 𝑛 = 𝜃𝑓 𝑛
MASTER METHOD
Master Theorem:
+𝐧 𝑇 𝑛
= 𝜃 𝐚−𝝐
+𝐧 𝑇 𝑛
= 𝜃 𝐚−𝝐
large n, then
𝐧𝟐 𝑇 𝑛 = 𝜃𝑓 𝑛
MASTER METHOD
Master Theorem:
+𝐧 𝑇 𝑛
= 𝜃 𝐚−𝝐
large n, then
𝐧𝟐 𝑇 𝑛 = 𝜃𝑓 𝑛
𝒕𝒉𝒆 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏
𝐧𝐥𝐨𝐠𝐛 𝐚
is larger than
f(n)
MASTER METHOD
Master Theorem:
for some constant ϵ>0,
𝐓 𝐧 = 𝟗 𝐓 𝐧Τ𝟑
(example-1)
= 𝐎 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛
1. If
+𝐧 𝑇 𝑛
= 𝜃 𝐚−𝝐
large n, then
𝐧𝟐 𝑇 𝑛 = 𝜃𝑓 𝑛
𝒕𝒉𝒆 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏
𝐧𝐥𝐨𝐠𝐛 𝐚
is larger than
𝒕𝒉𝒂𝒕 𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏 𝒊𝒔
MASTER METHOD
Master Theorem:
+𝐧
𝐚−𝝐
= 𝛉
𝐧
T(n) = a T(n/b) +
then
𝒏𝒍𝒐𝒈𝒃 𝒂 then 𝑇 𝑛 =𝜃
f(n), 𝑓 𝑛
= 𝜃 𝐧𝐥𝐨𝐠𝐛
𝑛 log 𝑏 𝑎 lg 𝑛
2. If
𝐚
a = 9 , b = 3, 3. If 𝑓 𝑛 = 𝛺 for some constant ϵ > 0
f(n) = n if 𝑎𝑓 𝑛Τ𝑏 ≤ 𝑐𝑓 𝑛
𝒇𝒊𝒏𝒅 𝐧𝐥𝐨𝐠𝐛 𝐚 =
and
𝐧 𝐥𝐨𝐠𝐛 𝐚+𝛜
for some constant c < 1 and all sufficiently
large n, then
𝐧𝟐 𝑇 𝑛 = 𝜃𝑓 𝑛
𝒕𝒉𝒆 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏
𝐧𝐥𝐨𝐠𝐛 𝐚
is larger than
𝒕𝒉𝒂𝒕 𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏 𝒊𝒔
MASTER METHOD
Master Theorem:
𝑓 𝑛 𝑛 log 𝑏 𝑎 lg 𝑛
2. If
a = 9 , b = 3,
𝐚
3. If 𝑓 𝑛 = 𝛺 for some constant ϵ > 0
f(n) = n if 𝑎𝑓 𝑛Τ𝑏 ≤ 𝑐𝑓 𝑛
𝒇𝒊𝒏𝒅 𝐧𝐥𝐨𝐠𝐛 𝐚 =
and
𝐧 𝐥𝐨𝐠𝐛 𝐚+𝛜
for some constant c < 1 and all sufficiently
𝐧𝟐
large n, then
𝒕𝒉𝒆 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏
𝑇 𝑛 = 𝜃𝑓 𝑛
𝐧𝐥𝐨𝐠𝐛 𝐚
is larger than
𝒕𝒉𝒂𝒕 𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏 𝒊𝒔
Master Theorem:
+𝐧
𝐚−𝝐
= 𝛉
𝐧
T(n) = a T(n/b) +
then
𝒏𝒍𝒐𝒈𝒃 𝒂 then 𝑇 𝑛 =𝜃
f(n), 𝑓 𝑛
= 𝜃 𝐧𝐥𝐨𝐠𝐛
𝑛 log 𝑏 𝑎 lg 𝑛
2. If
𝐚
a = 9 , b = 3, 3. If 𝑓 𝑛 = 𝛺 for some constant ϵ > 0
f(n) = n if 𝑎𝑓 𝑛Τ𝑏 ≤ 𝑐𝑓 𝑛
𝒇𝒊𝒏𝒅 𝐧𝐥𝐨𝐠𝐛 𝐚 =
and
𝐧 𝐥𝐨𝐠𝐛 𝐚+𝛜
for some constant c < 1 and all sufficiently
large n, then
𝐧𝟐 𝑇 𝑛 = 𝜃𝑓 𝑛
𝒕𝒉𝒆 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏
𝐧𝐥𝐨𝐠𝐛 𝐚
is larger than
𝒕𝒉𝒂𝒕 𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏 𝒊𝒔
Master Theorem:
(example-2)
𝐓 𝐧 = 𝑻 𝟐 𝒏Τ𝟑 +𝟏
Master Theorem:
(example-2) MASTER METHOD
𝐓 𝐧 = 𝑻 𝟐 𝒏Τ𝟑 +𝟏
𝐓 𝐧 = 𝑻 𝟐 𝒏Τ𝟑 +𝟏
𝐓 𝐧 = 𝑻 𝟐 𝒏Τ𝟑 +𝟏
a = 1 , b = 3/2, f(n) = 1
Master Theorem:
MASTER METHOD
for some constant ϵ>0, then 𝑇
𝐓 𝐧 = 𝑻 𝟐 𝒏Τ𝟑
(example-2)
= O 𝑛 log 𝑏
𝑓 𝑛 𝑛 =
1. If
+𝟏
𝑎−𝛜
𝜃
𝑛 log 𝑏 𝑎
=𝜃 then 𝑇 𝑛 = 𝜃 𝑛 log 𝑏 𝑎
𝑓 𝑛 lg 𝑛
2. If
𝑎𝑓 𝑛Τ𝑏≤ 𝑐𝑓 𝑛
f(n) = 1
a n d if 𝑎+𝜖
+𝟏 𝑇 𝑛
= 𝜃 𝐚−𝝐
then 𝑇 𝑛 =𝜃
then
2. If 𝑛 log 𝑏 𝑎
= 𝜃 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛 𝑛 log 𝑏 𝑎 lg 𝑛
T(n) = a T(n/b) + f 𝐚
(an),
= 1 , b = 3/2, 3. If 𝑓 𝑛 = 𝛺 for some constant ϵ > 0
f(n) = 1 if 𝑎𝑓 𝑛Τ𝑏 ≤ 𝑐𝑓 𝑛
and
𝐧 constant c 1 and all sufficiently
for some
𝐥𝐨𝐠𝐛 𝐚+𝛜
large n, then
𝑇 𝑛 = 𝜃𝑓 𝑛
Master Theorem:
MASTER METHOD
for some constant ϵ>0,
𝐓 𝐧 = 𝑻 𝟐 𝒏Τ𝟑
(example-2)
= 𝐎 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛
1. If
+𝟏 𝑇 𝑛
= 𝜃 𝐚−𝝐
then 𝑇 𝑛 =𝜃
then
2. If 𝑛 log 𝑏 𝑎
= 𝜃 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛 𝑛 log 𝑏 𝑎 lg 𝑛
T(n) = a T(n/b) + f 𝐚
(an),
= 1 , b = 3/2, 3. If 𝑓 𝑛 = 𝛺 for some constant ϵ > 0
f(n) = 1 if 𝑎𝑓 𝑛Τ𝑏 ≤ 𝑐𝑓 𝑛
and
𝐧 constant c 1 and all sufficiently
𝒇𝒊𝒏𝒅
𝐥𝐨𝐠 𝐚+𝛜
for some
𝐛
large n, then
𝐧𝐥𝐨𝐠𝐛 𝐚 𝑇 𝑛 = 𝜃𝑓 𝑛
Master Theorem:
MASTER METHOD
for some constant
𝐓 𝐧 = 𝑻 𝟐 𝒏Τ𝟑
(example-2)
= 𝐎 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛
1. If the
+𝟏 𝑇 𝑛
= 𝜃 𝐚−𝝐
ϵ>0, n
= 𝜃 𝐧𝐥𝐨𝐠𝐛 then 𝑇 𝑛= 𝜃 𝑛
2. If 𝑛 log 𝑏 𝑎 log 𝑏 𝑎
𝑓 𝑛 lg 𝑛
T(n) = a T(n/b) + f 𝐚
(an),
= 1 , b = 3/2, 3. If 𝑓 𝑛 = 𝛺 for some constant ϵ > 0
f(n) = 1 if 𝑎𝑓 𝑛Τ𝑏
𝒇𝒊𝒏𝒅 𝐥𝐨𝐠
𝐧�
for some constant
𝐛 𝐚+𝛜 c < 1 and all sufficiently
and
𝐧𝐥𝐨𝐠
𝐥𝐨𝐠𝟑/𝟐 large n, then
𝐚 � 𝑐𝑓 𝑛
= 𝐧
≤
𝟏 𝑇 𝑛 = 𝜃𝑓 𝑛
Master Theorem:
MASTER METHOD
for some constant
𝐓 𝐧 = 𝑻 𝟐 𝒏Τ𝟑
(example-2)
= 𝐎 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛
1. If the
+𝟏 𝑇 𝑛
= 𝜃 𝐚−𝝐
ϵ>0, n
= 𝜃 𝐧𝐥𝐨𝐠𝐛 then 𝑇 𝑛= 𝜃 𝑛
2. If 𝑛 log 𝑏 𝑎 log 𝑏 𝑎
𝑓 𝑛 lg 𝑛
T(n) = a T(n/b) + f 𝐚
(an),
= 1 , b = 3/2, 3. If 𝑓 𝑛 = 𝛺 for some constant ϵ > 0
f(n) = 1 if 𝑎𝑓 𝑛Τ𝑏
𝒇𝒊𝒏𝒅 𝐥𝐨𝐠𝐛 =
for some constant
𝐛 𝐚+𝛜 c < 1 and all sufficiently
and
𝐧𝐥𝐨𝐠
� large n, then
𝐧
𝑐𝑓 𝑛
≤
𝐚 𝒏
� 𝑇 𝑛 = 𝜃𝑓 𝑛
Master Theorem:
MASTER METHOD
for some constant ϵ>0,
𝐓 𝐧 = 𝑻 𝟐 𝒏Τ𝟑
(example-2)
= 𝐎 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛
1. If
+𝟏 𝑇 𝑛
= 𝜃 𝐚−𝝐
then 𝑇 𝑛 =𝜃
then
2. If 𝑛 log 𝑏 𝑎
= 𝜃 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛 𝑛 log 𝑏 𝑎 lg 𝑛
T(n) = a T(n/b) + f 𝐚
(an)= 1 , b = 3/2,
3. If 𝑓 𝑛 = 𝛺 for some constant ϵ > 0
f(n) = 1 if 𝑎𝑓 𝑛Τ𝑏 ≤ 𝑐𝑓 𝑛
𝐧
large n, then
𝐚 =𝟏 𝑇 𝑛 = 𝜃𝑓 𝑛
Master Theorem:
MASTER METHOD
for some constant ϵ>0,
𝐓 𝐧 = 𝑻 𝟐 𝒏Τ𝟑
(example-2)
= 𝐎 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛
1. If
+𝟏 𝑇 𝑛
= 𝜃 𝐚−𝝐
then 𝑇 𝑛 =𝜃
then
2. If 𝑛 log 𝑏 𝑎
= 𝜃 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛 𝑛 log 𝑏 𝑎 lg 𝑛
T(n) = a T(n/b) + f 𝐚
(an),
= 1 , b = 3/2, 3. If 𝑓 𝑛 = 𝛺 for some constant ϵ > 0
f(n) = 1 if 𝑎𝑓 𝑛Τ𝑏 ≤ 𝑐𝑓 𝑛
𝒇𝒊𝒏𝒅 𝐧𝐥𝐨𝐠𝐛 𝐚 ��
and
𝐧 constant c < 1 and all sufficiently
𝐥𝐨𝐠 𝐚+𝛜
=𝒏
for some
𝐛
large n, then
𝑇 𝑛 = 𝜃𝑓 𝑛
𝒕𝒉𝒆 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏
=𝟏
𝐧𝐥𝐨𝐠𝐛 𝐚
is equal to
f(n)
Master Theorem:
MASTER METHOD
for some constant ϵ>0,
𝐓 𝐧 = 𝑻 𝟐 𝒏Τ𝟑
(example-2)
= 𝐎 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛
1. If
+𝟏 𝑇 𝑛
= 𝜃 𝐚−𝝐
then 𝑇 𝑛 =𝜃
then
2. If 𝑛 log 𝑏 𝑎
= 𝜃 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛 𝑛 log 𝑏 𝑎 lg 𝑛
T(n) = a T(n/b) + f 𝐚
(an),
= 1 , b = 3/2, 3. If 𝑓 𝑛 = 𝛺 for some constant ϵ > 0
f(n) = 1 if 𝑎𝑓 𝑛Τ𝑏 ≤ 𝑐𝑓 𝑛
𝒇𝒊𝒏𝒅 𝐧𝐥𝐨𝐠𝐛 𝐚 ��
and
𝐧 constant c < 1 and all sufficiently
𝐥𝐨𝐠 𝐚+𝛜
=𝒏
for some
𝐛
large n, then
𝑇 𝑛 = 𝜃𝑓 𝑛
𝒕𝒉𝒆 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏
= 𝐥𝐨𝐠
𝟏 𝐚
𝐧 𝐛
is equal to
𝒕𝒉𝒂𝒕 𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏 𝒊𝒔
Master Theorem:
MASTER METHOD
for some constant ϵ>0,
𝐓 𝐧 = 𝑻 𝟐 𝒏Τ𝟑
(example-2)
= 𝐎 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛𝐓
1. If
+𝟏
𝐚−𝝐
= 𝛉
𝐧 then
𝒏𝒍𝒐𝒈𝒃 𝒂
= 𝛉 𝐧𝐥𝐨𝐠𝐛
𝐭𝐡𝐞𝐧 𝐓 𝐧 =
𝐟 𝐧
T(n) = a T(n/b) + f 𝛉 𝐧𝐥𝐨𝐠𝐛 𝐚 𝐥𝐠 𝐧
2. If
𝐚
(an),
= 1 , b = 3/2, 3. If 𝑓 𝑛 = 𝛺 for some constant ϵ > 0
f(n) = 1 if 𝑎𝑓 𝑛Τ𝑏 ≤ 𝑐𝑓 𝑛
𝒇𝒊𝒏𝒅 𝐥𝐨𝐠𝐛 = 𝒏��
and
𝐧 𝐥𝐨𝐠𝐛 𝐚+𝛜
for some constant c < 1 and all sufficiently
𝐧
large n, then
𝐚 =𝟏 𝑇 𝑛 = 𝜃𝑓 𝑛
𝒕𝒉𝒆 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏
𝐧𝐥𝐨𝐠𝐛 𝐚
is equal to
𝒕𝒉𝒂𝒕 𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏 𝒊𝒔
Master Theorem:
MASTER METHOD
for some constant ϵ>0,
𝐓 𝐧 = 𝑻 𝟐 𝒏Τ𝟑
(example-2)
= 𝐎 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛𝐓
1. If
+𝟏
𝐚−𝝐
= 𝛉
𝐧 then
𝒏𝒍𝒐𝒈𝒃 𝒂
= 𝛉 𝐧𝐥𝐨𝐠𝐛
𝐭𝐡𝐞𝐧 𝐓 𝐧 =
𝐟 𝐧
T(n) = a T(n/b) + f 𝛉 𝐧𝐥𝐨𝐠𝐛 𝐚 𝐥𝐠 𝐧
2. If
𝐚
(an),
= 1 , b = 3/2, 3. If 𝑓 𝑛 = 𝛺 for some constant ϵ > 0
f(n) = 1 if 𝑎𝑓 𝑛Τ𝑏 ≤ 𝑐𝑓 𝑛
𝒇𝒊𝒏𝒅 𝐥𝐨𝐠𝐛 = 𝒏��
and
𝐧 𝐥𝐨𝐠𝐛 𝐚+𝛜
for some constant c < 1 and all sufficiently
𝐧
large n, then
𝐚 =𝟏 𝑇 𝑛 = 𝜃𝑓 𝑛
𝒕𝒉𝒆 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏
𝐧𝐥𝐨𝐠𝐛 𝐚
is equal to
𝒕𝒉𝒂𝒕 𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏 𝒊𝒔
Master Theorem:
MASTER METHOD
for some constant ϵ>0,
𝐓 𝐧 = 𝑻 𝟐 𝒏Τ𝟑
(example-2)
= 𝐎 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛𝐓
1. If
+𝟏
𝐚−𝝐
= 𝛉
𝐧 then
𝒏𝒍𝒐𝒈𝒃 𝒂
= 𝛉 𝐧𝐥𝐨𝐠𝐛
𝐭𝐡𝐞𝐧 𝐓 𝐧 =
𝐟 𝐧
T(n) = a T(n/b) + f 𝛉 𝐧𝐥𝐨𝐠𝐛 𝐚 𝐥𝐠 𝐧
2. If
𝐚
(an),
= 1 , b = 3/2, 3. If 𝑓 𝑛 = 𝛺 for some constant ϵ > 0
f(n) = 1 if 𝑎𝑓 𝑛Τ𝑏 ≤ 𝑐𝑓 𝑛
𝒇𝒊𝒏𝒅 𝐥𝐨𝐠𝐛 = 𝒏��
and
𝐧 𝐥𝐨𝐠𝐛 𝐚+𝛜
for some constant c < 1 and all sufficiently
𝐧
large n, then
𝐚 =𝟏 𝑇 𝑛 = 𝜃𝑓 𝑛
𝒕𝒉𝒆 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏
𝐧𝐥𝐨𝐠𝐛 𝐚
is equal to
𝒕𝒉𝒂𝒕 𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏 𝒊𝒔
Master Theorem:
MASTER METHOD
for some constant ϵ>0,
𝐓 𝐧 = 𝑻 𝟐 𝒏Τ𝟑
(example-2)
= 𝐎 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛𝐓
1. If
+𝟏
𝐚−𝝐
= 𝛉
𝐧 then
𝒏𝒍𝒐𝒈𝒃 𝒂
= 𝛉 𝐧𝐥𝐨𝐠𝐛
𝐭𝐡𝐞𝐧 𝐓 𝐧 =
𝐟 𝐧
T(n) = a T(n/b) + f 𝛉 𝐧𝐥𝐨𝐠𝐛 𝐚 𝐥𝐠 𝐧
2. If
𝐚
(an),
= 1 , b = 3/2, 3. If 𝑓 𝑛 = 𝛺 for some constant ϵ > 0
f(n) = 1 if 𝑎𝑓 𝑛Τ𝑏 ≤ 𝑐𝑓 𝑛
𝒇𝒊𝒏𝒅 𝐥𝐨𝐠𝐛 = 𝒏��
and
𝐧 𝐥𝐨𝐠𝐛 𝐚+𝛜
for some constant c < 1 and all sufficiently
𝐧
large n, then
𝐚 =𝟏 𝑇 𝑛 = 𝜃𝑓 𝑛
𝒕𝒉𝒆 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏
𝐧𝐥𝐨𝐠𝐛 𝐚
is equal to
𝒕𝒉𝒂𝒕 𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏 𝒊𝒔
Master Theorem:
MASTER METHOD
for some constant ϵ>0,
𝐓 𝐧 = 𝑻 𝟐 𝒏Τ𝟑
(example-2)
= 𝐎 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛𝐓
1. If
+𝟏
𝐚−𝝐
= 𝛉
𝐧 then
𝒏𝒍𝒐𝒈𝒃 𝒂
= 𝛉 𝐧𝐥𝐨𝐠𝐛
𝐭𝐡𝐞𝐧 𝐓 𝐧 =
𝐟 𝐧
T(n) = a T(n/b) + f 𝛉 𝐧𝐥𝐨𝐠𝐛 𝐚 𝐥𝐠 𝐧
2. If
𝐚
(an),
= 1 , b = 3/2, 3. If 𝑓 𝑛 = 𝛺 for some constant ϵ > 0
f(n) = 1 if 𝑎𝑓 𝑛Τ𝑏 ≤ 𝑐𝑓 𝑛
𝒇𝒊𝒏𝒅 𝐥𝐨𝐠𝐛 = 𝒏��
and
𝐧 𝐥𝐨𝐠𝐛 𝐚+𝛜
for some constant c < 1 and all sufficiently
𝐧
large n, then
𝐚 =𝟏 𝑇 𝑛 = 𝜃𝑓 𝑛
𝒕𝒉𝒆 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏
𝐧𝐥𝐨𝐠𝐛 𝐚
is equal to
𝒕𝒉𝒂𝒕 𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏 𝒊𝒔
Master Theorem:
(example-3) MASTER METHOD
𝐓 𝐧 = 𝟑𝑻 𝒏Τ𝟒+ 𝒏 𝐥𝐠 𝒏
Master Theorem:
(example-3) MASTER METHOD
𝐓 𝐧 = 𝟑𝑻 𝒏Τ𝟒+ 𝒏 𝐥𝐠 𝒏
𝐓 𝐧 = 𝟑𝑻 𝒏Τ𝟒+ 𝒏 𝐥𝐠 𝒏
𝐓 𝐧 = 𝟑𝑻 𝒏Τ𝟒+ 𝒏 𝐥𝐠 𝒏
a = 3 , b = 4 , f(n) = 𝒏 𝐥𝐠 𝒏
Master Theorem:
MASTER METHOD
for some constant ϵ>0, then 𝑇
𝐓 𝐧 = 𝟑𝑻 𝒏Τ𝟒+ 𝒏
(example-3)
= O 𝑛 log 𝑏
𝑓 𝑛 𝑛 =
1. If
𝐥𝐠 𝒏
𝑎−𝛜
𝜃
𝑎𝑓 𝑛Τ𝑏≤
= 𝒏 𝐥𝐠 𝒏
𝑎+𝜖
for some constant c 1 and all sufficiently
a n d if
𝑐𝑓 𝑛
large n, then
𝑇 𝑛 = 𝜃𝑓 𝑛
Master Theorem:
MASTER METHOD
for some constant ϵ>0,
𝐓 𝐧 = 𝟑𝑻 𝒏Τ𝟒+ 𝒏
(example-3)
= 𝐎 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛
1. If
𝐥𝐠 𝒏 𝑇 𝑛
= 𝜃 𝐚−𝝐
large n, then
𝑇 𝑛 = 𝜃𝑓 𝑛
Master Theorem:
MASTER METHOD
for some constant ϵ>0,
𝐓 𝐧 = 𝟑𝑻 𝒏Τ𝟒+ 𝒏
(example-3)
= 𝐎 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛
1. If
𝐥𝐠 𝒏 𝑇 𝑛
= 𝜃 𝐚−𝝐
large n, then
𝐧𝐥𝐨𝐠𝐛 𝐚 𝑇 𝑛 = 𝜃𝑓 𝑛
Master Theorem:
MASTER METHOD
for some constant ϵ>0,
𝐓 𝐧 = 𝟑𝑻 𝒏Τ𝟒+ 𝒏
(example-3)
= 𝐎 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛
1. If
𝐥𝐠 𝒏 𝑇 𝑛
= 𝜃 𝐚−𝝐
𝒇𝒊𝒏𝒅 𝐥𝐨𝐠𝐧 𝐛𝐚
and
𝐧 constant c < 1 and all sufficiently
for some
𝐥𝐨𝐠𝐛 𝐚+𝛜
= 𝐧
large n, then
𝐥𝐨𝐠𝟒 𝟑 𝑇 𝑛 = 𝜃𝑓 𝑛
Master Theorem:
MASTER METHOD
for some constant ϵ>0,
𝐓 𝐧 = 𝟑𝑻 𝒏Τ𝟒+ 𝒏
(example-3)
= 𝐎 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛
1. If
𝐥𝐠 𝒏 𝑇 𝑛
= 𝜃 𝐚−𝝐
𝒇𝒊𝒏𝒅
and
𝐧 constant c < 1 and all sufficiently
𝐥𝐨𝐠 𝐚+𝛜
𝐥𝐨𝐠 = 𝟎.𝟕𝟗 for some
𝐛
𝐧
𝟑 large n, then
𝐚 𝒏 𝑇 𝑛 = 𝜃𝑓 𝑛
Master Theorem:
MASTER METHOD
for some constant ϵ>0,
𝐓 𝐧 = 𝟑𝑻 𝒏Τ𝟒+ 𝒏
(example-3)
= 𝐎 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛
1. If
𝐥𝐠 𝒏 𝑇 𝑛
= 𝜃 𝐚−𝝐
𝟑 large n, then
=𝒏 𝑇 𝑛 = 𝜃𝑓 𝑛
Master Theorem:
MASTER METHOD
for some constant ϵ>0,
𝐓 𝐧 = 𝟑𝑻 𝒏Τ𝟒+ 𝒏
(example-3)
= 𝐎 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛
1. If
𝐥𝐠 𝒏 𝑇 𝑛
= 𝜃 𝐚−𝝐
𝟑 large n, then
=𝒏 𝑇 𝑛 = 𝜃𝑓 𝑛
𝒕𝒉𝒆 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏f(n) ⅈ𝐬 𝐥𝐚𝐫𝐠𝐞𝐫
𝐭𝐡𝐚𝐧 𝐧𝐥𝐨𝐠𝐛 𝐚
Master Theorem:
MASTER METHOD
for some constant ϵ>0,
𝐓 𝐧 = 𝟑𝑻 𝒏Τ𝟒+ 𝒏
(example-3)
= 𝐎 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛
1. If
𝐥𝐠 𝒏 𝑇 𝑛
= 𝜃 𝐚−𝝐
𝟑 large n, then
=𝒏 𝑇 𝑛 = 𝜃𝑓 𝑛
𝒕𝒉𝒆 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏f(n) ⅈ𝐬 𝐥𝐚𝐫𝐠𝐞𝐫 𝐭𝐡𝐚𝐧 𝐧𝐥𝐨𝐠𝐛 𝐚
𝒔𝒐 𝒘𝒆 𝒄𝒂𝒏 𝒂𝒑𝒑𝒍𝒚 𝒄𝒂𝒔𝒆 − 𝟑 𝒂𝒏𝒅 𝒄𝒐𝒏𝒄𝒍𝒖𝒅𝒆
𝒕𝒉𝒂𝒕 𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏 𝒊𝒔
Master Theorem:
MASTER METHOD
for some constant ϵ>0,
𝐓 𝐧 = 𝟑𝑻 𝒏Τ𝟒+ 𝒏
(example-3)
= 𝐎 𝐧𝐥𝐨𝐠𝐛
𝑓 𝑛𝐓
1. If
𝐥𝐠 𝒏
𝐚−𝝐
= 𝛉
𝐧
T(n) = a T(n/b) + f
then
𝒏𝒍𝒐𝒈𝒃 𝒂 𝐭𝐡𝐞𝐧 𝐓 𝐧 =
(n), 𝐟 𝐧
= 𝛉 𝐧𝐥𝐨𝐠𝐛
𝛉 𝐧𝐥𝐨𝐠𝐛 𝐚 𝐥𝐠 𝐧
2. If
𝐚
a = 3 , b = 4 , f(n) 3. If 𝑓 𝑛 = 𝛺 for some constant ϵ > 0
= 𝒏 𝐥𝐠 𝒏 if 𝑎𝑓 𝑛Τ𝑏 ≤ 𝑐𝑓 𝑛 for some
𝒇𝒊𝒏𝒅 𝐧
and
𝐧c <
𝐥𝐨𝐠 𝐚+𝛜
𝐥𝐨𝐠𝐛 𝐚 = 𝒏𝟎.𝟕𝟗𝟑 constant
𝐛
𝐥𝐠 𝒏
𝐚−𝝐
= 𝛉
𝐧
T(n) = a T(n/b) + f
then
𝒏𝒍𝒐𝒈𝒃 𝒂 𝐭𝐡𝐞𝐧 𝐓 𝐧 =
(n), 𝐟 𝐧
= 𝛉 𝐧𝐥𝐨𝐠𝐛
𝛉 𝐧𝐥𝐨𝐠𝐛 𝐚 𝐥𝐠 𝐧
2. If
𝐚
a = 3 , b = 4 , f(n) 3. If 𝑓 𝑛 = 𝛺 for some constant ϵ > 0
= 𝒏 𝐥𝐠 𝒏 if 𝑎𝑓 𝑛Τ𝑏 ≤ 𝑐𝑓 𝑛 for some
𝒇𝒊𝒏𝒅 𝐧
and
𝐧c <
𝐥𝐨𝐠 𝐚+𝛜
𝐥𝐨𝐠𝐛 𝐚 = 𝒏𝟎.𝟕𝟗𝟑 constant
𝐛
𝐥𝐠 𝒏 = 𝛉
𝐚−𝝐
a = 3 , b = 4 , f(n)
𝐚
3. If 𝑓 𝑛 = 𝛺 for some constant ϵ > 0
= 𝒏 𝐥𝐠 𝒏 if 𝑎𝑓 𝑛Τ𝑏 ≤ 𝑐𝑓 𝑛 for some
𝒇𝒊𝒏𝒅 𝐧𝐥𝐨𝐠𝐛 𝐚 =
and
𝐧c <
constant
𝐥𝐨𝐠𝐛 𝐚+𝛜