Sudoku solver by graph coloring theory

In summary: You only have to program in the adjacency matrix once. It shouldn't take more than a half-hour. It would take longer than that to devise an algorithm to construct the matrix.
  • #1
BRN
108
10
Hello everybody!

I have to implement a sudoku solver in C ++ taking advantage by graph coloring theory, where each number to insert is a color of the associated graph node. In particular I would like to use the Welsh-Powell algorithm.

I find myself in trouble starting with this project and I ask you some advice.

I should start from the adjacency matrix associated to the graph of a sudoku 9x9. To build it by hand is a really boring job. Do you know some fastest technique to do this?

Once the graph is modeled, then, how could I manage the numbers (i.e. colors) already attributed to the graph nodes? The W-P algorithm must not color these nodes, because they are already, but must consider their presence...

Do you have any idea to give me?

Thanks so much!
 
Technology news on Phys.org
  • #2
So the application solves a sudoku puzzle. It doesn't generate it right?

First, I would start by searching around for examples of how others have done it.

The reason being is that your question is rather like asking how to build a car from scratch.
 
  • Like
Likes BRN
  • #3
BRN said:
I should start from the adjacency matrix associated to the graph of a sudoku 9x9. To build it by hand is a really boring job. Do you know some fastest technique to do this?

Once the graph is modeled, then, how could I manage the numbers (i.e. colors) already attributed to the graph nodes? The W-P algorithm must not color these nodes, because they are already, but must consider their presence...
You only have to program in the adjacency matrix once. It shouldn't take more than a half-hour. It would take longer than that to devise an algorithm to construct the matrix.

When you have code for the W-P algorithm, it might be possible to describe how to initialize the problem, but until then, it is difficult.
 
  • Like
Likes BRN and jedishrfu
  • #4
FactChecker said:
You only have to program in the adjacency matrix once. It shouldn't take more than a half-hour. It would take longer than that to devise an algorithm to construct the matrix.
I'm not so sure, that's an ## 81^2 ## matrix. Plenty of scope for human error.
BRN said:
Do you know some fastest technique to do this?
The obvious strategy is to add all row adjacencies, then all column adjacencies, then all square adjacencies. Each of these steps is a simple nest of iterators with carefully selected bounds and step size.
 
  • Like
Likes BRN and FactChecker
  • #5
pbuk said:
I'm not so sure, that's an ## 81^2 ## matrix. Plenty of scope for human error.
Oh! I stand corrected. I made a Sudoku solver and stored the arrangement in a completely different manner. I confused it with another puzzle solver.
Yes, you should make an algorithm to generate the matrix.
 
  • Like
Likes BRN and pbuk
  • #6
An easy way to generate new sudoku puzzles is to start with a known solution and apply sudoku allowed row col exchanges and remap the numbers.
 
  • Like
Likes BRN
  • #8
Hi everyone and thank you for your answers.

I made steps forward and now I can generate the adjacency matrix in this way:

Adjacency Matrix:
matrix Graph::makeAdjMatrix(int nNodes, int matOrder, int blkOrder)
{
    matrix adjacencyMatrix(nNodes);
    for (int i = 0; i < nNodes; i++)
    {
            adjacencyMatrix[i].resize(nNodes);
    }

    // Map nodes links in rows and columns
    for (unsigned int i = 0; i < nNodes; i++)
    {
        for(unsigned int j = 0; j < nNodes; j++)
        {
            if(j >= (i/matOrder)*matOrder && j < (i/matOrder)*matOrder + matOrder)
                adjacencyMatrix[i][j] = 1.;
            else
            if(j%matOrder == i%matOrder)
                adjacencyMatrix[i][j] = 1.;
            else
                adjacencyMatrix[i][j] = 0;

        }
    }

    // Map nodes links in every single block
    for (unsigned int i = 0; i < nNodes; i++)
    {
        for(unsigned int r = 0; r < blkOrder; r++)
        {
            for(unsigned int k = 0; k < blkOrder; k++)
            {
                if((i/matOrder)%blkOrder == 0 )
                    for(unsigned int j = 0; j < blkOrder; j ++)
                    {
                        adjacencyMatrix[i + matOrder * r][(i/blkOrder)*blkOrder + j + matOrder * k] = 1.;
                    }
            }
        }
    }

    return adjacencyMatrix;
}

However, now, I have a problem in loading a matrix ...
This my code:

Load Matrix:
matrix InputData::readMatrix(const std::string& nameFile)
{
    std::ifstream inFile;
    inFile.open(nameFile);
    int row, column;
    inFile >> row >> column;

    matrix inputMatrix(row);

    if (row == column)
    {
        for (int i = 0; i < row; ++i)
        {
            for (int j = 0; j < column; ++j)
            {
                inFile >> inputMatrix[i][j];
            }
        }
    }
    else
        std::cout << "Invalid dimension! Only square matrix allowed." << std::endl;

    inFile.close();
    return inputMatrix;
}

where with "matrix" I defined

Definition Matrix:
typedef std::vector<std::vector<int>> matrix;

This code don't work. What's wrong?
 
  • #9
Ok, maybe it's better if I open a new thread for this issue. Just to not create confusion in this.
 

1. What is graph coloring theory?

Graph coloring theory is a branch of mathematics that deals with coloring the vertices of a graph in such a way that no two adjacent vertices have the same color. This theory is often used in solving puzzles, such as Sudoku, where each number must appear only once in each row, column, and designated region.

2. How does graph coloring theory help in solving Sudoku?

Sudoku puzzles can be represented as a graph, with each cell being a vertex and each row, column, and region being a set of interconnected vertices. By using graph coloring theory, we can assign colors (or numbers) to each vertex in such a way that no two adjacent vertices have the same color, thus solving the Sudoku puzzle.

3. Can graph coloring theory be used for all types of Sudoku puzzles?

Yes, graph coloring theory can be used for all types of Sudoku puzzles, including traditional 9x9 puzzles, 4x4 puzzles, and even larger grid sizes. As long as the puzzle can be represented as a graph, graph coloring theory can be applied to solve it.

4. Is graph coloring theory the only method for solving Sudoku puzzles?

No, there are other methods for solving Sudoku puzzles, such as logical deduction and trial and error. However, graph coloring theory is a popular approach among mathematicians and scientists due to its mathematical basis and efficiency in solving puzzles.

5. Are there any limitations to using graph coloring theory in Sudoku puzzles?

Graph coloring theory may not be the most efficient method for solving Sudoku puzzles with multiple solutions. In such cases, other methods may be more suitable. Additionally, not all Sudoku puzzles may be solvable using graph coloring theory, as some may require more advanced techniques or may not have a solution at all.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Replies
1
Views
796
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
936
  • Programming and Computer Science
Replies
3
Views
5K
  • Atomic and Condensed Matter
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Precalculus Mathematics Homework Help
Replies
3
Views
2K
Back
Top