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)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top