Professional Documents
Culture Documents
Lec9 PDF
Lec9 PDF
6.046J/18.401J LECTURE 9
Randomly built binary search trees Expected node depth Analyzing height
Convexity lemma Jensens inequality Exponential height
Binary-search-tree sort
T Create an empty BST for i = 1 to n do TREE-INSERT(T, A[i]) Perform an inorder tree walk of T. Example: A = [3 1 8 2 6 7 5] 3 3 1 1 2 2 5 5 6 6 7 7
L7.2
8 8
Tree-walk time = O(n), but how long does it take to build the BST?
October 17, 2005
The expected time to build the tree is asymptotically the same as the running time of quicksort.
October 17, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L7.3
Node depth
The depth of a node = the number of comparisons made during TREE-INSERT. Assuming all input permutations are equally likely, we have Average node depth n 1 = E (# comparisons to insert node i ) n i =1
= 1 O(n lg n) n = O(lg n) .
October 17, 2005
(quicksort analysis)
L7.4
lg n
n n Ave. depth 1 n lg n + n 2 = O(lg n)
October 17, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson
h= n
L7.5
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. f
f(y) f(x) + f(y) f(x) f(x + y) x
October 17, 2005
x + y
y
L7.7
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 x1, x2, , xn, we have
Proof (continued)
Inductive step:
n f x k k = k =1
n 1 k + f x x ( 1 ) n n n k 1 n k =1
Algebra.
L7.9
Proof (continued)
Inductive step:
n f x k k = k =1
n 1 k + ( 1 ) f x x n n n k 1 n k =1 n1 k n f ( xn ) + (1 n ) f 1 xk n k =1
Convexity.
L7.10
Proof (continued)
Inductive step:
n f x k k = k =1
n 1 k ( 1 ) + f x x n n n k 1 n k =1 n1 k n f ( xn ) + (1 n ) f x k 1 n k =1
k n f ( xn ) + (1 n ) f ( xk ) k =11 n
n 1
Induction.
October 17, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L7.11
Proof (continued)
Inductive step:
n f x k k = k =1
n 1 k + f x ( 1 ) x n n n k 1 n k =1 n1 k n f ( xn ) + (1 n ) f x k 1 n k =1
k n f ( xn ) + (1 n ) f ( xk ) k =11 n
= k f ( xk ) .
k =1 n
n 1
Algebra.
L7.12
f k xk k f ( xk ) , k =1 k =1
assuming that these summations exist.
L7.13
L7.14
Taking the limit of both sides (and because the inequality is not strict):
1 lim f n n i =1 i
October 17, 2005
1 k xk lim n n k =1 i =1 i
n
k =1
k =1
f ( xk )
1 k xk
k =1
k f ( xk )
L7.15
Jensens inequality
Lemma. Let f be a convex function, and let X be a random variable. Then, f (E[X]) E[ f (X)]. Proof. = f ( E[ X ]) = f k Pr{ X k } k =
Definition of expectation.
L7.16
Jensens inequality
Lemma. Let f be a convex function, and let X be a random variable. Then, f (E[X]) E[ f (X)]. Proof. = f ( E[ X ]) = f k Pr{ X k } k =
k =
f (k ) Pr{X = k}
Jensens inequality
Lemma. Let f be a convex function, and let X be a random variable. Then, f (E[X]) E[ f (X)]. Proof. f ( E[ X ]) = f k X k = Pr{ } k =
k =
f (k ) Pr{X = k}
= E[ f ( X )] .
Analysis (continued)
Define the indicator random variable Znk as 1 if the root has rank k, Znk = 0 otherwise. Thus, Pr{Znk = 1} = E[Znk] = 1/n, and
Yn = Z nk (2 max{Yk 1 , Ynk }) .
k =1 n
L7.20
L7.21
Linearity of expectation.
L7.22
Independence of the rank of the root from the ranks of subtree roots.
L7.23
The max of two nonnegative numbers is at most their sum, and E[Znk] = 1/n.
October 17, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L7.24
k =1 n
k =1 n
n 1
L7.26
Substitution.
L7.27
Integral method.
L7.28
n 1
n 1
L7.31
L7.32
L7.33
L7.34
Post mortem
Q. Does the analysis have to be this hard? Q. Why bother with analyzing exponential height? Q. Why not just develop the recurrence on Xn = 1 + max{Xk1, Xnk} directly?
October 17, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L7.35
Thought exercises
See what happens when you try to do the analysis on Xn directly. Try to understand better why the proof uses an exponential. Will a quadratic do? See if you can find a simpler argument. (This argument is a little simpler than the one in the bookI hope its correct!)
L7.37