Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate OPTIONAL/UNION permutations more efficiently #1

Open
patrickkwang opened this issue May 20, 2020 · 2 comments
Open

Generate OPTIONAL/UNION permutations more efficiently #1

patrickkwang opened this issue May 20, 2020 · 2 comments

Comments

@patrickkwang
Copy link
Contributor

patrickkwang commented May 20, 2020

Instead of nesting UNIONs (2^n total), it may be possible to flatten them and use a smaller set of permutations.


We have a set of n things. We need to generate a set of permutations such that each unique subset of length m<=n occurs as a prefix in at least one of these permutations. We'd like the set of permutations to be as small as possible.

There is just one solution for n=2: f_2(a, b) = [ab, ba]

The current scheme just uses this and recurses for n>2. So for example, f_3(a, b, c) becomes f_2(f_2(a, b), c) = [abc, bac, cab, cba].

However, n=3 could be solved more efficiently by, for example: [abc, bca, cab].

For n=4, the current scheme generates 8 sets, but we can make do with 6, for example: [abcd, bcda, cdab, dabc, ac**, bd**]

The size of these sets is (weakly?) lower-bounded by max(n choose m | m<=n). That bound is achievable for n<=4 (at least), as shown in the examples above.

We need an algorithm for generating efficient sets, for arbitrary n. If that doesn't pan out, we can just tabulate solutions for a range of low n and enforce an upper bound, e.g. n<=4.

@cbizon
Copy link
Contributor

cbizon commented May 20, 2020

I'll think a bit more, but I suspect that come nested cyclic permutations will work. so for the 4 case you cycle
abcd
bcda
cdab
dabc

But to get some extra combos you need to now cycle the last 3 for the first two sequences, once each.

So for longer sequences there's probably a level of nesting for each additional length, and some number of sequences that you have to apply the nesting to?

@patrickkwang
Copy link
Contributor Author

We might could make this a bit more complicated in practice because we can share computation between permutations with shared prefixes. For example, we only have to compute a once for abcd and acbd. I think this does not impact the way we generate these permutations until at least n=6, and maybe never... But it may be something to revisit later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants