# Lecture 2: Using functions to model problems

• using functions to define sudoku solution
• using quantifiers (for all and there exists)

• new notation: cartesian product ($$A \times B$$), for all (∀), there exists (∃), in (∈)

## Notation

• If $$A$$ and $$B$$ are sets, then the cartesian product of $$A$$ and $$B$$, denoted $$A \times B$$ is the set of pairs where the first part is an element of $$A$$ and the second part is an element of $$B$$. For example, $$\{1,2\} \times \{a,b\} = \{(1,a), (1,b), (2,a), (2,b)\}$$.

• the upside-down A (∀) stands for for all. I tend to write out the words, but occasionally the symbols are useful. It is important to be able to go back and forth.

• the backwards E (∃) stands for there exists

• $$x \in X$$ means that $$x$$ is an element of the set $$X$$, or is read in, as in "$$∀ x \in X$$".

## 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:

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

• The order of "for all" and "there exists" is important. For example, we could rewrite condition 1 to "$$∃ j \in N$$ such that $$∀ n$$ and $$i \in N$$, $$s(i,j) = n$$, this would mean that there is a single column ($$j$$), and every number lies in every cell of column $$j$$, which is clearly nonsense.

• To tell whether a definition is good, it must have only one meaning. For example, I left a little out of the definition of $$box$$ given in lecture. I wrote "Let $$box : C → N$$ be given by the ith row and the jth column in the following table:" This definition is ambiguous, because I didn't tell you what $$i$$ and $$j$$ were. I disambiguated with an example, but the way it is described above is a better fix.