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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| class Path: def __init__(self, graph): self.graph = graph self.vertex_list = [] self.neighbor_dict = {'t_patt': {}, 'f_patt': {}} self.path_list = [] def find(self, depth=2): print(f"Initializing graph theory analysis with search depth {depth}...") self.initialize_vertex() self.initialize_neighbor() self.solve(depth) return self.graph def initialize_vertex(self): self.vertex_list = [] vertex_count = 0 for i in range(9): for j in range(9): if type(self.graph[i][j]) == list: for num in self.graph[i][j]: self.vertex_list.append(''.join(str(n) for n in [i,j,num])) vertex_count += 1 print(f" Created {vertex_count} vertices representing all candidate possibilities.") def pattern_node_list(self, idx_str, patt): ls = [] i = int(idx_str[0]) j = int(idx_str[1]) m = int(idx_str[2]) lst = [] for n in self.graph[i][j]: if n != m: lst.append([i,j,n]) if patt: if len(lst) == 1: ls += lst else: ls += lst lst = [] for k in range(9): if k != j: if type(self.graph[i][k]) == list: if m in self.graph[i][k]: lst.append([i,k,m]) if patt: if len(lst) == 1: ls += lst else: ls += lst lst = [] for k in range(9): if k != i: if type(self.graph[k][j]) == list: if m in self.graph[k][j]: lst.append([k,j,m]) if patt: if len(lst) == 1: ls += lst else: ls += lst lst = [] for k in range(9): for l in range(9): if k // 3 == i // 3 and l // 3 == j // 3: if k != i or l != j: if type(self.graph[k][l]) == list: if m in self.graph[k][l]: lst.append([k,l,m]) if patt: if len(lst) == 1: if lst[0] not in ls: ls += lst else: for item in lst: if item not in ls: ls.append(item) ls_str = [] for item in ls: idx_str_gen = ''.join(str(n) for n in item) ls_str.append(idx_str_gen) return ls_str def initialize_neighbor(self): self.neighbor_dict = {'t_patt': {}, 'f_patt': {}} for idx_str in self.vertex_list: self.neighbor_dict['t_patt'][idx_str] = self.pattern_node_list(idx_str, True) self.neighbor_dict['f_patt'][idx_str] = self.pattern_node_list(idx_str, False) print(f" Built neighbor relationships for {len(self.vertex_list)} vertices (true/false patterns).") def search_cycle(self, orig, idx_str, patt, depth=2): if patt: neighbor_ls = self.neighbor_dict['t_patt'][idx_str] else: neighbor_ls = self.neighbor_dict['f_patt'][idx_str] if depth % 2 == 0: if orig in neighbor_ls: self.path_list.append(orig) return True if depth == 0: return False for neighbor in neighbor_ls: if self.search_cycle(orig, neighbor, not patt, depth-1): self.path_list.append(neighbor) return True def solve(self, depth=2): print(f"Searching for logical paths and contradictions...") paths_found = 0 confirmations = 0 eliminations = 0 for idx_str in self.vertex_list: path_str = '\n 🔍 Analyzing path for candidate %s at row %d, column %d:' % (idx_str[2], int(idx_str[0])+1, int(idx_str[1])+1) if self.search_cycle(idx_str, idx_str, True, depth): paths_found += 1 confirmations += 1 for item in list(reversed(self.path_list)): path_str += ' → %s[%d,%d]' % (item[2], int(item[0])+1, int(item[1])+1) print(path_str) print(' ✓ CONFIRMED: Candidate %s at row %d, column %d (logical path found)' %(idx_str[2], int(idx_str[0])+1,int(idx_str[1])+1)) self.path_list = [] self.graph[int(idx_str[0])][int(idx_str[1])] = [int(idx_str[2])] elif self.search_cycle(idx_str, idx_str, False, depth): paths_found += 1 eliminations += 1 for item in list(reversed(self.path_list)): path_str += ' → %s[%d,%d]' % (item[2], int(item[0])+1, int(item[1])+1) print(path_str) print(' ✗ ELIMINATED: Candidate %s at row %d, column %d (contradiction found)' %(idx_str[2], int(idx_str[0])+1,int(idx_str[1])+1)) self.path_list = [] self.graph[int(idx_str[0])][int(idx_str[1])].remove(int(idx_str[2])) if paths_found == 0: print(" No logical paths found at current depth. Try increasing depth or use constraint propagation.") else: print(f"Path analysis completed: {confirmations} confirmations, {eliminations} eliminations.")
|