Lecture01 Slides
Lecture01 Slides
TAs: Abby Li, Claire Jin, David Tang, Efe Cekirge, Jonathan Liu, Nick Grill, Raaid
Tanveer, Sophie Liu, Dhruti Kuchibhotla, Summit Wei, Will Gay, Yoseph Mak
Grading and policies
8 Homeworks: 40%
Recitation Attendance 5%
2 Midterm exams 30%
Final exam 25%
• If you can’t make it to your section on a particular week, you may attend a
different section for your attendance credit (please email both your section
TAs and the TAs who run the section you will attend instead)
• If you can’t make it to any sections in a particular week and have a valid
excuse, email your section TAs and they will waive the requirement
Office hours
• See the course calendar! Usually regular, but changes may happen.
• Lastly, we plan to not put a hard stop at 9:30pm. If you need slightly
longer, you’re welcome to keep going a bit more until we need to go
home for bed :)
Goals of the course
Learn how to design algorithms and formally analyze them in several
different models / scenarios. Also practice implementing some!
Definition (Median) The median is the element such that exactly 𝑛/2 elements are larger
Simple algorithm (𝒌𝒕𝒉 smallest): Sort the array and output element 𝑘
Idea (Partial Sorting): Rearrange the numbers such that the 𝑘 th number is in position 𝑘,
and all elements less than the 𝑘 th number occur before position 𝑘
Recall: Randomized QuickSort
function quicksort(𝒂[𝟎 … 𝒏 − 𝟏]) {
select a random pivot element p = 𝑎𝑖 for a random 𝑖
let LESS = [𝑎𝑗 such that 𝑎𝑗 < 𝑝]
let GREATER = [𝑎𝑗 such that 𝑎𝑗 > 𝑝]
return quicksort(LESS) + [𝑝] + quicksort(GREATER)
}
• i.e., we are not assuming that our random-number generator gives us the worst possible
random numbers, and we are not analyzing the algorithm for a randomly chosen input.
Proof of Theorem
Warning: The proof is subtle because it uses probability. We must be careful to
not make false assumptions about how probability and randomness work…
𝑇 𝑛 ≤
Exercise: Show that picking any constant-fraction sized subset (e.g., a quarter, one
tenth) and taking the median doesn’t work.
We need to go deeper!
Note: This idea is extremely subtle. It took four Turing Award
winners to figure it out. We don’t expect that you would produce
this algorithm on your own.
• Finding the median of a smaller set almost worked, but it was just a bit
too much work since the “approximate median” wasn’t good enough.
𝑇 𝑛 ≤
Solving the recurrence
𝑇 𝑛 ≤ 𝑐𝑛 + 𝑇 𝑛/5 + 𝑇(7𝑛/10)
So, the total running time is…
𝑇 𝑛 ≤ 𝑐𝑛(1 + 9/10 + 9/10 2 + 9/10 3 + ⋯)
Summary of DeterministicSelect
function DeterministicSelect(𝑎 0 … 𝑛 − 1 , 𝑘) {
group the array into 𝑛/5 groups of size 5,
• The median of medians is the
find the median of each group key ingredient for getting a
recursively find the median of these medians, call it 𝑝
deterministic algorithm
// Below is the same as Randomized Quickselect
let LESS = [𝑎𝑗 such that 𝑎𝑗 < 𝑝] • To analyze the recurrence, we
let GREATER = [𝑎𝑗 such that 𝑎𝑗 > 𝑝] used the “stack of bricks”
if LESS ≥ 𝑘 then return DeterministicSelect(LESS, 𝑘)
else if LESS = 𝑘 then return 𝑝 method.
else return DeterministicSelect(RIGHT, 𝑘 − LESS − 1)
} • We could also prove it by
induction, but this requires us
to know the runtime already
Take-home messages for today
• Recursion is powerful, randomization is powerful.
• Analyzing randomized recursive algorithms is tricky. Be careful with
expected values!!
• Analyzing runtime via recurrence relations is very useful. We can do it
with several different techniques:
• Guess the answer and verify via induction
• Expand it out / “stack of bricks” if we don’t already know the answer
• You probably learned even more techniques in your prerequisite classes