Created
June 4, 2024 13:10
-
-
Save ssghost/72cc432afbce31a294d62b0e9865b4e3 to your computer and use it in GitHub Desktop.
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
from itertools import combinations | |
from typing import List, Set | |
def brute_set_cover(universe: Set[int], setlist: List[Set[int]]) -> int, List[Set[int]]: | |
n = len(setlist) | |
res = [] | |
for i in range(1, n+1): | |
for subset in combinations(setlist, i): | |
if set.union(*subset) == universe: | |
res.append(subset) | |
return len(res), res | |
def greedy_set_cover(universe: Set[int], setlist: List[Set[int]]) -> int, List[Set[int]]: | |
uncovered = universe.copy() | |
res = [] | |
while uncovered: | |
subset = max(setlist, key = lambda s: len(s & uncovered)) | |
res.append(subset) | |
uncovered -= subset | |
return len(res), res | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment