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

Invert Method #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Invert Method #2

wants to merge 3 commits into from

Conversation

zsiegel92
Copy link

@zsiegel92 zsiegel92 commented Jul 12, 2017

Hey there,

I added an "Invert" method that takes a subset of {1,...,n} as a list, and returns the index of that subset amongst the k-subsets of {1,...,n} in lexicographic order. That is the order in which your generateSubset method generates subsets, so this function is indeed an inverse.

I also changed print statements to be compatible with Python 3.

@zsiegel92
Copy link
Author

Hey, I've added an algebraically-generated inversion function. You can include the following as a method of the EnumeratedSubset class.

	def algebraicInvert(self,n,l):
		k=len(l)
		l.sort()
		return  self.choose(n,k) - self.choose(n-l[0],k) + sum([self.choose(n-l[i-1]-1,k-i) - self.choose(n-l[i],k-i) for i in range(1,k)]

This takes an integer n and a subset of {1,...,n}, given as Python list l. It returns the lexicographic index of the subset l.

That is,

a= EnumeratedSubsets()
l = a.generateSubsets(n,k,i)
print(a.algebraicInvert(n,l)) #prints i

prints i.

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

Successfully merging this pull request may close these issues.

1 participant