Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sympy
- import random
- from itertools import permutations
- # Only input lists with 3-elements.
- # Lists should be treated as sets.
- s = [1,2,3,4,5,6]
- c = [[1,4,3],[4,6,5],[1,3,2]]
- miss = []
- delete = []
- remove = []
- remove_t=[]
- no = 0
- def input_validation():
- # checking for missing elements.
- no = 0
- for a in range(0, len(c)):
- for b in range(0, len(c[a])):
- miss.append(c[a][b])
- for d in s:
- if d not in miss:
- print('no', d)
- no = 1
- break
- if no == 1:
- quit()
- # Throw out unwanted orderings of sub-lists
- for a in range(0, len(c)):
- for i in permutations(c[a], 3):
- if list(i) in c[a:]:
- if list(i) != c[a]:
- delete.append(list(i))
- for a in range(0, len(delete)):
- if delete[a] in c:
- del c[c.index(delete[a])]
- # Lists are treated as sets, so lists
- # with repeating elements is thrown out
- for rem in range(0, len(c)):
- if len(c[rem]) != len(set(c[rem])):
- remove.append(c[rem])
- for rem_two in range(0, len(remove)):
- if remove[rem_two] in c:
- del c[c.index(remove[rem_two])]
- # Throw out lists that have elements that do
- # not exist in s.
- for j in range(0, len(c)):
- for jj in range(0, len(c[j])):
- if any(elem not in s for elem in c[j]):
- remove_t.append(c[j])
- for rem_two in range(0, len(remove_t)):
- if remove_t[rem_two] in c:
- del c[c.index(remove_t[rem_two])]
- input_validation()
- # This reduction aims at reducing the running time
- # within the length of the input.
- # This is a bijective mapping of values in both C & S to their
- # index-location values.
- def reduction_t():
- s_copy = s.copy()
- for a in range(0, len(s)):
- s[a] = a
- for b in range(0, len(c)):
- for bb in range(0, len(c[b])):
- c[b][bb] = s_copy.index(c[b][bb])
- # Use primes to help spread values apart.
- # So that one value shouldn't replace another.
- # Causing a false positive.
- # (eg. C = [3], [3,4] == 10, and so
- # does S = 1,2,3,4. But 1 & 2 are missing.)
- def reduction_r():
- s_copy = s.copy()
- prime = 1
- for a in range(0, len(s)):
- while True:
- prime = prime + 1
- if sympy.isprime(prime) == True:
- if prime > a+1:
- if prime not in s:
- m = random.randint(2, len(c))
- if prime*m not in s:
- s[a] = prime*m
- break
- for b in range(0, len(c)):
- for bb in range(0, len(c[b])):
- c[b][bb] = s[c[b][bb]]
- reduction_t()
- reduction_r()
- r = []
- for a in range(0, len(c)):
- r.append(sum(c[a]))
- # Remove lists with repeating values
- r = [r[x] for x in range(len(r)) if not(r[x] in r[:x])]
- # k is our target-sum for SubsetSum function
- k = sum(s)
- # Courtesy to google search.
- # I did not write this code
- # below.
- def isSubsetSum(set, n, sum):
- # The value of subset[i][j] will be
- # true if there is a
- # subset of set[0..j-1] with sum equal to i
- subset =([[False for i in range(sum + 1)]
- for i in range(n + 1)])
- # If sum is 0, then answer is true
- for i in range(n + 1):
- subset[i][0] = True
- # If sum is not 0 and set is empty,
- # then answer is false
- for i in range(1, sum + 1):
- subset[0][i]= False
- # Fill the subset table in botton up manner
- for i in range(1, n + 1):
- for j in range(1, sum + 1):
- if j<set[i-1]:
- subset[i][j] = subset[i-1][j]
- if j>= set[i-1]:
- subset[i][j] = (subset[i-1][j] or
- subset[i - 1][j-set[i-1]])
- # uncomment this code to print table
- # for i in range(n + 1):
- # for j in range(sum + 1):
- # print (subset[i][j], end =" ")
- # print()
- return subset[n][sum]
- # Driver code
- if __name__=='__main__':
- set = r
- sum = k
- n = len(set)
- if (isSubsetSum(set, n, sum) == True):
- print("There should be an Exact Cover with sets of 3.")
- else:
- print("No")
- # This code is contributed by
- # sahil shelangia.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement