Advertisement
Guest User

Max Three Cover

a guest
Jan 5th, 2021
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. import json
  2. from copy import deepcopy
  3. import random
  4.  
  5. # integers only and list of lists only
  6.  
  7. s =  input("Input list of integers (no repeating elements) for S with commas (eg. 1,2,3,4...) : ").split(',')
  8. c =  input('enter list for C (eg. [[1,2,3],[4,5,6]]): ')
  9. c = json.loads(c)
  10. s = [int(a) for a in s]
  11. c_copy = deepcopy(c)
  12.  
  13.  
  14.  
  15. random.seed()
  16. miss = []
  17. delete = []
  18. remove = []
  19. remove_t=[]
  20. def input_validation():
  21.     # checking for missing elements.
  22.     for a in c:
  23.         for b in a:
  24.             miss.append(b)
  25.     for d in s:
  26.         if d not in miss:
  27.             print('False', d)
  28.             quit()
  29.     # Throw out unwanted orderings of sub-lists
  30.     for a in range(0, len(c)):
  31.         c[a] = sorted(c[a])
  32.     # Lists are treated as sets, so lists
  33.     # with repeating elements is thrown out
  34.     for r in range(0, len(c)):
  35.         if len(c[r]) != len(set(c[r])):
  36.             c[r] = [['xx']]
  37.     # Throw out lists that have elements that do
  38.     # not exist in s.
  39.     # Throw out lists with more than 3
  40.     # Throw out lists less than 3
  41.     # O(n^2*m) ??
  42.     index = -1
  43.     while index != len(c)-1:
  44.         index = index + 1
  45.         if len(c[index]) != 3:
  46.             del c[index]
  47.             index = index-1
  48.         for a in c[index]:
  49.             if a not in s:
  50.                 del c[index]
  51.                 index = index-1
  52.                 break
  53.        
  54.  
  55. s_copy = deepcopy(s)
  56. input_validation()
  57. # remove repeating lists
  58. c = [c[x] for x in range(len(c)) if not(c[x] in c[:x])]
  59. def bijective_mapp():
  60.     s_copy = deepcopy(s)
  61.     for a in range(0, len(s)):
  62.         s[a] = a+1
  63.     for b in range(0, len(c)):
  64.         for bb in range(0, len(c[b])):
  65.             c[b][bb] = s_copy.index(c[b][bb])+1
  66. bijective_mapp()
  67. c = [sorted(y) for y in c]
  68. def shuff(c, n):
  69.     for i in range(n-1,0,-1):
  70.         j = random.randint(0,i)
  71.         c[i],c[j] = c[j],c[i]
  72. c.append(sorted(c[len(c)-1]))
  73. n = len(s)
  74. steps = n*(2**1)*((n*(2**1))-1)*((n*(2**1))-2)//6
  75. reset = 0
  76. c_elem = set()
  77. set_cover = []
  78. partial = []
  79. for a in range(0, steps + 1):
  80.     reset = reset + 1
  81.     if reset > len(c):
  82.         c_elem = set()
  83.         reset = 0
  84.         shuff(c, len(c))
  85.         c = sorted(c, key=lambda parameter: parameter[0])        
  86.         set_cover = []
  87.     c.append(c[0])
  88.     del(c[0])
  89.     for l in c:
  90.         if not any(v in c_elem for v in l):
  91.             set_cover.append(l)
  92.             c_elem.update(l)
  93.     # if exact solution not found,
  94.     # then use partial solution
  95.     if set_cover not in partial:
  96.         partial.append(set_cover)
  97. list_of_partials = [len(r) for r in partial]
  98. k = partial[list_of_partials.index(max(list_of_partials))]
  99. # Reversing the Bijective mapping
  100. # to the original input.
  101. for a in range(0, len(k)):
  102.     for b in range(0, len(k[a])):
  103.         l = s.index(k[a][b])
  104.         k[a][b] = s_copy[l]
  105. for a in c_copy:
  106.     if sorted(a) in k:
  107.         l = k.index(sorted(a))
  108.         k[l] = a
  109. print(k)
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement