• Tidying up my Rough Drafts

    Tea B09/02/2020 at 18:58 0 comments

    import 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')
       ...
    Read more »

  • Probabilistic Greedy Algorithm for an NP-complete problem

    Tea B07/11/2020 at 19:59 0 comments

    import 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...
    Read more »

  • The Algorithm itself

    Tea B06/19/2020 at 01:14 0 comments

    from 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...
    Read more »