Ask Your Question
1

Removing certain entries from a list of combinations

asked 0 years ago

sgmth gravatar image

updated 0 years ago

Starting with an additive group Zn (group of integers modulo n) to find all combinations of m elements, removing all translates of a combination. By a translate T+i of combination T, I mean the combinations whose elements can be expressed as a+i for each a in T , where i is a fixed non-zero in Zn .

For example if we run this

n=int(input("Enter the order of the group Zn : "))
G=Zmod(n)
m=int(input("Enter the order of the  Combination required: "))

CMB= Combinations(G,m) 
show(CMB.list())

Then for n=5 and m=3 we get the following output
𝙲𝙼𝙱=[[0,1,2],[0,1,3],[0,1,4],[0,2,3],[0,2,4],[0,3,4],[1,2,3],[1,2,4],[1,3,4],[2,3,4]]

For example, here the translates of [0,1,2] are [1,2,3], [2,3,4],[0,3,4],[0,1,4] for i =,1,2,3,4, respectively

I want to retain only [0,1,2] and remove all its translates from the output.

Preview: (hide)

1 Answer

Sort by Β» oldest newest most voted
2

answered 0 years ago

Max Alekseyev gravatar image

updated 0 years ago

UPDATED 2025-01-24

What you ask for corresponds to (periodic) Lyndon words composed of the differences between consecutive elements (summing to n). Check this out:

n=4
m=3
G=Zmod(n)

import itertools
CMB = [ tuple(itertools.accumulate(map(G,list(w)*d))) for d in divisors(gcd(m,n)) for w in LyndonWords(n,m//d) if sum(w)*d==n ]
print( CMB )

Another possible solution:

def is_canonical(c):
    if c[0]: return False
    w = Word(c[i]-c[i-1] for i in range(len(c)))
    p = w.periods(True)
    if p: w = w[:p[0]]
    return w.is_lyndon()

CMB = list( filter(is_canonical, Combinations(G,m)) )
print( CMB )
Preview: (hide)
link

Comments

Thanks a lot Sir. Yes , they both work perfectly fine. Thanks again.

sgmth gravatar imagesgmth ( 0 years ago )

Thanks again Sir, for looking into the problem again. I'll go through once again and get back to confirm by testing with some cases

sgmth gravatar imagesgmth ( 0 years ago )

Sir, though I have not checked thoroughly with the outputs, but one error is still there. For n=8 and m=4, the first code has ten outputs and the second one has eleven. So, at least one of them has an error.

sgmth gravatar imagesgmth ( 0 years ago )

This was an issue in the second code. It should additionally required that c[0] is zero. Fixed now.

Max Alekseyev gravatar imageMax Alekseyev ( 0 years ago )

Yes Sir, thanks. Now its working fine.

sgmth gravatar imagesgmth ( 0 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 0 years ago

Seen: 236 times

Last updated: Jan 24