Pack Objects with Greedy Algorithm: Is It Right?

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

The forum discussion centers on a greedy algorithm designed to pack $N$ objects of uniform weight into boxes of varying capacities (1, 2, 5, 10, and 20 objects). The initial algorithm proposed was inefficient due to the use of multiple while-loops. An optimized version was presented, which utilizes division and modulus operations to reduce complexity to O(1). The final algorithm iterates through box sizes in a single loop, confirming that the complexity remains constant regardless of the number of objects.

PREREQUISITES
  • Understanding of greedy algorithms
  • Familiarity with basic programming constructs (loops, conditionals)
  • Knowledge of time complexity analysis
  • Experience with integer division and modulus operations
NEXT STEPS
  • Study the implementation of greedy algorithms in various programming languages
  • Learn about time complexity and Big O notation
  • Explore optimization techniques for algorithm efficiency
  • Investigate other packing problems and their solutions
USEFUL FOR

Software developers, algorithm enthusiasts, and computer science students looking to enhance their understanding of greedy algorithms and optimization techniques in programming.

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
4K
  • · 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