0% found this document useful (0 votes)
4 views3 pages

ps4

Uploaded by

sunshiyao0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views3 pages

ps4

Uploaded by

sunshiyao0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

Problem Set 4

Design and Analysis of Algorithms, Fall 2024

Due: November 22 23:59:59 (UTC+8), mail to [email protected].

Problem 1
Define a family H of hash functions from a finite set U to a finite set B to be ϵ-universal if for all pairs
of distinct elements k and l in U ,
Pr[h(k) = h(l)] ≤ ϵ
where the probability is over the choice of the hash function h drawn uniformly at random from the
family H. Prove that an ϵ-universal family of hash functions must have
1 1
ϵ≥ −
|B| |U |

Problem 2
Suppose we can insert or delete an element into a hash table in O(1) time. In order to ensure that our
hash table is always big enough, without wasting a lot of memory, we will use the following global
rebuilding rules: (1) after an insertion, if the table is more than 3/4 full, we allocate a new table twice as
big as our current table (this takes O(1) time), insert everything into the new table, and then free the old
table (this takes O(1) time); (2) after a deletion, if the table is less than 1/4 full, we allocate a new table
half as big as our current table (this takes O(1) time), insert everything into the new table, and then free
the old table (this takes O(1) time). Now, prove that for any sequence of insertions and deletions, the
amortized time per operation is still O(1).

Problem 3
Consider the following modified algorithm for incrementing a binary counter.

I NC(B[0, · · · , ∞])
1: i←0
2: while (B[i] = 1) do
3: B[i] ← 0
4: i←i+1
5: B[i] ← 1
6: S OMETHING E LSE(i)

The only difference from the standard algorithm is the function call at the end, to a black-box subroutine
called S OMETHING E LSE. Suppose we call I NC n times, starting with a counter with value 0. The amor-
tized time of each I NC clearly depends on the running time of S OMETHING E LSE. Let T (i) denote the
worst-case running time of S OMETHING E LSE(i). For example, we proved in class that I NC algorithm
runs in O(1) amortized time when T (i) = 0.

1
(a) What is the amortized time per I NC if T (i) = 4? Prove your answer.
(b) What is the amortized time per I NC if T (i) = 2i ? Prove your answer.
(c) What is the amortized time per I NC if T (i) = 4i ? Prove your answer.

Problem 4
Consider an ordinary binary search tree augmented by adding to each node x the attribute x.size giving
the number of keys stored in the subtree rooted at x. Let α be a constant in the range 1/2 ≤ α < 1. We
say that a given node x is α-balanced if x.lef t.size ≤ α · x.size and x.right.size ≤ α · x.size. The
tree as a whole is α-balanced if every node in the tree is α-balanced. The following amortized approach
to maintaining weight-balanced trees was suggested by G. Varghese.
(a) A 1/2-balanced tree is, in a sense, as balanced as it can be. Given a node x in an arbitrary binary
search tree, show how to rebuild the subtree rooted at x so that it becomes 1/2-balanced. Your algorithm
should run in time O(x.size), and it can use O(x.size) auxiliary storage.
(b) Show that performing a search in an n-node α-balanced binary search tree takes O(lg n) time.

For the remainder of this problem, assume that the constant α is strictly greater than 1/2. Suppose that
we implement I NSERT and D ELETE as usual for an n-node binary search tree, except that after every
such operation, if any node in the tree is no longer α-balanced, then we “rebuild” the subtree rooted
at the highest such node in the tree so that it becomes 1/2-balanced. We shall analyze this rebuilding
scheme using the potential method. For a node x in a binary search tree T , we define

∆(x) = |x.lef t.size − x.right.size| ,

and we define the potential of T as


 
X
Φ(T ) = c ·  ∆(x) ,
x∈T :∆(x)≥2

where c is a sufficiently large constant that depends on α.


(c) Argue that any binary search tree has nonnegative potential and a 1/2-balanced tree has potential 0.
(d) Suppose that m units of potential can pay for rebuilding an m-node subtree. How large must c be
in terms of α (i.e., express c as a function of α) in order for it to take O(1) amortized time to rebuild a
subtree that is not α-balanced? You need to justify your answer.
(e) Argue that inserting a node into or deleting a node from an n-node α-balanced tree costs O(lg n)
amortized time.

Problem 5
(a) Show the data structure that results and the answers returned by the Find operations in the following
Procedure 1, using linked-list representation with weighted-union heuristic. Assume that if the sets
containing xi and xj have same size, then operation Union(xi , xj ) appends xj ’s list onto xi ’s list.
(b) Consider a union-find data structure that uses union by height without path compression. For all
integers m and n such that m ≥ 2n, prove that there is a sequence of n MakeSet operations, followed
by m Union and Find operations, that require Ω(m log n) time to execute.

2
orithms Lecture 11: Disjoint Sets [Fa’13]

• Concat(S, T ) removes the strings S and T (given by pointers) from the data structure,
adds the concatenated string ST to the data structure, and returns a pointer to the
Procedure 1
new string.
1: for (i ← 1 to 16) do
• Reverse(S)
2: removes
MakeSet(x i)
the string S (given by a pointer) from the data structure, adds
the reversal of 1Stoto15the
3: for (i ← step data structure, and returns a pointer to the new
by 2) do ▷ i =string.
1, 3, 5, · · ·
Union(x , x )
• Lookup(S, k) returns the kth character in string S (given by a pointer), or Null if
4: i i+1
5: for (i ← 1 to 13 step by 4) do ▷ i = 1, 5, 9, · · ·
the length
6:
of the S, xis less
Union(x ) than k.
i i+2
7: Union(x , x )
Describe 1 5
and analyze
8: Union(x11 , x13 )
a simple data structure that supports Concat in O(log n) amortized
time, supports , x10 ) operation in O(1) worst-case time, and uses O(n) space, where n
every 1other
9: Union(x
is the sum10:ofFind(x
the current
2) string lengths. Unlike the similar problem in the previous lecture
11: Find(x9 )
note, there is no Split operation. [Hint: Why is this problem here?]

Problem 6
7. (a) Describe and analyze an algorithm to compute the size of the largest connected
of black pixels in an n × n bitmap B[1 .. n, 1 .. n].
Describe and analyze an algorithm to compute the size of the largest connected component of black
component
pixels in an n × n bitmap B[1 · · · n, 1 · · · n]. You should try to come up with an algorithm as fast as
For example,
possible, and yougiven
need to the bitmap
analyze below
the runtime as input,
of your your algorithm should return the
algorithm.
numberFor 9,example,
because given
thethelargest
bitmap below
conectedas input,black
your algorithm
componentshould(marked
return the number
with 9, because
white dots
the largest connected black component (marked with white dots on the right) contains 9 pixels.
on the right) contains nine pixels.

(b) Design and analyze an algorithm Blacken(i, j) that colors the pixel B[i, j] black and
returns the size of the largest black component in the bitmap. For full credit, the
amortized running time of your algorithm (starting with an all-white bitmap) must be
as small as possible.
For example, at each step in the sequence below, we blacken the pixel marked
with an X. The largest black component is marked with white dots; the number
underneath shows the correct output of the Blacken algorithm.

9 14 14 16 17

(c) What is the worst-case running time of your Blacken algorithm?

8. Consider the following game. I choose a positive3 integer n and keep it secret; your goal is
to discover this integer. We play the game in rounds. In each round, you write a list of at
most n integers on the blackboard. If you write more than n numbers in a single round,

You might also like