Assume I have a list of names, with length L
, sorted in alphabetical order. I want to split this list into N
groups with these requirements:
The requirements are in order of importance.
Is there a straightforward algorithm to accomplish this?
Assume the following list of 30 names which we want to split over 3 groups/rows:
Abel Alex Amir Aria Axel Cali Enzo Evan Ezra Finn Hank Ivan Jade Jake Joel June Kane Lara Leon Levi Liam Luca Milo Nico Noah Remy Rory Sage Sean Theo
A naive implementation would be to iterate over the list and try to split as close to positions 10 and 20 as possible:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
---|---|---|---|---|---|---|---|---|---|---|---|
Abel | Alex | Amir | Aria | Axel | Cali | Enzo | Evan | Ezra | Finn | ||
Hank | Ivan | Jade | Jake | Joel | June | Kane | Lara | Leon | Levi | Liam | Luca |
Milo | Nico | Noah | Remy | Rory | Sage | Sean | Theo |
Assume I have a list of names, with length L
, sorted in alphabetical order. I want to split this list into N
groups with these requirements:
The requirements are in order of importance.
Is there a straightforward algorithm to accomplish this?
Assume the following list of 30 names which we want to split over 3 groups/rows:
Abel Alex Amir Aria Axel Cali Enzo Evan Ezra Finn Hank Ivan Jade Jake Joel June Kane Lara Leon Levi Liam Luca Milo Nico Noah Remy Rory Sage Sean Theo
A naive implementation would be to iterate over the list and try to split as close to positions 10 and 20 as possible:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
---|---|---|---|---|---|---|---|---|---|---|---|
Abel | Alex | Amir | Aria | Axel | Cali | Enzo | Evan | Ezra | Finn | ||
Hank | Ivan | Jade | Jake | Joel | June | Kane | Lara | Leon | Levi | Liam | Luca |
Milo | Nico | Noah | Remy | Rory | Sage | Sean | Theo |
The first group is easy, as we can make our first split after 10 names. However, for our second split we want to keep all five names starting with 'L' in the same group. Since we have 2 names with the initial 'L' after position 20 and 3 names before, it makes sense to make our second split after 'Luca', before 'Milo'. Giving the above result. Splitting before 'Lara' would have resulted in a less even distribution.
But by moving 'Hank' from the second group to the first, we get an even more even distribution, proving that our naive algorithm is not optimal:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
---|---|---|---|---|---|---|---|---|---|---|
Abel | Alex | Amir | Aria | Axel | Cali | Enzo | Evan | Ezra | Finn | Hank |
Ivan | Jade | Jake | Joel | June | Kane | Lara | Leon | Levi | Liam | Luca |
Milo | Nico | Noah | Remy | Rory | Sage | Sean | Theo |
There will of course be situations where it is impossible to fulfill all requirements. For example if all names start with the same letter and N>1
. In this situation the requirement regarding evenness is impossible to achieve since the first group will be of length L
and the other groups will have length 0
.
I've been told the evenness requirement is unclear. I am not sure how to define evenness, but want a visually pleasing presentation of the grouped list of names. Please suggest improvements to the question if you can.
Some ideas:
The criteria is unclear. But here is a solution to a closely related problem.
Write the following straightforward function that runs in time O(n)
.
def allocate_groups (sizes, max_group_size):
...
Now do a binary search for the smallest possible max_group_size
that fits within N
groups. Your starting bounds are max(sizes)
to sum(sizes)
. This takes a fairly small number of passes.
This minimizes the max size. Note that if L
was large enough to make a big group, this would divide the other groups unevenly. If you want to improve that, you can take out the largest group, and repeat with the remaining sizes for N-1
groups, and the idea of a "barrier". Recurse. Then put your chosen groups together in the right order. This will force a very even distribution, even if there is a large group.
You can introduce a barrier by changing your initial naive function with:
def assign_groups(grouping_sizes, max_group_size):
...
Where grouping_sizes
is a list of lists of sizes. Note that this new function can be written in terms of the old allocate_groups
- one cluster of groups is allocated per list of sizes.