{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lec9 - Introduction to Algorithms 6.046J/18.401J LECTURE 9...

This preview shows pages 1–10. Sign up to view the full content.

October 17, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L7.1 Introduction to Algorithms 6.046J/18.401J L ECTURE 9 Randomly built binary search trees Expected node depth Analyzing height Convexity lemma Jensen’s inequality Exponential height Post mortem Prof. Erik Demaine

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

View Full Document
October 17, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L7.2 Binary-search-tree sort T ← ∅ Create an empty BST for i = 1 to n do T REE -I NSERT ( T , A [ i ]) Perform an inorder tree walk of T . Example: A = [3 1 8 2 6 7 5] 3 3 8 8 1 1 2 2 6 6 5 5 7 7 Tree-walk time = O ( n ) , but how long does it take to build the BST?
October 17, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L7.3 Analysis of BST sort BST sort performs the same comparisons as quicksort, but in a different order! 3 1 8 2 6 7 5 1 2 8 6 7 5 2 6 7 5 7 5 The expected time to build the tree is asymptot- ically the same as the running time of quicksort.

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

View Full Document
October 17, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L7.4 Node depth The depth of a node = the number of comparisons made during T REE -I NSERT . Assuming all input permutations are equally likely, we have Average node depth ( ) ) (lg ) lg ( 1 node insert to s comparison # 1 1 n O n n O n i E n n i = = = = . (quicksort analysis)
October 17, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L7.5 Expected tree height But, average node depth of a randomly built BST = O (lg n ) does not necessarily mean that its expected height is also O (lg n ) (although it is). Example. lg n n h = ) (lg 2 lg 1 n O n n n n n = + Ave. depth

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

View Full Document
October 17, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L7.6 Height of a randomly built binary search tree Outline of the analysis: Prove Jensen’s inequality , which says that f ( E [ X ]) E [ f ( X )] for any convex function f and random variable X . Analyze the exponential height of a randomly built BST on n nodes, which is the random variable Y n = 2 X n , where X n is the random variable denoting the height of the BST. Prove that 2 E [ X n ] E [2 X n ] = E [ Y n ] = O ( n 3 ) , and hence that E [ X n ] = O (lg n ) .
October 17, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L7.7 Convex functions A function f : R R is convex if for all α , β ≥ 0 such that α + β = 1 , we have f ( α x + β y ) ≤ α f ( x ) + β f ( y ) for all x , y R . α x + β y α f ( x ) + β f ( y ) f ( α x + β y ) x y f ( x ) f ( y ) f

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

View Full Document
October 17, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L7.8 Convexity lemma Lemma. Let f : R R be a convex function, and let α 1 , α 2 , …, α n be nonnegative real numbers such that k α k = 1 . Then, for any real numbers x 1 , x 2 , …, x n , we have ) ( 1 1 = = n k k k n k k k x f x f α α . Proof. By induction on n . For n = 1 , we have α 1 = 1 , and hence f ( α 1 x 1 ) ≤ α 1 f ( x 1 ) trivially.