Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Algorithm or expression to put n elements in k sets

  1. Apr 24, 2013 #1
    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?

  2. jcsd
  3. Apr 24, 2013 #2


    User Avatar
    2017 Award

    Staff: Mentor

    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.
  4. Apr 24, 2013 #3


    User Avatar
    Homework Helper

    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.
  5. Apr 24, 2013 #4
    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: Apr 24, 2013
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook