Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm...

50
Randomized Algorithms

Transcript of Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm...

Page 1: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Randomized Algorithms

Page 2: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Randomized Algorithms

• Algorithm can make random decisions

• Why randomized algorithms? Simple and efficient

• Examples: Symmetry-breaking, graph algorithms, quicksort, hashing, load balancing, cryptography, etc

Page 3: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Properties of Random Variables

For independent events A and B,

Pr(A and B) = Pr(A) Pr(B)

Union Bound: For any two events A and B,

Pr(A U B) <= Pr(A) + Pr(B)

Conditional Probability: For events A and B,

Pr(A | B) = Pr(A and B) / Pr(B)

Page 4: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Global Min-Cut

Example:

G G

A

V - A

Note: A global min-cut is not the same as an s-t min cut

How can we find a global min-cut using n - 1 max-flows?

We can do better for the global min-cut

Problem: Given undirected, unweighted graph G = (V, E), find a cut (A, V - A) which has the minimum number of edges across it

Page 5: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Karger’s Min-Cut Algorithm

Problem: Given undirected, unweighted graph G = (V, E), find a cut (A, V - A) which has the minimum number of edges across it

Karger’s Min-Cut Algorithm: 1. Repeat until two nodes remain:

Pick an edge e = (u, v) in E uniformly at random. Collapse u and v into a single node (allowing multiple edges)

2. Let u, v be the nodes. Output (U, V - U), where U = {nodes that went into u}

Page 6: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Karger’s Min-Cut Algorithm

Problem: Given undirected, unweighted graph G = (V, E), find a cut (A, V - A) which has the minimum number of edges across it

Karger’s Min-Cut Algorithm: 1. Repeat until two nodes remain:

Pick an edge e = (u, v) in E uniformly at random. Collapse u and v into a single node (allowing multiple edges)

2. Let u, v be the nodes. Output (U, V - U), where U = {nodes that went into u}

Example:

a b c d

e f g h

#edges to pick from = 14Pick (b, f) (probability 1/14)

Page 7: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Karger’s Min-Cut Algorithm

Problem: Given undirected, unweighted graph G = (V, E), find a cut (A, V - A) which has the minimum number of edges across it

Karger’s Min-Cut Algorithm: 1. Repeat until two nodes remain:

Pick an edge e = (u, v) in E uniformly at random. Collapse u and v into a single node (allowing multiple edges)

2. Let u, v be the nodes. Output (U, V - U), where U = {nodes that went into u}

Example:

a

bf

c d

e g h

#edges to pick from = 13Pick (g, h) (probability 1/13)

Page 8: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Karger’s Min-Cut Algorithm

Problem: Given undirected, unweighted graph G = (V, E), find a cut (A, V - A) which has the minimum number of edges across it

Karger’s Min-Cut Algorithm: 1. Repeat until two nodes remain:

Pick an edge e = (u, v) in E uniformly at random. Collapse u and v into a single node (allowing multiple edges)

2. Let u, v be the nodes. Output (U, V - U), where U = {nodes that went into u}

Example:

a

bf

c d

e gh

#edges to pick from = 12Pick (d, gh) (probability 1/6)

Page 9: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Karger’s Min-Cut Algorithm

Problem: Given undirected, unweighted graph G = (V, E), find a cut (A, V - A) which has the minimum number of edges across it

Karger’s Min-Cut Algorithm: 1. Repeat until two nodes remain:

Pick an edge e = (u, v) in E uniformly at random. Collapse u and v into a single node (allowing multiple edges)

2. Let u, v be the nodes. Output (U, V - U), where U = {nodes that went into u}

Example:

a

bf

c

e dgh

#edges to pick from = 10Pick (a, e) (probability 1/10)

Page 10: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Karger’s Min-Cut Algorithm

Problem: Given undirected, unweighted graph G = (V, E), find a cut (A, V - A) which has the minimum number of edges across it

Karger’s Min-Cut Algorithm: 1. Repeat until two nodes remain:

Pick an edge e = (u, v) in E uniformly at random. Collapse u and v into a single node (allowing multiple edges)

2. Let u, v be the nodes. Output (U, V - U), where U = {nodes that went into u}

Example:

ae bf

c

dgh

#edges to pick from = 9Pick (ae, bf) (probability 4/9)

Page 11: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Karger’s Min-Cut Algorithm

Problem: Given undirected, unweighted graph G = (V, E), find a cut (A, V - A) which has the minimum number of edges across it

Karger’s Min-Cut Algorithm: 1. Repeat until two nodes remain:

Pick an edge e = (u, v) in E uniformly at random. Collapse u and v into a single node (allowing multiple edges)

2. Let u, v be the nodes. Output (U, V - U), where U = {nodes that went into u}

Example:

aebf

c

dgh

#edges to pick from = 5Pick (c, dgh) (probability 3/5)

Page 12: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Karger’s Min-Cut Algorithm

Problem: Given undirected, unweighted graph G = (V, E), find a cut (A, V - A) which has the minimum number of edges across it

Karger’s Min-Cut Algorithm: 1. Repeat until two nodes remain:

Pick an edge e = (u, v) in E uniformly at random. Collapse u and v into a single node (allowing multiple edges)

2. Let u, v be the nodes. Output (U, V - U), where U = {nodes that went into u}

Example:

Done! Output (aebf, cdgh)

aebf cdgh

a b c d

e f g h

Original Graph:

Page 13: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Karger’s Algorithm: Analysis

Karger’s Min-Cut Algorithm: 1. Repeat until two nodes remain:

Pick an edge e = (u, v) in E uniformly at random. Collapse u and v into a single node (allowing multiple edges)

2. Let u, v be the nodes. Output (U, V - U), where U = {nodes that went into u}

Fact 1. If there are n nodes, then the average degree of a node is 2|E|/n

Page 14: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Karger’s Algorithm: Analysis

Karger’s Min-Cut Algorithm: 1. Repeat until two nodes remain:

Pick an edge e = (u, v) in E uniformly at random. Collapse u and v into a single node (allowing multiple edges)

2. Let u, v be the nodes. Output (U, V - U), where U = {nodes that went into u}

Fact 1. If there are n nodes, then the average degree of a node is 2|E|/n

Proof: Total degree of n nodes = 2|E|, n nodes in total

Page 15: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Karger’s Algorithm: Analysis

Karger’s Min-Cut Algorithm: 1. Repeat until two nodes remain:

Pick an edge e = (u, v) in E uniformly at random. Collapse u and v into a single node (allowing multiple edges)

2. Let u, v be the nodes. Output (U, V - U), where U = {nodes that went into u}

Fact 1. If there are n nodes, then the average degree of a node is 2|E|/n

Fact 2. The minimum cut size is at most 2|E|/n

Proof: From Fact 1, there is at least one node x with degree at most 2|E|/n

x

V - {x}

Page 16: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Karger’s Algorithm: Analysis

Karger’s Min-Cut Algorithm: 1. Repeat until two nodes remain:

Pick an edge e = (u, v) in E uniformly at random. Collapse u and v into a single node (allowing multiple edges)

2. Let u, v be the nodes. Output (U, V - U), where U = {nodes that went into u}

Fact 1. If there are n nodes, then the average degree of a node is 2|E|/n

Fact 2. The minimum cut size is at most 2|E|/n

Proof: From Fact 1, there is at least one node x with degree at most 2|E|/nThe cut (x, V - x) has deg(x) edges. The minimum cut size is thus at most deg(x) <= 2|E|/n

x

V - {x}

Page 17: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Karger’s Algorithm: Analysis

Karger’s Min-Cut Algorithm: 1. Repeat until two nodes remain:

Pick an edge e = (u, v) in E uniformly at random. Collapse u and v into a single node (allowing multiple edges)

2. Let u, v be the nodes. Output (U, V - U), where U = {nodes that went into u}

Fact 1. If there are n nodes, then the average degree of a node is 2|E|/n

Fact 2. The minimum cut size is at most 2|E|/n

Fact 3. If we choose an edge uniformly at random (uar), the probability that it lies across the min cut is at most 2/n

Proof: Follows directly from Fact 2

Page 18: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Karger’s Algorithm: Analysis

Karger’s Min-Cut Algorithm: 1. Repeat until two nodes remain:

Pick an edge e = (u, v) in E uniformly at random. Collapse u and v into a single node (allowing multiple edges)

2. Let u, v be the nodes. Output (U, V - U), where U = {nodes that went into u}

Fact 1. If there are n nodes, then the average degree of a node is 2|E|/n

Fact 2. The minimum cut size is at most 2|E|/n

Fact 3. If we choose an edge uniformly at random (uar), the probability that it lies across the min cut is at most 2/n

Page 19: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Karger’s Algorithm: Analysis

Karger’s Min-Cut Algorithm: 1. Repeat until two nodes remain:

Pick an edge e = (u, v) in E uniformly at random. Collapse u and v into a single node (allowing multiple edges)

2. Let u, v be the nodes. Output (U, V - U), where U = {nodes that went into u}

Fact 1. If there are n nodes, then the average degree of a node is 2|E|/n

Fact 2. The minimum cut size is at most 2|E|/n

Fact 3. If we choose an edge uniformly at random (uar), the probability that it lies across the min cut is at most 2/n

Observe: Bad case is when the algorithm selects an edge e across the min-cut

Page 20: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Karger’s Algorithm: Analysis

Karger’s Min-Cut Algorithm: 1. Repeat until two nodes remain:

Pick an edge e = (u, v) in E uniformly at random. Collapse u and v into a single node (allowing multiple edges)

2. Let u, v be the nodes. Output (U, V - U), where U = {nodes that went into u}

Fact 1. If there are n nodes, then the average degree of a node is 2|E|/n

Fact 2. The minimum cut size is at most 2|E|/n

Fact 3. If we choose an edge uniformly at random (uar), the probability that it lies across the min cut is at most 2/n

Observe: Bad case is when the algorithm selects an edge e across the min-cut

Pr[ Output cut = min-cut ] = Pr[First selected edge not in min-cut] x Pr[Second selected edge not in min-cut|1st edge] x ....

�✓1� 2

n

◆✓1� 2

n� 1

◆✓1� 2

n� 2

◆. . .

✓1� 2

3

=n� 2

n· n� 3

n� 1· n� 4

n� 2. . .

2

4· 13

=2

n(n� 1)

Thus, outputs min-cut w.p. 2/n2; can run it O(n2) times and pick the best of the outputs

Page 21: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Types of Randomized Algorithms

Monte Carlo Algorithm: Always has the same running timeNot guaranteed to return the correct answer (returns a correct answer only with some probability)

Las Vegas Algorithm: Always guaranteed to return the correct answerRunning time fluctuates (probabilistically)

Fact: Suppose a Monte Carlo algorithm succeeds w.p. p. Then, it can be made to succeed w.p. 1 - t for any (small) t by running it O(log (1/t)/p) time

Proof: Suppose we run the algorithm k times. Then, Pr[Algorithm is wrong every time] = (1 - p)k < twhen k = O(log (1/t)/p)

Page 22: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Expectation

Given discrete random variable X, which takes m values xi w.p. pi, the expectation E[X] is defined as:

Examples:

1. Let X = 1 if a fair coin toss comes up heads, 0 ow. What is E[X]?

2. We are tossing a coin with head probability p, tail probability 1 - p. Let X = #independent flips until first head. What is E[X]?

Pr[X = j] = p x (1 - p)j - 1

first j-1 tailshead on toss j

E[X] =mX

i=1

xi · Pr[X = xi] =mX

i=1

xipi

E[X] =1X

j=1

j · p(1� p)j�1 =p

1� p

1X

j=0

j(1� p)j =p

1� p· 1� p

p2=

1

p

Page 23: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Expectation

Given discrete random variable X, which takes m values xi w.p. pi, the expectation E[X] is defined as:

E[X] =mX

i=1

xi · Pr[X = xi] =mX

i=1

xipi

Linearity of Expectation: E[X + Y] = E[X] + E[Y]

Page 24: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Expectation

Given discrete random variable X, which takes m values xi w.p. pi, the expectation E[X] is defined as:

E[X] =mX

i=1

xi · Pr[X = xi] =mX

i=1

xipi

Linearity of Expectation: E[X + Y] = E[X] + E[Y]

Example: Guessing a card

Shuffle n cards, then turn them over one by one. Guess what the next card is.How many guesses are correct on expectation?

Let Xi = 1 if guess i is correct, 0 otherwise

Pr[Xi = 1] =1

n� i+ 1E[Xi] =

1

n� i+ 1

Expected # of correct guesses = E[nX

i=1

Xi] =nX

i=1

E[Xi] =nX

i=1

1

i= �(log n)

What if we insert the selected card into the pile randomly and pull another ?

Page 25: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Expectation

Given discrete random variable X, which takes m values xi w.p. pi, the expectation E[X] is defined as:

E[X] =mX

i=1

xi · Pr[X = xi] =mX

i=1

xipi

Linearity of Expectation: E[X + Y] = E[X] + E[Y]

Example: Coupon Collector’s Problem

Balls tossed randomly into n bins. How many balls in expectation before each bin has a ball?

Let Xj = time spent when there are exactly j non-empty bins (Phase j)

Let X = total #steps = X1 + X2 + ... + Xn-1

We move from phase j to j+1 when a ball hits one of n-j bins, so w.p. (n-j) /n

Therefore, E[Xj] = n/(n - j) [From previous slide on expected waiting times]

E[X] = E[X1 + .. + Xn-1] = E[X1] + .. + E[Xn-1] = n + n/2 + .. + n/(n-1) = �(n log n)

Page 26: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Expectation

Given discrete random variable X, which takes m values xi w.p. pi, the expectation E[X] is defined as:

E[X] =mX

i=1

xi · Pr[X = xi] =mX

i=1

xipi

Linearity of Expectation: E[X + Y] = E[X] + E[Y]

Example: Birthday Paradox

m balls tossed randomly into n bins. What is the expected #collisions?

For 1 <= i < j <= m, let Xij = 1 if balls i and j land in the same bin, 0 otherwise

Pr[ Xij = 1 ] = 1/n, so E[Xij] = 1/n

So, expected number of collisions from tossing m balls = X

i,j

E[Xij ] =

�m2

n=

m(m� 1)

2n

So when m < sqrt(2n), expected #collisions < 1; otherwise, it’s more

Page 27: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Variance

Given a random variable X, its variance Var[X] is defined as: Var(X) = E[(X - E[X])2]

Properties of Variance:

1. Var(X) = E[X2] - (E[X])2

2. If X and Y are independent random variables, then, Var(X + Y) = Var(X) + Var(Y)

3. For any constants a and b, Var(aX + b) = a2 Var(X)

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

20

40

60

80

100

120

Student Version of MATLAB

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

100

200

300

400

500

600

Student Version of MATLAB

Variance of a random variable measures its “spread”

High Variance Distribution Low Variance Distribution

Page 28: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Computing Percentiles

Given array A[1..n], find the k-th smallest element in A

Example: Median = n/2-th smallest element = 50th percentileHow to compute the median in O(n log n) time?

An f-th percentile is the value below which f percent of observations fall

f-th percentile

Area = f

X

P(X = x)

Page 29: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Randomized Selection

Given array A[1..n], find the k-th smallest element in A

A Divide and Conquer Algorithm:Select(A, k)

1. Pick an item v in A2. Let:

AL = all elements in A that are < vAM = all elements in A that are = vAR = all elements in A that are > v

3. Return:Select(AL, k) if k <= |AL|v if |AL| < k <= |AL| + |AM| Select(AR, k - |AL| - |AM|) otherwise

2 36 5 21 8 13 11 20 5 4 1

Example:

A = v = 5

AL = 2 4 1 AM = 5 5 AR = 36 21 8 13 11 20

How to select v?

Pick v uniformly at random in 1..n

Page 30: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Worst case: v = smallest or largest elementTime T(n) = O(n) (for splitting) + T(n-1)Solving the recurrence, T(n) = O(n2)!Pr(Worst Case) =

Best case: v is the k-th elementTime taken T(n) = O(n) (for splitting) + O(1)T(n) = O(n)!Pr(Best Case) >= 1/n

2

n· 2

n� 1· . . . 2

2⇥ 2n

n!

Given array A[1..n], find the k-th smallest element in A

A Divide and Conquer Algorithm:Select(A, k)

1. Pick an item v in A2. Let:

AL = all elements in A that are < vAM = all elements in A that are = vAR = all elements in A that are > v

3. Return:Select(AL, k) if k <= |AL|v if |AL| < k <= |AL| + |AM| Select(AR, k - |AL| - |AM|) otherwise

