r/askmath•
aretumer

An even sequence

Hey there.

I have a set of data i have to bring to an as evenly as possible sequence.

Example

A = 4 times

B = 2 times

C =1 time

So, ACABABA ACABABA etc. (This is not a political commentary!)

​

Problem is, i have a lot of data. I don't want to to it by hand. If i know the name of the mathematical and/or statistical concept of what i want i might find an excel solution online. As of right now i haven't found the right phrasing for google to find what i am looking for.

Thank you!

Posted Jun 22, 2020, 11:44 AM UTC

23
TheBB•

Sounds like a combinatorial optimization problem. You want to find a permutation that minimizes some quantity. You just need to formulate what that quantity is. "Evenness" doesn't quite do it. For example, why is 'ABACABA' not a solution? Or is it?

1
aretumerOP•

It is. The problem is the amount of data.

A 32

B 24

C 20

D 16

E 16

F 16

G 16

H 12

I 12

J 12

K 8

L 8

M 8

N 8

O 8

​

That is too much for me to sort by hand.

1
palordrolap•

Seems like you're effectively trying to put each letter X in positions 0/Xcount*total, 1/Xcount*total, 2/Xcount*total, ... up to (Xcount-1)/Xcount*total, rounding up each time.

Obviously this is often going to result in two or more different letters trying to get into the same space. At that point you have to decide how you want to rearrange things. Stealing the next unused place might work, for example.

The stealing of the next space over is often used in naive hashing functions, but here, the naivety keeps things close to where they need to be.

This is also kind of related to the Thue-Morse sequence which deals with arranging an infinite number of binary 1s and 0s in the most balanced way.

Here though, you have multiple different symbols, not just 0 and 1, and you also only have a finite number of them.

Anyway, working with your simple set you'd be trying to put A into positions 0/4 * 7, 1/4 * 7, 2/4 * 7 and 3/4 * 7 which round up to 0, 2, 4 and 6, which is exactly where you have them.

B would be trying to get into 0 and 4 and C would be trying to get into 0.

The best strategy would appear to be to fill in the most common first, because then the Bs are forced into 1 and 5 and C is forced into 3.

This gives ABACABA which is so balanced it's a palindrome.

There may be better ways of handling this, such as treating the middle of the sequence as zero and then alternating out from the middle, which would be more likely, I think, to give palindromes.

Which then makes me think that binary trees and rebalancing of them may also be a relevant related subject, but the concept is kind of stood on its head here because you're looking for the "most unsorted" version of the data rather than the most sorted.

1