Randomized Searching and Sorting
Overview
Kent Quanrud introduces randomized algorithms through three examples: random assignments for 3-SAT, randomized quicksort, and quickselect. He develops probability tools, especially linearity of expectation, then uses them to show that random 3-SAT assignments satisfy an expected 7/8 of clauses, derandomize that guarantee with conditional expectation, and analyze quicksort's expected O(n log n) comparisons by counting pairs.
Key takeaways
- Independent fair assignments satisfy each 3-SAT clause with probability 7/8, so an M-clause formula has an expected 7M/8 satisfied clauses and a 7/8 approximation to its optimum.
- Linearity of expectation applies even to dependent random variables, allowing the expected number of satisfied clauses to be computed without modeling how clauses overlap.
- Conditional expectation derandomizes the 3-SAT assignment: evaluate both settings of each variable under the remaining randomness and choose one that preserves the current expected score.
- Quicksort compares ranks i and j with probability 2/(j − i + 1), because one endpoint must be selected before any pivot between them.
- Summing quicksort's pairwise comparison probabilities produces harmonic numbers, whose Θ(log n) growth gives O(n log n) expected comparisons despite the algorithm's O(n²) worst case.
Chapters
- Kent Quanrud distinguishes deliberate coin flips in randomized algorithms from amortized analysis, which averages over operations in hindsight.
- Randomness can make algorithms simpler, while the analysis becomes more sophisticated.
- The focus is on algorithms that remain correct for every input, even when their performance or output quality is analyzed in expectation.
- For each variable in a 3-SAT formula, the simplest algorithm independently chooses true or false with equal probability, without inspecting the formula.
- A clause with three literals is unsatisfied only when all three literals are false, which occurs with probability 1/8.
- The resulting assignment satisfies an expected 7/8 of all clauses and therefore gives at least a 7/8 approximation to the maximum number satisfiable.
- Quicksort chooses a random input element as pivot, partitions the remaining elements into smaller and larger groups, then recurses on both.
- The lecture contrasts its expected O(n log n) running time with the possible O(n²) behavior from repeatedly choosing extreme pivots.
- Quanrud notes that the algorithm is simple to implement and presents a high-probability O(n log n) guarantee as a stronger result.
- Quickselect uses the same random-pivot partition as quicksort to locate a kth-smallest or kth-largest element.
- After partitioning, it recurses only into the side containing the target rank, unlike quicksort, which processes both sides.
- Its expected running time is linear, offering a practical alternative to the more elaborate median-of-medians method.
- Probability is introduced through events such as a coin landing heads, with complementary event probabilities summing to one.
- Conditional probability measures an event within the restricted cases where another event has occurred; dependence can change the result.
- The union bound gives P(A or B) ≤ P(A) + P(B), without requiring A and B to be independent.
- A random variable assigns numerical outcomes to experiments, such as the values from one or two dice.
- For independent coin tosses, the probability of two heads is 1/4; if both variables represent the same toss, it is 1/2.
- Expected value is calculated by multiplying each possible outcome by its probability and summing, as with a fair die's mean of 3.5.
- For any random variables X and Y, E[X + Y] = E[X] + E[Y], even when the variables are dependent.
- For a nonnegative random variable X, Markov's inequality gives P(X ≥ α) ≤ E[X]/α.
- Setting α to five times the mean shows that the chance of reaching at least five times the mean is at most 1/5.
- For one 3-SAT clause, independent random truth assignments make the probability of failure 1/8, so the satisfaction probability is 7/8.
- Enumerating joint outcomes for two overlapping clauses quickly becomes cumbersome, illustrating why analyzing every full assignment does not scale.
- The key simplification is to seek the average number of satisfied clauses rather than enumerate every possible assignment.
- For each clause i, define an indicator Yᵢ that is 1 when the clause is satisfied and 0 otherwise.
- The total number of satisfied clauses is the sum of the M indicators, and each has expectation 7/8.
- Linearity of expectation gives an expected total of 7M/8, regardless of how variables overlap between clauses.
- Condition on the first variable being true or false; the two conditional expected clause counts average to the original expectation.
- At least one of those choices must preserve or improve the current conditional expectation, so compute both and commit to the better one.
- Repeat for every variable, calculating each choice's expected satisfied-clause count from the remaining random variables, to obtain a deterministic assignment with at least 7M/8 satisfied clauses.
- A random pivot of rank k produces recursive subproblems of sizes k − 1 and n − k, plus O(n) partitioning work.
- A consistently central pivot gives the familiar merge-sort-style O(n log n) recurrence, while repeatedly extreme pivots can cost O(n²).
- Tracking every sequence of good and bad pivots is complicated, so the analysis instead counts individual comparisons.
- For each pair of sorted ranks i and j, define Xᵢⱼ as 1 if quicksort compares those elements and 0 otherwise.
- The sum of these pair indicators equals the total number of comparisons, which captures quicksort's running time up to constant factors.
- Ranks i and j are compared exactly when one of them becomes the first pivot among the elements with ranks i through j.
- For ranks i < j, the probability that they are compared is 2/(j − i + 1), because either endpoint must be the first pivot in that rank interval.
- Summing these probabilities over all pairs produces harmonic sums of the form 1 + 1/2 + … + 1/n.
- The harmonic number is Θ(log n), yielding O(n log n) expected comparisons; the lecture also mentions, without proving, tail bounds that make much larger costs unlikely.
- Quanrud distinguishes expected O(n log n) performance from a high-probability guarantee, where the chance of exceeding a larger multiple declines rapidly with n.
- The central analysis technique is to represent a complicated total with indicators and use linearity of expectation instead of enumerating execution histories.
- The lecture closes with course scheduling notes, including a canceled randomized-rounding lecture and the Thanksgiving break.
Transcript excerpt
0:05 Okay. All right. So today we're doing um a little bit of randomized algorithms. So um and these [snorts] are some of the the topics we'll do. Uh forget that. Um Okay. [snorts] So Right. So um last time we did amortized analysis. That was one kind of like average case analysis of algorithms because they're always in kind of in hindsight. And today we're going to do randomized algorithms that actually deliberately flip coins. And sometimes that actually is helpful. Often our algorithms are much simpler. Uh but the analysis becomes a little more sophisticated. Um one thing I want to keep our eye on is that all our algorithms will always work uh for any input. Okay? So we'll never lose the worst case guarantees even though the the word average is
0:51 floating around in there. And that's really important. So um so this is the other half of the word average. And so it will depend a little bit on your I know you've done some probability probably in discrete math. So we'll just see like how warm you are with it or if we should go over some stuff more slowly. You know, the slides are there. But uh the game plan is to first um just introduce three simple algorithms right away uh without analyzing them just to get it like a little more fun. And then we'll like go through all the usual definitions and depending on how comfortable you are, we can skip some or blah blah blah. And then we'll go back and analyze uh the algorithms. So I want to try to get back to algorithms as quickly as possible. Okay. So
1:37 [snorts] All right. So the first problem we'll look at is is 3-SAT, your favorite. We were just talking about it.
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Kent Quanrud.