Advertisement
Guest User

Max-Three-Cover Approximation

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