SlideShare a Scribd company logo
Optimal Binary
Search Tree
1.Preface
•OBST is one special kind of advanced tree.
•It focus on how to reduce the cost of the search of
the BST.
•It may not have the lowest height !
•It needs 3 tables to record probabilities, cost, and
root.
2.Premise
• It has n keys (representation k1,k2,…,kn) in sorted order (so that
k1<k2<…<kn), and we wish to build a binary search tree from
these keys. For each ki ,we have a probability pi that a search
will be for ki.
• In contrast of, some searches may be for values not in ki, and so
we also have n+1 “dummy keys” d0,d1,…,dn representating not
in ki.
• In particular, d0 represents all values less than k1, and dn
represents all values greater than kn, and for i=1,2,…,n-1, the
dummy key di represents all values between ki and ki+1.
*The dummy keys are leaves (external nodes), and the data
keys mean internal nodes.
3.Formula & Prove
• The case of search are two situations, one is success, and the
other, without saying, is failure.
• We can get the first statement :
Success Failure
• Because we have probabilities of searches for each key and each
dummy key, we can determine the expected cost of a search in a
given binary search tree T.
• Let us assume that the actual cost of a search is the number of
nodes examined, i.e., the depth of the node found by the search
in T,plus1(Assuming root at depth 0).
• Then the expected cost of a search in T is : (The second
statement)
Where depthT denotes a node’s depth in the tree T.
k2
k1 k4
k3 k5
k2
k1 k5
k4
k3
d0 d1
d2 d3 d4 d5
d0 d1
d2 d3
d4
d5
Figure (a)
Figure (b)
i 0 1 2 3 4 5
pi 0.15 0.10 0.05 0.10 0.20
qi 0.05 0.10 0.05 0.05 0.05 0.10
• By Figure (a), we can calculate the expected search cost node by node:
Node# Depth probability cost
k1 1 0.15 0.30
k2 0 0.10 0.10
k3 2 0.05 0.15
k4 1 0.10 0.20
K5 2 0.20 0.60
d0 2 0.05 0.15
d1 2 0.10 0.30
d2 3 0.05 0.20
d3 3 0.05 0.20
d4 3 0.05 0.20
d5 3 0.10 0.40
Cost=
Probability *
(Depth+1)
• And the total cost = (0.30 + 0.10 + 0.15 + 0.20 + 0.60 + 0.15 +
0.30 + 0.20 + 0.20 + 0.20 + 0.40 ) = 2.80
• So Figure (a) costs 2.80 ,on another, the Figure (b) costs 2.75,
and that tree is really optimal.
• We can see the height of (b) is more than (a) ,
• The key k5 has the greatest search probability of any key, yet
the root of the OBST shown is k2.
Step1:The structure of an OBST
•To characterize the optimal substructure of OBST,
we start with an observation about subtrees.
Consider any subtree of a BST.
•It must contain keys in a contiguous range ki,…,kj,
for some 1≦i ≦j ≦n.
•In addition, a subtree that contains keys ki,…,kj must
also have as its leaves the dummy keys di-1 ,…,dj.
•We need to use the optimal substructure to show that
we can construct an optimal solution to the problem
from optimal solutions to subproblems.
•Given keys ki ,…, kj, one of these keys, say kr (I ≦r
≦j), will be the root of an optimal subtree containing
these keys.
•The left subtree of the root kr will contain the keys
(ki ,…, kr-1) and the dummy keys( di-1 ,…, dr-1), and the
right subtree will contain the keys (kr+1 ,…, kj) and the
dummy keys( dr ,…, dj).
•As long as we examine all candidate roots kr, where I
≦r ≦j, and we determine all optimal binary search
trees containing ki ,…, kr-1 and those containing kr+1 ,…,
kj , we are guaranteed that we will find an OBST.
•There is one detail worth nothing about “empty”
subtrees.
•Suppose that in a subtree with keys ki,...,kj, we select ki
as the root.
•By the above argument, ki ‘s left subtree contains the
keys ki,…, ki-1.
•It is natural to interpret this sequence as containing no
keys. It is easy to know that subtrees also contain
dummy keys.
•The sequence has no actual keys but does contain the
single dummy key di-1.
•Symmetrically, if we select kj as the root, then kj‘s right
subtree contains the keys, kj+1 …,kj; this right subtree
contains no actual keys, but it does contain the dummy
key dj.
Step2: A recursive solution
•We are ready to define the value of an optimal solution
recursively.
•We pick our subproblem domain as finding an OBST
containing the keys ki,…,kj, where i≧1, j ≦n, and j ≧
i-1. (It is when j=i-1 that ther are no actual keys; we
have just the dummy key di-1.)
•Let us define e[i,j] as the expected cost of searching an
OBST containing the keys ki,…, kj.
•Ultimately, we wish to compute e[1,n].
• The easy case occurs when j=i-1. Then we have just the dummy
key di-1. The expected search cost is e[i,i-1]= qi-1.
• When j≧1, we need to select a root kr from among ki,…,kj and
then make an OBST with keys ki,…,kr-1 its left subtree and an
OBST with keys kr+1,…,kj its right subtree.
• By the time, what happens to the expected search cost of a
subtree when it becomes a subtree of a node?
• The answer is that the depth of each node in the subtree
increases by 1.
•By the second statement, the excepted search cost of
this subtree increases by the sum of all the probabilities
in the subtree. For a subtree with keys ki,…,kj let us
denote this sum of probabilities as
Thus, if kr is the root of an optimal subtree containing
keys ki,…,kj, we have
•We rewrite e[i,j] as
e[i,j]= e[i,r-1] + e[r+1,j]+w(i,j)
The recursive equation as above assumes that we know
which node kr to use as the root. We choose the root
that gives the lowest expected search cost, giving us
our final recursive formulation:
• The e[i,j] values give the expected search costs in OBST. To
help us keep track of the structure of OBST, we define root[i,j],
for 1≦i≦j≦n, to be the index r for which kr is the root of an
OBST containing keys ki,…,kj.
Step3: Computing the expected
search cost of an OBST
•We store the e[i.j] values in a table e[1..n+1, 0..n]
•The first index needs to run to n+1 rather than n
because in order to have a subtree containing only
the dummy key dn, we will need to compute and
store e[n+1,n].
•The second index needs to start from 0 because in
order to have a subtree containing only the dummy
key d0, we will need to compute and store e[1,0].
• We will use only the entries e[i,j] for which j≧i-1.
we also use a table root[i,j], for recording the root of
the subtree containing keys ki,…, kj. This table uses
only the entries for which 1≦i≦j≦n.
•We will need one other table for efficiency. Rather than
compute the value of w(i,j) from scratch every time we
are computing e[i,j] ----- we tore these values in a table
w[1..n+1,0..n].
•For the base case, we compute w[i,i-1] = qi-1 for 1≦i
≦n.
•For j≧i, we compute :
The algorithm takes as inputs the probabilities p1…..pn and
q0….qn and the size n, and it returns the tables e and root
The tables e[i,j], w[i,j], and root [i,j]computed by Optimal-
BST(for figure b given at slide 6)
Time Complexity
• The OPTIMAL-BST procedure takes Ɵ(n3) time, just like
MATRIX-CHAINORDER.
• We can easily see that its running time is O(n3) since its for
loops are nested three deep and each loop index takes on at
most n values.

More Related Content

Similar to 14_OptimalBinarySearchTree.ppt

Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structureShakil Ahmed
 
Computer security
Computer securityComputer security
Computer securityDavid Hoen
 
Computer security
Computer securityComputer security
Computer securityJames Wong
 
Computer security
Computer securityComputer security
Computer securityFraboni Ec
 
Computer security
Computer security Computer security
Computer security Harry Potter
 
Computer security
Computer security Computer security
Computer security Tony Nguyen
 
1 chapter1 introduction
1 chapter1 introduction1 chapter1 introduction
1 chapter1 introductionSSE_AndyLi
 
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog217-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2Shanmuganathan C
 
ICPC 2015, Tsukuba : Unofficial Commentary
ICPC 2015, Tsukuba: Unofficial CommentaryICPC 2015, Tsukuba: Unofficial Commentary
ICPC 2015, Tsukuba : Unofficial Commentaryirrrrr
 
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptxLecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptxlokeshgoud13
 
Privacy preserving queries on encrypted data
Privacy preserving queries on encrypted dataPrivacy preserving queries on encrypted data
Privacy preserving queries on encrypted datarohit_ainapure
 
PVEB Tree.pptx
PVEB Tree.pptxPVEB Tree.pptx
PVEB Tree.pptxSanthosh A
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programminghodcsencet
 

Similar to 14_OptimalBinarySearchTree.ppt (20)

Linear sorting
Linear sortingLinear sorting
Linear sorting
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
 
Computer security
Computer securityComputer security
Computer security
 
Computer security
Computer securityComputer security
Computer security
 
Computer security
Computer securityComputer security
Computer security
 
Computer security
Computer securityComputer security
Computer security
 
Computer security
Computer security Computer security
Computer security
 
Computer security
Computer securityComputer security
Computer security
 
Computer security
Computer security Computer security
Computer security
 
1 chapter1 introduction
1 chapter1 introduction1 chapter1 introduction
1 chapter1 introduction
 
Btreesprogramme
BtreesprogrammeBtreesprogramme
Btreesprogramme
 
17-dynprog2.ppt
17-dynprog2.ppt17-dynprog2.ppt
17-dynprog2.ppt
 
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog217-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
 
