SOFE3770U: Data Structure
Week 5: Randomized
Algorithms
Dr. Anwar Abdalbari
Based on lecture notes by Masoud Makrehchi 2024
Deterministic Algorithms
Vs.
Randomized Algorithms
2
Deterministic Algorithms
Goal: To prove that the algorithm solves the
problem correctly (always) and quickly
3
Deterministic Algorithms
• The algorithms we've seen so far have been
deterministic.
• We want to aim for properties like
• Good worst-case behavior.
• Getting exact solutions.
• Much of our complexity arises from the fact
that there is little flexibility here.
• Often find complex algorithms with nuanced
correctness proofs.
Randomized Algorithms
Beside input, algorithm takes a source of
random numbers (usually they are uniform) and
makes random choices during its execution
Results can vary even on a fixed input
5
Randomized Algorithms
• A randomized algorithm is an algorithm that
incorporates randomness as part of its
operation.
• Often aim for properties like
• Good average-case behavior.
• Getting exact answers with high probability.
• Getting answers that are close to the right answer.
• Often find very simple algorithms with dense
but clean analyses.
Randomized Algorithms: Use Case1
•A new online dating app designed to help users find potential partners.
•The app works like a game, showing one potential match at a time.
•Users do not know the total number of potential matches (N).
•Tom, a user, sees profiles randomly, one at a time.
•For each profile, Tom must either like (swipe right) or dislike (swipe left).
•Once a decision is made, it cannot be changed—no second chances.
•Tom can like at most n profiles, where n is much smaller than N.
•Interaction ends when Tom has used all n likes or when no more profiles remain.
•A like does not ensure a match; the other person must also like Tom back.
•Tom must decide carefully when to use his limited likes to maximize his chances of
getting the best possible matches.
Randomized Algorithms: Use Case2
•Fang is driving to her friend’s house, located midway along a long, one-way
street.
•Parking is allowed only on one side of the street.
•The street is busy, making it hard to find an open parking spot.
•On average, only about 10% of the nnn spots are available at a given time.
•Fang can see only one parking spot at a time as she passes by.
•She cannot look ahead to see upcoming spots.
•If she skips a spot, she cannot go back to take it later.
•Fang must decide the best time to park in order to minimize walking distance to
her friend’s house.
•She needs a strategy that balances the risk of parking too early (longer walk) vs.
too late (no spots left).
The hiring problem
• Suppose that you need to hire a new office assistant.
• You decide to use an employment agency.
• The employment agency sends you one candidate each day.
• You interview that person and then decide either to hire that person or not.
• You must pay the employment agency a small fee to interview an applicant (Ci).
• To actually hire an applicant is more costly, however, since you must fire your
current office assistant and pay a substantial hiring fee to the employment
agency (Ch).
• You are committed to having, at all times, the best possible person for the job.
9
HIRE-ASISTANT Algorithm
10 hiring (worst case)
1 hiring (best case)
3 hiring
10
Hiring Algorithm Example1
Example 1: 𝐴1 = 1 2 3 4 5 6 7 8 9 10
• Candidates arrive in increasing order of quality.
• Candidate 1 is better than candidate 0 (dummy) →
hire 1
• Candidate 2 is better than 1 → hire 2
• Candidate 3 is better than 2 → hire 3
• … and so on, until candidate 10 is hired.
• Total hires = 10
This is the worst case, because every candidate is
better than the last, so you keep hiring again and
again.
Hiring Algorithm Example2
Example 2: 𝐴2 = 10 9 8 7 6 5 4 3 2 1
• Candidates arrive in decreasing order of
quality.
• Candidate 10 comes first, and since 10 > 0
(dummy), we hire 10.
• After that, no one else is better than 10.
• Total hires = 1
This is the best case, because you hire only
once (the very first candidate is the best).
Hiring Algorithm Example3
Example 3: 𝐴3 = 5 2 1 8 4 7 10 9 3 6
Candidate 5 > 0 → hire 5.
Candidate 2 < 5 → no hire.
Candidate 1 < 5 → no hire.
Candidate 8 > 5 → hire 8.
Candidate 4 < 8 → no hire.
Candidate 7 < 8 → no hire.
Candidate 10 > 8 → hire 10.
Others < 10 → no hire.
Total hires = 3
This is an intermediate case: you end up hiring 3
times when new “best so far” candidates appear.
Cost Analyzing
• Interviews are cheap: maybe just your time and a cup of coffee. Let’s
call this cost 𝑐𝑖 .
• Hiring is expensive: when you actually hire someone, you spend
money on contracts, onboarding, paperwork, training, etc. Let’s call this
cost 𝑐ℎ .
• You must interview all 𝑛 candidates, no matter what. So the interview
cost is always 𝑐𝑖 × 𝑛 .This is fixed.
• But you only hire when you find someone better than all the people
you’ve seen so far. If you hire 𝑚 times, the hiring cost is 𝑐ℎ × 𝑚.
• Since hiring is much more expensive than interviewing, we care most
about minimizing how many times we hire.
• So the key analysis is:
• Interview cost = always the same (𝑐𝑖 𝑛)
• (𝑐ℎ 𝑚)
Hiring cost = depends on the order candidates arrive
• That’s why we focus on analyzing the number of hires, 𝑚 .
14
Cost Analyzing: Worst-case
analysis
15
Probabilistic analysis
• Using of probability in the analysis of problems.
• Analyzing the running time or other quantities
(such as cost in the hiring problem).
• We must use knowledge of, or make
assumptions about, the distribution of the
inputs.
• Computing an average-case, where we take
the average over the distribution of the possible
inputs.
16
Reasonable input distribution
Saying that the applicants come in a random
order is equivalent to saying that this list of
ranks is equally likely to be any one of the 𝑛!
permutations of the numbers 1 through 𝑛 .
Alternatively, we say that the ranks form a
uniform random permutation; that is, each of
the possible 𝑛! permutations appears with
equal probability.
17
Permutation
A permutation = rearrangement of items.
A random permutation = a rearrangement
where each possible order is equally likely.
Why Randomize?
• Prevents worst-case performance in
algorithms (e.g., Quicksort).
• Provides fairness and uniformity in
simulations.
• Helps in unbiased sampling and load
balancing.
Randomized algorithms
Behavior is determined not only by its input but also by values
produced by a random-number generator.
Call to RANDOM (a,b) returns an integer between a and b,
inclusive, with each such integer being equally likely. For
example, RANDOM(0,1) produces 0 with probability 1/2, and it
produces 1 with probability 1/2.
In general, we discuss the average-case running time when the
probability distribution is over the inputs to the algorithm.
20
Analysis of the hiring problem
21
Analysis of the hiring problem
(Cont.)
Even though we interview n people, we
actually hire only approximately ln n of
them, on average. So, the average-case
total hiring cost of (Ch* ln n).
22
Randomized algorithms : Fisher–Yates Shuffle
•Start with array A[1..n].
For i = n down to 2:
Pick random index j in [1, i].
Swap A[i] with A[j].
1
•Result: Each permutation has a probability
𝑛!
•Example
Input: [1, 2, 3]
Possible outputs:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]
(each with equal probability).
Randomized algorithms: Permute by sorting
We use a range of 1 to n^3 to make it likely that
all the priorities in P are unique.
For example, if our initial array is A ={1; 2; 3; 4} and we choose random
priorities P ={36; 3; 62; 19}, we would produce an array B={2; 4; 1; 3},
since the second priority is the smallest, followed by the fourth, then the
first, and finally the third.
24
Applications of Randomized Algorithms
Randomized Quicksort: shuffle array before
sorting.
Simulations: unbiased test cases.
Matching/Pairing Problems: fair random
assignment.