-
Tidying up my Rough Drafts
09/02/2020 at 18:58 • 0 commentsimport random from itertools import permutations import json # |S| creation 52 s = [] for a in range(1, 52): s.append(a) # Contains only One Solution c = [[4, 5, 39], [10, 11, 17], [4, 5, 44], [1, 2, 27], [1, 2, 41], [10, 11, 31], [4, 5, 51], [10, 11, 40], [1, 2, 37], [10, 11, 29], [7, 8, 24], [4, 5, 7], [4, 5, 35], [7, 8, 51], [1, 2, 44], [7, 8, 38], [7, 8, 40], [1, 2, 17], [7, 8, 9], [7, 8, 17], [4, 5, 11], [7, 8, 31], [1, 2, 3], [4, 5, 36], [10, 11, 23], [4, 5, 31], [10, 11, 51], [10, 11, 36], [1, 2, 48], [7, 8, 42], [16, 17, 18], [10, 11, 21], [1, 2, 7], [7, 8, 48], [10, 11, 42], [7, 8, 21], [7, 8, 10], [4, 5, 24], [34, 35, 36], [1, 2, 25], [1, 2, 46], [7, 8, 26], [1, 2, 50], [37, 38, 39], [1, 2, 21], [46, 47, 48], [4, 5, 13], [1, 2, 39], [1, 2, 19], [7, 8, 35], [4, 5, 8], [7, 8, 47], [1, 2, 22], [25, 26, 27], [7, 8, 22], [4, 5, 20], [4, 5, 9], [4, 5, 22], [10, 11, 46], [4, 5, 23], [7, 8, 41], [10, 11, 38], [1, 2, 31], [7, 8, 12], [1, 2, 49], [10, 11, 48], [10, 11, 45], [10, 11, 18], [4, 5, 16], [4, 5, 18], [28, 29, 30], [4, 5, 33], [1, 2, 20], [1, 2, 24], [7, 8, 32], [4, 5, 27], [7, 8, 36], [4, 5, 28], [4, 5, 29], [7, 8, 18], [1, 2, 42], [1, 2, 32], [1, 2, 9], [1, 2, 23], [1, 2, 14], [1, 2, 36], [4, 5, 32], [4, 5, 45], [7, 8, 20], [7, 8, 34], [4, 5, 37], [1, 2, 38], [31, 32, 33], [7, 8, 43], [4, 5, 21], [10, 11, 44], [1, 2, 6], [7, 8, 25], [4, 5, 50], [10, 11, 22], [7, 8, 50], [10, 11, 27], [7, 8, 28], [10, 11, 26], [1, 2, 47], [4, 5, 38], [4, 5, 30], [1, 2, 12], [10, 11, 50], [10, 11, 13], [10, 11, 19], [1, 2, 33], [4, 5, 19], [4, 5, 46], [7, 8, 46], [1, 2, 30], [7, 8, 27], [10, 11, 34], [10, 11, 39], [1, 2, 26], [4, 5, 17], [4, 5, 41], [4, 5, 10], [10, 11, 49], [10, 11, 32], [1, 2, 43], [19, 20, 21], [10, 11, 15], [7, 8, 15], [1, 2, 35], [1, 2, 8], [7, 8, 37], [7, 8, 44], [7, 8, 13], [22, 23, 24], [10, 11, 47], [10, 11, 24], [7, 8, 30], [10, 11, 12], [10, 11, 35], [7, 8, 49], [4, 5, 48], [1, 2, 4], [1, 2, 28], [10, 11, 28], [1, 2, 13], [1, 2, 16], [10, 11, 20], [7, 8, 14], [1, 2, 10], [40, 41, 42], [1, 2, 51], [1, 2, 34], [10, 11, 16], [7, 8, 11], [10, 11, 33], [7, 8, 23], [7, 8, 45], [43, 44, 45], [1, 2, 45], [1, 2, 18], [4, 5, 25], [4, 5, 47], [13, 14, 15], [4, 5, 14], [4, 5, 40], [4, 5, 34], [7, 8, 33], [1, 2, 15], [10, 11, 43], [4, 5, 49], [1, 2, 5], [10, 11, 37], [7, 8, 19], [49, 50, 51], [1, 2, 11], [4, 5, 43], [7, 8, 16], [4, 5, 6], [1, 2, 29], [4, 5, 26], [7, 8, 39], [4, 5, 12], [7, 8, 29], [1, 2, 40], [4, 5, 15], [4, 5, 42], [10, 11, 25], [10, 11, 14], [10, 11, 30], [10, 11, 41]] #c =[[1,2,3],[1,2,4],[1,2,5],[1,2,6],[3,4,5],[3,4,6],[3,4,7],[4,5,6],[4,5,7],[6,3,7],[7,10,11],[22,1,11],[22,27,29],[30,1,14],[30,13,14],[14,20,11],[25,27,29],[1,2,45],[14,1,2],[14,2,3],[14,5,1],[26,4,13],[26,7,17],[27,29,18],[19,27,13],[3,4,13],[19,2,1],[19,20,9],[18,3,2],[29,26,1],[5,3,11],[13,16,19],[13,20,21],[13,1,9],[17,19,23],[1,3,5],[5,3,2],[6,8,1],[7,30,23],[11,19,7],[7,4,28],[7,4,28],[18,13,1],[7,12,15],[15,2,3],[29,1,2],[29,2,3],[29,2,4],[29,5,4],[29,23,22],[3,26,5],[3,26,4],[3,25,3],[10,11,2],[30,1,2],[30,3,4],[30,4,5],[30,8,9],[30,18,19],[30,20,13],[6,22,23],[6,24,15],[18,1,2],[18,2,3],[18,4,5],[18,16,1],[24,26,27],[10, 2, 7], [10, 13, 14], [10, 5, 9], [6, 3, 8], [10, 26, 27], [10, 19, 16], [10, 25, 23], [10, 12, 15], [10, 29, 30], [10, 20, 21],[27,28,30],[28,29,30],[10,2,1],[10,7,8],[10,13,14],[10,11,8],[15,1,2],[15,8,4],[13,14,15],[11,12,15],[22,1,2],[22,21,3],[22,25,23],[10,4,8],[17,2,3],[17,2,4],[17,20,21],[18,2,3],[18,2,4],[18,19,16],[15,4,9],[4,5,1],[4,5,10],[5,7,9],[4,5,9]] # Solve Exact List Cover. # Just unhashtag these to enter inputs manually. #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] n = len(s) while_loop_steps = n*241*((n*241)-1)*((n*241)-2)//6 if len(s) % 3 != 0: print('invalid input') quit() # Sort list to remove # lists like [1,2,3] and [1,3,2] # but leave [1,2,3]. # If I wanted too use Brute force # just needs one. Shortens list # and improves execution time. delete = [] 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])] # remove lists # that have # repeating # elements remove = [] 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])] # remove lists # that have # elements # that don't # exist in S. remove=[] 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.append(c[j]) for rem_two in range(0, len(remove)): if remove[rem_two] in c: del c[c.index(remove[rem_two])] # remove repeating lists c = [c[x] for x in range(len(c)) if not(c[x] in c[:x])] # sort list from smallest to largest new_c = [] for a in range(0, len(c)): new_c.append(sorted(tuple(c[a]))) # sort c into like lists # (eg. [1,2,x], with all [1,2,x]'s sorted_new_c = [] for a in range(0, len(new_c)): for b in new_c[a+1:]: if c[a][0] == b[0]: if c[a][1] == b[1]: sorted_new_c.append(b) sorted_new_c.append(c[a]) # remove repeating lists sorted_new_c = [sorted_new_c[x] for x in range(len(sorted_new_c)) if not(sorted_new_c[x] in sorted_new_c[:x])] # I don't want to forget the other lists surpise_second_while_loop = [] for a in new_c: if a not in sorted_new_c: surpise_second_while_loop.append(a) sorted_new_c.append(a) c = sorted_new_c stop = 0 Potential_solution = [] opps = 0 failed_lists = 0 ss = s #def true_random(): # I am working on using a USB laser # so that I have my own True Random # Number Generator k = 870,782,524,200,987,829,741,339,528,214,217,490,25,805,249,156,263,476,848,873,487,985,741,743,301,1,855,134,957,264,649,274,713,348,738,819,740,95,290,324,255,425,912,69,718,602,759,681,124,673,919,378,249,482,724,989,823,466,155,637,526,883,769,696,51,917,501,748,405,374,226,109,205,946,177,16,899,622,973,773,909,959,673,865,736,730,182,664,225,982,413,397,463,224,644,283,870,782,776,476,256,664,755,718,70,263,38,437,996,7,369,981,72,14,836,525,654,975,48,203,617,773,101,863,992,221,374,567,886,492,743,516,725,911,747,767,908,145,794,440,235,447,733,531,532,649,875,908,318,175,160,31,236,978,395,177,452,663,945,271,654,336,497,430,526,290,824,379,692,65,960,764,68,498,73,33,606,54,363,641,931,171,553,29,60,341,497,358,504,385,35,740,595,556,26,641,86,425,397,908,164,203,783,302,817,746,913,682,468,583,155,311,339,333,625,189,448,239,157,257,189,546,618,295,400,977,605,192,472,394,692,915,913,249,128,820,610,956,33,712,549,436,999,933,933,864,46,866,984,563,76,150,260,512,441,3,534,516,520,885,2,305,965,71,740,57,891,390,148,972,689,719,294,725,757,871,808,941,187,202,274,44,346,104,948,572,19,381,886,676,265,479,941,709,975,603,148,892,816,577,132,605,537,614,138,227,641,188,37,750,173,278,502,323,946,771,848,998,652,865,324,261,732,819,362,910,60,504,273,12,508,760,566,597,991,99,759,526,981,942,294,45,247,321,120,641,759,505,631,396,187,910,944,357,618,800,642,36,245,113,641,333,460,517,411,783,563,501,21,25,546,671,729,73,552,831,274,868,849,986,234,427,467,509,302,294,725,902,451,444,455,23,797,975,590,575,703,909,939,729,972,978,190,133,239,254,416,507,543,704,95,300,76,462,192,14,529,342,230,116,785,393,844,45,692,440,84,231,577,643,107,33,973,187,671,990,79,122,778,627,441,125,193,673,570,18,482,272,746,535,47,873,773,613,356,987,968,924,389,651,622,202,676,854,82,566,535,590,972,876,742,615,646,514,688,618,209,302,292,210,488,387,63,490,558,800,741,930,841,274,371,706,774,611,731,142,793,219,789,94,678,584,545,503,730,587,14,270,303,739,681,47,352,727,158,673,890,621,52,359,437,906,396,785,727,94,939,294,733,411,961,398,633,331,840,380,858,989,683,162,958,20,952,351,749,514,460,462,895,488,205,441,611,396,867,347,141,267,379,151,855,780,77,703,285,159,48,259,71,349,965,289,586,488,331,83,435,679,711,155,997,583,92,130,568,453,857,67,796,729,891,538,748,666,726,800,643,468,25,505,446,34,62,677,97,773,914,676,3,124,578,218,337,628,602,682,52,239,181,912,615,329,870,505,992,367,675,43,115,421,13,983,145,891,925,363,142,208,214,704,445,143,449,323,266,800,860,484,458,570,287,254,606,947,285,36,317,965,769,126,716,973,856,535,481,917,956,743,71,802,427,542,791,362,993,758,770,181,817,502,18,97,586,514,621,687,472,164,184,771,981,5,694,179,581,601,620,578,955,261,589,349,847,729,647,944,923,933,191,847,739,625,884,208,2,875,28,809,264,901,444,267,652,526,225,372,323,821,499,829,757,949,893,186,938,907,65,195,909,412,167,679,808,834,912,258,665,18,748,678,193,634,15,530,677,579,562,565,930,992,396,133,353,382,186,445,881,830,231,624,565,915,556,651,641,638,542,303,758,1000,859,668,452,332,522,992,450,605,908,828,602,422,297,361,598,411,717,142,226,596,96,106,590,746,498,128,490,558,273,975,215,445,396,694,692,901,148,108,312,123,257,965,998,643,961,893,17,555,202,193,111,501,345,847,769,477,924,363,870,891,346,813,816,31,121,659,733,491,631,792,602,74,941,984,350,972,162,572,153,666,953,376,212,732,222,449,296,322,731,146,247,853,902,12,519,944,453,957,595,731,593,635,733,849,658,664,939,386,118,469,671,340,774,332,594,882,426,605,554,778,966,387,505,267,452,52,585,561,645,64,25,771,926,877,715,603,59,552,42,644,423,660,891,664,94,666,775,84,79,550,663,949,602,456,83,231,79,261,455,7,386,349,645,889,256,617,889,555,632,192,954,403,35,972,889,365,861,524,537,88,928,604,128,
Probabilistic Greedy Algorithm for an NP-complete problem
07/11/2020 at 19:59 • 0 commentsimport random from itertools import combinations from itertools import permutations import sympy import json import quantumrandom print('Welcome to EX3C-Probablistic-Algorithim') print('Enter your Exact Three Cover Instance') 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 = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30 #c =[[1,2,3],[1,2,4],[1,2,5],[1,2,6],[3,4,5],[3,4,6],[3,4,7],[4,5,6],[4,5,7],[6,3,7],[7,10,11],[22,1,11],[22,27,29],[30,1,14],[30,13,14],[14,20,11],[25,27,29],[1,2,45],[14,1,2],[14,2,3],[14,5,1],[26,4,13],[26,7,17],[27,29,18],[19,27,13],[3,4,13],[19,2,1],[19,20,9],[18,3,2],[29,26,1],[5,3,11],[13,16,19],[13,20,21],[13,1,9],[17,19,23],[1,3,5],[5,3,2],[6,8,1],[7,30,23],[11,19,7],[7,4,28],[7,4,28],[18,13,1],[7,12,15],[15,2,3],[29,1,2],[29,2,3],[29,2,4],[29,5,4],[29,23,22],[3,26,5],[3,26,4],[3,25,3],[10,11,2],[30,1,2],[30,3,4],[30,4,5],[30,8,9],[30,18,19],[30,20,13],[6,22,23],[6,24,15],[18,1,2],[18,2,3],[18,4,5],[18,16,1],[24,26,27],[10, 2, 7], [10, 13, 14], [10, 5, 9], [6, 3, 8], [10, 26, 27], [10, 19, 16], [10, 25, 23], [10, 12, 15], [10, 29, 30], [10, 20, 21],[27,28,30],[28,29,30],[10,2,1],[10,7,8],[10,13,14],[10,11,8],[15,1,2],[15,8,4],[13,14,15],[11,12,15],[22,1,2],[22,21,3],[22,25,23],[10,4,8],[17,2,3],[17,2,4],[17,20,21],[18,2,3],[18,2,4],[18,19,16],[15,4,9],[4,5,1],[4,5,10],[5,7,9],[4,5,9]] # [[1,2,3],[4,5,6],[4,5,7],[4,5,9],[4,6,9],[4,3,2],[4,7,8],[4,1,5],[1,2,4],[1,8,9],[2,7,8],[9,4,6],[5,1,3]] s = [int(a) for a in s] def check_for_exact_cover(jj): jj_flat = [item for sub in jj for item in sub] jj_set = set(jj_flat) if set(s) == jj_set and len(jj_set) == len(jj_flat): print('yes', jj) quit() # Well if length(c) is small # use brute force with polynomial constant if len(c) <= len(s)//3*2: for jj in combinations(c, len(s)//3): check_for_exact_cover(jj) if len(c) >= len(s)//3*2: for jj in combinations(c[0:len(s)//3*2], len(s)//3): check_for_exact_cover(jj) if len(c) >= len(s)//3*2: X = list(reversed(c)) for jj in combinations(X[0:len(s)//3*2], len(s)//3): check_for_exact_cover(jj) if len(c) <= len(s)//3*2: print('no') quit() # Please note that all items in lists are sorted # from smallest to largest . I did this because # I wanted to experiment with ordering and # how the algorithm selects lists based on # orderings! # Experimenting with # patterns in Primes. count = len(s)//3 while True: count += 1 if sympy.isprime(count): prime = count break n = len(s) # I need a large polynomial constant # to solve large instances of Exact # three cover. Naive method would # take forever. while_loop_steps = n*241*((n*241)-1)*((n*241)-2)//6 # Don't worry about this. #p = (len(s)//3)/while_loop_steps * 100 if len(s) % 3 != 0: print('invalid input') quit() # Sort list to remove # lists like [1,2,3] and [1,3,2] # but leave [1,2,3]. # If I wanted too use Brute force # just needs one. Shortens list # and improves execution time. delete = [] 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])] # remove lists # that have # repeating # elements remove = [] 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])] # remove lists # that have # elements # that don't # exist in S. remove=[] 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.append(c[j]) for rem_two in range(0, len(remove)): if remove[rem_two] in c: del c[c.index(remove[rem_two])] # remove repeating lists c = [c[x] for x in range(len(c)) if not(c[x] in c[:x])] # sort list from smallest to largest new_c = [] for a in range(0, len(c)): new_c.append(sorted(tuple(c[a]))) # sort c into like lists # (eg. [1,2,x], with all [1,2,x]'s sorted_new_c = [] for a in range(0, len(new_c)): for b in new_c[a+1:]: if c[a][0] == b[0]: if c[a][1] == b[1]: sorted_new_c.append(b) sorted_new_c.append(c[a]) # remove repeating lists sorted_new_c = [sorted_new_c[x] for x in range(len(sorted_new_c)) if not(sorted_new_c[x] in sorted_new_c[:x])] # I don't want to forget the other lists surpise_second_while_loop = [] for a in new_c: if a not in sorted_new_c: surpise_second_while_loop.append(a) sorted_new_c.append(a) c = sorted_new_c stop = 0 Potential_solution = [] opps = 0 failed_lists = 0 ss = s # add seed to the PRNG # In the original Algorithim # you are suppose to use # your own QRNG (quantum random number generator) # And then create a for loop that has len(c) steps. # Do not please... add 100 here. This is an # online server. 10,000+ requests is a bad idea!! # SO DO NOT CHANGE THE LOOP VALUES BELOW!! for a in range(0, 20): o = quantumrandom.randint(0, len(c)) random.seed(int(o)) 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(c[len(c)-1]) # use value prime to pre-shuffle list C for a in range(0, prime+1): shuff(c,len(c)) # while loop to see # if we can find # an Exact Three Cover # in poly-time. while stop <= while_loop_steps: # Shuffling c randomly # this seems to help # select a correct list shuff(c,len(c)) opps = opps + 1 stop = stop + 1 if len(Potential_solution) == len(ss)//3: # break if Exact # three cover is # found. print('YES SOLUTION FOUND!',Potential_solution) print('took',stop,'steps in while loop') failed_lists = failed_lists + 1 quit() # opps helps # me see # if I miss a correct # list if opps > len(c): if failed_lists < 1: s = set() opps = 0 sorted_new_c = [] for a in range(0, len(new_c)): for b in new_c[a+1:]: if c[a][0] == b[0]: if c[a][1] == b[1]: sorted_new_c.append(b) sorted_new_c.append(c[a]) sorted_new_c = [sorted_new_c[x] for x in range(len(sorted_new_c)) if not(sorted_new_c[x] in sorted_new_c[:x])] surpise_second_while_loop = [] for a in new_c: if a not in sorted_new_c: surpise_second_while_loop.append(a) sorted_new_c.append(a) c = sorted_new_c # Keep c[0] # and append to # end of list # del c[0] # to push >> # in list. c.append(c[0]) del [c[0]] Potential_solution = [] s = set() for l in c: if not any(v in s for v in l): Potential_solution.append(l) s.update(l) # I just found out # I could filter some # cases so run it a second time stop = 0 Potential_solution = [] opps = 0 failed_lists = 0 c = surpise_second_while_loop while stop <= while_loop_steps: # Shuffling c randomly # this seems to help # select a correct list opps = opps + 1 stop = stop + 1 shuff(c,len(c)) if len(Potential_solution) == len(ss)//3: # break if Exact # three cover is # found. print('YES SOLUTION FOUND!',Potential_solution) print('took',stop,'steps in while loop') failed_lists = failed_lists + 1 quit() # opps helps # me see # if I miss a correct # list if opps > len(c): if failed_lists < 1: s = set() opps = 0 sorted_new_c = [] for a in range(0, len(new_c)): for b in new_c[a+1:]: if c[a][0] == b[0]: if c[a][1] == b[1]: sorted_new_c.append(b) sorted_new_c.append(c[a]) sorted_new_c = [sorted_new_c[x] for x in range(len(sorted_new_c)) if not(sorted_new_c[x] in sorted_new_c[:x])] surpise_second_while_loop = [] for a in new_c: if a not in sorted_new_c: surpise_second_while_loop.append(a) sorted_new_c.append(a) c = sorted_new_c # Keep c[0] # and append to # end of list # del c[0] # to push >> # in list. c.append(c[0]) del [c[0]] Potential_solution = [] s = set() for l in c: if not any(v in s for v in l): Potential_solution.append(l) s.update(l) print('no')
The Algorithm itself
06/19/2020 at 01:14 • 0 commentsfrom itertools import groupby import random from itertools import combinations from itertools import permutations import sympy s = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30 c = [[1,2,3],[1,2,4],[1,2,5],[1,2,6],[3,4,5],[3,4,6],[3,4,7],[4,5,6],[4,5,7],[6,3,7],[7,10,11],[22,1,11],[22,27,29],[30,1,14],[30,13,14],[14,20,11],[25,27,29],[14,1,2],[14,2,3],[14,5,1],[26,4,13],[26,7,17],[27,29,18],[19,27,13],[3,4,13],[19,2,1],[19,20,9],[18,3,2],[29,26,1],[5,3,11],[13,16,19],[13,20,21],[13,1,9],[17,19,23],[1,3,5],[5,3,2],[6,8,1],[7,30,23],[11,19,7],[7,4,28],[7,4,28],[18,13,1],[7,12,15],[15,2,3],[29,1,2],[29,2,3],[29,2,4],[29,5,4],[29,23,22],[3,26,5],[3,26,4],[3,25,3],[10,11,2],[30,1,2],[30,3,4],[30,4,5],[30,8,9],[30,18,19],[30,20,13],[6,22,23],[6,24,15],[18,1,2],[18,2,3],[18,4,5],[18,16,1],[24,26,27],[10, 2, 7], [10, 13, 14], [10, 5, 9], [6, 3, 8], [10, 26, 27], [10, 19, 16], [10, 25, 23], [10, 12, 15], [10, 29, 30], [10, 20, 21],[27,28,30],[28,29,30],[10,2,1],[10,7,8],[10,13,14],[10,11,8],[15,1,2],[15,8,4],[13,14,15],[11,12,15],[22,1,2],[22,21,3],[22,25,23],[10,4,8],[17,2,3],[17,2,4],[17,20,21],[18,2,3],[18,2,4],[18,19,16],[15,4,9],[4,5,1],[4,5,10],[5,7,9],[4,5,9]] # Need a prime # to help spread # 3sets apart. count = len(s)//3 while True: count = count + 1 if sympy.isprime(count) == True: prime = count break # I'm going to need this for # my formula. # Here, I am counting # all possible Three Element # combinations. # But, its 241 times larger # 241 is prime. Theortically, # this should space them out # better. combinations_count = len(s)*241*((len(s)*241)-1)*((len(s)*241)-2)//6 combinations_three = len(s)*1*((len(s)*1)-1)*((len(s)*1)-2)//6 # The formula I need # to use to calculate # odds of finding # a len(s)//3 length # solution. p = (len(s)//3)/combinations_count * 100 if len(s) % 3 != 0: print('invalid input') quit() # Sort list to remove # sets like (1,2,3) and (1,3,2) # but leave (1,2,3) delete = [] 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])] # remove sets # that have # repeating # elements remove = [] 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])] # remove sets # that have # elements # that don't # exist in S. remove=[] 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.append(c[j]) for rem_two in range(0, len(remove)): if remove[rem_two] in c: del c[c.index(remove[rem_two])] # Remove repeating sets solutions =[c[x] for x in range(len(c)) if not(c[x] in c[:x])] # check left and right for solutions if len(c) >= len(s)//3*2: for jj in combinations(c[0:len(s)//3*2], len(s)//3): jj_flat = [item for sub in jj for item in sub] jj_set = set(jj_flat) if set(s) == jj_set and len(jj_set) == len(jj_flat): print('yes', jj) quit() if len(c) >= len(s)//3*2: X = list(reversed(c)) for jj in combinations(X[0:len(s)//3*2], len(s)//3): jj_flat = [item for sub in jj for item in sub] jj_set = set(jj_flat) if set(s) == jj_set and len(jj_set) == len(jj_flat): print('yes', jj) quit() # Well if length(c) is small # use brute force with polynomial constant if len(c) <= len(s)//3*2: for jj in combinations(c, len(s)//3): jj_flat = [item for sub in jj for item in sub] jj_set = set(jj_flat) if set(s) == jj_set and len(jj_set) == len(jj_flat): print('yes', jj) quit() if len(c) <= len(s)//3*2: quit() # will need these Three (what a prime!) # just in case my algorithim # needs to reverse in loop. length = len(solutions) ss = s c = solutions # Shuffle these # annoying # counter-examples # that might ruin # my day # Using prime number to help # spread apart. Its NOT # non-sequitir. Primes # have been observed # in nature to help # avoid conflict. # Cicada mating seasons, etc. # I'm applying the concept # from nature onto my algorithim. # It can't hurt! print('Initalizing', prime,'shuffles') for a in range(0, prime): random.shuffle(c) # while loop to see # if we can find # an Exact Three Cover # in poly-time. stop = 0 Potential_solution = [] while True: if len(Potential_solution) == len(ss) // 3: # break if Exact # three cover is # found. print(Potential_solution, 'YES') print('took',stop,'steps in while loop') break # Keep c[0] # and append to # end of list # del c[0] # to push >> # in list. c.append(c[0]) del [c[0]] Potential_solution = [] s = set() stop = stop + 1 for l in c: if not any(v in s for v in l): Potential_solution.append(l) s.update(l) if len(Potential_solution) != len(ss)//3: if stop == length: # Reverse list just # to reduce false -/+ # answer. for cc in range(0, len(c)): c = list(reversed(c)) if stop >= combinations_three: break # Repeating while loop a # second time. The first # while loop was to address # a bug that would get stuck # in an infinite loop. stop = 0 Potential_solution = [] while stop <= int(100/p*combinations_count): # Shuffling c randomly # to reduce chance of # error. random.shuffle(c) if len(Potential_solution) == len(ss) // 3: # break if Exact # three cover is # found. print(Potential_solution, 'yes') print('took',stop,'steps in while loop') break # Keep c[0] # and append to # end of list # del c[0] # to push >> # in list. c.append(c[0]) del [c[0]] Potential_solution = [] s = set() stop = stop + 1 for l in c: if not any(v in s for v in l): Potential_solution.append(l) s.update(l) if len(Potential_solution) != len(ss)//3: if stop == length: # Reverse list just # to reduce false -/+ # answer. for cc in range(0, len(c)): c = list(reversed(c)) random.shuffle(c)