Design and Analysis of Algorithm(18CS42)
Module 4
Dynamic programming
Lecture Presentation
Divya K S
Dept. of CSE
OPTIMAL BINARY SEARCH TREE
• Basic definitions
• Concepts of Optimal Binary Search Tree
• Example Problem
• Assignment
BINARY SEARCH TREE
• In A Binary Search Tree, The Value Of All The Nodes In The Left Sub-tree Is Less Than The Value Of The
Root.
• Similarly, Value Of All The Nodes In The Right Sub-tree Is Greater Than Or Equal To The Value Of The
Root.
• This Rule Will Be Recursively Applied To All The Left And Right Sub-trees Of The Root.
EXAMPLE
KEYS 1, 2,3
BST 3BST 2
BST 4
BST 1
BST
5
DEPTH=0,HEGHT=1
DEPTH=1,HEIGHT=2
DEPTH=2 HEIGHT=3
HEIGHT*FREQUEN
1*1+2*2+2*6=17
Cost: 13
• Using dynamic programming find the optimal binary search tree for
the given data.
Keys 10 12 16 21
Frequency 4 2 6 3
0 1 2 3 4
1 0 4
2 0 2
3 0 6
4 0 3
5 0
1 2 3 4
Keys 10 12 16 21
Frequency 4 2 6 3
Solution: Solution matrix
Formula:
0 1 2 3 4
1 0 4 8(1)
2 0 2 10(3
)
3 0 6
4 0 3
5 0
C[1,2]=
K=1
C[1,0]+C[2,2]=0+2=2---------
ROOT(1)
K=2
C[1,1]+C[3,2]=4+0=4
+ 6===8
C[2,3]=
I=2
J=3
K=2
C[2,1]+C[3,3]=0+6=6
K=3
C[2,2]+C[4,3]=2+0=2
+8===10
0 1 2 3 4
1 0 4 8(1) 20(3
)
26(3
)
2 0 2 10(3
)
16(3
)
3 0 6 12(3
)
4 0 3
5 0
C[3,4]
k=3
C[3,2]+C[4,4]=0+3=3
i=3,j=4 k=4
C[3,3]+C[5,4]=6+0=6
Frequency=9
=====12ROOT(3)
Minimum cost =2
16
10 21
12 Optimal Binary search
1*6=6
2*4+2*3=14
3*2=6
ALGORITHM OPTIMAL BINARY SEARCH TREE
ASSIGNMENT

Lecture optimal binary search tree

  • 1.
    Design and Analysisof Algorithm(18CS42) Module 4 Dynamic programming Lecture Presentation Divya K S Dept. of CSE
  • 2.
    OPTIMAL BINARY SEARCHTREE • Basic definitions • Concepts of Optimal Binary Search Tree • Example Problem • Assignment
  • 3.
    BINARY SEARCH TREE •In A Binary Search Tree, The Value Of All The Nodes In The Left Sub-tree Is Less Than The Value Of The Root. • Similarly, Value Of All The Nodes In The Right Sub-tree Is Greater Than Or Equal To The Value Of The Root. • This Rule Will Be Recursively Applied To All The Left And Right Sub-trees Of The Root.
  • 4.
  • 5.
  • 6.
    BST 3BST 2 BST4 BST 1 BST 5
  • 7.
  • 8.
  • 11.
  • 12.
    • Using dynamicprogramming find the optimal binary search tree for the given data. Keys 10 12 16 21 Frequency 4 2 6 3 0 1 2 3 4 1 0 4 2 0 2 3 0 6 4 0 3 5 0 1 2 3 4 Keys 10 12 16 21 Frequency 4 2 6 3 Solution: Solution matrix
  • 13.
    Formula: 0 1 23 4 1 0 4 8(1) 2 0 2 10(3 ) 3 0 6 4 0 3 5 0 C[1,2]= K=1 C[1,0]+C[2,2]=0+2=2--------- ROOT(1) K=2 C[1,1]+C[3,2]=4+0=4 + 6===8 C[2,3]= I=2 J=3 K=2 C[2,1]+C[3,3]=0+6=6 K=3 C[2,2]+C[4,3]=2+0=2 +8===10
  • 14.
    0 1 23 4 1 0 4 8(1) 20(3 ) 26(3 ) 2 0 2 10(3 ) 16(3 ) 3 0 6 12(3 ) 4 0 3 5 0 C[3,4] k=3 C[3,2]+C[4,4]=0+3=3 i=3,j=4 k=4 C[3,3]+C[5,4]=6+0=6 Frequency=9 =====12ROOT(3) Minimum cost =2 16 10 21 12 Optimal Binary search 1*6=6 2*4+2*3=14 3*2=6
  • 15.
  • 16.