0% found this document useful (0 votes)
91 views104 pages

DAA CS203 2021 Module1 Update4

The document outlines the syllabus for the course Design and Analysis of Algorithms (DAA) taught by Dr. Praveen Kumar Alapati at Mahindra University. The syllabus is divided into 5 modules that cover topics such as asymptotic analysis, divide and conquer algorithms, greedy algorithms, dynamic programming, backtracking, NP-hard problems, and multi-threaded algorithms. Assessment includes two minor exams, an end term exam, tutorials, and labs. Reference books and online resources are also listed.

Uploaded by

Praveen Alapati
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)
91 views104 pages

DAA CS203 2021 Module1 Update4

The document outlines the syllabus for the course Design and Analysis of Algorithms (DAA) taught by Dr. Praveen Kumar Alapati at Mahindra University. The syllabus is divided into 5 modules that cover topics such as asymptotic analysis, divide and conquer algorithms, greedy algorithms, dynamic programming, backtracking, NP-hard problems, and multi-threaded algorithms. Assessment includes two minor exams, an end term exam, tutorials, and labs. Reference books and online resources are also listed.

Uploaded by

Praveen Alapati
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/ 104

Design and Analysis of Algorithms

Academic Year: 2020 - 21

Dr. Praveen Kumar Alapati


[email protected]
Department of Computer Science and Engineering
Ecole Centrale School of Engineering

Dr. Praveen (Mahindra University) DAA CS203 1 / 60


DAA: Design and Analysis of Algorithms Syllabus

Module -1
I Introduction
I Properties of Algorithms
I Asymptotic Notations
I Space Complexity and Time Complexity
I Masters Theorem with proof
I Amortized Analysis: Aggregate Method.

Dr. Praveen (Mahindra University) DAA CS203 2 / 60


DAA: Design and Analysis of Algorithms Syllabus

Module -2
I Divide and Conquer General Method
I Finding the Maximum and Minimum
I Defective Chessboard
I Block Matrix Multiplication
I Strassen’s Matrix Multiplication.
I Greedy General Method
I Knapsack Problem
I Single Source Shortest Path Problem
I Optimal Merge Patterns and Huffman codes.

Dr. Praveen (Mahindra University) DAA CS203 3 / 60


DAA: Design and Analysis of Algorithms Syllabus

Module -3
I Dynamic Programming General method
I Matrix Chain Multiplication
I 0/1 Knapsack Problem
I All-pairs Shortest Path Problem
I Travelling Salesperson Problem
I String Editing
I Reliability Design Problem
I Optimal Binary Search Tree.

Dr. Praveen (Mahindra University) DAA CS203 4 / 60


DAA: Design and Analysis of Algorithms Syllabus

Module -4
I Backtracking General Method
I N-Queens Problem
I Sum of Subsets Problem.
I Branch and Bound (BB), Control Abstractions for LCBB,
FIFOBB, and LIFOBB
I 15-Puzzle Problem
Module -5
I P-Space and Exp-Space Problems
I NP-Hard and NP-Complete Problems
I Basics of Dynamic Multi-threading
I Multi-threaded Matrix Multiplication

Dr. Praveen (Mahindra University) DAA CS203 5 / 60


DAA: Design and Analysis of Algorithms Syllabus

Reference Books:
1 Introduction to Algorithms, 3rd edition, T.H.Cormen,
C.E.Leiserson, R.L.Rivest and C.Stein.
2 Fundamentals of Computer Algorithms, Ellis Horowitz, Satraj
Sahni and Rajasekaran.
3 Algorithms, 4th edition, Robert Sedgewick.
4 Design and Analysis of Computer Algorithms, Aho, Ullman, and
Hopcroft.
Web Resources:
1 Algorithms by Robert Sedgewik
2 Algorithms by Abdul Bari
3 MIT - Open Courseware Videos on Algorithms
4 Data Structures and Algorithms

Dr. Praveen (Mahindra University) DAA CS203 6 / 60


DAA: Design and Analysis of Algorithms Syllabus
Minor 1 15%
Minor 2 15%
End Exam 30%
Tutorials 10%
Labs 30%
Table 1: Marks Distribution

Lab Instructor: Sri. Brahmaiah G


Submission Guide Lines:
I Mail-ID: [email protected]
I Sub:TEAMNUM LAB NUM
I Attach.Name and Type: (TEAMNUM LAB NUM).zip
I Late Submission<=3-Days:50%.
I Write a readme file to understand your solutions.
I Submit source files only (C or JAVA).
Dr. Praveen (Mahindra University) DAA CS203 7 / 60
Problem

Write an algorithm to arrange a set of elements in the


ascending order.
1 Selection Sort
2 Merge Sort
3 Bubble Sort
4 Insertion Sort
5 Quick Sort

Dr. Praveen (Mahindra University) DAA CS203 8 / 60


Problem

Write an algorithm to arrange a set of elements in the


ascending order.
1 Selection Sort
2 Merge Sort
3 Bubble Sort
4 Insertion Sort
5 Quick Sort

Dr. Praveen (Mahindra University) DAA CS203 8 / 60


Selection Sort

Dr. Praveen (Mahindra University) DAA CS203 9 / 60


Merge Sort

Dr. Praveen (Mahindra University) DAA CS203 10 / 60


Merge Sort Analysis

Dr. Praveen (Mahindra University) DAA CS203 11 / 60


Analysis of Simple Functions
Sum of all elements in an array A[1:n]
Algorithm 1: Sum(A,n)
Result: Sum of all elements in an array A[1:n]
1 Res = 0
2 for i = 1 to i ≤ n do
3 Res = Res + A[i]
4 return Res;

Algorithm 2: RSum(A,n)
Result: Sum of all elements in an array A[1:n]
1 if n ≤ 0 then return 0;
2 else return A[n] + RSum(A, n − 1);
Recurrence Relation: T (n) = 2 + T (n − 1) and T (0) = 2.
Consider a sequence: 2, 4, 6, 8, ...,(an ), ...
an = an−1 + 2, where a0 = 2.
Dr. Praveen (Mahindra University) DAA CS203 12 / 60
Solving a Simple Recurrence Relation

Solve an = an−1 + 2, where a0 = 2


an = an−1 + 2
an−1 = an−2 + 2
an−2 = an−3 + 2
.
.
.
a2 = a1 + 2
a1 = a0 + 2
Add all the above
an = a0 + 2 × n =⇒ an = 2 × (n + 1).

Dr. Praveen (Mahindra University) DAA CS203 13 / 60


Towers of Hanoi

Dr. Praveen (Mahindra University) DAA CS203 14 / 60


Analysis of Simple Recursive Functions

Towers of Hanoi Problem: There are three towers, namely A, B


and C. Stack of disks on one tower (Say A) in the decreasing order of
size from bottom top. Disks are heavy, so only one can be moved at
a time. One has to move all the disks from Tower A to Tower
B by using Tower C. No larger disk can be placed on the top
of smaller disk.
Algorithm TH(n, A, B, C)
1 if(n≥1){
2 TH(n-1, A, C, B);
3 Move top disk from tower A to B
4 TH(n-1, C, B, A); }
Recurrence Relation: T (n) = 2 × T (n − 1) + 2 and T (0) = 1.

Dr. Praveen (Mahindra University) DAA CS203 15 / 60


Analysis of Simple Recursive Functions

Towers of Hanoi Problem: There are three towers, namely A, B


and C. Stack of disks on one tower (Say A) in the decreasing order of
size from bottom top. Disks are heavy, so only one can be moved at
a time. One has to move all the disks from Tower A to Tower
B by using Tower C. No larger disk can be placed on the top
of smaller disk.
Algorithm TH(n, A, B, C)
1 if(n≥1){
2 TH(n-1, A, C, B);
3 Move top disk from tower A to B
4 TH(n-1, C, B, A); }
Recurrence Relation: T (n) = 2 × T (n − 1) + 2 and T (0) = 1.

Dr. Praveen (Mahindra University) DAA CS203 15 / 60


Analysis of Simple Recursive Functions

Towers of Hanoi Problem: There are three towers, namely A, B


and C. Stack of disks on one tower (Say A) in the decreasing order of
size from bottom top. Disks are heavy, so only one can be moved at
a time. One has to move all the disks from Tower A to Tower
B by using Tower C. No larger disk can be placed on the top
of smaller disk.
Algorithm TH(n, A, B, C)
1 if(n≥1){
2 TH(n-1, A, C, B);
3 Move top disk from tower A to B
4 TH(n-1, C, B, A); }
Recurrence Relation: T (n) = 2 × T (n − 1) + 2 and T (0) = 1.

Dr. Praveen (Mahindra University) DAA CS203 15 / 60


Solving a Simple Recurrence Relation

Solve Recurrence Relation: T (n) = 2 × T (n − 1) + 2 and T (0) = 1.

I T (n) = 2 × T (n − 1) + 2
I T (n) = 2 × [2 × T (n − 2) + 2] + 2 = 22 × T (n − 2) + 22 + 2
I T (n) = 22 × [2 × T (n − 3) + 2] + 22 + 2 = 23 × T (n − 3) + 23 + 22 + 2
.
.
.
I T (n) = 2n × T (n − n) + 2n + 2n−1 + ... + 23 + 22 + 2
2(2n −1)
I T (n) = 2n × T (0) + (2−1)
I T (n) = 2n + 2 × 2n − 2
I T (n) = 3 × 2n − 2

Dr. Praveen (Mahindra University) DAA CS203 16 / 60


Solving a Simple Recurrence Relation
Solve Recurrence Relation: T (n) = T ( n2 ) + 1
where T (1) = 1 and n = 2k .

I T (n) = T ( n2 ) + 1
I T ( n2 ) = T ( n4 ) + 1
I T ( n4 ) = T ( n8 ) + 1
.
.
.
n
I T ( k−1
2
) = T ( nk ) + 1
2
Add all the above
T (n) = T ( 2nk ) + k
T (n) = T ( nn ) + log2 (n)
∴ T (n) = 1 + log2 (n).

Dr. Praveen (Mahindra University) DAA CS203 17 / 60


Solving a Simple Recurrence Relation

Solve Recurrence Relation: T (n) = 2 × T ( n2 ) + 1


Where T (1) = 1 and n = 2k .

I T (n) = 2 × T ( n2 ) + 1
I T (n) = 2 × [2 × T ( n4 ) + 1] + 1 = 22 × T ( 2n2 ) + 21 + 1
I T (n) = 22 × [2 × T ( 2n3 ) + 1] + 21 + 1 = 23 × T ( 2n3 ) + 22 + 21 + 1
.
.
.
I T (n) = 2k × T ( nk ) + 2k−1 + 2k−2 + ... + 22 + 21 + 1
2
(2k −1)
I T (n) = 2k × T (1) + (2−1)
I T (n) = n + n − 1
I T (n) = 2n − 1

Dr. Praveen (Mahindra University) DAA CS203 18 / 60


Solving a Simple Recurrence Relation

Solve Recurrence Relation: T (n) = 2 × T ( n2 ) + n


Where T (1) = 1 and n = 2k .

I T (n) = 2 × T ( n2 ) + n
I T (n) = 2 × [2 × T ( n4 ) + n2 ] + n = 22 × T ( 2n2 ) + 2n
I T (n) = 22 × [2 × T ( 2n3 ) + 2n2 ] + 2n = 23 × T ( 2n3 ) + 3n
.
.
.
I T (n) = 2k × T ( nk ) + kn
2
I T (n) = 2k × T (1) + kn
I T (n) = n + log2 (n) × n

Dr. Praveen (Mahindra University) DAA CS203 19 / 60


Analysis of Simple Recursive Functions

Fibonacci Number( Namely, FibNum)


Alg. FibNum(n)
1 if(n≤1)
2 return n;
3 else
4 return FibNum(n-1)+FibNum(n-2);

Dr. Praveen (Mahindra University) DAA CS203 20 / 60


Analysis of Simple Recursive Functions

Permutation Generator of all elements in an Array A[1:n]. Assume


that all elements are distinct.
Algorithm 3: Perm(A,k,n)
Result: Permutations of all elements in A[1:n]
1 if k==n then
2 Print(A[1:n]);
3 else
4 for i = k to k ≤ n do
5 t=A[k]; A[k]=A[i]; A[i]=t;
6 Perm(A,k+1,n);
7 t=A[k]; A[k]=A[i]; A[i]=t;

Initial call is Perm(A,1,n).

Dr. Praveen (Mahindra University) DAA CS203 21 / 60


Simple Patterns for Analysis

Swapping a and b.
Alg. Swap(a, b)
1 temp = a;
2 a = b;
3 b = temp;
Sum of all elements in an array.
Alg. Sum(A, n)
1 S=0;
2 for (i = 0; i < n; i + +)
3 S = S + A[i];
4 return S;

Dr. Praveen (Mahindra University) DAA CS203 22 / 60


Simple Patterns for Analysis

Swapping a and b.
Alg. Swap(a, b)
1 temp = a;
2 a = b;
3 b = temp;
Sum of all elements in an array.
Alg. Sum(A, n)
1 S=0;
2 for (i = 0; i < n; i + +)
3 S = S + A[i];
4 return S;

Dr. Praveen (Mahindra University) DAA CS203 22 / 60


Simple Patterns for Analysis

Sum of two matrices A and B of order n X n:


Alg. MatrixSum(A, B, n)
1 for (i = 0; i < n; i + +)
2 for (j = 0; j < n; j + +)
3 C [i, j] = A[i, j] + B[i, j];
Multiplication of A and B (order n X n):
Alg. MatrixMultiplication(A, B, n)
1 for (i = 0; i < n; i + +)
2 for (j = 0; j < n; j + +)
3 C [i, j] = 0;
4 for (k = 0; k < n; k + +)
5 C [i, j] = C [i, j] + A[i, k] ∗ B[k, j];

Dr. Praveen (Mahindra University) DAA CS203 23 / 60


Simple Patterns for Analysis

Sum of two matrices A and B of order n X n:


Alg. MatrixSum(A, B, n)
1 for (i = 0; i < n; i + +)
2 for (j = 0; j < n; j + +)
3 C [i, j] = A[i, j] + B[i, j];
Multiplication of A and B (order n X n):
Alg. MatrixMultiplication(A, B, n)
1 for (i = 0; i < n; i + +)
2 for (j = 0; j < n; j + +)
3 C [i, j] = 0;
4 for (k = 0; k < n; k + +)
5 C [i, j] = C [i, j] + A[i, k] ∗ B[k, j];

Dr. Praveen (Mahindra University) DAA CS203 23 / 60


Simple Patterns for Analysis

1 for (i = 0; i < n; i = i + 2)
Stmt;
2 for (i = 0; i < n; i + +)
for (j = 0; j ≤ i; j + +)
Stmt;
3 P = 0;
for (i = 1; P ≤ n; i + +)
P = P + i;
4 for (i = 0; i ∗ i < n; i + +) Stmt;
5 for (i = 1; i ≤ n; i = i ∗ 2) Stmt;
6 for (i = n; i > 1; i = i/2) Stmt;

Dr. Praveen (Mahindra University) DAA CS203 24 / 60


Simple Patterns for Analysis

1 for (i = 0; i < n; i = i + 2)
Stmt;
2 for (i = 0; i < n; i + +)
for (j = 0; j ≤ i; j + +)
Stmt;
3 P = 0;
for (i = 1; P ≤ n; i + +)
P = P + i;
4 for (i = 0; i ∗ i < n; i + +) Stmt;
5 for (i = 1; i ≤ n; i = i ∗ 2) Stmt;
6 for (i = n; i > 1; i = i/2) Stmt;

Dr. Praveen (Mahindra University) DAA CS203 24 / 60


Simple Patterns for Analysis

1 for (i = 0; i < n; i = i + 2)
Stmt;
2 for (i = 0; i < n; i + +)
for (j = 0; j ≤ i; j + +)
Stmt;
3 P = 0;
for (i = 1; P ≤ n; i + +)
P = P + i;
4 for (i = 0; i ∗ i < n; i + +) Stmt;
5 for (i = 1; i ≤ n; i = i ∗ 2) Stmt;
6 for (i = n; i > 1; i = i/2) Stmt;

Dr. Praveen (Mahindra University) DAA CS203 24 / 60


Simple Patterns for Analysis

1 for (i = 0; i < n; i = i + 2)
Stmt;
2 for (i = 0; i < n; i + +)
for (j = 0; j ≤ i; j + +)
Stmt;
3 P = 0;
for (i = 1; P ≤ n; i + +)
P = P + i;
4 for (i = 0; i ∗ i < n; i + +) Stmt;
5 for (i = 1; i ≤ n; i = i ∗ 2) Stmt;
6 for (i = n; i > 1; i = i/2) Stmt;

Dr. Praveen (Mahindra University) DAA CS203 24 / 60


Simple Patterns for Analysis

1 for (i = 0; i < n; i = i + 2)
Stmt;
2 for (i = 0; i < n; i + +)
for (j = 0; j ≤ i; j + +)
Stmt;
3 P = 0;
for (i = 1; P ≤ n; i + +)
P = P + i;
4 for (i = 0; i ∗ i < n; i + +) Stmt;
5 for (i = 1; i ≤ n; i = i ∗ 2) Stmt;
6 for (i = n; i > 1; i = i/2) Stmt;

Dr. Praveen (Mahindra University) DAA CS203 24 / 60


Simple Patterns for Analysis

1 for (i = 0; i < n; i = i + 2)
Stmt;
2 for (i = 0; i < n; i + +)
for (j = 0; j ≤ i; j + +)
Stmt;
3 P = 0;
for (i = 1; P ≤ n; i + +)
P = P + i;
4 for (i = 0; i ∗ i < n; i + +) Stmt;
5 for (i = 1; i ≤ n; i = i ∗ 2) Stmt;
6 for (i = n; i > 1; i = i/2) Stmt;

Dr. Praveen (Mahindra University) DAA CS203 24 / 60


Simple Patterns for Analysis

1 for (i = 1; i ≤ n; i = i + +)
for (j = 1; j ≤ n; j = j ∗ 2) Stmt;
2 P=1
for (i = 1; i ≤ n; i = i ∗ 2){ P++ }
for (j = 1; j ≤ P; j = j ∗ 2) Stmt;

Dr. Praveen (Mahindra University) DAA CS203 25 / 60


Simple Patterns for Analysis

1 for (i = 1; i ≤ n; i = i + +)
for (j = 1; j ≤ n; j = j ∗ 2) Stmt;
2 P=1
for (i = 1; i ≤ n; i = i ∗ 2){ P++ }
for (j = 1; j ≤ P; j = j ∗ 2) Stmt;

Dr. Praveen (Mahindra University) DAA CS203 25 / 60


Big-Oh ( O) Notation

A function f(n) is O(g (n)) iff there exist positive constants c and n0
such that f(n) ≤ c*g(n), ∀n, n ≥ n0 .
(O(g (n)) = {f (n): there exists positive constants c and n0 such that
0 ≤ f (n) ≤ c.g (n), ∀n ≥ n0 })

Dr. Praveen (Mahindra University) DAA CS203 26 / 60


Omega ( Ω) Notation

A function f(n) is Ω(g (n)) iff there exist positive constants c and n0
such that f(n) ≥ c*g(n), ∀n, n ≥ n0 .
(Ω(g (n)) = {f (n): there exists positive constants c and n0 such that
0 ≤ c.g (n) ≤ f (n), ∀n ≥ n0 })

Dr. Praveen (Mahindra University) DAA CS203 27 / 60


Theta ( Θ) Notation

A function f(n) is Θ(g (n)) iff there exist positive constants c1 , c2 and
n0 such that c1 *g(n) ≤ f(n) ≤ c2 *g(n), ∀n, n ≥ n0 .
(Θ(g (n)) = {f (n): there exists positive constants c1 , c2 , and n0 such
that 0 ≤ c1 .g (n) ≤ f (n) ≤ c2 .g (n), ∀n ≥ n0 })

Dr. Praveen (Mahindra University) DAA CS203 28 / 60


Little-oh and Little-omega Notations

o − notation:For any two functions


f (n) and g (n), f (n) is o(g (n)) iff
limn→∞ gf (n)
(n)
= 0.
(o(g (n)) = {f (n): For any positive
constant c > 0, there exits a constant
n0 > 0 such that 0 ≤ f (n) < c.g (n),
∀n ≥ n0 }).

ω − notation:For any two func-


tions f (n) and g (n), f (n) is ω(g (n)) iff
limn→∞ gf (n)
(n)
= 0.
(ω(g (n)) = {f (n): For any positive
constant c > 0, there exits a constant
n0 > 0 such that 0 ≤ c.g (n) < f (n),
∀n ≥ n0 }).

Dr. Praveen (Mahindra University) DAA CS203 29 / 60


1 For any two functions f (n) and g (n), we have f (n) is Θ(g (n)) if and
only if f (n) is O(g (n)) and f (n) is Ω(g (n)).
2 If f (n) is Θ(g (n)) and g (n) is Θ(h(n)) =⇒ f (n) is Θ(h(n)).
3 If f (n) is O(g (n)) and g (n) is O(h(n)) =⇒ f (n) is O(h(n)).
4 If f (n) is Ω(g (n)) and g (n) is Ω(h(n)) =⇒ f (n) is Ω(h(n)).
5 If f (n) is o(g (n)) and g (n) is o(h(n)) =⇒ f (n) is o(h(n)).
6 If f (n) is ω(g (n)) and g (n) is ω(h(n)) =⇒ f (n) is ω(h(n)).
7 f (n) is Θ(g (n)) ⇐⇒ g (n) is Θ(f (n)).
8 f (n) is O(g (n)) ⇐⇒ g (n) is Ω(f (n)).
9 f (n) is o(g (n)) ⇐⇒ g (n) is ω(f (n)).

Dr. Praveen (Mahindra University) DAA CS203 30 / 60


Growth Rate of Functions

Dr. Praveen (Mahindra University) DAA CS203 31 / 60


Basic Idea of Merge Sort

Dr. Praveen (Mahindra University) DAA CS203 32 / 60


Merge Sort (2-way Merge Sort)

Solve Recurrence Relation: T (n) = 2 × T ( n2 ) + n


Where T (1) = 1 and n = 2k .

I T (n) = 2 × T ( n2 ) + n
I T (n) = 2 × [2 × T ( n4 ) + n2 ] + n = 22 × T ( 2n2 ) + 2n
I T (n) = 22 × [2 × T ( 2n3 ) + 2n2 ] + 2n = 23 × T ( 2n3 ) + 3n
.
.
.
I T (n) = 2k × T ( nk ) + kn
2
I T (n) = 2k × T (1) + kn
I T (n) = n + log2 (n) × n

Dr. Praveen (Mahindra University) DAA CS203 33 / 60


Basic Idea of Merge Sort

Dr. Praveen (Mahindra University) DAA CS203 34 / 60


Merge Sort (3-way Merge Sort)

Solve Recurrence Relation: T (n) = 3 × T ( n3 ) + n


Where T (1) = 1 and n = 3k .

I T (n) = 3 × T ( n3 ) + n
I T (n) = 3 × [3 × T ( n9 ) + n3 ] + n = 32 × T ( 3n2 ) + 2n
I T (n) = 32 × [3 × T ( 3n3 ) + 3n2 ] + 3n = 33 × T ( 3n3 ) + 3n
.
.
.
I T (n) = 3k × T ( nk ) + kn
3
I T (n) = 3k × T (1) + kn
I T (n) = n + log3 (n) × n

Dr. Praveen (Mahindra University) DAA CS203 35 / 60


Solve Recurrence Relations (using − − − − − − −−)

Recurrences of the form: T (n) = aT (n/b) + f (n)


Where T (1) = 1 and n = b k

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


2 Solve T (n) = 2T (n/2) + n
3 Solve T (n) = T (n/2) + n

Dr. Praveen (Mahindra University) DAA CS203 36 / 60


Master Method: Basic Idea

Recurrences of the form: T (n) = aT (n/b) + f (n), where a ≥ 1,


b > 1, and f (n) is some function. a is branching factor, b determines
how deep the tree goes, f (n) determines the weight of each level.
(T (n) is usually only defined for integer n).

Dr. Praveen (Mahindra University) DAA CS203 37 / 60


Master’s Theorem

We are given the recurrence T (n) = aT (n/b) + f (n), where a ≥ 1,


b > 1, and f (n) is some function.

1 T (n) = a(aT ( bn2 ) + f ( bn )) + f (n)= a2 T ( bn2 ) + af ( bn ) + f (n)


2 T (n) = a3 T ( bn3 ) + a2 f ( bn2 ) + af ( bn ) + f (n)
3 ...
4 ...
5 T (n) = ak T ( bnk ) + ak−1 f ( bk−1
n n
) + ak−2 f ( bk−2 ) + ... + af ( bn ) + f (n)
If n = b k then k = logb (n)
n n
T (n) = alogb (n) T (1) + ak−1 f ( bk−1 ) + ak−2 f ( bk−2 ) + ... + af ( bn ) + f (n)
T (n) = nlogb (a) + k−1 j j
P
j=0 a · f (n/b )
log (n)−1 j
∴ T (n) = Θ(nlogb (a) ) + j=0b a · f (n/b j )
P

Dr. Praveen (Mahindra University) DAA CS203 38 / 60


Master’s Theorem

We are given the recurrence T (n) = aT (n/b) + f (n), where a ≥ 1,


b > 1, and f (n) is some function.
Plog (n)−1 j
From the Recursion Tree, T (n) = Θ(nlogb (a) ) + j=0b a · f (n/b j )
1 If f (n) = O(nlogb (a)− ) for some constant  > 0,
T (n) = Θ(nlogb (a) ).
2 If f (n) = Θ(nlogb (a) ), T (n) = Θ(nlogb (a) log n).
3 If f (n) = Ω(nlogb (a)+ ) for some constant  > 0, and
a f (n/b) ≤ c f (n) for some constant c < 1 and all sufficiently
large n, T (n) = Θ(f (n)).
4 If f (n) = Θ(nlogb (a) logk (n)) for k ≥ 0,
T (n) = Θ(nlogb (a) logk+1 n).

Dr. Praveen (Mahindra University) DAA CS203 39 / 60


Master’s Theorem

We are given the recurrence T (n) = aT (n/b) + f (n), where a ≥ 1,


b > 1, and f (n) is some function.
Plog (n)−1 j
From the Recursion Tree, T (n) = Θ(nlogb (a) ) + j=0b a · f (n/b j )
1 If f (n) = O(nlogb (a)− ) for some constant  > 0,
T (n) = Θ(nlogb (a) ).
2 If f (n) = Θ(nlogb (a) ), T (n) = Θ(nlogb (a) log n).
3 If f (n) = Ω(nlogb (a)+ ) for some constant  > 0, and
a f (n/b) ≤ c f (n) for some constant c < 1 and all sufficiently
large n, T (n) = Θ(f (n)).
4 If f (n) = Θ(nlogb (a) logk (n)) for k ≥ 0,
T (n) = Θ(nlogb (a) logk+1 n).

Dr. Praveen (Mahindra University) DAA CS203 39 / 60


Master’s Theorem

We are given the recurrence T (n) = aT (n/b) + f (n), where a ≥ 1,


b > 1, and f (n) is some function.
Plog (n)−1 j
From the Recursion Tree, T (n) = Θ(nlogb (a) ) + j=0b a · f (n/b j )
1 If f (n) = O(nlogb (a)− ) for some constant  > 0,
T (n) = Θ(nlogb (a) ).
2 If f (n) = Θ(nlogb (a) ), T (n) = Θ(nlogb (a) log n).
3 If f (n) = Ω(nlogb (a)+ ) for some constant  > 0, and
a f (n/b) ≤ c f (n) for some constant c < 1 and all sufficiently
large n, T (n) = Θ(f (n)).
4 If f (n) = Θ(nlogb (a) logk (n)) for k ≥ 0,
T (n) = Θ(nlogb (a) logk+1 n).

Dr. Praveen (Mahindra University) DAA CS203 39 / 60


Master’s Theorem

We are given the recurrence T (n) = aT (n/b) + f (n), where a ≥ 1,


b > 1, and f (n) is some function.
Plog (n)−1 j
From the Recursion Tree, T (n) = Θ(nlogb (a) ) + j=0b a · f (n/b j )
1 If f (n) = O(nlogb (a)− ) for some constant  > 0,
T (n) = Θ(nlogb (a) ).
2 If f (n) = Θ(nlogb (a) ), T (n) = Θ(nlogb (a) log n).
3 If f (n) = Ω(nlogb (a)+ ) for some constant  > 0, and
a f (n/b) ≤ c f (n) for some constant c < 1 and all sufficiently
large n, T (n) = Θ(f (n)).
4 If f (n) = Θ(nlogb (a) logk (n)) for k ≥ 0,
T (n) = Θ(nlogb (a) logk+1 n).

Dr. Praveen (Mahindra University) DAA CS203 39 / 60


Master’s Theorem (Basic Justification)

In the recursion tree, nlogb (a) is the number of leaves on the tree.
1 If f (n) polynomially smaller than nlogb (a) then the number of leaves
dominates in the recurrence: Θ(nlogb (a) ) (Rule1).
2 If f (n) polynomially equal to nlogb (a) then f (n) makes a contribution
at every level: Θ(f (n) log n) (Rule 2).
3 If f (n) polynomially bigger than than nlogb (a) then the first appearance
of f (n) in the tree dominates in the recurrence: Θ(f (n)) (Rule 3).
(There is a “regularity” condition on f (n) should be satisfied).
Note1: There is a polynomial gap on either side. It’s not enough for f (n)
to be bigger (or smaller) than nlogb (a) , it has to be polynomially so (i.e.,
the  > 0).
Note2: If f (n) is just a log factor bigger than nlogb (a) ,
T (n) = Θ(nlogb (a) logk+1 n).

Dr. Praveen (Mahindra University) DAA CS203 40 / 60


Master’s Theorem (Basic Justification)

In the recursion tree, nlogb (a) is the number of leaves on the tree.
1 If f (n) polynomially smaller than nlogb (a) then the number of leaves
dominates in the recurrence: Θ(nlogb (a) ) (Rule1).
2 If f (n) polynomially equal to nlogb (a) then f (n) makes a contribution
at every level: Θ(f (n) log n) (Rule 2).
3 If f (n) polynomially bigger than than nlogb (a) then the first appearance
of f (n) in the tree dominates in the recurrence: Θ(f (n)) (Rule 3).
(There is a “regularity” condition on f (n) should be satisfied).
Note1: There is a polynomial gap on either side. It’s not enough for f (n)
to be bigger (or smaller) than nlogb (a) , it has to be polynomially so (i.e.,
the  > 0).
Note2: If f (n) is just a log factor bigger than nlogb (a) ,
T (n) = Θ(nlogb (a) logk+1 n).

Dr. Praveen (Mahindra University) DAA CS203 40 / 60


Master’s Theorem (Basic Justification)

In the recursion tree, nlogb (a) is the number of leaves on the tree.
1 If f (n) polynomially smaller than nlogb (a) then the number of leaves
dominates in the recurrence: Θ(nlogb (a) ) (Rule1).
2 If f (n) polynomially equal to nlogb (a) then f (n) makes a contribution
at every level: Θ(f (n) log n) (Rule 2).
3 If f (n) polynomially bigger than than nlogb (a) then the first appearance
of f (n) in the tree dominates in the recurrence: Θ(f (n)) (Rule 3).
(There is a “regularity” condition on f (n) should be satisfied).
Note1: There is a polynomial gap on either side. It’s not enough for f (n)
to be bigger (or smaller) than nlogb (a) , it has to be polynomially so (i.e.,
the  > 0).
Note2: If f (n) is just a log factor bigger than nlogb (a) ,
T (n) = Θ(nlogb (a) logk+1 n).

Dr. Praveen (Mahindra University) DAA CS203 40 / 60


Master’s Theorem (Basic Justification)

In the recursion tree, nlogb (a) is the number of leaves on the tree.
1 If f (n) polynomially smaller than nlogb (a) then the number of leaves
dominates in the recurrence: Θ(nlogb (a) ) (Rule1).
2 If f (n) polynomially equal to nlogb (a) then f (n) makes a contribution
at every level: Θ(f (n) log n) (Rule 2).
3 If f (n) polynomially bigger than than nlogb (a) then the first appearance
of f (n) in the tree dominates in the recurrence: Θ(f (n)) (Rule 3).
(There is a “regularity” condition on f (n) should be satisfied).
Note1: There is a polynomial gap on either side. It’s not enough for f (n)
to be bigger (or smaller) than nlogb (a) , it has to be polynomially so (i.e.,
the  > 0).
Note2: If f (n) is just a log factor bigger than nlogb (a) ,
T (n) = Θ(nlogb (a) logk+1 n).

Dr. Praveen (Mahindra University) DAA CS203 40 / 60


Examples
1 T (n) = 9T (n/3) + n
a = 9, b = 3, f (n) = n,
nlogb (a) = n2 . Here, f(n) is polynomially smaller than nlogb (a) .
So, T (n) = Θ(n2 ). (Case 1 of Master’s Theorem)
3
2 T (n) = 3T (n/7) + n 4
3
a = 3, b = 7, f (n) = n 4 = n0.75 ,
nlogb (a) ≈ n0.56 . f (n) is polynomially bigger nlogb (a) .
3
So, T (n) = Θ(n 4 ). (Case 3 of Master’s Theorem)
3 T (n) = 2T (n/2) + n
a = 2, b = 2, f (n) = n,
nlogb (a) = n. Here, f(n) is polynomially equal to nlogb (a) .
So, T (n) = Θ(n logb (n)). (Case 2 of Master’s Theorem)
4 T (n) = T (n/2) + log n
a = 1, b = 2, f (n) = log n
nlogb (a) = 1. f (n) is not polynomially bigger nlogb (a) ,
but f(n) is log factor bigger than nlogb (a) .
Dr. Praveen (Mahindra University) 2 DAA CS203 41 / 60
Examples
1 T (n) = 9T (n/3) + n
a = 9, b = 3, f (n) = n,
nlogb (a) = n2 . Here, f(n) is polynomially smaller than nlogb (a) .
So, T (n) = Θ(n2 ). (Case 1 of Master’s Theorem)
3
2 T (n) = 3T (n/7) + n 4
3
a = 3, b = 7, f (n) = n 4 = n0.75 ,
nlogb (a) ≈ n0.56 . f (n) is polynomially bigger nlogb (a) .
3
So, T (n) = Θ(n 4 ). (Case 3 of Master’s Theorem)
3 T (n) = 2T (n/2) + n
a = 2, b = 2, f (n) = n,
nlogb (a) = n. Here, f(n) is polynomially equal to nlogb (a) .
So, T (n) = Θ(n logb (n)). (Case 2 of Master’s Theorem)
4 T (n) = T (n/2) + log n
a = 1, b = 2, f (n) = log n
nlogb (a) = 1. f (n) is not polynomially bigger nlogb (a) ,
but f(n) is log factor bigger than nlogb (a) .
Dr. Praveen (Mahindra University) 2 DAA CS203 41 / 60
Examples
1 T (n) = 9T (n/3) + n
a = 9, b = 3, f (n) = n,
nlogb (a) = n2 . Here, f(n) is polynomially smaller than nlogb (a) .
So, T (n) = Θ(n2 ). (Case 1 of Master’s Theorem)
3
2 T (n) = 3T (n/7) + n 4
3
a = 3, b = 7, f (n) = n 4 = n0.75 ,
nlogb (a) ≈ n0.56 . f (n) is polynomially bigger nlogb (a) .
3
So, T (n) = Θ(n 4 ). (Case 3 of Master’s Theorem)
3 T (n) = 2T (n/2) + n
a = 2, b = 2, f (n) = n,
nlogb (a) = n. Here, f(n) is polynomially equal to nlogb (a) .
So, T (n) = Θ(n logb (n)). (Case 2 of Master’s Theorem)
4 T (n) = T (n/2) + log n
a = 1, b = 2, f (n) = log n
nlogb (a) = 1. f (n) is not polynomially bigger nlogb (a) ,
but f(n) is log factor bigger than nlogb (a) .
Dr. Praveen (Mahindra University) 2 DAA CS203 41 / 60
Examples
1 T (n) = 9T (n/3) + n
a = 9, b = 3, f (n) = n,
nlogb (a) = n2 . Here, f(n) is polynomially smaller than nlogb (a) .
So, T (n) = Θ(n2 ). (Case 1 of Master’s Theorem)
3
2 T (n) = 3T (n/7) + n 4
3
a = 3, b = 7, f (n) = n 4 = n0.75 ,
nlogb (a) ≈ n0.56 . f (n) is polynomially bigger nlogb (a) .
3
So, T (n) = Θ(n 4 ). (Case 3 of Master’s Theorem)
3 T (n) = 2T (n/2) + n
a = 2, b = 2, f (n) = n,
nlogb (a) = n. Here, f(n) is polynomially equal to nlogb (a) .
So, T (n) = Θ(n logb (n)). (Case 2 of Master’s Theorem)
4 T (n) = T (n/2) + log n
a = 1, b = 2, f (n) = log n
nlogb (a) = 1. f (n) is not polynomially bigger nlogb (a) ,
but f(n) is log factor bigger than nlogb (a) .
Dr. Praveen (Mahindra University) 2 DAA CS203 41 / 60
Solution of T (n) = T (n/2) + log n

Given Recurrence Relation is


T (n) = T (n/2) + log(n)
T (n/2) = T (n/4) + log(n/2)
T (n/4) = T (n/8) + log(n/4)
...
...
...
T (n/2k−1 ) = T (n/2k ) + log(n/2k−1 )
Add all the above equations then we get
T (n) = T (n/2K ) + log(n) + log(n/2) + log(n/4) + ... + log(n/2k−1 )
T (n) = T (1) + log(n) + (log(n) − 1) + (log(n) − 2) + ... + (log(n) − k + 1)
T (n) = T (1) + k × log(n) − 1 − 2... − (k − 1)
T (n) = T (1) + log(n) × log(n) − (log(n)−1)×log(n))
2
T (n) = c1 × log2 (n) + c2

Dr. Praveen (Mahindra University) DAA CS203 42 / 60


Solution of T (n) = T (n/2) + log n

Given Recurrence Relation is


