Created
November 10, 2020 16:05
-
-
Save sharmaeklavya2/f85b638da52e20631056ed548d2b9ba2 to your computer and use it in GitHub Desktop.
Check if a set family is a matroid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import ast | |
def check_matroid(indsets): | |
if () not in indsets: | |
return () | |
# check hereditary | |
for S in indsets: | |
if S: | |
for x in S: | |
L = list(S) | |
L.remove(x) | |
if tuple(L) not in indsets: | |
return (S, x) | |
# check augmentation | |
for S_1 in indsets: | |
for S_2 in indsets: | |
if len(S_1) < len(S_2): | |
valid = False | |
for x in set(S_2) - set(S_1): | |
S_3 = set(S_1) | |
S_3.add(x) | |
if tuple(sorted(S_3)) in indsets: | |
valid = True | |
if not valid: | |
return (S_1, S_2) | |
return True | |
if __name__ == '__main__': | |
indsets = set() | |
for S in ast.literal_eval(input()): | |
indsets.add(tuple(sorted(set(S)))) | |
print(check_matroid(indsets)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment