Advertisement
Guest User

Max 3-cover

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