T (n) = T (n/2) + log(n)
T (n/2) = T (n/4) + log(n/2)
T (n/4) = T (n/8) + log(n/4)
...
...
...
T (n/2k−1 ) = T (n/2k ) + log(n/2k−1 )
Add all the above equations then we get
T (n) = T (n/2K ) + log(n) + log(n/2) + log(n/4) + ... + log(n/2k−1 )
T (n) = T (1) + log(n) + (log(n) − 1) + (log(n) − 2) + ... + (log(n) − k + 1)
T (n) = T (1) + k × log(n) − 1 − 2... − (k − 1)
T (n) = T (1) + log(n) × log(n) − (log(n)−1)×log(n))
2
T (n) = c1 × log2 (n) + c2

Dr. Praveen (Mahindra University) DAA CS203 42 / 60


Solution of T (n) = T (n/2) + log n

Given Recurrence Relation is


T (n) = T (n/2) + log(n)
T (n/2) = T (n/4) + log(n/2)
T (n/4) = T (n/8) + log(n/4)
...
...
...
T (n/2k−1 ) = T (n/2k ) + log(n/2k−1 )
Add all the above equations then we get
T (n) = T (n/2K ) + log(n) + log(n/2) + log(n/4) + ... + log(n/2k−1 )
T (n) = T (1) + log(n) + (log(n) − 1) + (log(n) − 2) + ... + (log(n) − k + 1)
T (n) = T (1) + k × log(n) − 1 − 2... − (k − 1)
T (n) = T (1) + log(n) × log(n) − (log(n)−1)×log(n))
2
T (n) = c1 × log2 (n) + c2

Dr. Praveen (Mahindra University) DAA CS203 42 / 60


Solution of T (n) = T (n/2) + log n

Given Recurrence Relation is


T (n) = T (n/2) + log(n)
T (n/2) = T (n/4) + log(n/2)
T (n/4) = T (n/8) + log(n/4)
...
...
...
T (n/2k−1 ) = T (n/2k ) + log(n/2k−1 )
Add all the above equations then we get
T (n) = T (n/2K ) + log(n) + log(n/2) + log(n/4) + ... + log(n/2k−1 )
T (n) = T (1) + log(n) + (log(n) − 1) + (log(n) − 2) + ... + (log(n) − k + 1)
T (n) = T (1) + k × log(n) − 1 − 2... − (k − 1)
T (n) = T (1) + log(n) × log(n) − (log(n)−1)×log(n))
2
T (n) = c1 × log2 (n) + c2

Dr. Praveen (Mahindra University) DAA CS203 42 / 60


Solution of T (n) = T (n/2) + log n

Given Recurrence Relation is


T (n) = T (n/2) + log(n)
T (n/2) = T (n/4) + log(n/2)
T (n/4) = T (n/8) + log(n/4)
...
...
...
T (n/2k−1 ) = T (n/2k ) + log(n/2k−1 )
Add all the above equations then we get
T (n) = T (n/2K ) + log(n) + log(n/2) + log(n/4) + ... + log(n/2k−1 )
T (n) = T (1) + log(n) + (log(n) − 1) + (log(n) − 2) + ... + (log(n) − k + 1)
T (n) = T (1) + k × log(n) − 1 − 2... − (k − 1)
T (n) = T (1) + log(n) × log(n) − (log(n)−1)×log(n))
2
T (n) = c1 × log2 (n) + c2

Dr. Praveen (Mahindra University) DAA CS203 42 / 60


Solution of T (n) = T (n/2) + log n

Given Recurrence Relation is


T (n) = T (n/2) + log(n)
T (n/2) = T (n/4) + log(n/2)
T (n/4) = T (n/8) + log(n/4)
...
...
...
T (n/2k−1 ) = T (n/2k ) + log(n/2k−1 )
Add all the above equations then we get
T (n) = T (n/2K ) + log(n) + log(n/2) + log(n/4) + ... + log(n/2k−1 )
T (n) = T (1) + log(n) + (log(n) − 1) + (log(n) − 2) + ... + (log(n) − k + 1)
T (n) = T (1) + k × log(n) − 1 − 2... − (k − 1)
T (n) = T (1) + log(n) × log(n) − (log(n)−1)×log(n))
2
T (n) = c1 × log2 (n) + c2

Dr. Praveen (Mahindra University) DAA CS203 42 / 60


Solution of T (n) = T (n/2) + log n

Given Recurrence Relation is


T (n) = T (n/2) + log(n)
T (n/2) = T (n/4) + log(n/2)
T (n/4) = T (n/8) + log(n/4)
...
...
...
T (n/2k−1 ) = T (n/2k ) + log(n/2k−1 )
Add all the above equations then we get
T (n) = T (n/2K ) + log(n) + log(n/2) + log(n/4) + ... + log(n/2k−1 )
T (n) = T (1) + log(n) + (log(n) − 1) + (log(n) − 2) + ... + (log(n) − k + 1)
T (n) = T (1) + k × log(n) − 1 − 2... − (k − 1)
T (n) = T (1) + log(n) × log(n) − (log(n)−1)×log(n))
2
T (n) = c1 × log2 (n) + c2

Dr. Praveen (Mahindra University) DAA CS203 42 / 60


Solution of T (n) = T (n/2) + log n

Given Recurrence Relation is


T (n) = T (n/2) + log(n)
T (n/2) = T (n/4) + log(n/2)
T (n/4) = T (n/8) + log(n/4)
...
...
...
T (n/2k−1 ) = T (n/2k ) + log(n/2k−1 )
Add all the above equations then we get
T (n) = T (n/2K ) + log(n) + log(n/2) + log(n/4) + ... + log(n/2k−1 )
T (n) = T (1) + log(n) + (log(n) − 1) + (log(n) − 2) + ... + (log(n) − k + 1)
T (n) = T (1) + k × log(n) − 1 − 2... − (k − 1)
T (n) = T (1) + log(n) × log(n) − (log(n)−1)×log(n))
2
T (n) = c1 × log2 (n) + c2

Dr. Praveen (Mahindra University) DAA CS203 42 / 60


Master’s Theorem Proof

Let a ≥ 1, b > 1, and f (n) is some non-negative function defined on exact


