Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- from copy import deepcopy
- import random
- # integers only and list of lists only
- s = input("Input list of integers (no repeating elements) for S with commas (eg. 1,2,3,4...) : ").split(',')
- c = input('enter list for C (eg. [[1,2,3],[4,5,6]]): ')
- c = json.loads(c)
- s = [int(a) for a in s]
- random.seed()
- miss = []
- delete = []
- remove = []
- remove_t=[]
- def input_validation():
- # checking for missing elements.
- for a in c:
- for b in a:
- miss.append(b)
- for d in s:
- if d not in miss:
- print('False', d)
- quit()
- # Throw out unwanted orderings of sub-lists
- for a in range(0, len(c)):
- c[a] = sorted(c[a])
- # Lists are treated as sets, so lists
- # with repeating elements is thrown out
- for r in range(0, len(c)):
- if len(c[r]) != len(set(c[r])):
- c[r] = [['xx']]
- # Throw out lists that have elements that do
- # not exist in s.
- # Throw out lists with more than 3
- # Throw out lists less than 3
- # O(n^2*m) ??
- index = -1
- while index != len(c)-1:
- index = index + 1
- if len(c[index]) != 3:
- del c[index]
- index = index-1
- for a in c[index]:
- if a not in s:
- del c[index]
- index = index-1
- break
- s_copy = deepcopy(s)
- input_validation()
- # remove repeating lists
- c = [c[x] for x in range(len(c)) if not(c[x] in c[:x])]
- c_copy = deepcopy(c)
- one_s = []
- # See if there are sets
- # with one element.
- # and check them for a
- # cover.
- for a in c:
- if len(a) == 1:
- if a not in one_s:
- one_s.append(a)
- if len(one_s) == len(s):
- print(one_s)
- quit()
- def bijective_mapp():
- s_copy = deepcopy(s)
- for a in range(0, len(s)):
- s[a] = a+1
- for b in range(0, len(c)):
- for bb in range(0, len(c[b])):
- c[b][bb] = s_copy.index(c[b][bb])+1
- bijective_mapp()
- c = [sorted(y) for y in c]
- def shuff(c, n):
- for i in range(n-1,0,-1):
- j = random.randint(0,i)
- c[i],c[j] = c[j],c[i]
- c.append(sorted(c[len(c)-1]))
- n = len(s)
- steps = n*(2**1)*((n*(2**1))-1)*((n*(2**1))-2)//6
- reset = 0
- c_elem = set()
- set_cover = []
- partial = []
- for a in range(0, steps + 1):
- reset = reset + 1
- if reset > len(c):
- c_elem = set()
- reset = 0
- shuff(c, len(c))
- c = sorted(c, key=lambda parameter: parameter[0])
- set_cover = []
- c.append(c[0])
- del(c[0])
- for l in c:
- if not any(v in c_elem for v in l):
- set_cover.append(l)
- c_elem.update(l)
- # if exact solution not found,
- # then use partial solution
- if set_cover not in partial:
- partial.append(set_cover)
- list_of_partials = [len(r) for r in partial]
- k = partial[list_of_partials.index(max(list_of_partials))]
- # Reversing the Bijective mapping
- # to the original input. But, its sorted.
- for a in range(0, len(k)):
- for b in range(0, len(k[a])):
- l = s.index(k[a][b])
- k[a][b] = s_copy[l]
- print(k)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement