Vanshaj Shah

Posted on

# Greedy Algorithm

In life, we often make decisions that seem best for us at the moment. Whether it’s choosing the quickest route home, picking the tastiest snack, or taking the job offer that pays the most right now, we tend to go for the option that gives us the most immediate benefit. Just like a person who grabs every opportunity for personal gain without thinking about the future, greedy algorithms mirror the way some people make decisions by always going for the immediate benefit or the most rewarding option without considering the long-term consequences.

## How it works?!

Greedy algorithms are designed to be simple and quick. Just like how you might decide what to eat based on what looks good, a greedy algorithm makes decisions based on what’s most immediately beneficial. This makes them very efficient in solving problems, but they don’t always find the absolute best solution—just like how you might miss out on a great dish further down the buffet table because you filled your plate too soon. Greedy algorithms are great because they are simple, fast, and often get the job done. But just like in real life, where making decisions based solely on immediate benefits can sometimes lead to regret, greedy algorithms don’t always find the best solution. They’re a bit like taking shortcuts—they can save you time, but they might also make you miss out on something better.

Greedy algorithms work best when the problem is such that making the best choice at each step leads to the best overall solution. For example, if you’re packing a bag and want to maximize space, a greedy approach might work because each item you pack fills the bag a bit more. But if the problem is more complex—like choosing a career path or planning for retirement—other approaches might be better.

## Real-life Examples

1. Think about paying for something with coins. If you want to minimize the number of coins you use, you might start by giving the largest denomination coin first. If you need to pay ₹87, you’d first use a ₹50 coin, then a ₹20, a ₹10, a ₹5, and finally two ₹1 coins. This is a greedy approach because at each step, you’re choosing the coin that gets you closest to your goal with the fewest coins.
``````function CoinChange(coins[], amount):
sort(coins in descending order)

num_coins = 0

for each coin in coins:
if amount == 0:
break

coin_count = floor(amount / coin)
num_coins = num_coins + coin_count
amount = amount - (coin_count * coin)available coins
if amount > 0:
return -1
return num_coins
``````
1. Imagine you’re offered several jobs. A greedy approach would be to pick the one with the highest salary without considering other factors like work-life balance or growth opportunities. While you might end up with a good paycheck, you could miss out on long-term benefits.

2. Kruskal’s Algorithm: This algorithm works like someone trying to make connections with the least effort. It connects the closest points first, ensuring that the overall path is as short as possible, just like you might choose to finish the easy tasks on your to-do list first.

``````function Kruskal(graph):
mst = [] order
sort(graph.edges by weight)vertex
disjoint_set = DisjointSet(graph.vertices)
for each edge (u, v) in graph.edges:belong
set_u = disjoint_set.find(u)
set_v = disjoint_set.find(v)MST
if set_u != set_v: