05_dprog_II

05_dprog_II - Chapter 5 Dynamic programming II - The...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 5 Dynamic programming II - The Recursion Strikes Back By Sariel Har-Peled , September 17, 2007 Version: 0.4 No, mademoiselle, I dont capture elephants. I content myself with living among them. I like them. I like looking at them, listening to them, watching them on the horizon. To tell you the truth, Id give anything to become an elephant myself. Thatll convince you that Ive nothing against the Germans in particular: theyre just men to me, and thats enough. The roots of heaven, Romain Gary 5.1 Optimal Search Trees Given a binary search tree T , the time to search for an element x , that is stored in T , is O (1 + depth ( T , x )), where depth ( T , x ) denotes the depth of x in T (i.e., this is the length of the path connecting x with the root of T ). Problem 5.1.1 Given a set of n (sorted) keys A [1 . . . n ], build the best binary search tree for the elements of A . 4 12 21 32 45 45 32 12 4 21 Two possible search trees for the set A = [4 , 12 , 21 , 32 , 45]. Note, that we store the values in the internal node of the binary trees. The figure on the right shows two pos- sible search trees for the same set of numbers. Clearly, if we are accessing the number 12 all the time, the tree on the left would be better to use than the tree on the right. Usually, we just build a balanced binary tree, and this is good enough. But assume that we have additional in- formation about what is the frequency in which we access the element A [ i ], for i = 1 , . . . , n . Namely, we know that A [ i ] is going be accessed f [ i ] times, for i = 1 , . . . , n . This work is licensed under the Creative Commons Attribution-Noncommercial 3.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. 1 In this case, we know that the total search time for a tree T is S ( T ) = n X i = 1 ( depth ( T , i ) + 1) f [ i ], where depth ( T , i ) is the depth of the node in T storing the value A [ i ]. Assume that A [ r ] is the value stored in the root of the tree T . Clearly, all the values smaller than A [ r ] are in the subtree left T , and all values larger than r are in right T . Thus, the total search time, in this case, is S ( T ) = r- 1 X i = 1 ( depth (left T , i ) + 1) f [ i ] + price of access to root z }| { n X i = 1 f [ i ] + n X i = r + 1 ( depth ( right T , i ) + 1 ) f [ i ] . Observe, that if T is the optimal search tree for the access frequencies f [1] , . . . , f [ n ], then The subtree left T must be optimal for the elements accessing it (i.e., A [1 . . . r- 1] where r is the root)....
View Full Document

Page1 / 5

05_dprog_II - Chapter 5 Dynamic programming II - The...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online