Lecture from: 26.09.2024 | Video: Videos ETHZ | Rui Zhangs Notes | Official Script
Star Search
In many real-world systems like social networks or organizations, there can be a special individual called a star.
A star has two key traits:
- Everyone knows the star
- The star knows no one
Our task is simple: if there is a star in a group of people, we need to find them with the fewest possible questions. We can ask one person if they know another person.
It’s important to note that this “knowing” relation is not symmetric. If person knows person , it doesn’t mean that knows .
Algorithm 0: The Naive Way
The simplest way to solve this would be to ask each person about every other person. This means asking questions — too many!
Algorithm 1: Divide and Conquer
Instead of brute-forcing, we can use a more efficient approach inspired by recursion or divide-and-conquer (like we’ve seen in earlier algorithms).
How it works:
For (since is the base case), follow these steps:
- Recursively find the star, , among the first people.
- Check if is the star among all people by:
- Asking if is the star for the last person (just 2 questions).
- If isn’t the star for , check if is the star among the first people (ask questions).
Best Case
The best scenario is when turns out to be the actual star in the group. This requires only 2 questions for each person, meaning we ask questions in total.
Worst Case
In the worst case, isn’t the star, and we end up asking more questions in step 2.2. This could lead us back to the same amount of work as the naive algorithm, questions.
Algorithm 2: Avoid Excluding the Star
Can we improve the worst-case scenario? Yes!
In Algorithm 1, the worst case happens if we mistakenly exclude the real star during the recursive steps. We can avoid this by slightly modifying the approach:
- Before running the recursion, check if the second last person, , knows the last person . If they do, swap them out, since by definition they cannot be the star, and continue. This ensures we won’t accidentally exclude the star, i.e. the star is guaranteed to be part of the people.
- Once we reduce the group to two people, check if one of them is the star. If there’s no star, we’ll know for sure because our method guarantees the star, if it exists, remains in the group.
Worst-Case Scenario
In Algorithm 2, we make sure not to accidentally exclude the real star during the process. This improves the worst-case scenario compared to Algorithm 1, even though the general approach remains similar.
Here’s how it works:
- Each time we check whether the current potential star, , is still the star for the newly added person , we need to ask 2 questions.
- Additionally, before we proceed to the next recursive step, we make sure that the potential star is included in the next group of people by asking 1 more question. This ensures we don’t exclude the real star.
By summing up the total number of operations for the entire process, we find that the algorithm requires only operations in the worst case — a significant improvement over the naive approach!
Note to self: we also did a bit of 1) Comparing Algorithms and 2) Asymptotic Notation.
Continue here: 03 Max Subarray Sum