lecture09 - Introduction to Algorithms...

Info icon This preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 9 Prof. Charles E. Leiserson
Image of page 1

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

View Full Document Right Arrow Icon
Introduction to Algorithms Day 17 L9.2 © 2001 by Charles E. Leiserson 3 3 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] 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?
Image of page 2
Introduction to Algorithms Day 17 L9.3 © 2001 by Charles E. Leiserson 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.
Image of page 3

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

View Full Document Right Arrow Icon
Introduction to Algorithms Day 17 L9.4 © 2001 by Charles E. Leiserson 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 ( ) ) (lg ) lg ( 1 node insert to s comparison # 1 1 n O n n O n i E n n i = = = = Average node depth . (quicksort analysis)
Image of page 4
Introduction to Algorithms Day 17 L9.5 © 2001 by Charles E. Leiserson 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
Image of page 5

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

View Full Document Right Arrow Icon
Introduction to Algorithms Day 17 L9.6 © 2001 by Charles E. Leiserson Height of a randomly built binary search tree 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 ) . Outline of the analysis:
Image of page 6
Introduction to Algorithms Day 17 L9.7 © 2001 by Charles E. Leiserson 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
Image of page 7

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

View Full Document Right Arrow Icon
Introduction to Algorithms Day 17 L9.8 © 2001 by Charles E. Leiserson Convexity lemma Lemma. Let f : R R be a convex function, and let { α 1 , α 2 , …, α n } be a set of nonnegative constants such that k α k = 1 . Then, for any set { x 1 , x 2 , …, x n } of real numbers, 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. .
Image of page 8
Introduction to Algorithms Day 17 L9.9 © 2001 by Charles E. Leiserson Proof (continued) + = = = 1 1 1 1 ) 1 ( n k k n k n n n n k k k x x f x f α α α α α Inductive step: Algebra.
Image of page 9

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

View Full Document Right Arrow Icon
Introduction to Algorithms Day 17 L9.10 © 2001 by Charles E. Leiserson Proof (continued)
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern