Don Bosco Institute of Technology
Advanced Data structure and Analysis
Module 4: Dynamic Algorithms
Prof. Prasad Padalkar
Topics
● Introduction to Dynamic Algorithms
○ 0/1 Knapsack
○ All pair shortest path
○ Travelling Salesman problem
○ Matrix Chain Multiplication
○ Optimal Binary Search tree
Binary Search Tree – Advantage
40
20 60
10 30
50 70
55 - Search No. of Comparisons : 55 & 40 - 1st
60 & 55 - 2nd
50 & 55 - 3rd
Null & 55 - 4th
Binary Search Tree
Consider 3 keys :- 10, 20, 30
Binary Search Tree
30
20
10
Binary Search Tree
Now if along with the keys their is also frequency of search defined, then we need to find
the optimal tree structure for the given frequency.
E.g. Keys 10 20 30
Frequency 3 5 7
Case 1: Cost of search = 1 x 3 + 2 x 5 + 3 x 7 = 34
Case 2: Cost of search = 1 x 3 + 2 x 7 + 3 x 5 = 32
Case 3: Cost of search = 1 x 5 + 2 x 3 + 2 x 7 = 25
Case 4: Cost of search = 1 x 7 + 2 x 3 + 3 x 5 = 28
Case 5: Cost of search = 1 x 7 + 2 x 5 + 3 x 3 = 26
Optimal Binary Search Tree
Consider a problem for finding the optimal BST for following:
Index 1 2 3 4
Keys 10 20 30 40
Frequency 4 2 6 3
Optimal Binary Search Tree
j
0 1 2 3 4
0
0 4 81 203 263
1
0 2 103 163
i
2
0 6 123
3
0 3
4
0
Optimal Binary Search Tree
Step 1: Set all diagonal elements C[0,0] — C[4,4] = 0
Step 2: Fill all the diagonal elements j - i = 1 i.e. C[0,1], C[1,2], C[2,3], C[3,4]
C[0,1] = index 1 ⇒ freq(10) = 4 , C[1,2] = index 2 ⇒ freq(20) = 2 and so on.
Step 3: Fill all the diagonal elements j - i = 2 i.e. C[0,2], C[1,3], C[2,4]
C[0,2] = index 1 & 2
10 1x4 20 1x2 1x4+2x2=8
1 x 2 + 2 x 4 = 10
min(8, 10)
20 2x 2
10 2x4
Optimal Binary Search Tree
Formula for C[0,2]
C[i,j] = min { C[i, k-1] + C[k, j] + W(i,j)} for i <k <= j
If root is index 1 k = 1 ⇒ C[0,2] = C[0,0] + C[1,2] +W(0,2)
= 0 + 2 + (4 +2) = 8
If root is index 2 k = 2⇒ C[0,2] = C[0,1] + C[2,2] + W(0,2)
= 4 + 0 + (4 + 2) = 10
min(8,10) = 8 and the minimum is at index 1.
Optimal Binary Search Tree
Optimal Binary Search Tree
Optimal Binary Search Tree
Optimal Binary Search Tree
Optimal Binary Search Tree
Optimal Binary Search Tree
Creating the
optimal BST
Optimal Binary Search Tree
Time Complexity calculation is similar to Matrix Chain multiplication and time
complexity O(n^3)
Space complexity is O(n^2)
Thank you