Skip to content

Instantly share code, notes, and snippets.

@ssghost
Created June 4, 2024 13:10
Show Gist options
  • Save ssghost/72cc432afbce31a294d62b0e9865b4e3 to your computer and use it in GitHub Desktop.
Save ssghost/72cc432afbce31a294d62b0e9865b4e3 to your computer and use it in GitHub Desktop.
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