ICPC 2015, Tsukuba : Unofficial Commentary
ICPC 2015, Tsukuba: Unofficial CommentaryICPC 2015, Tsukuba: Unofficial Commentary
ICPC 2015, Tsukuba : Unofficial Commentary
 
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptxLecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
 
Privacy preserving queries on encrypted data
Privacy preserving queries on encrypted dataPrivacy preserving queries on encrypted data
Privacy preserving queries on encrypted data
 
PVEB Tree.pptx
PVEB Tree.pptxPVEB Tree.pptx
PVEB Tree.pptx
 
220exercises2
220exercises2220exercises2
220exercises2
 
B trees
B treesB trees
B trees
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programming
 

Recently uploaded

Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.PrashantGoswami42
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptssuser9bd3ba
 
shape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptxshape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptxVishalDeshpande27
 
Introduction to Casting Processes in Manufacturing
Introduction to Casting Processes in ManufacturingIntroduction to Casting Processes in Manufacturing
Introduction to Casting Processes in Manufacturingssuser0811ec
 
2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edgePaco Orozco
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationRobbie Edward Sayers
 
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxThe Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxCenterEnamel
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdfKamal Acharya
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdfKamal Acharya
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Dr.Costas Sachpazis
 
Arduino based vehicle speed tracker project
Arduino based vehicle speed tracker projectArduino based vehicle speed tracker project
Arduino based vehicle speed tracker projectRased Khan
 
Explosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdfExplosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdf884710SadaqatAli
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacksgerogepatton
 
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdfA CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdfKamal Acharya
 
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...Amil baba
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwoodseandesed
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfKamal Acharya
 
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsAtif Razi
 
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical EngineeringC Sai Kiran
 

Recently uploaded (20)

Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
shape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptxshape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptx
 
Introduction to Casting Processes in Manufacturing
Introduction to Casting Processes in ManufacturingIntroduction to Casting Processes in Manufacturing
Introduction to Casting Processes in Manufacturing
 
2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxThe Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Arduino based vehicle speed tracker project
Arduino based vehicle speed tracker projectArduino based vehicle speed tracker project
Arduino based vehicle speed tracker project
 
Explosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdfExplosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdf
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdfA CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
 
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdf
 
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
 
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
 