powers of b. (
Θ(1) if n = 1
T (n) =
aT (n/b) + f (n) if n = b i

where i is a positive integer.


Plog (n)−1
Then T (n) = Θ(nlogb (a) ) + j=0b f (n/b j )
i.e. T (n) = Θ(nlogb (a) ) + g (n)
Plog (n)−1 j
Where g (n) = j=0b a f (n/b j ).
1 If f (n) = O(nlogb (a)− ) for some constant  > 0, g (n) = O(nlogb (a) ).
2 If f (n) = Θ(nlogb (a) ), g (n) = Θ(nlogb (a) log n).
3 If a f (n/b) ≤ c f (n) for some constant c < 1 and all sufficiently large
n, g (n) = Θ(f (n)).

Dr. Praveen (Mahindra University) DAA CS203 43 / 60


Master’s Theorem Proof: Case1
Plog (n)−1 j
Case1: Given g (n) = j=0b a f (n/b j ) and f (n) = O(nlogb (a)− )
for some constant  > 0, then g (n) = O(nlogb (a) ).

1 Proof: If f (n) = O(nlogb (a)− ) then f (n/b j ) = O((n/b j )logb (a)− )


Plog (n)−1 j n log (a)−
2 g (n) = O( j=0b a ( bj ) b ) (by substitution)
Plogb (a)−1 j n log (a)− Plog (n)−1 ab j
3
j=0 a ( bj ) b = nlogb (a)− j=0b ( blogb (a) )
Plogb (n)−1
4 = nlogb (a)− j=0 (b  )j
 logb (n) −1 
5 = nlogb (a)− ( b b  −1
) = nlogb (a)− ( bn −1
−1
) < nlogb (a)− n
6 Hence, we can write g (n) = O(nlogb (a) )
T (n) = Θ(nlogb (a) ) + g (n) = Θ(nlogb (a) ) + O(nlogb (a) ) = Θ(nlogb (a) )

Dr. Praveen (Mahindra University) DAA CS203 44 / 60


Master’s Theorem Proof: Case1
Plog (n)−1 j
Case1: Given g (n) = j=0b a f (n/b j ) and f (n) = O(nlogb (a)− )
for some constant  > 0, then g (n) = O(nlogb (a) ).

1 Proof: If f (n) = O(nlogb (a)− ) then f (n/b j ) = O((n/b j )logb (a)− )


Plog (n)−1 j n log (a)−
2 g (n) = O( j=0b a ( bj ) b ) (by substitution)
Plogb (a)−1 j n log (a)− Plog (n)−1 ab j
3
j=0 a ( bj ) b = nlogb (a)− j=0b ( blogb (a) )
Plogb (n)−1
4 = nlogb (a)− j=0 (b  )j
 logb (n) −1 
5 = nlogb (a)− ( b b  −1
) = nlogb (a)− ( bn −1
−1
) < nlogb (a)− n
6 Hence, we can write g (n) = O(nlogb (a) )
T (n) = Θ(nlogb (a) ) + g (n) = Θ(nlogb (a) ) + O(nlogb (a) ) = Θ(nlogb (a) )

Dr. Praveen (Mahindra University) DAA CS203 44 / 60


Master’s Theorem Proof: Case1
Plog (n)−1 j
Case1: Given g (n) = j=0b a f (n/b j ) and f (n) = O(nlogb (a)− )
for some constant  > 0, then g (n) = O(nlogb (a) ).

1 Proof: If f (n) = O(nlogb (a)− ) then f (n/b j ) = O((n/b j )logb (a)− )


Plog (n)−1 j n log (a)−
2 g (n) = O( j=0b a ( bj ) b ) (by substitution)
Plogb (a)−1 j n log (a)− Plog (n)−1 ab j
3
j=0 a ( bj ) b = nlogb (a)− j=0b ( blogb (a) )
Plogb (n)−1
4 = nlogb (a)− j=0 (b  )j
 logb (n) −1 
5 = nlogb (a)− ( b b  −1
) = nlogb (a)− ( bn −1
−1
) < nlogb (a)− n
6 Hence, we can write g (n) = O(nlogb (a) )
T (n) = Θ(nlogb (a) ) + g (n) = Θ(nlogb (a) ) + O(nlogb (a) ) = Θ(nlogb (a) )

Dr. Praveen (Mahindra University) DAA CS203 44 / 60


Master’s Theorem Proof: Case1
Plog (n)−1 j
Case1: Given g (n) = j=0b a f (n/b j ) and f (n) = O(nlogb (a)− )
for some constant  > 0, then g (n) = O(nlogb (a) ).

1 Proof: If f (n) = O(nlogb (a)− ) then f (n/b j ) = O((n/b j )logb (a)− )


Plog (n)−1 j n log (a)−
2 g (n) = O( j=0b a ( bj ) b ) (by substitution)
Plogb (a)−1 j n log (a)− Plog (n)−1 ab j
3
j=0 a ( bj ) b = nlogb (a)− j=0b ( blogb (a) )
Plogb (n)−1
4 = nlogb (a)− j=0 (b  )j
 logb (n) −1 
5 = nlogb (a)− ( b b  −1
) = nlogb (a)− ( bn −1
−1
) < nlogb (a)− n
6 Hence, we can write g (n) = O(nlogb (a) )
T (n) = Θ(nlogb (a) ) + g (n) = Θ(nlogb (a) ) + O(nlogb (a) ) = Θ(nlogb (a) )

Dr. Praveen (Mahindra University) DAA CS203 44 / 60


Master’s Theorem Proof: Case1
Plog (n)−1 j
Case1: Given g (n) = j=0b a f (n/b j ) and f (n) = O(nlogb (a)− )
for some constant  > 0, then g (n) = O(nlogb (a) ).

1 Proof: If f (n) = O(nlogb (a)− ) then f (n/b j ) = O((n/b j )logb (a)− )


Plog (n)−1 j n log (a)−
2 g (n) = O( j=0b a ( bj ) b ) (by substitution)
Plogb (a)−1 j n log (a)− Plog (n)−1 ab j
3
j=0 a ( bj ) b = nlogb (a)− j=0b ( blogb (a) )
Plogb (n)−1
4 = nlogb (a)− j=0 (b  )j
 logb (n) −1 
5 = nlogb (a)− ( b b  −1
) = nlogb (a)− ( bn −1
−1
) < nlogb (a)− n
6 Hence, we can write g (n) = O(nlogb (a) )
T (n) = Θ(nlogb (a) ) + g (n) = Θ(nlogb (a) ) + O(nlogb (a) ) = Θ(nlogb (a) )

Dr. Praveen (Mahindra University) DAA CS203 44 / 60


Master’s Theorem Proof: Case1
Plog (n)−1 j
Case1: Given g (n) = j=0b a f (n/b j ) and f (n) = O(nlogb (a)− )
for some constant  > 0, then g (n) = O(nlogb (a) ).

1 Proof: If f (n) = O(nlogb (a)− ) then f (n/b j ) = O((n/b j )logb (a)− )


Plog (n)−1 j n log (a)−
2 g (n) = O( j=0b a ( bj ) b ) (by substitution)
Plogb (a)−1 j n log (a)− Plog (n)−1 ab j
3
j=0 a ( bj ) b = nlogb (a)− j=0b ( blogb (a) )
Plogb (n)−1
4 = nlogb (a)− j=0 (b  )j
 logb (n) −1 
5 = nlogb (a)− ( b b  −1
) = nlogb (a)− ( bn −1
−1
) < nlogb (a)− n
6 Hence, we can write g (n) = O(nlogb (a) )
T (n) = Θ(nlogb (a) ) + g (n) = Θ(nlogb (a) ) + O(nlogb (a) ) = Θ(nlogb (a) )

Dr. Praveen (Mahindra University) DAA CS203 44 / 60


Master’s Theorem Proof: Case1
Plog (n)−1 j
Case1: Given g (n) = j=0b a f (n/b j ) and f (n) = O(nlogb (a)− )
for some constant  > 0, then g (n) = O(nlogb (a) ).

1 Proof: If f (n) = O(nlogb (a)− ) then f (n/b j ) = O((n/b j )logb (a)− )


Plog (n)−1 j n log (a)−
2 g (n) = O( j=0b a ( bj ) b ) (by substitution)
Plogb (a)−1 j n log (a)− Plog (n)−1 ab j
3
j=0 a ( bj ) b = nlogb (a)− j=0b ( blogb (a) )
Plogb (n)−1
4 = nlogb (a)− j=0 (b  )j
 logb (n) −1 
5 = nlogb (a)− ( b b  −1
) = nlogb (a)− ( bn −1
−1
) < nlogb (a)− n
6 Hence, we can write g (n) = O(nlogb (a) )
T (n) = Θ(nlogb (a) ) + g (n) = Θ(nlogb (a) ) + O(nlogb (a) ) = Θ(nlogb (a) )

Dr. Praveen (Mahindra University) DAA CS203 44 / 60


Master’s Theorem Proof: Case2
Plog (n)−1 j
Given g (n) = j=0b a f (n/b j ) and f (n) = Θ(nlogb (a) ) then
g (n) = Θ(nlogb (a) logb (n)).

1 Proof: If f (n) = Θ(nlogb (a) ), then f (n/b j ) = Θ((n/b j )logb (a) )


Plog (n)−1 j n log (a)
2 g (n) = Θ( j=0b a ( bj ) b ) (by substitution)
Plogb (a)−1 j n log (a) Plog (n)−1 a j
3
j=0 a ( bj ) b = nlogb (a) j=0b ( blogb (a) )
4 = nlogb (a) logb (n)
5 Hence, we can write g (n) = Θ(nlogb (a) logb (n))
T (n) = Θ(nlogb (a) ) + g (n) = Θ(nlogb (a) ) + Θ(nlogb (a) logb (n))
∴ T (n) = Θ(nlogb (a) logb (n))

Dr. Praveen (Mahindra University) DAA CS203 45 / 60


Master’s Theorem Proof: Case2
Plog (n)−1 j
Given g (n) = j=0b a f (n/b j ) and f (n) = Θ(nlogb (a) ) then
g (n) = Θ(nlogb (a) logb (n)).

1 Proof: If f (n) = Θ(nlogb (a) ), then f (n/b j ) = Θ((n/b j )logb (a) )


Plog (n)−1 j n log (a)
2 g (n) = Θ( j=0b a ( bj ) b ) (by substitution)
Plogb (a)−1 j n log (a) Plog (n)−1 a j
3
j=0 a ( bj ) b = nlogb (a) j=0b ( blogb (a) )
4 = nlogb (a) logb (n)
5 Hence, we can write g (n) = Θ(nlogb (a) logb (n))
T (n) = Θ(nlogb (a) ) + g (n) = Θ(nlogb (a) ) + Θ(nlogb (a) logb (n))
∴ T (n) = Θ(nlogb (a) logb (n))

Dr. Praveen (Mahindra University) DAA CS203 45 / 60


Master’s Theorem Proof: Case2
Plog (n)−1 j
Given g (n) = j=0b a f (n/b j ) and f (n) = Θ(nlogb (a) ) then
g (n) = Θ(nlogb (a) logb (n)).

1 Proof: If f (n) = Θ(nlogb (a) ), then f (n/b j ) = Θ((n/b j )logb (a) )


Plog (n)−1 j n log (a)
2 g (n) = Θ( j=0b a ( bj ) b ) (by substitution)
Plogb (a)−1 j n log (a) Plog (n)−1 a j
3
j=0 a ( bj ) b = nlogb (a) j=0b ( blogb (a) )
4 = nlogb (a) logb (n)
5 Hence, we can write g (n) = Θ(nlogb (a) logb (n))
T (n) = Θ(nlogb (a) ) + g (n) = Θ(nlogb (a) ) + Θ(nlogb (a) logb (n))
∴ T (n) = Θ(nlogb (a) logb (n))

Dr. Praveen (Mahindra University) DAA CS203 45 / 60


Master’s Theorem Proof: Case2
Plog (n)−1 j
Given g (n) = j=0b a f (n/b j ) and f (n) = Θ(nlogb (a) ) then
g (n) = Θ(nlogb (a) logb (n)).

1 Proof: If f (n) = Θ(nlogb (a) ), then f (n/b j ) = Θ((n/b j )logb (a) )


Plog (n)−1 j n log (a)
2 g (n) = Θ( j=0b a ( bj ) b ) (by substitution)
Plogb (a)−1 j n log (a) Plog (n)−1 a j
3
j=0 a ( bj ) b = nlogb (a) j=0b ( blogb (a) )
4 = nlogb (a) logb (n)
5 Hence, we can write g (n) = Θ(nlogb (a) logb (n))
T (n) = Θ(nlogb (a) ) + g (n) = Θ(nlogb (a) ) + Θ(nlogb (a) logb (n))
∴ T (n) = Θ(nlogb (a) logb (n))

Dr. Praveen (Mahindra University) DAA CS203 45 / 60


Master’s Theorem Proof: Case2
Plog (n)−1 j
Given g (n) = j=0b a f (n/b j ) and f (n) = Θ(nlogb (a) ) then
g (n) = Θ(nlogb (a) logb (n)).

1 Proof: If f (n) = Θ(nlogb (a) ), then f (n/b j ) = Θ((n/b j )logb (a) )


Plog (n)−1 j n log (a)
2 g (n) = Θ( j=0b a ( bj ) b ) (by substitution)
Plogb (a)−1 j n log (a) Plog (n)−1 a j
3
j=0 a ( bj ) b = nlogb (a) j=0b ( blogb (a) )
4 = nlogb (a) logb (n)
5 Hence, we can write g (n) = Θ(nlogb (a) logb (n))
T (n) = Θ(nlogb (a) ) + g (n) = Θ(nlogb (a) ) + Θ(nlogb (a) logb (n))
∴ T (n) = Θ(nlogb (a) logb (n))

Dr. Praveen (Mahindra University) DAA CS203 45 / 60


Master’s Theorem Proof: Case3
Plog (n)−1 j
Given g (n) = j=0b a f (n/b j ) and a f (n/b) ≤ c f (n) for some
constant c < 1 and all sufficiently large n, g (n) = Θ(f (n)).
Plog (n)−1 j
1 Proof: g (n) = j=0b a f (n/b j )
2 g (n) = f (n)+a1 f (n/b 1 )+a2 f (n/b 2 )+...+alogb (n)−1 f (n/b logb (n)−1 )
3 g (n) > f (n) =⇒ g (n) is Ω(f (n)) (Def. of Ω Notation)
4 g (n) ≤ f (n) + c 1 f (n) + c 2 f (n) + ... + c logb (n)−1 f (n)
g (n) ≤ ∞ j
P
j=0 c f (n)
5

1
6 g (n) ≤ 1−c
f (n) =⇒ g (n) is O(f (n)) (Def. of O Notation)
7 Hence, g (n) is Θ(f (n))
T (n) = Θ(nlogb (a) ) + g (n) = Θ(nlogb (a) ) + Θ(f (n)) = Θ(f (n))

Hint: f (n/b 2 ) ≤ ( ca ) f (n/b) ≤ ( ca ) ( ca )f (n) =⇒ a2 f (n/b 2 ) ≤ c 2 f (n).


Dr. Praveen (Mahindra University) DAA CS203 46 / 60
Master’s Theorem Proof: Case3
Plog (n)−1 j
Given g (n) = j=0b a f (n/b j ) and a f (n/b) ≤ c f (n) for some
constant c < 1 and all sufficiently large n, g (n) = Θ(f (n)).
Plog (n)−1 j
1 Proof: g (n) = j=0b a f (n/b j )
2 g (n) = f (n)+a1 f (n/b 1 )+a2 f (n/b 2 )+...+alogb (n)−1 f (n/b logb (n)−1 )
3 g (n) > f (n) =⇒ g (n) is Ω(f (n)) (Def. of Ω Notation)
4 g (n) ≤ f (n) + c 1 f (n) + c 2 f (n) + ... + c logb (n)−1 f (n)
g (n) ≤ ∞ j
P
j=0 c f (n)
5

1
6 g (n) ≤ 1−c
f (n) =⇒ g (n) is O(f (n)) (Def. of O Notation)
7 Hence, g (n) is Θ(f (n))
T (n) = Θ(nlogb (a) ) + g (n) = Θ(nlogb (a) ) + Θ(f (n)) = Θ(f (n))

Hint: f (n/b 2 ) ≤ ( ca ) f (n/b) ≤ ( ca ) ( ca )f (n) =⇒ a2 f (n/b 2 ) ≤ c 2 f (n).


Dr. Praveen (Mahindra University) DAA CS203 46 / 60
Master’s Theorem Proof: Case3
Plog (n)−1 j
Given g (n) = j=0b a f (n/b j ) and a f (n/b) ≤ c f (n) for some
constant c < 1 and all sufficiently large n, g (n) = Θ(f (n)).
Plog (n)−1 j
1 Proof: g (n) = j=0b a f (n/b j )
2 g (n) = f (n)+a1 f (n/b 1 )+a2 f (n/b 2 )+...+alogb (n)−1 f (n/b logb (n)−1 )
3 g (n) > f (n) =⇒ g (n) is Ω(f (n)) (Def. of Ω Notation)
4 g (n) ≤ f (n) + c 1 f (n) + c 2 f (n) + ... + c logb (n)−1 f (n)
g (n) ≤ ∞ j
P
j=0 c f (n)
5

1
6 g (n) ≤ 1−c
f (n) =⇒ g (n) is O(f (n)) (Def. of O Notation)
7 Hence, g (n) is Θ(f (n))
T (n) = Θ(nlogb (a) ) + g (n) = Θ(nlogb (a) ) + Θ(f (n)) = Θ(f (n))

Hint: f (n/b 2 ) ≤ ( ca ) f (n/b) ≤ ( ca ) ( ca )f (n) =⇒ a2 f (n/b 2 ) ≤ c 2 f (n).


Dr. Praveen (Mahindra University) DAA CS203 46 / 60
Master’s Theorem Proof: Case3
Plog (n)−1 j
Given g (n) = j=0b a f (n/b j ) and a f (n/b) ≤ c f (n) for some
constant c < 1 and all sufficiently large n, g (n) = Θ(f (n)).
Plog (n)−1 j
1 Proof: g (n) = j=0b a f (n/b j )
2 g (n) = f (n)+a1 f (n/b 1 )+a2 f (n/b 2 )+...+alogb (n)−1 f (n/b logb (n)−1 )
3 g (n) > f (n) =⇒ g (n) is Ω(f (n)) (Def. of Ω Notation)
4 g (n) ≤ f (n) + c 1 f (n) + c 2 f (n) + ... + c logb (n)−1 f (n)
g (n) ≤ ∞ j
P
j=0 c f (n)
5

1
6 g (n) ≤ 1−c
f (n) =⇒ g (n) is O(f (n)) (Def. of O Notation)
7 Hence, g (n) is Θ(f (n))
T (n) = Θ(nlogb (a) ) + g (n) = Θ(nlogb (a) ) + Θ(f (n)) = Θ(f (n))

Hint: f (n/b 2 ) ≤ ( ca ) f (n/b) ≤ ( ca ) ( ca )f (n) =⇒ a2 f (n/b 2 ) ≤ c 2 f (n).


Dr. Praveen (Mahindra University) DAA CS203 46 / 60
Master’s Theorem Proof: Case3
Plog (n)−1 j
Given g (n) = j=0b a f (n/b j ) and a f (n/b) ≤ c f (n) for some
constant c < 1 and all sufficiently large n, g (n) = Θ(f (n)).
Plog (n)−1 j
1 Proof: g (n) = j=0b a f (n/b j )
2 g (n) = f (n)+a1 f (n/b 1 )+a2 f (n/b 2 )+...+alogb (n)−1 f (n/b logb (n)−1 )
3 g (n) > f (n) =⇒ g (n) is Ω(f (n)) (Def. of Ω Notation)
4 g (n) ≤ f (n) + c 1 f (n) + c 2 f (n) + ... + c logb (n)−1 f (n)
g (n) ≤ ∞ j
P
j=0 c f (n)
5

1
6 g (n) ≤ 1−c
f (n) =⇒ g (n) is O(f (n)) (Def. of O Notation)
7 Hence, g (n) is Θ(f (n))
T (n) = Θ(nlogb (a) ) + g (n) = Θ(nlogb (a) ) + Θ(f (n)) = Θ(f (n))

Hint: f (n/b 2 ) ≤ ( ca ) f (n/b) ≤ ( ca ) ( ca )f (n) =⇒ a2 f (n/b 2 ) ≤ c 2 f (n).


Dr. Praveen (Mahindra University) DAA CS203 46 / 60
Master’s Theorem Proof: Case3
Plog (n)−1 j
Given g (n) = j=0b a f (n/b j ) and a f (n/b) ≤ c f (n) for some
constant c < 1 and all sufficiently large n, g (n) = Θ(f (n)).
Plog (n)−1 j
1 Proof: g (n) = j=0b a f (n/b j )
2 g (n) = f (n)+a1 f (n/b 1 )+a2 f (n/b 2 )+...+alogb (n)−1 f (n/b logb (n)−1 )
3 g (n) > f (n) =⇒ g (n) is Ω(f (n)) (Def. of Ω Notation)
4 g (n) ≤ f (n) + c 1 f (n) + c 2 f (n) + ... + c logb (n)−1 f (n)
g (n) ≤ ∞ j
P
j=0 c f (n)
5

1
6 g (n) ≤ 1−c
f (n) =⇒ g (n) is O(f (n)) (Def. of O Notation)
7 Hence, g (n) is Θ(f (n))
T (n) = Θ(nlogb (a) ) + g (n) = Θ(nlogb (a) ) + Θ(f (n)) = Θ(f (n))

Hint: f (n/b 2 ) ≤ ( ca ) f (n/b) ≤ ( ca ) ( ca )f (n) =⇒ a2 f (n/b 2 ) ≤ c 2 f (n).


Dr. Praveen (Mahindra University) DAA CS203 46 / 60
Master’s Theorem Proof: Case3
Plog (n)−1 j
Given g (n) = j=0b a f (n/b j ) and a f (n/b) ≤ c f (n) for some
constant c < 1 and all sufficiently large n, g (n) = Θ(f (n)).
Plog (n)−1 j
1 Proof: g (n) = j=0b a f (n/b j )
2 g (n) = f (n)+a1 f (n/b 1 )+a2 f (n/b 2 )+...+alogb (n)−1 f (n/b logb (n)−1 )
3 g (n) > f (n) =⇒ g (n) is Ω(f (n)) (Def. of Ω Notation)
4 g (n) ≤ f (n) + c 1 f (n) + c 2 f (n) + ... + c logb (n)−1 f (n)
g (n) ≤ ∞ j
P
j=0 c f (n)
5

1
6 g (n) ≤ 1−c
f (n) =⇒ g (n) is O(f (n)) (Def. of O Notation)
7 Hence, g (n) is Θ(f (n))
T (n) = Θ(nlogb (a) ) + g (n) = Θ(nlogb (a) ) + Θ(f (n)) = Θ(f (n))

Hint: f (n/b 2 ) ≤ ( ca ) f (n/b) ≤ ( ca ) ( ca )f (n) =⇒ a2 f (n/b 2 ) ≤ c 2 f (n).


Dr. Praveen (Mahindra University) DAA CS203 46 / 60
Master’s Theorem Proof: Case3
Plog (n)−1 j
Given g (n) = j=0b a f (n/b j ) and a f (n/b) ≤ c f (n) for some
constant c < 1 and all sufficiently large n, g (n) = Θ(f (n)).
Plog (n)−1 j
1 Proof: g (n) = j=0b a f (n/b j )
2 g (n) = f (n)+a1 f (n/b 1 )+a2 f (n/b 2 )+...+alogb (n)−1 f (n/b logb (n)−1 )
3 g (n) > f (n) =⇒ g (n) is Ω(f (n)) (Def. of Ω Notation)
4 g (n) ≤ f (n) + c 1 f (n) + c 2 f (n) + ... + c logb (n)−1 f (n)
g (n) ≤ ∞ j
P
j=0 c f (n)
5

1
6 g (n) ≤ 1−c
f (n) =⇒ g (n) is O(f (n)) (Def. of O Notation)
7 Hence, g (n) is Θ(f (n))
T (n) = Θ(nlogb (a) ) + g (n) = Θ(nlogb (a) ) + Θ(f (n)) = Θ(f (n))

Hint: f (n/b 2 ) ≤ ( ca ) f (n/b) ≤ ( ca ) ( ca )f (n) =⇒ a2 f (n/b 2 ) ≤ c 2 f (n).


Dr. Praveen (Mahindra University) DAA CS203 46 / 60
Exercise Problems

Give asymptotic upper and lower bounds for T (n) in each of the following
recurrences. Assume that T (n) is constant for n ≥ 2. Make your bounds
as tight as possible, and justify your answers.
a. 2T (n/2) + n4 .
b. T (7n/10) + n.
c. 16T (n/4) + n2 .
d. 7T (n/3) + n2 .
e. 7T (n/2) + n2 .

f. 2T (n/4) + n.
g. T (n − 2) + n2 .

h. T ( n) + 1.

i. 2T ( n) + log n.

Dr. Praveen (Mahindra University) DAA CS203 47 / 60


Substitution Method for Solving Recurrences
I Guess the form of solution
I Use mathematical induction to find the constants and show that the
solution works
Solve the recurrence relation T (n) = 2 T (b n2 c) + n.
Proof : Guess the solution is T(n) = O(n log n).
Use mathematical induction, to prove T(n) ≤ c · n log n, ∀n, n ≥ n0 .
Assume that this bound holds ∀m, m < n
Consider m = b n2 c, then T (b n2 c) ≤ c · b n2 c log b n2 c
T (n) ≤ 2(c · b n2 c log b n2 c) + n ≤ c · n log b n2 c + n
T (n) ≤ c · n log n − c · n log 2 + n
T (n) ≤ c · n log n − cn + n
T (n) ≤ c · n log n (c = 2 and ∀n, n ≥ 2)
Verify the the base case.
Similarly, solve the following recurrence relations:
1 T (n) = 2 T (b n c + 17) + n
2
2 T (n) = T (b n c) + T (d n e) + 1
2 2
Dr. Praveen (Mahindra University) DAA CS203 48 / 60
Binary Trees

Dr. Praveen (Mahindra University) DAA CS203 49 / 60


Recursion Tree Method

Dr. Praveen (Mahindra University) DAA CS203 50 / 60


Recursion Tree Method

Assume that T(n) is O(n log 3 n) (i.e. ≤ d n log 3 n)


2 2

n n 2n 2n
T (n) ≤ d( ) lg( ) + d( ) lg( ) + cn
3 3 3 3
n 2n
= d( )(lg(n) − lg(3)) + d( )(lg(2n) − lg (3)) + cn
3 3
2n
= dn lg(n) + d( ) − dn lg(3) + cn
3
2n
≤ dn lg(n), if d( ) − dn lg(3) + cn ≤ 0
3
c
≤ dn lg(n), if d ≥
(lg(3) − 23 )

Dr. Praveen (Mahindra University) DAA CS203 51 / 60


Recursion Tree Method

Assume that T(n) is Ω(n log3 n) (i.e. ≥ d n log3 n)


n n 2n 2n
T (n) ≥ d( ) lg( ) + d( ) lg( ) + cn
3 3 3 3
n 2n
= d( )(lg(n) − lg(3)) + d( )(lg(2n) − lg (3)) + cn
3 3
2n
= dn lg(n) + d( ) − dn lg(3) + cn
3
2n
≥ dn lg(n), if d( ) − dn lg(3) + cn ≥ 0
3
c
≥ dn lg(n), if d ≤
(lg(3) − 23 )
≥ dn lg(n), if d ≤ 3c

Dr. Praveen (Mahindra University) DAA CS203 52 / 60


Use a recursion tree to give an asymptotically tight solution to the
recurrence T (n) = T (αn) + T ((1 − α)n) + cn, where α is a constant
in the range 0 < α < 1 and c > 0 is also a constant.
Proof: Let α ≥ 1 − α. Our guess for an upper bound is

T (n) ≤ dn log 1 n, ∀n ≥ n0 ,
α

where d and n0 are positive constants.


Substituting into the recurrence yields

T (n) ≤ dαn lg(αn) + d(1 − α)n lg((1 − α)n) + cn


= dαn lg α + dαn lg n + d(1 − α)n lg(1 − α) + d(1 − α)n lg n + cn
= dαn lg α + dαn lg n + d(1 − α)n lg(1 − α) + dn lg n − dαn lg n + cn
= dn lg n + dn(α lg α + (1 − α) lg(1 − α)) + cn
≤ dn lg n, if d(α lg α + (1 − α) lg(1 − α)) + c ≤ 0.

Dr. Praveen (Mahindra University) DAA CS203 53 / 60


The tree is perfect until level log 1 n. So, our guess for a lower bound is
1−α

T (n) ≥ dn log 1 n, n ≥ n0 ,
1−α

where d, and n0 are positive constants.


Substituting into the recurrence yields

T (n) ≥ dαn lg(αn) + d(1 − α)n lg((1 − α)n) + cn


= dαn lg α + dαn lg n + d(1 − α)n lg(1 − α) + d(1 − α)n lg n + cn
= dαn lg α + dαn lg n + d(1 − α)n lg(1 − α) + dn lg n − dαn lg n + cn
= dn lg n + dn(α lg α + (1 − α) lg(1 − α)) + cn
≥ dn lg n, if d(α lg α + (1 − α) lg(1 − α)) + c ≥ 0

Homework 1: Solve the recurrence relation T (n) = 3T ( n4 ) + cn2 using


Recursion Tree Method.
Homework 2: Use a recursion tree to determine a good asymptotic
upper bound on the recurrence T(n)=T(n-1)+T(n/2)+n. Use
substitution method to verify your answer.
Dr. Praveen (Mahindra University) DAA CS203 54 / 60
Algorithm

Figure 1: Abu Jafar Mohammad ibn Musa al Khowarizmi

Properties (Characteristics) of Algorithms:


I Input: An algorithm takes Zero or More external quantities.
I Output: An algorithm produces at least One output.
I Definiteness: Each instruction of an algorithm is Unambiguous.
I Finiteness: In all possible cases an algorithm has to terminate
after finite number of steps.
I Effectiveness: Each instruction of an algorithm must be
carried-out manually in a finite amount of time.
Dr. Praveen (Mahindra University) DAA CS203 55 / 60
How a Study on Algorithms will be Useful?

I To know how to devise algorithms.

I To show how to validate algorithms: An algorithm


produces correct output for all possible legal inputs.
I To provide analysis on algorithms:
I Priori Analysis: The process of determining computing time
and storage space of an algorithm.
I Posteriori Analysis: The process of measuring the
performance (time and footprint) of a correct program by
considering different data sets.

Dr. Praveen (Mahindra University) DAA CS203 56 / 60


Time and Space Complexities

Time Complexity of an algorithm refers to the amount of computer time


the algorithm needs to run to completion.
Space Complexity of an algorithm refers to the amount of memory the
algorithm needs to run to completion.
Space requirement can be divided into two parts:
I Fixed-part: Independent of the characteristics of the inputs and
outputs i.e., Space for code and space for simple variables (or
fixed-size variables).
I Variable-part: Dependent on the characteristics of the program (size
and type of the input) and recursion stack space.

Dr. Praveen (Mahindra University) DAA CS203 57 / 60


Step and Step Counts

Step is a segment of algorithm (program) and its execution time is


independent of the instance characteristics.

I Best-case Step Count: It is the minimum number of steps that can


be executed for given parameters.
I Worst-case Step Count: It is the maximum number of steps that
can be executed for given parameters.
I Average-case Step Count: It is the average number of steps that
are executed by considering the different instances.

Dr. Praveen (Mahindra University) DAA CS203 58 / 60


Linear Search Algorithm

An algorithm searches an unsorted array of elements for the required


element.
Alg. LinearSearch (list, required)
for each item in the list
if an item is the required one
return the item’s location
end if
end for

Dr. Praveen (Mahindra University) DAA CS203 59 / 60


Binary Search Algorithm
Alg. BinarySearch
List - Sorted List
Size - Size of the List
Required - item to be searched
lowerBound = 0
upperBound = Size − 1
while lowerBound < upperBound
midPoint = lowerBound + (upperBound − lowerBound)/2
if List[midPoint] = Required
return midPoint
else if List[midPoint] < Required
lowerBound = midPoint + 1
else
upperBound = midPoint − 1
end while
return -1

Dr. Praveen (Mahindra University) DAA CS203 60 / 60

You might also like