Advertisement
Guest User

Max-3-cover (len(s)//3-1)

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