How to select v?

Pick v uniformly at random in 1..n

Randomized Selection

Page 31: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Given array A[1..n], find the k-th smallest element in A

A Divide and Conquer Algorithm:Select(A, k)

1. Pick an item v in A2. Let:

AL = all elements in A that are < vAM = all elements in A that are = vAR = all elements in A that are > v

3. Return:Select(AL, k) if k <= |AL|v if |AL| < k <= |AL| + |AM| Select(AR, k - |AL| - |AM|) otherwise

Average case: Let T(n) be the expected running time on an array of size n

How to select v?

Pick v uniformly at random in 1..n

Lucky split: v is the m-th smallest element, for n/4 <= m <= 3n/4. Pr[Lucky Split] = 1/2

T(n) <= Time to split + Pr[Lucky Split] x T(array of size <= 3n/4) + Pr[Unlucky Split] x T(array of size <= n)<= n + (1/2) T(3n/4) + (1/2) T(n)

Solving, T(n) <= T(3n/4) + 2n = O(n)

Randomized Selection

Page 32: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Randomized Sorting

Given array A[1..n], sort A

QuickSort:Sort(A)

1. Pick an item v in A2. Let:

AL = all elements in A that are < vAM = all elements in A that are = vAR = all elements in A that are > v

3. Return:Sort(AL) + AM + Sort(AR)

How to select v?

Pick v uniformly at random in 1..n

Best case: [n/2, n/2] splitRunning Time T(n) = (time to split) + 2 T(n/2) = n + 2 T(n/2)Solving, T(n) = O(n log n)

Worst case: [1, n - 1] splitRunning Time T(n) = (time to split) + T(1) + T(n - 1) = n + 1 + T(n - 1)Solving, T(n) = O(n2)

Page 33: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Given array A[1..n], sort A

QuickSort:Sort(A)

1. Pick an item v in A2. Let:

AL = all elements in A that are < vAM = all elements in A that are = vAR = all elements in A that are > v

3. Return:Sort(AL) + AM + Sort(AR)

How to select v?

Pick v uniformly at random in 1..n

Average case: Let T(n) be the expected running time on an array of size n

T(n) = Time to split + expected time to sort AL and AR

= n+nX

i=1

Pr[v is the ithsmallest element in A] · (T (i) + T (n� i))

= n+1

n

nX

i=1

(T (i) + T (n� i))

Exercise: Solve the recurrence to T(n) = O(n log n). Use:

Randomized Sorting

n�1X

k=1

k log k 1

2

n2log n� 1

8

n2

Page 34: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

MAX 3SAT

3-SAT Problem: Given a boolean formula F consisting of:n variables x1, x2, .., xn m clauses of size 3 of the form xi V xj V xk or not(xi) V xj V xk

Is there an assignment of true/false values to variables s.t. all clauses are true?

Example: 3 variables: x1, .., x3

Clauses: x1 V x2 V x3, not(x1) V x2 V x3, x1 V not(x2)V x3, x1Vx2 V not(x3), not(x1) V not(x2) V x3, not(x1) Vx2 V not(x3), x1 V not(x2) V not(x3), not(x1) V not(x2) V not(x3)

Unsatisfiable!

MAX-3SAT Problem: Given a boolean formula F consisting of:n variables x1, x2, .., xn m clauses of size 3 of the form xi V xj V xk or not(xi) V xj V xk

Find an assignment of true/false values to variables to satisfy the most clauses

Example:

Any assignment satisfies 7 out of 8 clauses

Page 35: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

MAX-3SAT Problem: Given a boolean formula F consisting of:n variables x1, x2, .., xn m clauses of size 3 of the form xi V xj V xk or not(xi) V xj V xk

Find an assignment of true/false values to variables to satisfy the most clauses

A Randomized MAX-3SAT Algorithm: Set each variable to 0/1 independently with probability 1/2 each

How to get a solution with >= 7m/8 satisfied clauses?Fact: P = Pr[Solution has >= 7m/8 satisfied clauses] >= 1/8m

Define: Zi = 1, if clause i is satisfied by the assignment Zi = 0, otherwise

Pr[Zi = 0] = (1/2) . (1/2) . (1/2) = 1/8E[Zi] = 7/8Let Z = Z1 + Z2 + ... + Zm = #satisfied clausesE[Z] = E[Z1 + Z2 + ... + Zm] = E[Z1] + E[Z2] + .. + E[Zm] = 7m/8 = E[#satisfied clauses]

Proof: Let pj = Pr[Solution has j satisfied clauses], k = largest integer < 7m/8 7m

8= E[Z] =

kX

j=0

jpj +mX

j=k+1

jpj k +mP ! P �7m8 � k

m� 1

8mAs m, k are integers, 7m/8 - k >= 1/8

MAX 3SAT

Page 36: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

MAX-3SAT Problem: Given a boolean formula F consisting of:n variables x1, x2, .., xn m clauses of size 3 of the form xi V xj V xk or not(xi) V xj V xk

Find an assignment of true/false values to variables to satisfy the most clauses

A Randomized MAX-3SAT Algorithm: Set each variable to 0/1 independently with probability 1/2 each

Define: Zi = 1, if clause i is satisfied by the assignment Zi = 0, otherwise

Pr[Zi = 0] = (1/2) . (1/2) . (1/2) = 1/8E[Zi] = 7/8Let Z = Z1 + Z2 + ... + Zm = #satisfied clausesE[Z] = E[Z1 + Z2 + ... + Zm] = E[Z1] + E[Z2] + .. + E[Zm] = 7m/8 = E[#satisfied clauses]

How to get a solution with >= 7m/8 satisfied clauses?Fact: Pr[Solution has >= 7m/8 satisfied clauses] >= 1/8mSolution: Run algorithm 8m log(1/t) times independently. W.p. 1 - t, there will be a solution with at least 7m/8 satisfied clauses.

MAX 3SAT

Page 37: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Variance

Given a random variable X, its variance Var[X] is defined as: Var(X) = E[(X - E[X])2]

If X and Y are independent random variables, then, Var(X + Y) = Var(X) + Var(Y)

What if X and Y are not independent?

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

20

40

60

80

100

120

Student Version of MATLAB

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

100

200

300

400

500

600

Student Version of MATLAB

High Variance Distribution Low Variance Distribution

Cov(X, Y) = E(XY) - E(X) E(Y) = E( (X - E[X]) (Y - E[Y]))

Cov(X, Y) measures how closely X, Y are “correlated” (in a loose sense)

Var(X+Y) = Var(X) + Var(Y) + 2 Cov(X, Y) [for general r.v. X and Y]

What is Cov(X, Y) if X and Y are independent?

Page 38: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Inequality 1: Markov’s Inequality

If X is a random variable which takes non-negative values, and a > 0, then

Pr[X � a] E[X]

a

x0

Pr(X=x)

a

Pr(X >= a)

Example: n tosses of an unbiased coin. X = #heads

E[X] = n/2. Let a = 3n/4. By Markov’s Inequality, Pr(X >= a) <= 2/3. But what is it really?

Summary: Markov’s inequality can be weak, but it only requires E[X] to be finite!

Pr[X ⇥ 3n

4] �

✓✓n

3n/4

◆+

✓n

3n/4 + 1

◆+ . . .+

✓n

n

◆◆· 2�n � n · 2�n ·

✓n

n/4

⇣nk

⌘k

✓n

k

⇣nek

⌘k

Fact: If n >= k

� n · 2�n · (4e)n/4 � n(e/4)n/4 < (e/3)n/4 for large n

Page 39: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Inequality 2: Chebyshev’s Inequality

If X is a random variable and a > 0, then

Example: n tosses of an unbiased coin. X = #heads

E[X] = n/2

Let a = n/4, so that we compute Pr(X >= 3n/4). By Chebyshev, Pr(X >= 3n/4) <= 4/n

Summary: Chebyshev’s inequality can also be weak, but only requires finite Var[X], E[X]

Pr[|X � E[X]| ⇤ a] ⇥ V ar(X)

a2

Var[X] = n/4 (how would you compute this?)

From last slide, Pr(X >= 3n/4) <= cn/4 for some constant c < 1, and large enough n

x0

Pr(X=x)

E[X] + a

Pr(X >= E[X] + a)Pr(X <= E[X] - a)

E[X] - a

Page 40: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Inequality 3: Chernoff Bounds

x0

Pr(X=x)

a

Pr(X>=(1+t)E[X])

Let X1, .., Xn be independent 0/1 random variables, and X = X1 + .. + Xn. Then, for any t>0,

Moreover, for t < 1,

Pr(X � (1 + t)E[X]) ✓

et

(1 + t)(1+t)

◆E[X]

Pr(X (1� t)E[X]) e�12 t

2E[X]

Pr(X<=(1-t)E[X])

Example: n tosses of an unbiased coin. X=#heads= X1 + ... + Xn where Xi=1 if toss i =headE[X] = n/2. Pr[ X >= 3n/4] = Pr[ X >= (1 + 1/2) E[X]), so t = 1/2

Thus from Chernoff Bounds, Pr(X ⇥ 3n/4) �

⇣e1/2 · (2/3)3/2

⌘n/2� (0.88)n/2

Summary: Stronger bound, but needs independence!

Page 41: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Chernoff Bounds: Simplified Version

x0

Pr(X=x)

a

Pr(X>=(1+t)E[X])

Let X1, .., Xn be independent 0/1 random variables, and X = X1 + .. + Xn. Then, for any t>0,

Moreover, for t < 1,

Pr(X � (1 + t)E[X]) ✓

et

(1 + t)(1+t)

◆E[X]

Pr(X (1� t)E[X]) e�12 t

2E[X]

Pr(X<=(1-t)E[X])

Simplified Version:

Let X1, .., Xn be independent 0/1 random variables, and X = X1 + .. + Xn. Then, for t<2e -1, Pr(X > (1 + t)E[X]) e�t2E[X]/4

Page 42: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Randomized Algorithms

• Contention Resolution

• Some Facts about Random Variables

• Global Minimum Cut Algorithm

• Randomized Selection and Sorting

• Max 3-SAT

• Three Concentration Inequalities

• Hashing and Balls and Bins

Page 43: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Hashing and Balls-n-Bins

Problem: Given a large set S of elements x1, .., xn, store them using O(n) space s.t it is easy to determine whether a query item q is in S or not

Popular Data Structure: A Hash table

123

n

TableLinked list of all xi s.t h(xi) = 2

What is the query time of the algorithm?

Algorithm:1. Pick a completely random function

2. Create a table of size n, initialize it to null

3. Store xi in the linked list at position h(xi) of table

h : U � {1, . . . , n}

4. For a query q, look at the linked list at location h(q) of table to see if q is there

Page 44: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Hashing and Balls-n-Bins

Problem: Given a large set S of elements x1, .., xn, store them using O(n) space s.t it iseasy to determine whether a query item q is in S or not

123

n

Table

Algorithm:1. Pick a completely random function h

2. Create a table of size n, initialize it to null

3. Store xi in the linked list at position h(xi) of table

4. For a query q, check the linked list at location h(q)

Average Query Time: Suppose q is picked at random s.t it is equally likely to hash to1, .., n. What is the expected query time?

Expected Query Time = nX

i=1

Pr[q hashes to location i] · (length of list at T [i])]

=

1

n

X

i

(length of list at T [i]) =1

n· n = 1

Page 45: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Hashing and Balls-n-Bins

Problem: Given a large set S of elements x1, .., xn, store them using O(n) space s.t it iseasy to determine whether a query item q is in S or not

123

n

Table

Algorithm:1. Pick a completely random function h

2. Create a table of size n, initialize it to null

3. Store xi in the linked list at position h(xi) of table

4. For a query q, check the linked list at location h(q)

Worst Case Query Time: For any q, what is the query time? (with high probability overthe choice of hash functions)

Equivalent to the following Balls and bins Problem:Suppose we toss n balls u.a.r into n bins. What is the max #balls in a bin with high probability?

With high probability (w.h.p) = With probability 1 - 1/poly(n)

Page 46: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Balls and Bins, again

Suppose we toss n balls u.a.r into n bins. What is the max load of a bin with high probability?

Some Facts:

1. The expected load of each bin is 1

2. What is the probability that each bin has load 1?

3. What is the expected #empty bins?

Probability =

# permutations

# ways of tossing n balls to n bins

=

n!

nn

Pr[Bin i is empty] =

✓1� 1

n

◆n

E[# empty bins] = n

✓1� 1

n

◆n

= �(n) ( (1-1/n)n lies between 1/4 and 1/e for n>=2 )

Page 47: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Balls and Bins

Suppose we toss n balls u.a.r into n bins. What is the max load of a bin with high probability?

Let Xi = #balls in bin i

⇣nk

⌘k

✓n

k

⇣nek

⌘k

Fact: If n >= k

Let t =c log n

log log nfor constant c

log

✓t

e

◆t

= t log t� t =

c log n

log log n· (log c+ log log n� log log log n)

� c

2

log n � 2 log n, for c � 4

For large n, this is

� 1

2

log log n

Therefore, w.p. 1/n2, there are at least t balls in Bin i. What is Pr(All bins have <= t balls) ?

Applying Union Bound, Pr(All bins have <=t balls) >= 1 - 1/n

⇣ne

t

⌘t 1

nt

⇣et

⌘t 1

n2

From Fact Would like this for whp condition

Pr(Xi � t) ✓n

t

◆1

nt

Page 48: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Balls and Bins

Suppose we toss n balls u.a.r into n bins. What is the max load of a bin with high probability?

Fact: W.p. 1-1/n, the maximum load of each bin is at most O(log n/log log n)

Let Xi = #balls in bin i

Pr(Xi � t) �✓n

t

◆1

nt

✓1� 1

n

◆n�t

�⇣nt

⌘t· 1

nt· e�1 � 1

ettAt least 1/en1/3

for t = log n/3 log log n

Let Yi = 1 if bin i has load t or more, Pr(Yi = 1) >= 1/en1/3

Y = Y1 + Y2 + .. + Yn

= 0 otherwise

Pr(Y = 0) = Pr(No bin has load t or more) <= Pr(|Y - E[Y]| >= E[Y]) Which concentrationbound to use?

Using Chebyshev, Pr(|Y - E[Y]| >= E[Y]) <= Var(Y)/E(Y)2

E(Y) >= n2/3 /e

Fact: The max loaded bin has (log n/3log log n) balls with probability at least 1 - const./n(1/3)

Page 49: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

Balls and Bins

Suppose we toss n balls u.a.r into n bins. What is the max load of a bin with high probability?

Fact: W.p. 1-1/n, the maximum load of each bin is at most O(log n/log log n)

Let Yi = 1 if bin i has load t or more, Pr(Yi = 1) >= 1/en1/3

Y = Y1 + Y2 + .. + Yn

= 0 otherwise

Pr(Y = 0) = Pr(No bin has load >= t) <= Pr(|Y - E[Y]| >= E[Y]) <= Var(Y)/E(Y)2 Chebyshev

Var[Y] = Var[(Y1 + .. + Yn)2] =

Now if i is not j, Yi and Yj are negatively correlated, which means that E[YiYj ] < E[Yi]E[Yj ]

X

i

V ar(Yi) + 2X

i 6=j

(E[YiYj ]� E[Yi]E[Yj ])

V ar(Y ) �nX

i=1

V ar(Yi) � n · 1Thus,

Fact: The max loaded bin has (log n/3log log n) balls with probability at least 1 - const./n(1/3)

E(Y) >= n2/3 /e

Pr(Y = 0) V ar(Y )

E(Y )2 ne2

n4/3 e2

n1/3

Page 50: Randomized Algorithms - University of California, San Diego · Randomized Algorithms • Algorithm can make random decisions • Why randomized algorithms? Simple and efficient •

The Power of Two Choices

Problem: Given a large set S of elements x1, .., xn, store them using O(n) space s.t it is easy to determine whether a query item q is in S or not

123

n

TableLinked list of all xi s.t h(xi) = 2

Algorithm:1. Pick two completely random functions

2. Create a table of size n, initialize it to null

3. Store xi at linked list at position h1(xi) or h2(xi), whichever is shorter

4. For a query q, look at the linked list at location h1(q) and h2(q) of table to see if q is there

Equivalent to the following Balls and Bins Problem: Toss n balls into n bins. For each ball, pick two bins u.a.r and put the ball into the lighter of the two bins.

h1 : U � {1, . . . , n}, and h2 : U � {1, . . . , n}

What is the worst case query time? Answer: O(log log n) (proof not in this class)