14_OptimalBinarySearchTree.ppt

  • 2. 1.Preface •OBST is one special kind of advanced tree. •It focus on how to reduce the cost of the search of the BST. •It may not have the lowest height ! •It needs 3 tables to record probabilities, cost, and root.
  • 3. 2.Premise • It has n keys (representation k1,k2,…,kn) in sorted order (so that k1<k2<…<kn), and we wish to build a binary search tree from these keys. For each ki ,we have a probability pi that a search will be for ki. • In contrast of, some searches may be for values not in ki, and so we also have n+1 “dummy keys” d0,d1,…,dn representating not in ki. • In particular, d0 represents all values less than k1, and dn represents all values greater than kn, and for i=1,2,…,n-1, the dummy key di represents all values between ki and ki+1. *The dummy keys are leaves (external nodes), and the data keys mean internal nodes.
  • 4. 3.Formula & Prove • The case of search are two situations, one is success, and the other, without saying, is failure. • We can get the first statement : Success Failure
  • 5. • Because we have probabilities of searches for each key and each dummy key, we can determine the expected cost of a search in a given binary search tree T. • Let us assume that the actual cost of a search is the number of nodes examined, i.e., the depth of the node found by the search in T,plus1(Assuming root at depth 0). • Then the expected cost of a search in T is : (The second statement) Where depthT denotes a node’s depth in the tree T.
  • 6. k2 k1 k4 k3 k5 k2 k1 k5 k4 k3 d0 d1 d2 d3 d4 d5 d0 d1 d2 d3 d4 d5 Figure (a) Figure (b) i 0 1 2 3 4 5 pi 0.15 0.10 0.05 0.10 0.20 qi 0.05 0.10 0.05 0.05 0.05 0.10
  • 7. • By Figure (a), we can calculate the expected search cost node by node: Node# Depth probability cost k1 1 0.15 0.30 k2 0 0.10 0.10 k3 2 0.05 0.15 k4 1 0.10 0.20 K5 2 0.20 0.60 d0 2 0.05 0.15 d1 2 0.10 0.30 d2 3 0.05 0.20 d3 3 0.05 0.20 d4 3 0.05 0.20 d5 3 0.10 0.40 Cost= Probability * (Depth+1)
  • 8. • And the total cost = (0.30 + 0.10 + 0.15 + 0.20 + 0.60 + 0.15 + 0.30 + 0.20 + 0.20 + 0.20 + 0.40 ) = 2.80 • So Figure (a) costs 2.80 ,on another, the Figure (b) costs 2.75, and that tree is really optimal. • We can see the height of (b) is more than (a) , • The key k5 has the greatest search probability of any key, yet the root of the OBST shown is k2.
  • 9. Step1:The structure of an OBST •To characterize the optimal substructure of OBST, we start with an observation about subtrees. Consider any subtree of a BST. •It must contain keys in a contiguous range ki,…,kj, for some 1≦i ≦j ≦n. •In addition, a subtree that contains keys ki,…,kj must also have as its leaves the dummy keys di-1 ,…,dj.
  • 10. •We need to use the optimal substructure to show that we can construct an optimal solution to the problem from optimal solutions to subproblems. •Given keys ki ,…, kj, one of these keys, say kr (I ≦r ≦j), will be the root of an optimal subtree containing these keys. •The left subtree of the root kr will contain the keys (ki ,…, kr-1) and the dummy keys( di-1 ,…, dr-1), and the right subtree will contain the keys (kr+1 ,…, kj) and the dummy keys( dr ,…, dj). •As long as we examine all candidate roots kr, where I ≦r ≦j, and we determine all optimal binary search trees containing ki ,…, kr-1 and those containing kr+1 ,…, kj , we are guaranteed that we will find an OBST.
  • 11. •There is one detail worth nothing about “empty” subtrees. •Suppose that in a subtree with keys ki,...,kj, we select ki as the root. •By the above argument, ki ‘s left subtree contains the keys ki,…, ki-1. •It is natural to interpret this sequence as containing no keys. It is easy to know that subtrees also contain dummy keys. •The sequence has no actual keys but does contain the single dummy key di-1. •Symmetrically, if we select kj as the root, then kj‘s right subtree contains the keys, kj+1 …,kj; this right subtree contains no actual keys, but it does contain the dummy key dj.
  • 12. Step2: A recursive solution •We are ready to define the value of an optimal solution recursively. •We pick our subproblem domain as finding an OBST containing the keys ki,…,kj, where i≧1, j ≦n, and j ≧ i-1. (It is when j=i-1 that ther are no actual keys; we have just the dummy key di-1.) •Let us define e[i,j] as the expected cost of searching an OBST containing the keys ki,…, kj. •Ultimately, we wish to compute e[1,n].
  • 13. • The easy case occurs when j=i-1. Then we have just the dummy key di-1. The expected search cost is e[i,i-1]= qi-1. • When j≧1, we need to select a root kr from among ki,…,kj and then make an OBST with keys ki,…,kr-1 its left subtree and an OBST with keys kr+1,…,kj its right subtree. • By the time, what happens to the expected search cost of a subtree when it becomes a subtree of a node? • The answer is that the depth of each node in the subtree increases by 1.
  • 14. •By the second statement, the excepted search cost of this subtree increases by the sum of all the probabilities in the subtree. For a subtree with keys ki,…,kj let us denote this sum of probabilities as Thus, if kr is the root of an optimal subtree containing keys ki,…,kj, we have
  • 15. •We rewrite e[i,j] as e[i,j]= e[i,r-1] + e[r+1,j]+w(i,j) The recursive equation as above assumes that we know which node kr to use as the root. We choose the root that gives the lowest expected search cost, giving us our final recursive formulation:
  • 16. • The e[i,j] values give the expected search costs in OBST. To help us keep track of the structure of OBST, we define root[i,j], for 1≦i≦j≦n, to be the index r for which kr is the root of an OBST containing keys ki,…,kj.
  • 17. Step3: Computing the expected search cost of an OBST •We store the e[i.j] values in a table e[1..n+1, 0..n] •The first index needs to run to n+1 rather than n because in order to have a subtree containing only the dummy key dn, we will need to compute and store e[n+1,n]. •The second index needs to start from 0 because in order to have a subtree containing only the dummy key d0, we will need to compute and store e[1,0]. • We will use only the entries e[i,j] for which j≧i-1. we also use a table root[i,j], for recording the root of the subtree containing keys ki,…, kj. This table uses only the entries for which 1≦i≦j≦n.
  • 18. •We will need one other table for efficiency. Rather than compute the value of w(i,j) from scratch every time we are computing e[i,j] ----- we tore these values in a table w[1..n+1,0..n]. •For the base case, we compute w[i,i-1] = qi-1 for 1≦i ≦n. •For j≧i, we compute :
  • 19. The algorithm takes as inputs the probabilities p1…..pn and q0….qn and the size n, and it returns the tables e and root
  • 20. The tables e[i,j], w[i,j], and root [i,j]computed by Optimal- BST(for figure b given at slide 6)
  • 21. Time Complexity • The OPTIMAL-BST procedure takes Ɵ(n3) time, just like MATRIX-CHAINORDER. • We can easily see that its running time is O(n3) since its for loops are nested three deep and each loop index takes on at most n values.