Commentary
This lecture was a little bit of a shitshow. I followed along in the beginning, started only getting the gist of things when the lecturer introduced the harder celebrity problem, and didn't closely listen to the rest.
I decided I would listen to it like a podcast and be okay with reviewing the slides later, watching YouTube videos explaining the concepts, and going to recitation. But it turns out it's hard to make it through 20 more minutes when you have minimal grounding in what they're talking about now.
Also, I was watching in bed, which is perhaps unsuitable for lectures where I actually have to think a lot.
I've gathered that I should get gud at coming up with recurrence relations and unrolling them into closed expressions. I noticed from the chat that some students were able to do this quickly.
For example, for the first version of the celebrity problem, one of the instructors said in the chat: "We unrolled the recursion:"
Tn=Tn−1+1=Tn−2+1+1=...=1+1+...+1=n−1
For harder celebrity problem, why -4? TA: "We overestimated by 4. We said 3 checks per layer, but in layer 1 we used 0 and layer 2 we used only 2."
A student also asked whether celebrities can be found as sinks in a digraph, or something. A TA replied: "A celebrity does have to be a sink - but also needs an incoming edge from all other nodes." I think it would be good to explore more of these concepts and applications/implementations:
Content
I slowly went through the slides the day after the lecture.
Celebrity Problem (1 exists)
- Celebrity: everyone knows them, they don't know anyone
- By definition, there can be at most one celebrity in a set of people
- All we can ask a person is, "Do you know this person?"
-
Solution
- Base case: n=1, person is a celebrity
- Recursive step: Arbitrarily pick A,B from S, a group of n people
- If A knows B, then A cannot be a celebrity, and B might be a celebrity, so perform the algorithm on S−{A}, a set of n−1 people.
- If A doesn't know B, then B cannot be a celebrity, and A might be a celebrity, so perform the algorithm on S−{B}
- We can express this as the following recurrence: Tn=Tn−1+1 for n≥2, T1=0
- Rolling out the recurrence, we get Tn=Tn−1+1=Tn−2+1+1=1+1+...+1=n−1
- Therefore, Tn=n−1 and this is an O(n) algorithm.
Celebrity Problem (0 or 1 exists)
Polish National Flag Problem
- Use minimum number of swaps to move all red pegs to left of all the white pegs
-
Initial algorithm
- Search for leftmost white, rightmost red peg. If white to left of red, swap pegs and repeat; else done
- Analysis (swaps only): In worst case, once we swap we can ignore the two ends, so Tn=Tn−2+1=n/2, where 1 comes from comparing positions
- Analysis (swaps + search): Searching for leftmost white, rightmost red pegs each takes O(n) in worst case. Tn=Tn−2+O(n)+O(n)+1=Tn−2+O(n)=O(n2)
-
Better algorithm
- No need to search over and over for leftmost, rightmost pegs
- Instead, use a left pointer and right pointer, each of which moves O(n) across all searches and swaps
- Comparisons are O(n) total and swaps are O(n) total, so O(n) algorithm
Dutch National Flag Problem
Videos