Lecture 2: Using functions to model problems

Notation

Sudoku

As an example, we use functions to carefully describe the solution to a sudoku puzzle. We are not describing a process to find the solution, only the properties that a solution must have.

We start by making up some notation: let \(N = \{1, 2, \dots, 9\}\) be the set of numbers. Let \(C = N \times N = \{(1,1), (1,2), \dots, (1,9), (2,1), \dots, (9,9)\}\) be the set of cells.

We define a helper function \(box : C → N\) that gives us the "big box" containing the given cell. That is, \(box(i,j)\) gives the number in the \(i\)th row and the \(j\)th column of the following table:

box definition (click for LaTeX source)

box definition (click for LaTeX source)

Now we can define a sudoku solution. A sudoku solution must have a single number in each cell. Therefore, we model it as a function \(s : C → N\); given the cell \((i,j)\), \(s(i,j)\) gives the number in the corresponding cell. I am using the convention that \(i\) is a row number and \(j\) is a column number, but nothing about the formal definition requires this.

Sudoku solutions must satisfy three properties. We list them informally and formally:

  1. Every number appears in some column of every row: for all \(n \in N\) and \(i \in N\), there exists some \(j \in N\) such that \(s(i,j) = n\).

  2. Every number appears in some row of every column: ∀ \(n \in N\), ∀ \(j \in N\), ∃ \(i \in N\) such that \(s(i,j) = n\).

  3. The same number doesn't appear twice in the same box: for all cells \(c_1\) and \(c_2 \in C\), if \(box(c_1) = box(c_2)\) and \(c_1 \neq c_2\), then \(s(c_1) \neq s(c_2)\).

We wrote property 3 in a different style from properties one and 2 only for demonstration purposes; we could have written instead:

3'. Every number appears in some cell of each box: for all \(b \in N\) and \(n \in N\), there exists some \(c \in C\) such that \(box(c) = b\) and \(s(c) = n\).

We will discuss the equivalence of 3 and 3' next lecture.

Important notes not discussed yet