Greedy Algorithm in Grabbing Fritters
Yesterday afternoon I was hanging out at the local coffee stand with the dorm guys. Right at that moment, the vendor brought out a full plate of mixed fried fritters: vegetable fritters, tempeh, stuffed tofu, and fried bananas, still steaming hot.
If you observe closely, this moment of fighting over fritters is actually a perfect social experiment to prove how the Greedy Algorithm works.
In computer science, the Greedy Algorithm is an approach where you take the most profitable choice (local optimum) at every step, hoping that those choices will lead you to the most optimal overall solution (global optimum). Basically, you are greedy. You see a big profit right in front of your eyes, you immediately snatch it without thinking about the long-term effects.
My friend, let us call him A, is a strong believer in this algorithm. As soon as the plate was put down, his hand immediately grabbed the two biggest and crispiest vegetable fritters on top of the pile. At a glance, this is a brilliant decision. He got maximum value in the first iteration. The best local optimum.
But the problem is, he forgot that his stomach capacity (knapsack capacity) is limited, and he also forgot that the fritters are not the only resource on the table. While he was busy chewing his second vegetable fritter, the vendor brought out a super delicious special peanut sauce. Now, it turns out this peanut sauce pairs much better with the stuffed tofu, not the vegetable fritters.
A's stomach capacity was almost full because the vegetable fritters are very heavy and filling. When he wanted to grab the stuffed tofu to dip in the sauce, it turned out the stuffed tofu was already gone, taken by B who used a Dynamic Programming algorithm.
B is the patient type of person. He did not immediately grab the biggest one at the start. He first scanned the overall resources available on the table, remembered the peanut sauce arrival time from his past experiences, then he calculated the optimal combination. He chose to take the stuffed tofu first which is smaller in size, leaving space in his stomach, and when the sauce came out, he got a much larger combination of enjoyment (global optimum) compared to A.
This is the fatal flaw of the Greedy Algorithm. It is very fast at making decisions (O(n) or O(n log n) if you sort first), but it very often gets trapped in a local optimum and is blind to the bigger scenario.
In real life, we are often like A. You see a job vacancy with a high salary at a toxic company, you immediately take it (greedy). Once you are in, your mental health is ruined and you have no time for upskilling. On the other hand, someone chooses a smaller salary but a work environment that supports them to learn new technologies. Two years later, this second person is able to jump to a unicorn company with triple the salary.
The Greedy Algorithm is not entirely wrong. For certain problems like finding the shortest route using Dijkstra or returning change using the largest coins first, it is indeed the fastest and most accurate solution. But for complex things like a career, finding a partner, or choosing fritters, you need a wider time horizon.
Do not be easily blinded by instant rewards right in front of your eyes. Sometimes you have to let go of the crispiest vegetable fritter today to get the stuffed tofu plus peanut sauce that is far more satisfying later.
Think about the optimal combination, manage your space, and do not let yourself regret a five-second emotional decision.
- Khay