fork download
  1. from operator import itemgetter
  2. from pprint import pprint
  3.  
  4. def find_maximum_set(sets):
  5. """
  6. Tries to find the largest combined set, formed from the given sets, such
  7. that all used sets are distinct.
  8.  
  9. Returns (size, indexes, union) where indexes are the index of all the sets
  10. used.
  11.  
  12. Note: This is an approximation, and will not always return the absolute
  13. maximum, but should be very close in most cases.
  14. """
  15. results = []
  16. for si,s in enumerate(sets):
  17. sn = len(s)
  18. new_set = set(s) # if nothing else works, add the set by itself
  19. new_len = sn
  20. new_is = [si]
  21.  
  22. # try to combine it with all the previous results, picking the one that
  23. # would produce the largest union
  24. for rn,ris,r in results:
  25. if r.isdisjoint(s):
  26. rs = r.union(s)
  27. if rn+sn > new_len:
  28. new_set = rs
  29. new_len = rn+sn
  30. new_is = ris + [si]
  31.  
  32. # add the new set to the result collection
  33. results.append((new_len,new_is,new_set))
  34.  
  35. # return the largest result
  36. return max(results, key=itemgetter(0))
  37.  
  38. def find_all_maximum_sets(sets):
  39. """
  40. Merges sets such that the size of the largest sets are maximized.
  41.  
  42. >>> find_all_maximum_sets([[1,5,7], [2,6], [3,4,5], [1,2] , [4], [1]])
  43. [set([1, 2, 4, 5, 6, 7]), set([1, 2, 3, 4, 5]), set([1])]
  44. """
  45. sets = list(sets)
  46. result = []
  47. while len(sets) > 0:
  48. _, indexes, largest = find_maximum_set(sets)
  49. result.append(largest)
  50. sets = [s for i,s in enumerate(sets) if i not in indexes]
  51. return result
  52.  
  53. pprint(find_all_maximum_sets([[1,5,7], [2,6], [3,4,5], [1,2] , [4], [1]]))
Success #stdin #stdout 0.08s 8832KB
stdin
Standard input is empty
stdout
[set([1, 2, 4, 5, 6, 7]), set([1, 2, 3, 4, 5]), set([1])]