Registry Synced

Randomized Algorithms

718 words
4 min read

Reading compass

Now ยท ๐ŸŽฏ Learning Objectives

Randomized Algorithms

๐ŸŽฏ Learning Objectives

  • Distinguish Monte Carlo vs. Las Vegas algorithms
  • Analyze expected running time using probability
  • Apply randomness to graph cut and SAT problems
  • Test primality using Miller-Rabin
  • Derive concentration bounds (Chernoff, Markov)

1. Types of Randomization

TypeAlways Correct?Always Fast?Example
Las VegasYesExpectedRandomized quicksort
Monte CarloWith high probabilityYesMiller-Rabin primality

2. Randomized Min-Cut (Karger's Algorithm)

2.1 Algorithm

Contract random edges until 2 nodes remain. The edge between them = cut.
python
import random
def karger_min_cut(graph):
    vertices = list(graph.keys())
    while len(vertices) > 2:
        u = random.choice(vertices)
        v = random.choice([n for n in graph[u] if n in vertices])
        # Merge u and v
        vertices.remove(v)
        graph[u].extend(graph[v])
        # Remove self-loops
        graph[u] = [x for x in graph[u] if x in vertices]
    return len(graph[vertices[0]])

2.2 Analysis

  • Probability a specific min-cut survives one contraction: โ‰ฅ1โˆ’2/n\geq 1 - 2/n
  • Probability it survives all nโˆ’2n-2 contractions: โ‰ฅ2n(nโˆ’1)=ฮฉ(1/n2)\geq \frac{2}{n(n-1)} = \Omega(1/n^2)
  • Repeat O(n2)O(n^2) times โ†’ success probability โ‰ฅ1โˆ’1/e\geq 1 - 1/e

3. Randomized MAX-SAT

3.1 Algorithm

Randomly assign each variable True/False with probability 0.5. Expected performance: Each clause with kk literals is satisfied with probability 1โˆ’(1/2)k1 - (1/2)^k. For 3SAT: E[satisfied]โ‰ฅ(1โˆ’1/8)m=7m/8E[\text{satisfied}] \geq (1 - 1/8)m = 7m/8 (87.5% approximation).

3.2 Derandomization via Conditional Expectations

Process variables one by one. For each variable, try both True and False, compute expected value if remaining variables are random. Choose setting that maximizes conditional expectation. Guarantees at least 7m/87m/8 clauses satisfied (deterministically).

4. Miller-Rabin Primality Test

4.1 Algorithm

For odd nn, write nโˆ’1=2sโ‹…dn-1 = 2^s \cdot d. Choose random aโˆˆ[2,nโˆ’2]a \in [2, n-2]. Compute x=admodโ€‰โ€‰nx = a^d \mod n. If x=1x = 1 or x=โˆ’1x = -1, declare probably prime. Otherwise, repeat sโˆ’1s-1 times: x=x2modโ€‰โ€‰nx = x^2 \mod n. If x=โˆ’1x = -1, declare probably prime. If no x=โˆ’1x = -1 found, nn is composite.

4.2 Error Analysis

IterationsError Bound
1<1/4< 1/4
kk<(1/4)k< (1/4)^k
10<10โˆ’6< 10^{-6}

5. Concentration Bounds

InequalityWhen to UseStatement
MarkovNon-negative random variableP(Xโ‰ฅt)โ‰คE[X]/tP(X \geq t) \leq E[X]/t
ChebyshevKnown variance$P(
ChernoffSum of independent variablesP(Xโ‰ฅ(1+ฮด)ฮผ)โ‰คeโˆ’ฮด2ฮผ/3P(X \geq (1+\delta)\mu) \leq e^{-\delta^2\mu/3}

6. Common Pitfalls

Pitfall: Assuming Independence Where None Exists

The mistake: Applying Chernoff bound to dependent random variables. Correct approach: Use union bound or more sophisticated concentration inequalities (Azuma, McDiarmid) for dependent variables.

7. Key Concepts Reference

AlgorithmTypeComplexitySuccess
Karger min-cutMonte CarloO(n2logโกn)O(n^2 \log n)1โˆ’1/e1 - 1/e
Randomized MAX-SATLas Vegas (derandomizable)O(m)O(m)0.875-approx
Miller-RabinMonte CarloO(klogโก3n)O(k \log^3 n)(1/4)k(1/4)^k error
QuickselectLas VegasO(n)O(n) expectedAlways correct

8. ๐Ÿ“ Practice Questions

Q1: Run one iteration of Karger's on Kโ‚„ (complete graph on 4 vertices). What's the probability of finding the minimum cut?
Answer: Min cut of Kโ‚„ = 3. Probability a specific min-cut survives: 2/(4ร—3) = 1/6 per iteration. With O(n2)=16O(nยฒ) = 16 iterations, success probability โ‰ˆ 1โˆ’(5/6)16โ‰ˆ0.941 - (5/6)^{16} โ‰ˆ 0.94. Q2: For a 3SAT formula with 100 clauses, what's the expected number satisfied by random assignment?
Answer: Each clause satisfied with probability 7/8. Expected = 100 ร— 7/8 = 87.5 clauses. By linearity of expectation, this holds even if clauses share variables. Q3: Miller-Rabin says n is probably prime with k=5 iterations. What's the error probability?
Answer: Error probability < (1/4)^5 = 1/1024 โ‰ˆ 0.001. So there's a 99.9% chance n is prime. For cryptographic applications, 20+ iterations are used (error < 2^{-40}). Q4: Derandomize the MAX-SAT algorithm. What guarantee do we get?
Answer: Process variables sequentially. For each variable, compute expected satisfied clauses if we set it True vs. False (with remaining variables random). Choose the better setting. This guarantees at least 7/8 of MAX-SAT optimum (deterministically). This is the method of conditional expectations. Q5: Apply Markov's inequality: Expected runtime of algorithm is 1 hour. What's P(runtime > 10 hours)?
Answer: P(X > 10) โ‰ค E[X]/10 = 1/10 = 10%. Markov's inequality is very weak but requires no assumptions about distribution.

9. ๐Ÿ”— Cross-References

Document outline

Keep your place and jump directly to a heading.

Table of contents

No note selected

Open a chapter to see its outline, backlinks, and sources here.