Randomized Algorithms CS648

advertisement
Randomized Algorithms
CS648
Lecture 11
• BPWM : The final algorithm
• Hashing – (part 1)
1
Boolean Product Witness Matrix (BPWM)
Problem: Given two Boolean matrices A and B, and their Boolean product C,
compute a matrix ๐‘พ, such that:
๐‘พ๐‘–๐‘— stores a witness for each (๐‘–, ๐‘—) with ๐‘ช๐‘–๐‘— = 1.
Conclusion of last lecture
Theorem:
Given two Boolean matrices ๐‘จ and ๐‘ฉ, and integer ๐’•,
there is a randomized Monte Carlo algorithm to compute witnesses
for all those pairs which have ๐’• witnesses.
• The running time is O(๐’๐Ž ๐ฅ๐จ๐  ๐’).
• The error probability is at most
๐Ÿ
๐’๐’„−๐Ÿ
?
Questions:
1. How to compute witnesses for all pairs in O(๐’๐Ž ๐ฅ๐จ๐  ๐Ÿ ๐’) time.
2. How to convert Monte Carlo to Las Vegas ?
Randomized algorithm for Computing Witnesses for
all pairs with exactly ๐’• witnesses
//The pseudo code for sampling the (indices of) columns
Sample(๐‘›, ๐‘)
{ ๐‘…๏ƒŸ ∅;
For each ๐‘– ∈ [๐‘›] do:
add ๐‘– to ๐‘… with probability ๐‘;
return ๐‘…;
}
Randomized algorithm for Computing Witnesses for
all pairs with exactly ๐’• witnesses
Compute-Witnesses(๐ด,๐ต,๐‘ก)
{
{ ๐‘…๏ƒŸ Sample(๐‘›, 1/๐‘ก);
For each ๐‘–, ๐‘— ∈ [๐‘›]
if ๐‘— ∈ ๐‘… then ๐ด๐‘–๐‘— ๏ƒŸ ๐‘—โˆ™ ๐ด๐‘–๐‘— ;
else
๐ด๐‘–๐‘— ๏ƒŸ0;
๐ท′ ๏ƒŸ ๐ด โˆ™ ๐ต;
For each ๐‘–, ๐‘— ∈ [๐‘›]
′
If ๐ท๐‘–๐‘—
is a witness for (๐‘–, ๐‘—)
′
๐‘Š๐‘–๐‘— ๏ƒŸ ๐ท๐‘–๐‘—
}
}
Time complexity: O(๐’๐Ž )
1
Probability of finding a witness for a single pair (๐‘–, ๐‘—): ??
>
๐‘’
Focus on a single pair (๐‘–,๐‘—)
Let there be ๐‘ค witnesses for (๐‘–,๐‘—) such that ๐‘ก ≤ ๐‘ค < 2๐‘ก
Question: If each column is selected independently with ๐‘ = 1/๐‘ก ,
what is the probability that exactly one out of ๐‘ค witnesses for (๐‘–,๐‘—) survives ?
Answer:
=
๐‘ค 1
1 ๐‘ก
1 ๐‘ค−1
๐‘ก
1−
1 ๐‘ค
1−๐‘ก
1 2๐‘ก
≥
≥ 1−๐‘ก
1
≥ ๐‘’2
= 0.135 …
>
1
8
Randomized algorithm for Computing Witnesses for
all pairs with witness count ∈ [๐’•, ๐Ÿ๐’•)
Compute-Witnesses(๐ด,๐ต,๐‘ก)
{
{ ๐‘…๏ƒŸ Sample(๐‘›, 1/๐‘ก);
For each ๐‘–, ๐‘— ∈ [๐‘›]
if ๐‘— ∈ ๐‘… then ๐ด๐‘–๐‘— ๏ƒŸ ๐‘—โˆ™ ๐ด๐‘–๐‘— ;
else
How to reduce the error
๐Ÿ
probability to < ๐’๐’„ ?
๐ด๐‘–๐‘— ๏ƒŸ0;
๐ท′ ๏ƒŸ ๐ด โˆ™ ๐ต;
For each ๐‘–, ๐‘— ∈ [๐‘›]
′
If ๐ท๐‘–๐‘—
is a witness for (๐‘–, ๐‘—)
Repeat the entire process
๐’„ ๐ฅ๐จ๐  8/7 ๐’ times.
′
๐‘Š๐‘–๐‘— ๏ƒŸ ๐ท๐‘–๐‘—
}
}
Time complexity: O(๐’๐Ž )
1 7
Probability of failing to find a witness for a single pair (๐‘–, ๐‘—): ??< 1 − <
8 8
Randomized algorithm for Computing Witnesses for
all pairs with witness count ∈ [๐’•, ๐Ÿ๐’•)
Compute-Witnesses(๐ด,๐ต,๐‘ก)
{ Repeat ๐’„ ๐ฅ๐จ๐  ๐Ÿ–/๐Ÿ• ๐’ times
{ ๐‘…๏ƒŸ Sample(๐‘›, 1/๐‘ก);
For each ๐‘–, ๐‘— ∈ [๐‘›]
if ๐‘— ∈ ๐‘… then ๐ด๐‘–๐‘— ๏ƒŸ ๐‘—โˆ™ ๐ด๐‘–๐‘— ;
else
๐ด๐‘–๐‘— ๏ƒŸ0;
๐ท′ ๏ƒŸ ๐ด โˆ™ ๐ต;
For each ๐‘–, ๐‘— ∈ [๐‘›]
′
If ๐ท๐‘–๐‘—
is a witness for (๐‘–, ๐‘—)
′
๐‘Š๐‘–๐‘— ๏ƒŸ ๐ท๐‘–๐‘—
}
}
Time complexity: O(๐’๐Ž ๐ฅ๐จ๐  ๐’)
๐Ÿ
< ๐’„
Probability of failing to find a witness for a single pair (๐‘–, ๐‘—): ??
๐’
Randomized algorithm for Computing Witnesses for
all pairs with witness count ∈ [๐’•, ๐Ÿ๐’•)
Compute-Witnesses(๐ด,๐ต,๐‘ก)
{ Repeat ๐’„ ๐ฅ๐จ๐  ๐Ÿ–/๐Ÿ• ๐’ times
{ ๐‘…๏ƒŸ Sample(๐‘›, 1/๐‘ก);
For each ๐‘–, ๐‘— ∈ [๐‘›]
if ๐‘— ∈ ๐‘… then ๐ด๐‘–๐‘— ๏ƒŸ ๐‘—โˆ™ ๐ด๐‘–๐‘— ;
else
How to compute
witnesses for all pairs
in O(๐’๐Ž ๐ฅ๐จ๐  ๐Ÿ ๐’) time ?
๐ด๐‘–๐‘— ๏ƒŸ0;
๐ท′ ๏ƒŸ ๐ด โˆ™ ๐ต;
For each ๐‘–, ๐‘— ∈ [๐‘›]
′
If ๐ท๐‘–๐‘—
is a witness for (๐‘–, ๐‘—)
Let there be ๐‘š pairs that
have exactly ๐‘ก witnesses.
Apply Union theorem …
′
๐‘Š๐‘–๐‘— ๏ƒŸ ๐ท๐‘–๐‘—
}
}
Time complexity: O(๐’๐Ž ๐ฅ๐จ๐  ๐’)
๐’Ž
Prob. of failing to find a witness for any pair having witness count ∈ [๐’•, ๐Ÿ๐’•) : ??
< ๐’๐’„ <
๐Ÿ
๐’๐’„−๐Ÿ
Randomized algorithm for Computing Witnesses for
all pairs
Compute-Witnesses(๐ด,๐ต)
{ For ๐‘˜ = 1 to ๐ฅ๐จ๐  ๐Ÿ ๐’
{ Repeat ๐’„ ๐ฅ๐จ๐  ๐Ÿ–/๐Ÿ• ๐’ times
{ ๐‘…๏ƒŸ Sample(๐‘›, 1/2๐‘˜ );
For each ๐‘–, ๐‘— ∈ [๐‘›]
if ๐‘— ∈ ๐‘… then ๐ด๐‘–๐‘— ๏ƒŸ ๐‘—โˆ™ ๐ด๐‘–๐‘— ;
else
๐ด๐‘–๐‘— ๏ƒŸ0;
๐ท′ ๏ƒŸ ๐ด โˆ™ ๐ต;
For each ๐‘–, ๐‘— ∈ [๐‘›]
′
If ๐ท๐‘–๐‘—
is a witness for (๐‘–, ๐‘—)
′
๐‘Š๐‘–๐‘— ๏ƒŸ ๐ท๐‘–๐‘—
}
}
}
Time complexity: O(๐’๐Ž ๐ฅ๐จ๐  ๐Ÿ ๐’)
Question:
What is Prob. that witness is not found for a
given pair (๐‘–,๐‘—) ?
Answer: ? < ๐Ÿ
๐’„
๐’
Question:
What is Prob. that witness is not found for at
least one pair ?
Answer: < ๐Ÿ
๐’๐’„−๐Ÿ
How to
If there is transform
even a single
to Laspair with
nonzero witnesses
Vegas but
algowe
? fail to find
even one, run the O(๐’๐Ÿ‘ ) algo.
Expected running time = O(๐’๐Ž ๐ฅ๐จ๐  ๐Ÿ ๐’)
Boolean Product Witness Matrix (BPWM)
Theorem: Given two Boolean matrices A and B, and their Boolean product C,
there exists a Las Vegas algorithm for computing a matrix ๐‘พ, such that:
๐‘พ๐‘–๐‘— stores a witness for each (๐‘–, ๐‘—) with ๐‘ช๐‘–๐‘— = 1.
The expected running time of the algorithm is O(๐’๐Ž ๐ฅ๐จ๐  ๐Ÿ ๐’).
Homework:
Show that the running time of the algo is concentrated around O(๐’๐Ž ๐ฅ๐จ๐  ๐Ÿ ๐’).
(modify the algorithm if needed)
Hashing
(part 1)
Problem Definition
• ๐‘ผ = 1,2, … , ๐‘š called universe
• ๐‘บ ⊆ ๐‘ผ and ๐‘  = |๐‘บ|
• ๐‘ โ‰ช๐‘š
Examples:
๐‘š = 1018 , ๐‘  = 103
Aim
Maintain a data structure for storing ๐‘บ to support the search query :
“Does ๐‘– ∈ ๐‘บ ?” for any given ๐‘– ∈ ๐‘ผ.
Solutions
Solutions with worst case guarantees
Solution for static ๐‘บ :
• Array storing ๐‘บ in sorted order
Solution for dynamic ๐‘บ :
• Height Balanced Search trees (AVL trees, Red-Black trees,…)
Time per operation: O(log ๐‘ ),
Space: O(๐‘ )
Alternative:
Time per operation: O(1),
Space: O(๐‘š)
Solutions used in practice with no worst case guarantees
Hashing.
Hashing
• Hash table:
๐‘ป: an array of size ๐’.
• Hash function
๐’‰ : ๐‘ผ๏ƒ  [๐’]
Answering a Query: “Does ๐‘– ∈ ๐‘บ ?”
1. ๐‘˜๏ƒŸ๐’‰(๐‘–);
2. Search the list stored at ๐‘ป[๐‘˜].
Properties of ๐’‰ :
• ๐’‰ ๐‘– computable in O(1) time.
• Space required by ๐’‰: O(1).
How many words
needed to encode ๐’‰ ?
๐‘ป
0
1
โ‹ฎ
โ‹ฎ
๐’−๐Ÿ
Elements of ๐‘บ
Collision
Definition: Two elements ๐‘–, ๐‘— ∈ ๐‘ผ are
said to collide under hash function ๐’‰ if
๐’‰ ๐‘– =๐’‰ ๐‘—
Worst case time complexity of searching
an item ๐‘– :
No. of elements in ๐‘บ colliding with ๐‘–.
A Discouraging fact:
No hash function can be found
which is good for all ๐‘บ.
Proof:
At least ๐‘š/๐‘› elements from ๐‘ผ are
mapped to a single index in ๐‘ป.
๐‘ป
0
1
โ‹ฎ
โ‹ฎ
๐’−๐Ÿ
Collision
Definition: Two elements ๐‘–, ๐‘— ∈ ๐‘ผ are
said to collide under hash function ๐’‰ if
๐’‰ ๐‘– =๐’‰ ๐‘—
Worst case time complexity of searching
an item ๐‘– :
No. of elements in ๐‘บ colliding with ๐‘–.
A Discouraging fact:
No hash function can be found
which is good for all ๐‘บ.
Proof:
At least ๐‘š/๐‘› elements from ๐‘ผ are
mapped to a single index in ๐‘ป.
๐‘ป
0
1
โ‹ฎ
โ‹ฏ
โ‹ฎ
๐’−๐Ÿ
๐‘š/๐‘›
Hashing
• A very popular heuristic since 1950’s
• Achieves O(1) search time in practice
• Worst case guarantee on search time: O(๐’”)
Question: Can we have a hashing ensuring
• O(1) worst case guarantee on search time.
• O(๐’”) space.
• Expected O(๐’”) preprocessing time.
The following result gave an answer in affirmative๏Š
Michael Fredman, Janos Komlos, Endre Szemeredy. Storing a Sparse Table
with O(1) Worst Case Access Time. Journal of the ACM (Volume 31, Issue 3),
1984.
WHY DOES HASHING WORK SO
WELL IN PRACTICE ?
Why does hashing work so well in Practice ?
Question: What is the simplest hash function ๐’‰ : ๐‘ผ๏ƒ  [๐’] ?
Answer: ๐’‰ ๐‘– = ๐‘– ๐ฆ๐จ๐ ๐‘›
Hashing works so well in practice because the set ๐‘บ is usually a uniformly
random subset of ๐‘ผ.
Let us give a theoretical reasoning for this fact.
Why does hashing work so well in Practice ?
Let ๐‘ฆ1 , ๐‘ฆ2 , … , ๐‘ฆ๐‘  denote ๐‘  elements
selected randomly uniformly from ๐‘ผ to
form ๐‘บ.
Question:
What is expected number of elements
colliding with ๐‘ฆ1 ?
Answer: Let ๐‘ฆ1 takes value ๐‘–.
P(๐‘ฆ๐‘— collides with ๐‘ฆ1 ) = ??
1
2
โ‹ฎ
๐‘–−๐‘›
๐‘–
๐‘–+๐‘›
How many possible values
can ๐‘ฆ๐‘— take ? ๐‘š − 1
How many possible values
can collide with ๐‘– ?
๐‘– + 2๐‘›
๐‘– + 3๐‘›
โ‹ฎ
m
Why does hashing work so well in Practice ?
Let ๐‘ฆ1 , ๐‘ฆ2 , … , ๐‘ฆ๐‘  denote ๐‘  elements
selected randomly uniformly from ๐‘ผ to
form ๐‘บ.
Question:
What is expected number of elements
colliding with ๐‘ฆ1 ?
Answer: Let ๐‘ฆ1 takes value ๐‘–.
P(๐‘ฆ๐‘— collides with ๐‘ฆ1 ) =
๐‘š
๐‘›
1
2
โ‹ฎ
๐‘–−๐‘›
๐‘–
๐‘–+๐‘›
−1
๐‘š−1
๐‘– + 2๐‘›
Expected number of elements of ๐‘บ
colliding with ๐‘ฆ1 =
=
๐‘š
๐‘›
−1
๐‘š−1
๐‘– + 3๐‘›
โ‹ฎ
(๐‘  − 1)
= ๐‘‚ 1 for ๐‘› = ๐Ž(๐‘ )
m
Values which may
collide with ๐‘–
under the hash
function ๐’‰ ๐‘ฅ =
๐’™ ๐ฆ๐จ๐ ๐‘›
Why does hashing work so well in Practice ?
Conclusion
1.
๐’‰ ๐‘– = ๐‘– ๐ฆ๐จ๐ ๐‘› works so well because
for a uniformly random subset of ๐‘ผ,
the expected number of collision at an index of ๐‘ป is O(1).
It is easy to fool this hash function such that it achieves O(s) search time.
(do it as a simple exercise).
This makes us think:
“How can we achieve worst case O(1) search time for a given set ๐‘บ.”
HOW TO ACHIEVE WORST CASE
O(1) SEARCH TIME
Key idea to achieve worst case O(1) search time
Observation: Of course, no single hash function is good for every possible ๐‘บ.
But we may strive for a hash function which is good for a given ๐‘บ.
A promising direction:
Find out a family of hash functions H such that
• For any given ๐‘บ, many of them are good.
The notion of goodness is captured
formally by Universal hash family in
the following slide.
• Select a function randomly from H and try for ๐‘บ.
UNIVERSAL HASH FAMILY
Universal Hash Family
Definition: A collection ๐‘ฏ of hash-functions is said to be c-universal
if there exists a constant ๐‘ such that for any ๐‘–, ๐‘— ∈ ๐‘ผ,
๐๐’‰∈๐‘Ÿ ๐‘ฏ ๐’‰ ๐‘– = ๐’‰ ๐‘—
≤
๐‘
๐‘›
Question: Does there exist a Universal hash family whose hash functions have a compact
encoding?
Answer:
Yes and it is very simple too ๏Š
STATIC HASHING
WORST CASE O(1) SEARCH TIME
The Journey
One Milestone in Our Journey:
• A perfect hash function using hash table of size O(๐‘  2 )
Tools Needed:
• Universal Hash Family where ๐‘ is a small constant
• Elementary Probability
Spend at least 30 minutes today to
see how the milestone can be
achieved. It is simple and beautiful ๏Š
Download