Algorithm or expression to put n elements in k sets

  • Thread starter xeon123
  • Start date
I have 17 elements, and I want to put them in 3 sets. This makes 2 sets with 6 elements, and 1 set with 5 elements. Now I want to find an algorithm to partition n elements in k sets.

Can anyone give me an algorithm, or a math expression for me to implement in a algorithm?

Thanks
 
32,683
8,556
There are many ways to put n elements in k sets. Do you want the set sizes to be similar to each other?
A naive approach would be n/k elements per set. This is not always an integer, but you can fix this by rounding in an appropriate way.
 

rcgldr

Homework Helper
8,576
472
You could add 1 element at a time to each set, cycling through all the sets as you add elements. If the number of elements isn't an multiple of the number of sets, you just run out of elements before cycling through all the sets on the last cycle.
 
695
2
Is there a 'distance' function, a way to quantify how close or how similar two items are (so that you place similar items in the same set)? (Examples would be assigning customers to their closest service center, or grouping objects of similar colors.)

If this sounds like the case, it may be a clustering problem; one possible algorithm, not difficult to implement, could be this one. (Or, for a clearer explanation that Wikipedia, see here.)
 
Last edited:

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving

Hot Threads

Top