-1

I have a list of class Item:

public class Item
{
   public int Id { get; set; }
   public string Name { get; set; }
   public int ItemSize { get; set; }
   public int? ContainerId { get; set; }
}

and also a class Container

public class Container
{
   public int Id { get; set; }
   public int ContainerSize { get; set; }
}

A container have a Max Value for the property Size. I need to assign each object of List to a container, taking care about these rules:

  1. Object of List Item which share the same name MUST be placed on the same Container. Of course it is impossible to have a number of Item with the same Name with TotalSize > Max Container Size.

  2. I have to create the less possibile number of Containers

Any advice is greatly appreciated.

Duncan_McCloud
  • 543
  • 9
  • 24
  • 1
    So if you have more items with the same name than the maximal container size, then you should put the overflown items into a new container or just don't care about the overflow ? – Attila Jan 13 '14 at 11:57
  • 2
    you tagged it right: use a knapsack algorithm – chtenb Jan 13 '14 at 11:58
  • @AttilaBujáki I can't have the "overflow". The input Data will prevent it. – Duncan_McCloud Jan 13 '14 at 12:01
  • 1
    Least possible number of containers? Easy - create one container that has a container size of `MyListItemList.Count` and put everything in it. Both your conditions are then satisfied. I'm guessing though this isn't the complete problem and that actually you are given a set of containers with a fixed size to begin with ... – Mashton Jan 13 '14 at 12:04
  • @Mashton A container have a Max Value for ContainerSize Property – Duncan_McCloud Jan 13 '14 at 12:06
  • I recommend you to read the book "Introduction to Algorithms", 3rd edition of Thomas H. Corner & Others, page 465. It addresses you problem in depth, is better to understand and solve, than having someone doing it for you. – João Pinho Jan 13 '14 at 12:19

1 Answers1

1

Split the items by Container ID and use a knapsack algorithm to solve the separate problem instances.

http://en.wikipedia.org/wiki/Knapsack_problem

chtenb
  • 14,924
  • 14
  • 78
  • 116