Advertisement
Guest User

Approximation for 3-Cover (no overlap)

a guest
Feb 20th, 2021
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.61 KB | None | 0 0
  1. import json
  2. from copy import deepcopy
  3. import random
  4. import quantumrandom
  5.  
  6. # Seeding the PRNG with True Randomness
  7. # from a Quantum Random Number Generator
  8. seed = quantumrandom.randint(1, 100)
  9. random.seed(int(seed))
  10. # integers only and list of lists only
  11. s =  input("Input list of integers (no repeating elements) for S with commas (eg. 1,2,3,4...) : ").split(',')
  12. c =  input('enter list for C (eg. [[1,2,3],[4,5,6]]): ')
  13. c = json.loads(c)
  14. s = [int(a) for a in s]
  15.  
  16. miss = []
  17. delete = []
  18. def input_validation():
  19.     # checking for missing elements.
  20.     for a in c:
  21.         for b in a:
  22.             miss.append(b)
  23.     for d in s:
  24.         if d not in miss:
  25.             print('False', d)
  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.     for a in range(0, len(c)):
  39.       for b in c[a]:
  40.         if c[a].count(b) > 1:
  41.           delete.append(c[a])
  42.           break
  43.         if len(c[a]) != 3:
  44.           delete.append(c[a])
  45.       for bb in c[a]:
  46.         if bb not in s:
  47.           delete.append(c[a])
  48.     for b in delete:
  49.       if b in c:
  50.         del c[c.index(b)]
  51. s_copy = deepcopy(s)
  52. c_copy = deepcopy(c)
  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. # shuffle the groups
  64. # of sorted c[m][0]'s
  65. # without losing the
  66. # sort
  67. def shuffle_subgroups():
  68.     sublist = []
  69.     new_c = []
  70.     count = 0
  71.     X = []
  72.     while count != max(cc):
  73.         count = count + 1
  74.         for a in range(0, len(cc)):
  75.             if cc[a] == count:
  76.                 sublist.append(c[a])
  77.             if a == len(cc)-1:
  78.                 new_c.append(list(sublist))
  79.                 sublist = []
  80.     random.shuffle(new_c)
  81.     for a in new_c:
  82.         for b in a:
  83.             X.append(b)
  84.     return X
  85. n = len(s)
  86. steps = n*(2**1)*((n*(2**1))-1)*((n*(2**1))-2)//6
  87. reset = 0
  88. c_elem = set()
  89. set_cover = []
  90. partial = []
  91. bookmark = 0
  92. for a in range(0, steps + 1):
  93.     reset = reset + 1
  94.     if reset > len(c):
  95.         random.shuffle(c)
  96.         c_elem = set()
  97.         reset = 0
  98.         c = sorted(c, key=lambda parameter: parameter[0])
  99.         if bookmark == 0:
  100.             cc = [a[0] for a in c]
  101.             bookmark = 1
  102.         c = shuffle_subgroups()
  103.         set_cover = []
  104.     c.append(c[0])
  105.     del(c[0])
  106.     for l in c:
  107.         if not any(v in c_elem for v in l):
  108.             set_cover.append(l)
  109.             c_elem.update(l)
  110.     # if exact solution not found,
  111.     # then use partial solution
  112.     if set_cover not in partial:
  113.         partial.append(set_cover)
  114. list_of_partials = [len(r) for r in partial]
  115. k = partial[list_of_partials.index(max(list_of_partials))]
  116. # Reversing the Bijective mapping
  117. # to the original input.
  118. for a in range(0, len(k)):
  119.     for b in range(0, len(k[a])):
  120.         l = s.index(k[a][b])
  121.         k[a][b] = s_copy[l]
  122. for a in range(0, len(k)):
  123.   k[a] = sorted(k[a])
  124. for a in c_copy:
  125.   if sorted(a) in k:
  126.     l = k.index(sorted(a))
  127.     k[l] = a
  128. print("{:.0%}".format(len(k)/(len(s)//3)),'of the Universe is covered without overlap.',k)
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement