EXP3
EXP3
Theory:
Using the A* search algorithm to solve the Sudoku problem is an interesting approach. A* is
an informed searching technique that combines elements of both breadth-first search and
best-first search. It uses a heuristic function to estimate the cost to reach the goal from a
given state and maintains a priority queue to explore nodes in a way that minimizes this
estimated cost. Let's break down how A* can be applied to solve Sudoku and analyse its
properties.
Sudoku Problem: Sudoku is a number-placement puzzle where the objective is to fill a 9x9
grid with digits so that each column, each row, and each of the nine 3x3 subgrids (called
"regions") contain all of the digits from 1 to 9. The puzzle starts with some cells already
filled, and the goal is to fill in the remaining cells while adhering to the Sudoku rules.
3. Goal State: The goal state is a completely filled Sudoku grid that adheres to the Sudoku
rules.
4. Operators: The operators in Sudoku are choosing a row, column, and digit to place in a
cell that is currently empty while ensuring it doesn't violate Sudoku rules.
5. Heuristic Function (h): A* requires a heuristic function that estimates the cost from the
current state to the goal. In Sudoku, a common heuristic is to count the number of empty
cells in the current state. This heuristic is admissible because it never overestimates the
true cost to the goal, as filling in an empty cell will reduce the count.
6. Cost Function (g): The cost function measures the actual cost from the initial state to the
current state. In Sudoku, the cost can be the number of steps (moves) taken to reach the
current state.
Dept of Computer Engineering., Sardar Patel Institute of Technology, Andheri (West) Page 1
7. A* Search: A* maintains a priority queue of states to explore. At each step, it selects the
state with the lowest f(n) value, where f(n) = g(n) + h(n). It expands the selected state by
applying valid operators and adds resulting states to the queue.
Code:
import heapq
import time
total_steps = 0
for i in range(4):
if board[row][i] == num or board[i][col] == num:
return False
return True
def heuristic(board):
def solve_sudoku(board):
global total_steps
intermediate_states = []
Dept of Computer Engineering., Sardar Patel Institute of Technology, Andheri (West) Page 2
while priority_queue:
estimated_cost, current_board = heapq.heappop(priority_queue)
empty_cells = [(i, j) for i in range(4) for j in range(4) if
current_board[i][j] == 0]
if not empty_cells:
intermediate_states.append(new_board)
def get_user_input():
sudoku_board = []
print("Enter the Sudoku puzzle (4x4 grid):")
for i in range(4):
row = input(f"Enter values for row {i + 1} separated by spaces:
").split()
if len(row) != 4:
print("Invalid input. Please enter 4 values for each row.")
return None
row = [int(cell) if cell.isdigit() else 0 for cell in row]
sudoku_board.append(row)
return sudoku_board
user_input_board = get_user_input()
if user_input_board:
Dept of Computer Engineering., Sardar Patel Institute of Technology, Andheri (West) Page 3
start_time = time.time()
solution, intermediate_states = solve_sudoku(user_input_board)
end_time = time.time()
if solution:
print("Solution Found:")
for row in solution:
print(row)
else:
print("No solution found.")
print("\nIntermediate States:")
for i, state in enumerate(intermediate_states):
print(f"Step {i + 1}:")
for row in state:
print(row)
print()
Output:
Dept of Computer Engineering., Sardar Patel Institute of Technology, Andheri (West) Page 4
Dept of Computer Engineering., Sardar Patel Institute of Technology, Andheri (West) Page 5
Dept of Computer Engineering., Sardar Patel Institute of Technology, Andheri (West) Page 6