IBM Developer

Article

Mastering optimization: Comparing greedy algorithm and bin packing

Choose the right optimization technique to save time and resources

By Soumya Sv

Imagine you’re packing your suitcase for a trip. You want to fit as many clothes as possible without exceeding the weight limit. But what if you also have to consider different sizes of items and the best way to distribute the weight? This, in essence, is what optimization is all about—finding the best possible way to do something efficiently.

This article is the first in a planned series focused on optimization for developers. In this part, we explore foundational concepts behind popular optimization strategies like greedy algorithms and bin packing. In the next article, we'll dive deeper into how these approaches apply to real-world developer workflows through hands-on code examples and problem-solving.

In this article, we’ll help you choose the right type of optimization for your programming requirements. We'll look at:

  • Greedy optimization
  • Bin packing optimization
  • Backtracking
  • The connection between greedy optimization plus backtracking and bin packing

What is optimization?

Optimization is about making decisions that maximize or minimize a certain objective while satisfying a set of constraints. Whether it’s reducing costs, maximizing efficiency, or using resources effectively, optimization is everywhere.

Some examples of where optimization plays an important role include:

  • Logistics: Finding the best delivery routes
  • Finance: Making investment decisions
  • Computer science: Data compression, scheduling, and resource allocation
  • Daily life: Choosing the fastest way to get home during rush hour

A general optimization problem can be represented as follows:

alt

Where:

alt

Now, let’s explore two popular optimization techniques: greedy optimization and bin packing optimization.

Greedy optimization

The greedy algorithm follows a simple principle:

At each step, choose the best possible option without worrying about future consequences.

This strategy works if local best choices lead to the global best solution. Let's look at an example.

Example: Complete the most tasks in a day

Imagine you have a busy schedule and want to fit in as many meetings or tasks as possible in a single day. Each task has a start time and an end time, and tasks cannot overlap. Your goal is to maximize the number of non-overlapping tasks you can complete.

The greedy approach includes the following activities:

  • Sort all tasks by their end time (earliest ending tasks first).
  • Pick the task that ends first (leaving space for more tasks later).
  • Move to the next task that starts after the last one ended.
  • Repeat until no more tasks can fit.

This approach works because choosing shorter tasks first leaves more room for others. For developers, greedy optimization might be used for:

  • Scheduling: Assigning tasks or meetings efficiently
  • Network routing: Choosing the shortest path in communication networks
  • Huffman coding: Data compression techniques

When greedy fails—backtracking to the rescue

Greedy works well when local decisions lead to the best global solution. But what happens when that’s not the case? Let's return to our example and introduce a problem.

Example: The "shortest job first" scheduling problem

Imagine you're a manager at a factory where multiple tasks need to be completed, each requiring a different amount of time. Your goal is to minimize the total time taken to complete all tasks.

A greedy strategy would be to always pick the task with the shortest processing time first because it ensures that smaller tasks don’t get stuck behind long tasks, leading to lower overall waiting time. This is called the shortest job first (SJF) strategy.

Now, suppose we have four tasks with the following processing times:

Task Processing Time (minutes)
A 8
B 4
C 2
D 6

We'll apply the greedy approach using the SJF strategy:

  1. Sort tasks by processing time (smallest first): C (2 mins) → B (4 mins) → D (6 mins) → A (8 mins)
  2. Execute them in the following order, with the total time taken for each task (including waiting time for previous tasks):

    • C finishes at 2 minutes
    • B finishes at 2 + 4 = 6 minutes
    • D finishes at 6 + 6 = 12 minutes
    • A finishes at 12 + 8 = 20 minutes
    • Total waiting time = 0 + 2 + 6 + 12 = 20 minutes

You can see how the greedy approach helps minimize the total waiting time!

When does greedy fail?

So far, our example seems to be the perfect situation for the greedy approach to optimization. But while the SJF strategy is optimal if all tasks are available at the start, it fails in real-time scheduling where new tasks arrive dynamically.

Imagine a long task—say, 15 minutes—is already running, and a short task of 1 minute arrives later. The greedy approach won’t interrupt the long task, leading to delays in processing the short one.

A smarter approach would be to dynamically adjust task execution when a shorter job arrives. This is called backtracking, or dynamic programming. Think of it as opting for long-term efficiency over immediate gains!

Bin packing optimization

The bin packing algorithm is all about fitting objects into the minimum number of bins while respecting size constraints. Imagine you’re loading a truck with boxes—your goal is to minimize the number of trucks needed. That's the essence of bin packing.

Example: Packing items into bins

Say we have bins with a maximum weight limit of 10 kg and items weighing 7 kg, 5 kg, 6 kg, and 4 kg. There are multiple strategies we could use:

  • First fit: Place each item in the first bin that has enough space.
  • Best fit: Place each item in the bin that leaves the least extra space.
  • First fit decreasing (FFD): Sort items in descending order, then apply the first fit strategy.

So where is bin packing used? It's all around us:

  • Manufacturing: Optimizing raw material usage
  • Logistics and shipping: Minimizing shipping costs
  • Cloud computing: Allocating storage efficiently

Greedy vs. bin packing: Which one to use?

The choice of whether to use greedy or bin packing optimization depends on your unique requirements, but you can use the following matrix to get clues as to which to use:

Greedy Bin packing
Decision-making Quick, step-by-step choices Efficient resource allocation
Best for Problems where local choices lead to a global solution Minimizing wasted space
Guarantees optimal solution? Not always Often better than Greedy alone
Example Choosing meetings to attend Packing items into trucks efficiently

We can start to see some general rules when comparing optimization techniques:

  • Greedy is fast but not always perfect.
  • Backtracking helps when greedy fails.
  • Bin packing helps minimize space usage in real-world applications.

Greedy-plus-backtracking vs. bin packing: What's the difference?

At first glance, the combination of greedy and backtracking looks quite similar to bin packing. Both techniques aim to make efficient choices while considering future consequences.

However, on closer examination, we can see that, the two techniques solve similar problems, their approach is very different.

Let's look at one more example.

Example: Finding your way out of a building

Imagine you're trying to find your way out of a building to get to a location on the street. In this scenario:

  • Greedy plus backtracking is like trying random doors, realizing you're stuck, and then going back to try another route. You might eventually find the exit, but it’s inefficient because you’re correcting mistakes along the way.
  • Bin packing is like having a building map and planning the best path from the start. You know which doors to take and you avoid unnecessary backtracking altogether.

Let's look at our table again:

Greedy + backtracking Bin packing
What it does Picks choices step by step, fixing mistakes when needed Systematically arranges items to minimize the number of bins
Goal Finds the best solution step by step Minimizes the number of bins required
Decision-making Chooses first, corrects later Allocates items based on best fit strategies
Speed Slower due to trial and error More structured and optimized from the beginning
Example When the order of selections matters (for example, scheduling tasks) When efficient grouping is key (for example, packing items in boxes)

Summary and next steps

We've looked at what optimization is, examined how it's used in the real world, and drilled down into two useful techniques, greedy algorithm and bin packing.

As a developer, here’s what you’ve learned:

  • Greedy algorithms are your go-to for fast, local decisions that may not always be optimal but are quick to implement.
  • Backtracking becomes necessary when those fast decisions need correcting.
  • Bin packing is ideal when you're looking to minimize space, memory, or resource usage efficiently—especially in scenarios like cloud allocation or logistics.

When writing code, the choice of algorithm directly impacts performance, maintainability, and resource usage. This conceptual understanding is your first step toward building smarter systems.

Check out these resources to go further:

Now that you've learned the "why" behind optimization, next up is the "how." Watch for the next article in this series where you'll get ready to transform these strategies into real Python code, one smart decision at a time.