MHB Pack Objects with Greedy Algorithm: Is It Right?

  • Thread starter Thread starter evinda
  • Start date Start date
AI Thread Summary
The discussion centers on developing a greedy algorithm to efficiently pack N objects into boxes of varying capacities (1, 2, 5, 10, and 20). The initial algorithm proposed involves multiple while-loops to decrement the number of remaining objects and count the boxes needed. However, it is pointed out that the algorithm incorrectly returns the remaining objects instead of the box count. Participants suggest optimizing the algorithm by replacing the while-loops with division and modulus operations, which simplifies the code and reduces complexity. The revised algorithm counts boxes using a loop over the box sizes, which maintains efficiency. The complexity of the algorithm is debated, with some suggesting it could be O(log N) based on the operations, while others argue for a constant time complexity due to the fixed number of operations involved. Ultimately, the discussion concludes with a more streamlined version of the algorithm that retains clarity and efficiency without impacting performance.
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)
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top