Algorithm or expression to put n elements in k sets

Click For Summary
SUMMARY

The discussion focuses on partitioning 17 elements into 3 sets, specifically with 2 sets containing 6 elements each and 1 set containing 5 elements. A naive approach suggests distributing elements evenly by assigning approximately n/k elements per set, adjusting for non-integer results by rounding. The conversation also introduces the concept of using a 'distance' function to group similar items, indicating that this could be a clustering problem. A recommended algorithm for implementation is mentioned, along with a reference for further clarity.

PREREQUISITES
  • Understanding of basic algorithms and data structures
  • Familiarity with partitioning concepts in combinatorics
  • Knowledge of clustering algorithms and distance functions
  • Basic programming skills for algorithm implementation
NEXT STEPS
  • Research the "K-Means Clustering Algorithm" for grouping similar items
  • Explore "Greedy Algorithms" for efficient partitioning strategies
  • Learn about "Dynamic Programming" techniques for optimal set partitioning
  • Investigate "Distance Metrics" such as Euclidean and Manhattan distances
USEFUL FOR

This discussion is beneficial for computer scientists, algorithm developers, and data analysts interested in partitioning techniques and clustering methodologies.

xeon123
Messages
90
Reaction score
0
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
 
Physics news on Phys.org
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.
 
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.
 
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:

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
906
Replies
3
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K