Apex Institute of Technology
Department of Computer Science & Engineering
Bachelor of Engineering (Computer Science & Engineering)
Design and Analysis of Algorithms– (21CSH-282)
Prepared By: Mr. Vikas Kumar (E13657)
05/25/2024 DISCOVER . LEARN . EMPOWER
1
Content
OBST
Matrix Chain Multiplication,
2
Optimal Binary Search Tree
A set of integers are given in the sorted order and another array freq to
frequency count.
Our task is to create a binary search tree with those data to find the
minimum cost for all searches.
An auxiliary array cost[n, n] is created to solve and store the solution of
subproblems.
Cost matrix will hold the data to solve the problem in a bottom-up
manner.
3
Example
Input:
The key values as node and the frequency.
Keys = {10, 12, 20}
Frequency = {34, 8, 50}
Output:
The minimum cost is 142.
4
Possible BST from the given values
For case 1, the cost is: (34*1) + (8*2) + (50*3) = 200
For case 2, the cost is: (8*1) + (34*2) + (50*2) = 176.
Similarly for case 5, the cost is: (50*1) + (34 * 2) + (8 * 3) = 142
(Minimum)
5
Algorithm
6
Matrix Chain Multiplication
It is a Method under Dynamic Programming in which previous
output is taken as input for next.
Here, 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
7
Example
Let us have 3 matrices, A1,A2,A3 of order (10 x 100), (100 x 5) and
(5 x 50) respectively.
Three Matrices can be multiplied in two ways:
A1,(A2,A3): First multiplying(A2 and A3) then multiplying and
resultant withA1.
(A1,A2),A3: First multiplying(A1 and A2) then multiplying and
resultant withA3.
No of Scalar multiplication in Case 1 will be:
(100 x 5 x 50) + (10 x 100 x 50) = 25000 + 50000 = 75000
No of Scalar multiplication in Case 2 will be:
(100 x 10 x 5) + (10 x 5 x 50) = 5000 + 2500 = 7500
8
Number of ways for parenthesizing the matrices:
There are very large numbers of ways of parenthesizing these
matrices. If there are n items, there are (n-1) ways in which the
outer most pair of parenthesis can place.
(A1) (A2,A3,A4,................An)
Or (A1,A2) (A3,A4 .................An)
Or (A1,A2,A3) (A4 ...............An)
........................
Or(A1,A2,A3.............An-1) (An)
It can be observed that after splitting the kth matrices, we are left
with two parenthesized sequence of matrices: one consist 'k'
matrices and another consist 'n-k' matrices.
9
Development of Dynamic Programming Algorithm
Characterize the structure of an optimal solution.
Define the value of an optimal solution recursively.
Compute the value of an optimal solution in a bottom-up fashion.
Construct the optimal solution from the computed information.
10
Algortihm
11
ALGORITHM
12
Example
13
References
Text books:
Cormen, Leiserson, Rivest, Stein, “Introduction to Algorithms”, Prentice
Hall of India, 3rd edition 2012. problem, Graph coloring.
Websites:
https://www.tutorialspoint.com/Optimal-Binary-Search-Tree
14
THANK YOU