Pack Objects with Greedy Algorithm: Is It Right?

  • MHB
  • Thread starter evinda
  • Start date
In summary, the conversation discusses a greedy algorithm for packing objects into boxes and ways to optimize it. The algorithm involves using boxes of different capacities and filling them completely to prevent objects from moving during transfer. The algorithm is found to be greedy as it always selects the biggest option at each step. Suggestions are made to optimize the algorithm and reduce its complexity, and the complexity is determined to be O(1) based on the number of operations.
  • #1
evinda
Gold Member
MHB
3,836
0
Hello! (Wave)

At a relocation we have $N$ objects with the same weight that we want to pack in boxes. We have at our disposal boxes of capacity 1,2,5,10 and 20 objects (so many that we want from each size). In order the objects not to move at the transfer, each box should be completely full. I want to write and analyze a greedy algorithm that computes the minimum number of boxes needed to pack the objects.

I have thought of the following algorithm:

Code:
boxes=0
remaining=N
while (remaining>=20) {
         boxes=boxes+1
         remaining=remaining-20
}
while (remaining>=10) {
         boxes=boxes+1
         remaining=remaining-10
}
while (remaining>=5) {
         boxes=boxes+1
         remaining=remaining-5
}
while (remaining>=2) {
         boxes=boxes+1
         remaining=remaining-2
}
while (remaining>=1) {
         boxes=boxes+1
         remaining=remaining-1
}
return remaining
Is my algorithm right? Could something be improved?

The algorithm is greedy, isn't it? (Thinking)
 
Technology news on Phys.org
  • #2
evinda said:
Is my algorithm right?

Hey evinda!

You are returning [M]remaining[/M] as the result of the algorithm.
But that one will always be zero, won't it?
I don't think that is the result of the algorithm. (Worried)

evinda said:
The algorithm is greedy, isn't it?

Yep. At every step you select the biggest option. And that makes it greedy. (Nod)

evinda said:
Could something be improved?

We might optimize it a bit and eliminate the while-loops.
Instead of:
Code:
while (remaining>=20) {
         boxes=boxes+1
         remaining=remaining-20
}
we can do:
Code:
boxes += remaining / 20
remaining -= remaining % 20
can't we?
It reduces the complexity of the algorithm. (Nerd)
 
  • #3
Klaas van Aarsen said:
Hey evinda!

You are returning [M]remaining[/M] as the result of the algorithm.
But that one will always be zero, won't it?
I don't think that is the result of the algorithm. (Worried)

Yes, right... [m]boxes[/m] should be the result of the algorithm... (Tmi)

Klaas van Aarsen said:
Yep. At every step you select the biggest option. And that makes it greedy. (Nod)

I see! (Blush)

Klaas van Aarsen said:
We might optimize it a bit and eliminate the while-loops.
Instead of:
Code:
while (remaining>=20) {
         boxes=boxes+1
         remaining=remaining-20
}
we can do:
Code:
boxes += remaining / 20
remaining -= remaining % 20
can't we?
It reduces the complexity of the algorithm. (Nerd)
Shouldn't the command of remaining be remaining=remaining%20 ? I have applied it at an example... (Blush)So the algorithm is the following, right?
Code:
boxes=0
remaining=N
boxes+=remaining/20
remaining=remaining%20
boxes+=remaining/10
remaining=remaining%10
boxes+=remaining/5
remaining=remaining%5
boxes+=remaining/2
remaining=remaining%2
boxes+=remaining
return boxes
The complexity of the algorithm is the complexity of the operations of division and modulus done.

The division and computation of modulus of $a$ and $b$, where $a,b$ arbitrary, takes $O(\log{a} \cdot \log{b})$ time, doesn't it?

So the complexity of the algorithm is $O(\log{20} \cdot \log{N})=O(\log{N})$, right?Or is something of the above wrong? (Thinking)
 
  • #4
evinda said:
Shouldn't the command of remaining be remaining=remaining%20 ? I have applied it at an example...

It is. (Tmi)

evinda said:
So the algorithm is the following, right?

Yep. (Nod)

evinda said:
The complexity of the algorithm is the complexity of the operations of division and modulus done.

The division and computation of modulus of $a$ and $b$, where $a,b$ arbitrary, takes $O(\log{a} \cdot \log{b})$ time, doesn't it?

So the complexity of the algorithm is $O(\log{20} \cdot \log{N})=O(\log{N})$, right?

It depends on the complexity of division and modulus.
Rather than making the assumption that they are $\log(a)\cdot\log(b)$, I propose to simply count the number of operations and leave it at that.
Then the complexity is:
$$4 \operatorname{divisions} + 4 \operatorname{moduli} + 5 \operatorname{additions} = O(1)$$
(Thinking)

Btw, we can simplify it a little more with:
Code:
    int boxes = 0;
    for (int b : {20, 10, 5, 2, 1}) {
        boxes += N / b;
        N %= b;
    }
    return boxes;
That doesn't impact the complexity though. (Nerd)
 
  • #5
Klaas van Aarsen said:
It depends on the complexity of division and modulus.
Rather than making the assumption that they are $\log(a)\cdot\log(b)$, I propose to simply count the number of operations and leave it at that.
Then the complexity is:
$$4 \operatorname{divisions} + 4 \operatorname{moduli} + 5 \operatorname{additions} = O(1)$$
(Thinking)

Btw, we can simplify it a little more with:
Code:
    int boxes = 0;
    for (int b : {20, 10, 5, 2, 1}) {
        boxes += N / b;
        N %= b;
    }
    return boxes;
That doesn't impact the complexity though. (Nerd)

I see... Thanks lot! (Blush)
 

FAQ: Pack Objects with Greedy Algorithm: Is It Right?

H2: What is a greedy algorithm?

A greedy algorithm is a type of algorithm that makes decisions based on the current best option without considering the potential long-term consequences. It is often used for optimization problems where finding the best solution is the main goal.

H2: How does a greedy algorithm work?

A greedy algorithm works by making the locally optimal choice at each step, without considering the global optimal solution. It starts with an empty solution and adds elements one at a time, choosing the best option at each step. This process continues until the desired solution is reached.

H2: When is a greedy algorithm the right approach for packing objects?

A greedy algorithm is a good approach for packing objects when the goal is to maximize the value or minimize the cost of the packed objects. It is also useful when the problem can be broken down into subproblems and the optimal solution to the overall problem can be found by combining the optimal solutions to the subproblems.

H2: What are the advantages of using a greedy algorithm for packing objects?

One of the main advantages of using a greedy algorithm for packing objects is its efficiency. It has a relatively simple implementation and runs in linear time, making it a good choice for large datasets. Additionally, it often produces good solutions, although they may not always be the optimal solution.

H2: Are there any disadvantages to using a greedy algorithm for packing objects?

Yes, there are some disadvantages to using a greedy algorithm for packing objects. One major disadvantage is that it may not always produce the optimal solution. In some cases, a greedy algorithm may lead to a solution that is close to the optimal, but not the best possible solution. Additionally, a greedy algorithm may not work for all types of optimization problems, so it is important to carefully consider the problem before deciding to use a greedy algorithm.

Similar threads

Replies
2
Views
1K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
1
Views
806
Replies
7
Views
2K
Replies
1
Views
1K
Replies
2
Views
1K
Replies
17
Views
1K
Replies
2
Views
2K
Back
Top