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)

If we really wanted, we could write down a formula for the \(box\) function (something like \(box(i,j) := \lfloor (i-1)/3 \rfloor + 3 \lfloor (j-1)/3 \rfloor + 1\)). But it isn't necessary: it doesn't make the function any clearer than the table. When writing definitions, optimize for clarity. Use mathematical notation when it helps clarify, but avoid it when it obscures.

Now we can define a solved sudoku board. A solved sudoku board 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 (except in the definition of \(box\)).

Sudoku solutions \(s\) must satisfy three properties. We list them informally and formally:

  1. For any row, and any two different columns, the numbers in that row and those columns are different. With lots of notation: \(∀ i ∈ N, ∀ j ∈ N, j' ∈ N\), if \(j \neq j'\) then \(s(i,j) \neq s(i,j')\)

  2. For any column and any two different rows, the numbers in that column and those rows are different: \(∀ j, ∀ i, i',\) if \(s(i,j) = s(i',j)\) then \(i = i'\).

  3. Two different cells in the same box must have different values: \(∀ c_1, c_2\), if \(box(c_1) = box(c_2)\) but \(c_1 \neq c_2\), then \(s(c_1) \neq s(c_2)\).

A few notes about these definitions:

At the end of lecture, we posited a different informal way to state the sudoku rules: instead of saying no two entries are the same, we instead might require that every number appears somewhere. We'll discuss this alternative in the next lecture.

Some questions that were asked

Q: Can we model a solved sudoku problem as a set of 9 sets of numbers?

A: No, there is not enough data. For example, \(\{\{1,2,3,\dots\},\dots\} = \{\{3,2,1,\dots\},\dots\}\), so we can't distinguish between two boards with the first columns in a different order. Moreover, we couldn't represent a board like \(\{\{1,2,3,\dots,9\}, \{9,8,7,\dots\}, \dots\}\), because the first two columns are the same set, and so this set would only have 8 columns.

Q: Can we model a solved problem as a 9-tuple of 9-tuples of numbers?

A: Yes. However, this would make the rules harder to state (try it!); you'd need notation for extracting the \(k\)th element of a tuple. You might write the \(i\)th entry of the \(j\)th entry of the board as \(s_{ij}\), but then your definition would be exactly the same as the function definition: instead of writing \(s(i,j)\) you'd write \(s_{ij}\). Also, it wouldn't be a good example for function notation.