Advertisement
Guest User

Probabilistic Polynomial Solution for Exact Three Cover

a guest
Jun 13th, 2020
1,520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. from itertools import groupby
  2. import random
  3. from itertools import combinations
  4. from itertools import permutations
  5.  
  6. s = 1,2,3,4,5,6,7,8,9
  7. c = [[1,2,3],[4,5,7],[4,5,6],[1,2,8],[7,8,9],[1,2,4],[1,2,5],[1,2,7],[1,4,5]]
  8.  
  9. # I am creating a value
  10. # so that I can break in
  11. # an upcoming while loop
  12.  
  13. xx = s * 2
  14. combinations_count = 0
  15. for i in combinations(xx, 3):
  16.     combinations_count = combinations_count + 1
  17.    
  18.  
  19. if len(s) % 3 != 0:
  20.     print('invalid input')
  21.     quit()
  22.  
  23. ss = s
  24.  
  25. # Sort list to remove
  26. # sets like (1,2,3) and (1,3,2)
  27. # but leave (1,2,3)
  28.  
  29. delete = []
  30. for a in range(0, len(c)):
  31.     for i in permutations(c[a], 3):
  32.         if list(i) in c[a:]:
  33.             if list(i) != c[a]:
  34.                 delete.append(list(i))
  35.  
  36. for a in range(0, len(delete)):
  37.     if delete[a] in c:
  38.         del c[c.index(delete[a])]
  39.  
  40.  
  41. # remove sets
  42. # that have
  43. # repeating
  44. # elements
  45.  
  46. remove = []
  47. for rem in range(0, len(c)):
  48.     if len(c[rem]) != len(set(c[rem])):
  49.         remove.append(c[rem])
  50.  
  51. for rem_two in range(0, len(remove)):
  52.     if remove[rem_two] in c:
  53.         del c[c.index(remove[rem_two])]
  54.  
  55. # remove sets
  56. # that have
  57. # elements
  58. # that don't
  59. # exist in S.
  60.  
  61. remove=[]
  62. for j in range(0, len(c)):
  63.    for jj in range(0, len(c[j])):
  64.         if any(elem not in s for elem in c[j]):
  65.             remove.append(c[j])
  66.  
  67. for rem_two in range(0, len(remove)):
  68.     if remove[rem_two] in c:
  69.         del c[c.index(remove[rem_two])]
  70.  
  71.  
  72. # Remove repeating sets
  73.  
  74. solutions =[c[x] for x in range(len(c)) if not(c[x] in c[:x])]
  75.  
  76.  
  77. c = solutions
  78.  
  79.  
  80. length = len(solutions)
  81. ss = s
  82.  
  83. # while loop to see
  84. # if we can find
  85. # an Exact Three Cover
  86. # in poly-time.
  87.  
  88. stop = 0
  89. w = 0
  90. new = []
  91.  
  92. # Shuffle these
  93. # annoying
  94. # counter-examples
  95. # that might ruin
  96. # my day
  97.  
  98.  
  99. print('Initalizing', combinations_count,'shuffles')
  100. for a in range(0, combinations_count):
  101.     random.shuffle(c)
  102.  
  103.  
  104. stop = 0
  105.  
  106. while True:
  107.  
  108.     # Shuffling c randomly
  109.     # to reduce chance of
  110.     # error.
  111.    
  112.     random.shuffle(c)
  113.    
  114.  
  115.     if len(new) == len(ss) // 3:
  116.         # break if Exact
  117.         # three cover is
  118.         # found.
  119.         #print(new)
  120.         w = w + 1
  121.         print(new, 'yes')
  122.         print('took',stop,'steps in while loop')
  123.         break
  124.        
  125.        
  126.  
  127.     # Keep c[0]
  128.     # and append to
  129.     # end of list
  130.     # del c[0]
  131.     # to push >>
  132.     # in list.
  133.    
  134.     c.append(c[0])
  135.     del [c[0]]
  136.     new = []
  137.     s = set()
  138.     stop = stop + 1
  139.     for l in c:
  140.         if not any(v in s for v in l):
  141.             new.append(l)
  142.             s.update(l)
  143.            
  144.  
  145.     if len(new) != len(ss)//3:
  146.         if stop == length:
  147.           # Reverse list just
  148.           # to reduce false -/+
  149.           # answer.
  150.          for cc in range(0, len(c)):
  151.               c = list(reversed(c))
  152.               random.shuffle(c)
  153.     if stop >= combinations_count:
  154.         print('no')
  155.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement