


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Class: Algorithms; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Fall 2008;
Typology: Assignments
1 / 4
This page cannot be seen from the preview
Don't miss anything!



You can decide whether it’s worth playing or not as follows. Imagine that the nodes at even levels (where it’s your turn) are OR gates, the nodes at odd levels (where it’s Death’s turn) are AND gates. Each gate gets its input from its children and passes its output to its parent. White and black leaves stand represent TRUE and FALSE inputs, respectively. If the output at the top of the tree is TRUE, then you can win and live forever! If the output at the top of the tree is FALSE, you should challenge Death to a game of Twister instead.
(a) Describe and analyze a deterministic algorithm to determine whether or not you can win. [Hint: This is easy!] (b) Unfortunately, Death won’t let you even look at every node in the tree. Describe and analyze a randomized algorithm that determines whether you can win in O ( 3 n ) expected time. [Hint: Consider the case n = 1 .] (c) [Extra credit] Describe and analyze a randomized algorithm that determines whether you can win in O ( cn ) expected time, for some constant c < 3. [Hint: You may not need to change your algorithm at all.]
RANDOMSELECT( A [1 .. n ], r ) : k ← PARTITION( A [1 .. n ], RANDOM( n )) if r < k return RANDOMSELECT( A [1 .. k − 1 ], r ) else if r > k return RANDOMSELECT( A [ k + 1 .. n ], r − k ) else return A [ k ]
(a) State a recurrence for the expected running time of RANDOMSELECT, as a function of n and r. (b) What is the exact probability that RANDOMSELECT compares the i th smallest and j th smallest elements in the input array? The correct answer is a simple function of i , j , and r. [Hint: Check your answer by trying a few small examples.] (c) Show that for any n and r , the expected running time of RANDOMSELECT is Θ( n ). You can use either the recurrence from part (a) or the probabilities from part (b). ? (d) [Extra Credit] Find the exact expected number of comparisons executed by RANDOMSELECT, as a function of n and r.
? 6. [Extra credit] In the usual theoretical presentation of treaps, the priorities are random real
numbers chosen uniformly from the interval [ 0 , 1 ], but in practice, computers only have access to random bits. This problem asks you to analyze a modification of treaps that takes this limitation into account. Suppose the priority of a node v is abstractly represented as an infinite sequence πv [ 1 .. ∞] of random bits, which is interpreted as the rational number
priority ( v ) =
i = 1
πv [ i ] · 2 − i^.
However, only a finite number v_ of these bits are actually known at any given time. When a node _v_ is first created, _none_ of the priority bits are known: _v = 0. We generate (or ‘reveal’) new random bits only when they are necessary to compare priorities. The following algorithm compares the priorities of any two nodes in O ( 1 ) expected time:
LARGERPRIORITY( v , w ): for i ← 1 to ∞ if i > vv ← i ; πv [ i ] ← RANDOMBIT if i > ww ← i ; πw [ i ] ← RANDOMBIT if πv [ i ] > πw [ i ] return v else if πv [ i ] < πw [ i ] return w
Suppose we insert n items one at a time into an initially empty treap. Let L =
v `v^ denote the total number of random bits generated by calls to LARGERPRIORITY during these insertions.
(a) Prove that E [ L ] = Θ( n ). (b) Prove that E [ v_ ] = Θ( 1 ) for any node _v_. [Hint: This is equivalent to part (a). Why?] (c) Prove that _E_ [ _ root] = Θ(log n ). [Hint: Why doesn’t this contradict part (b)?]