CS3401 Algorithms Unit III
CS3401 Algorithms Unit III
Mr.C.Karthikeyan
Assistant Professor, Department of CSE
Karpagam Institute of Technology
CS3401 Algorithms | Unit III 1
Course Objectives
• Understand and apply the algorithm analysis techniques on
searching and sorting algorithms
• Critically analyze the efficiency of graph algorithms
• Understand different algorithm design techniques
• Solve programming problems using state space tree
• Understand the concepts behind NP Completeness,
Approximation algorithms and randomized algorithms.
CS3401 Algorithms | Unit III 2
Course Outcomes
• Develop algorithms for various computing problems and time and space
complexity analysis.
• Apply graph algorithms to solve problems and analyze their efficiency.
• Analyze the different design techniques like divide and conquer, dynamic
programming and Greedy techniques to solve problems.
• Make use of the state space tree method for solving computational
problems.
• Solve problems using approximation algorithms and randomized
algorithms.
CS3401 Algorithms | Unit III 3
Course Syllabus
COURSE SYLLABUS
Mr.C.Karthikeyan
Assistant Professor, Department of CSE
Karpagam Institute of Technology
CS3401 Algorithms | Unit III 7
Divide and Conquer methodology: Finding maximum and minimum -
solution to solution to
sub problem 1 sub problem 2
left and right end. 4. Finally, get the overall maximum and
minimum by comparing the min and max of
2. Divide array by calculating mid index both halves.
i.e. mid = (l + r)/2 if(l==r){
if(X[mid]<min)
3. Recursively find the maximum and min=X[mid];
if(X[mid]>max)
minimum of left part by calling the max=X[mid];
return;
same function. }
i.e. maxmin(X, l, mid)
CS3401 Algorithms | Unit III
5. Store max and min and return it. 13
Finding Maximum and Minimum
Program:
void maxmin(int X[],int l,int r){
int mid=(l+r)/2;
#include<stdio.h> if(l==r){
void maxmin(int[], int, int); if(X[mid]<min)
int max,min; min=X[mid];
int main(){ if(X[mid]>max)
int X[]={134,123,111,5115,48}; max=X[mid];
int l=0,r=4; return;
max=min=X[0]; }
maxmin(X,l,r); maxmin(X,l,mid);
printf("%d %d",max,min); maxmin(X,mid+1,r);}
return 0;
}
CS3401 Algorithms | Unit III
14
Finding Maximum and Minimum : Analysis
There are two recursive calls made in this algorithm, for each half divided sub list.
Time required for computing max and min will be
T(n) = T(n/2)+ T(n/2)+2
T(n) = 1
T(n) = 2T(n/2) + 2
= 2 [ 2T(n/4) + 2] + 2
= 2(2[2T(n/8) + 2] + 2) +2
= 8T(n/8) + 10
Continuing in this fashion a recursive equation can be obtained. If we put n= 2k
− +
T(n) = 2 k-1 T(2) + 𝑘−1
𝑖=1 2𝑖 = 2
𝑘 1 2𝑘 − 2
T(n) = 3n / 2-2
Ignoring the order of magnitude, The time complexity is O(n)
CS3401 Algorithms | Unit III
15
Merge Sort
solution to solution to
sub problem 1 sub problem 2
35 21 40 2 8 75 11
Is Left Index <
arrayYes
Divide the m=0+(3-1)/2
in to Two halves
Right Index
35 21 40 2 8 75 11
Is Left Index <
Divide the arrayYes
m=leftindex+(rightindex-1)/2
in to Two halves
Right Index
35 21 40 2 8 75
CS3401 Algorithms | Unit III 27
Merge Sort : Example
35 21 40 2 8 75
21 35 2 40 8 75 11
2 21 35 40 8 11 75
2 8 11 21 35 40 75
Time Complexity
• The best case complexity is O(log n).
• The worst case complexity is O(n*log n).
• The average case complexity is O(n*log n).
5 3 1 9 8
j j
1 3 5 8 9
j j
Since
Since
3 >3 1,
> 1,
i stops
j Moves
there Since
Since 98 >> 8,
9, ji Moves
stops
Time Complexity
• The best case complexity is O(n*log n).
• The worst case complexity is O(n2).
• The avearage case complexity is O(n*log n).
• Memoization is a top-down approach where we cache the results of function calls and
return the cached result if the function is called again with the same inputs.
• It is used when we can divide the problem into sub problems and the sub problems have
overlapping sub problems.
• Memoization is typically implemented using recursion and is well-suited for problems that
have a relatively small set of inputs.
Bottom-up approach
• Tabulation is a bottom-up approach where we store the results of the subproblems in a
table and use these results to solve larger subproblems until we solve the entire problem.
• It is used when we can define the problem as a sequence of subproblems and the sub
problems do not overlap.
• Tabulation is typically implemented using iteration and is well-suited for problems that
CS3401 Algorithms | Unit III 57
have a large set of inputs.
Dynamic programming : Introduction
58
Comparison of memoization and tabulation:
• The Matrix Chain multiplication problem is the classic dynamic programming problem.
• Chain means one matrix's column is equal to the second matrix's row [always].
In general:
• If A = ⌊aij⌋ is a p x q matrix B = ⌊bij⌋ is a q x r matrix C = ⌊cij⌋ is a p x r matrix then
• To construct an optimal solution, let us define s [i,j] to be the value of 'k' at which we
can split the product Ai Ai+1 .....Aj.
Step 3: Computing Optimal Costs
• To obtain an optimal parenthesization i.e. s [i, j] = k such that m [i,j] = m [i, k] + m [k +
1, j] + pi-1 pk pj
Initial Tables :
0 1 2 3 4
1 2 3 4
1 0 0.1
1 1
2 0 0.2
2 2
3 0 0.4
3 3
4 0 0.3
4 4
5 0
83
Optimal Binary Search Trees (OBST)
84
Using formula : C[i,i-1]=0 so C[1,0]=C[2,1]=C[3,2]=C[4,3]=C[5,4]=C[6,5]=0
C[i,i]=Pi ==> C[1,1]=0.1; C[2,2]=0.2 ; C[3,3]=0.4; C[4,4]=0.3
R[i,i]=i==> R[1,1]=1 ; C[2,2]=2 ; C[3,3]=3; C[4,4]=4
Let us compute C[i,j] using the following formula:
𝒋
C[i,j]=min { C[i.k-1] + C[k+1,j] + 𝒔=𝒊 𝑷𝒔
C[1,0]+C[2,2]+P1+P2 C[2,1]+C[3,3]+P2+P3
K=1 =0+0.2+0.1+0.2 K=2 =0+0.4+0.2+0.4
=0.5 =1.0
C[1,2] C[2,3]
C[1,1]+C[3,2]+P1+P2 C[2,2]+C[4,3]+P2+P3
K=2 =0.1+0+0.1+0.2 K=3 =0.2+0+0.2+0.4
=0.4 =0.8
Minimum value at K=2 Minimum value at K=3
C[1,2]=0.4 and R[1,2]=2 C[2,3]=0.8 and R[2,3]=3
84
Optimal Binary Search Trees (OBST)
85
C[1,0]+C[2,3]+P1+P2+P3
K=1 =0+0.8+0.1+0.4+0.2
C[3,2]+C[4,4]+P3+P4
=1.5
K=3 =0+0.3+0.4+0.3
C[1,1]+C[3,3]+P1+P2+P3
=1.0
C[3,4] C[1,3] K=2 =0.1+0.4+0.1+0.4+0.2
C[3,3]+C[5,4]+P3+P4
=1.2
K=4 =0.4+0+0.4+0.3
C[1,2]+C[4,3]+P1+P2+P3
=1.1
K=3 =0.4+0+0.1+0.4+0.2
Minimum value at K=3
=1.1
C[3,4]=1.0 and R[3,4]=3
Minimum value at K=3
C[1,3]=1.1 and R[1,3]=3
85
Optimal Binary Search Trees (OBST)
86
C[1,0]+C[2,4]+P1+P2+P3+P4
K=1 =0+1.4+0.1+0.2+0.4+0.3
C[2,1]+C[3,4]+P2+P3+P4
=2.4
K=2 =0+1.0+0.2+0.4+0.3
C[1,1]+C[3,4]+P1+P2+P3+P4
=1.9
K=2 =0.1+1.0+0.1+0.2+0.4+0.3
C[2,2]+C[4,4]+P2+P3+P4
=2.1
C[2,4] K=3 =0.2+0.3+0.2+0.4+0.3 C[1,4]
C[1,2]+C[4,4]+P1+P2+P3+P4
=1.4
K=3 =0.4+0.3+0.1+0.2+0.4+0.3
C[2,3]+C[5,4]+P2+P3+P4
=1.7
K=4 =0.8+0+0.2+0.4+0.3
C[1,3]+C[5,4]+P1+P2+P3+P4
=1.7
K=4 =1.1+0+0.1+0.2+0.4+0.3
Minimum value at K=3
=2.1
C[2,4]=1.4 and R[2,4]=3
Minimum value at K=3
C[1,4]=1.7 and R[1,4]=3 86
Optimal Binary Search Trees (OBST)
87
Final Tables:
Cost table
0 1 2 3 4 Root table
1 0 0.1 0.4 1.1 1.7 1 2 3 4
2 0 0.2 0.8 1.4 1 1 2 3 3
3 0 0.4 1.0 2 2 3 3
4 0 0.3 3 3 3
5 0 4 4
To build a tree R[1,4]=3
Here i=1,j=4 and k=3
93
Activity Selection Problem : Example
94
Given 10 activities along with their start and end time as
S = (A1 A2 A3 A4 A5 A6 A7 A8 A9 A10)
Si = (1,2,3,4,7,8,9,9,11,12)
fi = (3,5,4,7,10,9,11,13,12,14)
Solution:
• Arranging the activities in
increasing order of end time
94
Activity Selection Problem : Example
95
• Now, schedule A1
• Next schedule A3 as A1 and A3 are non-interfering.
• Next skip A2 as it is interfering.
• Next, schedule A4 as A1 A3 and A4 are non-interfering, then next, schedule A6 as
A1 A3 A4 and A6 are non-interfering.
• Skip A5 as it is interfering.
• Next, schedule A7 as A1 A3 A4 A6 and A7 are non-interfering.
• Next, schedule A9 as A1 A3 A4 A6 A7 and A9 are non-interfering.
• Skip A8 as it is interfering.
• Next, schedule A10 as A1 A3 A4 A6 A7 A9 and A10 are non-interfering.
• Thus the final Activity schedule is:
95
Optimal Merge Pattern
97
Optimal Merge Pattern
Algorithm: TREE (n) 98
for i := 1 to n – 1 do
declare new node
node.leftchild := least (list)
node.rightchild := least (list)
node.weight) := ((node.leftchild).weight) + ((node.rightchild).weight)
insert (list, node);
return least (list);
Example :
• Consider the given files, f1, f2, f3, f4 and f5 with 20, 30, 10, 5 and 30 number of elements
respectively.
• If merge operations are performed according to the provided sequence, then
M1 = merge f1 and f2 => 20 + 30 = 50
M2 = merge M1 and f3 => 50 + 10 = 60
M3 = merge M2 and f4 => 60 + 5 = 65
M4 = merge M3 and f5 => 65 + 30 = 95
98
Hence, the total number of operations is 50 + 60 + 65 + 95 = 270
Optimal Merge Pattern
Sorting the numbers according to their size in an ascending order, we 99
get the following
sequence : f4, f3, f1, f2, f5
Hence, merge operations can be performed on this sequence
M1 = merge f4 and f3 => 5 + 10 = 15
M2 = merge M1 and f1 => 15 + 20 = 35
M3 = merge M2 and f2 => 35 + 30 = 65
M4 = merge M3 and f5 => 65 + 30 = 95
Therefore, the total number of operations is 15 + 35 + 65 + 95 = 210
Obviously, this is better than the previous one.
Solving the problem the spcified algorithm:
Initial Set
99
Optimal Merge Pattern 10
0
Step 3
Step 1
Step 2
Step 4
100
Hence, the solution takes 15 + 35 + 60 + 95 = 205 number of comparisons.
Huffman Trees : Introduction
10
1
• The huffman trees are constructed for encoding a given text, each character is associated
with some bit sequence. Such a bit sequence is called code word.
Encoding of two types:
• Fixed length encoding : It is kind of encoding in which each character is associated with
a bit string of some fixed length.
• Variable length encoding : It is kind of encoding in which each character is associated
with a code word of different length.
Example:
Character A B C D E
Probability 0.40 0.1 0.25 0.2 0.15
101
Huffman Trees : Introduction
10
2
102
Huffman Trees : Introduction
10
3
103
Huffman Trees : Introduction
10
4
104