Pack Objects with Greedy Algorithm: Is It Right?

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the development and analysis of a greedy algorithm for packing objects into boxes of varying capacities. Participants explore the correctness of the algorithm, potential optimizations, and the complexity of the operations involved.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant proposes a greedy algorithm to minimize the number of boxes needed to pack a given number of objects.
  • Another participant questions the algorithm's output, noting that returning the remaining objects would always yield zero.
  • Some participants agree that the algorithm is greedy, as it selects the largest box size available at each step.
  • There is a suggestion to optimize the algorithm by replacing while-loops with division and modulus operations to reduce complexity.
  • One participant proposes that the complexity of the algorithm is $O(\log{N})$, based on the operations involved.
  • Another participant challenges this assumption, suggesting that the complexity should be considered as a constant based on the number of operations performed.
  • A simplified version of the algorithm using a loop over box sizes is presented, though it is noted that this does not impact the overall complexity.

Areas of Agreement / Disagreement

Participants express differing views on the complexity of the algorithm and the correctness of the initial output. There is no consensus on the best way to analyze the complexity or the final form of the algorithm.

Contextual Notes

Participants discuss the assumptions regarding the complexity of division and modulus operations, indicating that these may vary based on implementation and context.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
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
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)
 
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)
 
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)
 
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)
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 29 ·
Replies
29
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
Replies
16
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K