0% found this document useful (0 votes)
14 views14 pages

Prolog Suduko Solver

The document discusses the implementation of a Sudoku solver using Prolog, a logic programming language ideal for constraint satisfaction problems. It outlines the basics of Sudoku, the advantages of using Prolog for solving such puzzles, and provides a sample code demonstrating the solver's functionality. The conclusion emphasizes Prolog's effectiveness in handling Sudoku through logical reasoning and backtracking.

Uploaded by

227r1a66d8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views14 pages

Prolog Suduko Solver

The document discusses the implementation of a Sudoku solver using Prolog, a logic programming language ideal for constraint satisfaction problems. It outlines the basics of Sudoku, the advantages of using Prolog for solving such puzzles, and provides a sample code demonstrating the solver's functionality. The conclusion emphasizes Prolog's effectiveness in handling Sudoku through logical reasoning and backtracking.

Uploaded by

227r1a66d8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Prolog Suduko Solver

N.ADITHYA (247R5A6623)
V.HARSHITH SAGAR (247R5A6624)
B.VAMSHI (247R5A6625)
N.SNEHA (247R5A6626)
Introduction to Prolog Sudoku Solver

Prolog is a logic programming language well-


suited for solving constraint satisfaction
problems like Sudoku.

A Sudoku puzzle consists of a 9x9 grid that


needs to be filled with digits from 1 to 9.

The challenge is to ensure that each digit


appears only once in each row, column, and 3x3
subgrid.
Basics of Sudoku

Sudoku requires the player to fill in the empty


cells of the grid.

Each number must be unique across its row,


column, and 3x3 box.

The game is typically presented with some cells


pre-filled as clues to guide the solution.
Why Use Prolog for Sudoku?

Prolog's declarative nature allows for


straightforward expression of constraints.

The inherent backtracking mechanism in Prolog


is ideal for exploring possible solutions.

Logic programming facilitates a clear and


concise representation of the Sudoku rules.
Prolog Basics

Prolog is based on first-order logic and uses


facts and rules to represent knowledge.

Variables in Prolog can be used to denote


unknown values that the program needs to solve
for.

The language uses a resolution-based inference


mechanism to derive conclusions from the
knowledge base.
Representing the Sudoku Grid

The Sudoku grid can be represented as a list of


lists in Prolog.

Each inner list corresponds to a row in the


Sudoku grid.

Empty cells can be represented as variables,


allowing Prolog to assign values dynamically.
Defining Constraints

Constraints ensure that no number repeats in


rows, columns, or boxes.

Prolog can use predicates to express these


constraints logically.

The use of the 'all_different' constraint is crucial


for validating each row, column, and box.
Backtracking Algorithm

The backtracking algorithm attempts to fill the


grid recursively by trying different values.

If a conflict arises, Prolog backtracks to the


previous step and tries a different value.

This process continues until a solution is found


or all possibilities are exhausted.
Advantages of Prolog Sudoku Solver

The Prolog Sudoku solver can quickly find


solutions due to its efficient search capabilities.

The code tends to be compact and easy to read,


making it accessible for beginners.

Prolog can easily handle varying difficulties of


Sudoku puzzles with the same logic.
Example of a Prolog Sudoku Solver

A simple Prolog program can demonstrate


solving a Sudoku puzzle with minimal code.

The program defines the grid, constraints, and


the backtracking search in a few predicates.

Here is a basic example of how the


implementation looks in Prolog syntax.
CODE
% A Sudoku puzzle is represented as a list of 9 lists, each containing 9 numbers.
% Each number can be a variable (for unknown cells) or a fixed number (for known cells).
% Sudoku solver
solve(Sudoku) :-
% Flatten the Sudoku board into a list of 81 elements
flatten(Sudoku, Vars),
% Ensure all variables are between 1 and 9
Vars ins 1..9,
% Apply constraints to the rows, columns, and subgrids
valid_rows(Sudoku),
valid_columns(Sudoku),
valid_subgrids(Sudoku),
% Label the variables (this tries all possible values for the variables)
label(Vars).
% Constraint for valid rows (no repetition)
valid_rows([]).
valid_rows([Row|Rest]) :-
all_different(Row),
valid_rows(Rest).
% Constraint for valid columns (no repetition)
valid_columns([Col1, Col2, Col3, Col4, Col5, Col6, Col7, Col8, Col9]) :-
all_different(Col1),
all_different(Col2),
all_different(Col3),
all_different(Col4),
all_different(Col5),
all_different(Col6),
all_different(Col7),
all_different(Col8), Input:
all_different(Col9).
% Constraint for valid subgrids (no repetition)
valid_subgrids(Sudoku) :- test_sudoku([
subgrid(Sudoku, 1, Subgrid1), [5, 3, 0, 0, 7, 0, 0, 0, 0],
subgrid(Sudoku, 2, Subgrid2),
subgrid(Sudoku, 3, Subgrid3), [6, 0, 0, 1, 9, 5, 0, 0, 0],
subgrid(Sudoku, 4, Subgrid4), [0, 9, 8, 0, 0, 0, 0, 6, 0],
subgrid(Sudoku, 5, Subgrid5),
subgrid(Sudoku, 6, Subgrid6), [8, 0, 0, 0, 6, 0, 0, 0, 3],
subgrid(Sudoku, 7, Subgrid7), [4, 0, 0, 8, 0, 3, 0, 0, 1],
subgrid(Sudoku, 8, Subgrid8),
subgrid(Sudoku, 9, Subgrid9), [7, 0, 0, 0, 2, 0, 0, 0, 6],
all_different(Subgrid1), [0, 6, 0, 0, 0, 0, 2, 8, 0],
all_different(Subgrid2),
all_different(Subgrid3), [0, 0, 0, 4, 1, 9, 0, 0, 5],
all_different(Subgrid4), [0, 0, 0, 0, 8, 0, 0, 7, 9]
all_different(Subgrid5),
all_different(Subgrid6), ]).
all_different(Subgrid7),
all_different(Subgrid8),
all_different(Subgrid9).
% Helper function to extract each subgrid from the Sudoku
subgrid(Sudoku, N, Subgrid) :-
N1 is (N - 1) // 3 * 3, % Calculate the starting row of the subgrid
N2 is (N - 1) mod 3 * 3, % Calculate the starting column of the subgrid
Output:
subgrid_helper(Sudoku, N1, N2, Subgrid).
% Helper function to collect elements from the Sudoku grid for a subgrid
subgrid_helper([], _, _, []).
Sudoku = [
subgrid_helper([Row|Rows], N1, N2, Subgrid) :- [5, 3, 4, 6, 7, 8, 9, 1, 2],
nth0(N1, Row, A),
nth0(N2, Row, B),
[6, 7, 2, 1, 9, 5, 3, 4, 8],
nth0(N3, Row, C), [1, 9, 8, 3, 4, 2, 5, 6, 7],
append([A,B,C], Rest, Subgrid),
subgrid_helper(Rows, N1+1, N2, Rest).
[8, 5, 9, 7, 6, 1, 4, 2, 3],
% Test puzzle (replace the zeros with your puzzle grid) [4, 2, 6, 8, 5, 3, 7, 9, 1],
test_sudoku([
[5, 3, 0, 0, 7, 0, 0, 0, 0],
[7, 1, 3, 9, 2, 4, 8, 5, 6],
[6, 0, 0, 1, 9, 5, 0, 0, 0], [9, 6, 1, 5, 3, 7, 2, 8, 4],
[0, 9, 8, 0, 0, 0, 0, 6, 0],
[8, 0, 0, 0, 6, 0, 0, 0, 3],
[2, 8, 7, 4, 1, 9, 6, 3, 5],
[4, 0, 0, 8, 0, 3, 0, 0, 1], [3, 4, 5, 2, 8, 6, 1, 7, 9]
[7, 0, 0, 0, 2, 0, 0, 0, 6],
[0, 6, 0, 0, 0, 0, 2, 8, 0],
].
[0, 0, 0, 4, 1, 9, 0, 0, 5],
[0, 0, 0, 0, 8, 0, 0, 7, 9]
]).
% Query example
?- solve(Sudoku), print(Sudoku).
Conclusion

Prolog provides a powerful framework for


solving Sudoku puzzles using logical reasoning.

Understanding Prolog can enhance one's ability


to tackle similar constraint satisfaction
problems.

The combination of logic programming and


backtracking makes Prolog an excellent choice
for Sudoku solvers.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy