DAA CS203 2021 Module1 Update4
DAA CS203 2021 Module1 Update4
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.
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.
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.
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
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
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
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
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).
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
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
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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 })
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 })
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 })
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
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
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).
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).
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).
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).
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))
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))
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))
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))
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))
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))
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))
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))
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.
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 )
T (n) ≤ dn log 1 n, ∀n ≥ n0 ,
α
T (n) ≥ dn log 1 n, n ≥ n0 ,
1−α