Lecture 2: Using functions to model problems

• Last semester

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

• new notation: cartesian product ($$A \times B$$), for all ($$∀$$), function $$f : A → B$$.

• review exercise: describe solutions to some other kind logic puzzle.

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 symbol $$f : A → B$$ indicates that $$f$$ is a function with domain $$A$$ and codomain $$B$$.

• The symbol $$:=$$ is used to emphasise that an equation is the definition of a variable or term.

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:

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:

• Note that the formal (symbolic) defintions are much harder to read than the English description. When writing, your goal is to convey understanding to the reader. The informal description helps the reader build a mental picture, while the formal description helps the reader connect that picture to the objects you are using to model the picture. Optimize for communication.

• Although rules 1 and 2 are completely parallel, I chose to formalize them in slightly different ways (the only reason to do this is to demonstrate both styles). In general, a statement of the form "if P then Q" is equivalent to "the only way that Q is false is if P is also false" or more succinctly, "if not Q then not P". These statements ("if P then Q" and "if not Q then not P") are called contrapositives. In this case, the contrapositive is sometimes easier to work with, because it's sometimes easier to reason about things being equal than things being unequal.

• In rules 2 and 3, I left off the fact that $$i$$ and $$j$$ are in $$N$$, and that $$c_1$$ and $$c_2$$ are in $$C$$. This can be inferred from the fact that $$(i,j)$$ and $$c$$ are plugged into $$s$$, and the domain of $$s$$ contains cells (which are pairs of numbers). It is also suggested by the fact that I'm consistently using $$i$$,$$j$$, and $$k$$ for numbers, and $$c$$ for cells. As with the other stylistic decisions, you should optimize for clarity.

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.

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.
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.