Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Guy solution in Clear category for Break Rings by hutch31
import itertools
def break_rings(rings):
def reduce(r):
rv = set()
for r in rings:
rv = set.union(rv, r)
return rv
ring_list = sorted(reduce(rings))
highest_ring = ring_list[-1]
def break_ring(links, ring):
rv = []
for l in links:
if ring not in l:
rv.append(l)
return rv
broken = 1
for broken in range(1,highest_ring-1):
for break_list in itertools.combinations(ring_list, broken):
c = rings
for b in break_list:
c = break_ring(c, b)
# if no rings are left connected, we have found a minimal set
if len(c) == 0:
return broken
# shouldn't get here
return highest_ring-1
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
assert break_rings(({1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {4, 6})) == 3, "example"
assert break_rings(({1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4})) == 3, "All to all"
assert break_rings(({5, 6}, {4, 5}, {3, 4}, {3, 2}, {2, 1}, {1, 6})) == 3, "Chain"
assert break_rings(({8, 9}, {1, 9}, {1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {8, 7})) == 5, "Long chain"
June 17, 2015