Assignment 4 Graduate Algorithms - Fall 2008 | CS 573, Assignments of Algorithms and Programming

Material Type: Assignment; Class: Algorithms; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Fall 2008;

Typology: Assignments

Pre 2010

Uploaded on 03/16/2009

koofers-user-18s
koofers-user-18s 🇺🇸

8 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 573: Graduate Algorithms, Fall 2008
Homework 4
Due at 11:59:59pm, Wednesday, October 31, 2008
Groups of up to three students may submit a single, common solution. Please neatly print (or
typeset) the full name, NetID, and the HW0 alias (if any) of every group member on the first page
of your submission.
Unless a problem explicitly states otherwise, you can assume the existence of a function
RANDOM
(
k
),
which returns an integer uniformly distributed in the range
{
1
,
2
, . . . , k}
in
O
(1)time; the argu-
ment
k
must be a positive integer. For example,
RANDOM
(2)simulates a fair coin flip, and
RANDOM(1)always returns 1.
1.
Death knocks on your door one cold blustery morning and challenges you to a game. Death knows
that you are an algorithms student, so instead of the traditional game of chess, Death presents you
with a complete binary tree with 4
n
leaves, each colored either black or white. There is a token at
the root of the tree. To play the game, you and Death will take turns moving the token from its
current node to one of its children. The game will end after 2
n
moves, when the token lands on a
leaf. If the final leaf is black, you die; if it’s white, you will live forever. You move first, so Death
gets the last turn.
∨∨∨
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 ORgates, 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 FALS E 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 FAL SE, 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
arandomized 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.]
pf3
pf4

Partial preview of the text

Download Assignment 4 Graduate Algorithms - Fall 2008 | CS 573 and more Assignments Algorithms and Programming in PDF only on Docsity!

CS 573: Graduate Algorithms, Fall 2008

Homework 4

Due at 11:59:59pm, Wednesday, October 31, 2008

  • Groups of up to three students may submit a single, common solution. Please neatly print (or typeset) the full name, NetID, and the HW0 alias (if any) of every group member on the first page of your submission.
  • Unless a problem explicitly states otherwise, you can assume the existence of a function RANDOM( k ), which returns an integer uniformly distributed in the range { 1 , 2 ,... , k } in O ( 1 ) time; the argu- ment k must be a positive integer. For example, RANDOM( 2 ) simulates a fair coin flip, and RANDOM( 1 ) always returns 1.
  1. Death knocks on your door one cold blustery morning and challenges you to a game. Death knows that you are an algorithms student, so instead of the traditional game of chess, Death presents you with a complete binary tree with 4 n^ leaves, each colored either black or white. There is a token at the root of the tree. To play the game, you and Death will take turns moving the token from its current node to one of its children. The game will end after 2 n moves, when the token lands on a leaf. If the final leaf is black, you die; if it’s white, you will live forever. You move first, so Death gets the last turn.

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.]

  1. Consider the following randomized algorithm for choosing the largest bolt. Draw a bolt uniformly at random from the set of n bolts, and draw a nut uniformly at random from the set of n nuts. If the bolt is smaller than the nut, discard the bolt, draw a new bolt uniformly at random from the unchosen bolts, and repeat. Otherwise, discard the nut, draw a new nut uniformly at random from the unchosen nuts, and repeat. Stop either when every nut has been discarded, or every bolt except the one in your hand has been discarded. What is the exact expected number of nut-bolt tests performed by this algorithm? Prove your answer is correct. [Hint: What is the expected number of unchosen nuts and bolts when the algorithm terminates?]
  2. (a) Prove that the expected number of proper descendants of any node in a treap is exactly equal to the expected depth of that node. (b) Why doesn’t the Chernoff-bound argument for depth imply that, with high probability, every node in a treap has O (log n ) descendants? The conclusion is obviously bogus—every n -node treap has one node with exactly n descendants!—but what is the flaw in the argument? (c) What is the expected number of leaves in an n -node treap? [Hint: What is the probability that in an n -node treap, the node with k th smallest search key is a leaf?]
  3. The following randomized algorithm, sometimes called “one-armed quicksort”, selects the r th smallest element in an unsorted array A [ 1 .. n ]. For example, to find the smallest element, you would call RANDOMSELECT( A , 1 ); to find the median element, you would call RANDOM- SELECT( A , b n/ 2 c). The subroutine PARTITION( A [ 1 .. n ], p ) splits the array into three parts by compar- ing the pivot element A [ p ] to every other element of the array, using n − 1 comparisons altogether, and returns the new index of the pivot element.

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 ], rk ) 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 > vvi ; πv [ i ] ← RANDOMBIT if i > wwi ; π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)?]