-
Notifications
You must be signed in to change notification settings - Fork 15
/
AC3.py
81 lines (57 loc) · 1.48 KB
/
AC3.py
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
# IMPLEMENTATION OF AC3 ALGORITHM
import queue
from CSP import *
#THE MAIN AC-3 ALGORITHM
def AC3(csp):
q = queue.Queue()
for arc in csp.constraints:
q.put(arc)
i = 0
while not q.empty():
(Xi, Xj) = q.get()
i = i + 1
if Revise(csp, Xi, Xj):
if len(csp.values[Xi]) == 0:
return False
for Xk in (csp.peers[Xi] - set(Xj)):
q.put((Xk, Xi))
#display(csp.values)
return True
#WORKING OF THE REVISE ALGORITHM
def Revise(csp, Xi, Xj):
revised = False
values = set(csp.values[Xi])
for x in values:
if not isconsistent(csp, x, Xi, Xj):
csp.values[Xi] = csp.values[Xi].replace(x, '')
revised = True
return revised
#CHECKS IF THE GIVEN ASSIGNMENT IS CONSISTENT
def isconsistent(csp, x, Xi, Xj):
for y in csp.values[Xj]:
if Xj in csp.peers[Xi] and y!=x:
return True
return False
#DISPLAYS THE SUDOKU IN THE GRID FORMAT
def display(values):
for r in rows:
if r in 'DG':
print ("------------------------------------------------------------------------------")
for c in cols:
if c in '47':
print (' | ', values[r+c], ' ',end=' ')
else:
print (values[r+c], ' ',end=' ')
print (end='\n')
#CHECKS IF THE SUDOKU IS COMPLETE OR NOT
def isComplete(csp):
for variable in squares:
if len(csp.values[variable])>1:
return False
return True
#WRITES THE SOLVED SUDOKU IN THE FORM OF A STRING
def write(values):
output = ""
for variable in squares:
output = output + values[variable]
return output