⚠️ 换一张图,比值正好顶到 2
换一张图:三条互不相连的边 [(0, 1), (2, 3), (4, 5)]。贪心和最优各要几个点?
from itertools import combinations
E2 = [(0, 1), (2, 3), (4, 5)]
V2 = [0, 1, 2, 3, 4, 5]
def greedy_vc(edges):
cover, used = [], set()
for a, b in edges:
if a not in used and b not in used:
cover += [a, b]
used |= {a, b}
return sorted(cover)
def opt_vc(edges, vs):
for k in range(len(vs) + 1):
for c in combinations(vs, k):
s = set(c)
if all(a in s or b in s for a, b in edges):
return sorted(s)
return sorted(vs)
print(str(len(greedy_vc(E2))) + "/" + str(len(opt_vc(E2, V2))))
全部评论