\documentclass{article}

\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{changepage}  % For adjustwidth environment
\usepackage{colortbl}  % Provides the  \arrayrulecolor command.
\usepackage{environ}
\usepackage{framed}  % For leftbar environment
\usepackage[hidelinks]{hyperref}  % For \autoref
\usepackage{listings} \lstset{language=Python}
\usepackage{mathtools}  % For \shortintertext
\usepackage{parskip}  % Don't indent paragraphs; skip instead.
\usepackage{ragged2e}  % FlushLeft environment
\usepackage{varwidth}
\usepackage{url}
\usepackage{xcolor}

% Code style, based on one from CSC236,.
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
 \lstdefinestyle{mystyle}{
    commentstyle=\color{codepurple},
    keywordstyle=\color{blue},
    %numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\ttfamily\small,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    %numbers=left,
    %numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2,
}
\lstset{style=mystyle}


% Stuff that should already be on slides.
\NewEnviron{onslide}{{

    \newdimen\slidewidth
    \slidewidth=\linewidth
    % Without this, I get errors beginning "Overfull \hbox (6.79999pt too
    % wide)"
    \advance\slidewidth by -7pt

    \medskip

    \fbox{\parbox{\slidewidth}{\BODY}}

    \medskip

  }}

% Stuff I should write or draw during lecture.
\NewEnviron{writelive}{\vspace{1ex}\begin{leftbar}{\BODY}\end{leftbar}}

\newenvironment{indentone}{\begin{adjustwidth}{2em}{0em}}{\end{adjustwidth}}


% Binary and unary all-uppercase text operators. \mathop gives too little space
% around the binary ones; e.g. \(P \op{AND} Q\) needs lots of space between
% those capital letters.
\newcommand\op[1]{{\ \mathrm{#1}\ }}
\newcommand\uop[1]{{\mathrm{#1}}}

\newcommand\AND{{\op{AND}}}
\newcommand\IFF{{\op{IFF}}}
\newcommand\IMPLIES{{\op{IMPLIES}}}
\newcommand\NOT{{\uop{NOT}}}
\newcommand\OR{{\op{OR}}}
\newcommand\XOR{{\op{XOR}}}

\newcommand\FALSE{{\mathrm{F}}}
\newcommand\TRUE{{\mathrm{T}}}

\DeclareMathOperator\concat{+\!\!+}
\DeclareMathOperator\Bkt{Bkt}
\DeclareMathOperator\leftT{left}
\DeclareMathOperator\rightT{right}

\newcommand\stringLit[1]{{\texttt{"#1"}}}

\newcommand\ComplexNums{{\mathbb{C}}}
\newcommand\Ints{{\mathbb{Z}}}
\newcommand\Nats{{\mathbb{N}}}
\newcommand\Reals{{\mathbb{R}}}
\newcommand\Rationals{{\mathbb{Q}}}

\newcommand\lbkt{{\texttt{[}}}
\newcommand\rbkt{{\texttt{]}}}

\newcommand\powerset{{\mathcal{P}}}

\newcommand\optional[1]{\textcolor[rgb]{0.5,0.5,0.3}{#1}}

% Warning: this resets the colour to black using \arrayrulecolor.
\newcommand{\greyline}{\arrayrulecolor{black!20}\hline\arrayrulecolor{black}}

% Don't put theorem text in italics, because in this document, italics is for
% descriptions of things I should write or have on slides.
\theoremstyle{definition}
\newtheorem{theorem}{Theorem}
\newtheorem*{theorem*}{Theorem}
\newtheorem*{definition*}{Definition}
\newtheorem*{lemma*}{Lemma}

\begin{document}

These are \emph{rough notes} based on my own notes I prepared for the Week~7
lecture. They go with the annotated lecture slides you can see on the ``More''
page of the course website. Although they are rough, I'm providing them here in
case they help you remember what we did in lecture.

\section{Legend}

\begin{onslide}
Text that should already be on slides.

\emph{Descriptions of other things that should already be on slides.}
\end{onslide}

\begin{writelive}
Text I should write during lecture.

\emph{Descriptions of other things I should draw or write or do.}
\end{writelive}

\section{Countable sets}

\begin{onslide}
\begin{definition*}
A function \(f : A \to B\) is \emph{surjective} (or \emph{onto}) if \ldots
\end{definition*}
\end{onslide}

\begin{writelive}
\(\forall y \in B .\> \exists x \in A .\> f(x) = y\)

\emph{(arrow to ``if'')} In definitions, if often means iff.
\end{writelive}

\begin{onslide}
\begin{definition*}
A function \(f : A \to B\) is \emph{surjective} (or \emph{onto}) if
\(\forall y \in B .\> \exists x \in A .\> f(x) = y\).
\end{definition*}

\bigskip

\begin{definition*}
A set \(C\) is \emph{countable} if it is empty or there exists a surjective
function \(f : \Nats \to C\).
\end{definition*}

\bigskip

\begin{tabular}{c|l}
  \(C\) & countable? \\
  \hline
  \(\{ 1, 2, 3 \}\) & \\
  \greyline
  \(\Nats\) & \\
  \greyline
  \(\Ints\) &
\end{tabular}
\end{onslide}

\begin{writelive}
\emph{(Beside \(\{ 1, 2, 3 \}\))} Yes: \(f(1)=1, f(2)=2, f(3)=3, f(\text{other})=1\).

\emph{(Beside \(\Nats\))} Yes: \(f(n)=n\).

\emph{(Beside \(\Ints\))} Yes: \(0, -1, 1, -2, 2, -3, \dotsc\).
\[f(n) =
\begin{cases}
\frac {n} {2}, & \text{\(n\) even} \\
- \frac {n + 1} {2}, & \text{\(n\) odd}
\end{cases}
\]

\emph{(circle ``it is empty or'')} Needed? Try to make
\(f : \Nats \to \emptyset\). \(f(0) = \ ?\) No functions
\(f : \Nats \to \emptyset\) exist.

Exercise: write a simpler definition that doesn't need a special case.
\end{writelive}

\begin{onslide}
\begin{theorem}\label{thm:countabilityClosed}
\begin{itemize}
\item[]  % This item gets put on the same line as "Theorem N.".
\item If \(A\) and \(B\) are countable, so is \(A \cup B\).
\item If \(A\) is countable and \(B \subseteq A\), then \(B\) is countable.
\item If \(A\) and \(B\) are countable, so is \(A \times B\).
\end{itemize}
\end{theorem}
\end{onslide}

We won't prove the whole theorem, but let's see why natural numbers times
natural numbers is countable.

\begin{writelive}
\(\Nats \times \Nats\) is countable:

\begin{tabular}{c|cccc}
  & 0 & 1 & 2 & \ldots \\
  \hline
  0 & 0 & 1 & 3 \\
  1 & 2 & 4 \\
  2 & 5 \\
  \vdots
\end{tabular}

\(f(k)=\) coordinates where \(k\) appears. E.g. \(f(4) = (1, 1)\).

Exercise: write a formula for \(f(k)\).
\end{writelive}

\begin{onslide}
\begin{theorem}\label{thm:rationalsCountable}
\(\Rationals\) is countable.
\end{theorem}

\bigskip

\begin{lemma*}
If \(A\) is nonempty and countable and there is a surjective function
\(f : A \to B\), then \(B\) is countable.
\end{lemma*}
\end{onslide}

\begin{writelive}
Proof of \autoref{thm:rationalsCountable}:

Define \(f : \Ints \times \Ints^+ \to \Rationals^+\) by \(f(x, y) = x / y\).

\(f\) is surjective. (Definition of \(\Rationals\)?) By
\autoref{thm:countabilityClosed}, \(\Ints \times \Ints^+\) is countable, so by
Lemma, \(\Rationals\) is countable.
\end{writelive}

\begin{onslide}
\begin{theorem}
\(\{0, 1\}^*\) (the set of finite binary sequences) is countable.
\end{theorem}
\end{onslide}

\begin{writelive}
\(\lambda, 0, 1, 00, 01, 10, 11, 000, \dotsc\)

\(f : \Nats \times \Nats \to \{0, 1\}^*\)
\end{writelive}

How can we define a surjective function from the naturals times the naturals to
the set of finite binary sequences?

\begin{writelive}
\[f(i, j) =
\begin{cases}
\text{\(j\) as an \(i\)-digit-long binary number}, & j < 2^i \\
\lambda, & \text{otherwise}
\end{cases}
\]

\(f\) surjective, so by Lemma, \(\{0, 1\}^*\) is countable.
\end{writelive}

\section{Diagonalization}

\subsection{\(\powerset( \Nats )\) is uncountable}

\begin{onslide}
\begin{definition*}
The \emph{power set} of a set \(A\), written \(\powerset(A)\), is the set of
all subsets of \(A\).
\end{definition*}

\bigskip

If \(A\) has \(n\) elements, how many elements does \(\powerset(A)\) have?

Is \( P( \Nats ) \) countable?
\end{onslide}

\begin{writelive}
\(2^n\)
\end{writelive}

If we only want the finite subsets, that's countable. But there's no way to get
all the infinite subsets.

\begin{onslide}
\emph{(Keep definition of power set.)}

\bigskip

\begin{theorem}
\(\powerset(\Nats)\) is uncountable.
\end{theorem}
\end{onslide}

\begin{writelive}
\emph{(arrow to ``uncountable'') not countable}

Consider any \( f : \Nats \to \powerset( \Nats ) \).

\begin{tabular}{c|cccc}
  \(x \in S\)? & 0 & 1 & 2 & \ldots \\
  \hline
  f(0) \\
  f(1) \\
  f(2) \\
  \vdots
\end{tabular}

\(f(0) = \emptyset\) \emph{(Fill in \(f(0)\) row with F \ldots.)}
\(f(1) = \Nats\) \emph{(Fill in \(f(1)\) row with T \ldots.)}
\(f(2) =\) even numbers \emph{(Fill in \(f(2)\) row with T F T F \ldots.)}

\emph{(Circle the first few diagonal entries.)}

Make a new set \(D\) which is the \underline{opposite} of the diagonal. It
is not in the range of \(f\).
\end{writelive}

We've shown that no function from naturals to power set of naturals is a
surjection. So, the power set of the naturals is uncountable. Let's turn that
into a concise proof.

\begin{writelive}
Proof:

Suppose \(\powerset(\Nats)\) is countable. Then \(\exists\) surjective
\(f : \Nats \to \powerset(\Nats)\).

Let \(D = \{ n \in \Nats | n \not\in f(n) \}\).
\end{writelive}

\(D\) is that set we made by flipping everything on the diagonal.

\begin{writelive}
Since \(f\) surjective, \(\exists\) \(d \in \Nats\) s.t.\ \(f(d) = D\).
\end{writelive}

So, our set appears on row \(d\) in that table.

\begin{writelive}
Is \(d \in D\)?

Case 1: Suppose \(d \in D\). Then \(d \in f(d)\), so \(d \not \in D\).
Contradiction.

Case 2: Suppose \(d \not \in D\). Then \(d \not \in f(d)\), so \(d \in D\).
Contradiction.

\(\powerset(\Nats)\) is not countable. \quad (proof by contradiction) \qed

Cantor's diagonal argument
\end{writelive}

This is called Cantor's diagonal argument. The method used here is sometimes
called \emph{diagonalization}.

\subsection{The Halting Problem}

\begin{onslide}
\textbf{The Halting Problem}

Determine whether a program will keep running forever, or eventually stop.
\end{onslide}

The Halting Problem is about figuring out whether a program will eventually
halt, meaning stop running.

Who has accidentally written a function that just runs forever instead of
returning?

\begin{onslide}
\textbf{The Halting Problem}
\end{onslide}

(The following code also goes on the slide, but Latex is not cooperating.)
\begin{lstlisting}
def halts(P: str, x: str):
  """Returns True if P is a Python function definition, and
  that function applied to x would eventually finish running
  ("halt").
  """
  ...

>>> halts("""
... def g(x):
...   return x
... """, "hello")
True
>>> halts("""
... def h(x):
...   while True:
...     print("Still going")
... """, "abc")
False
\end{lstlisting}

\begin{onslide}
\emph{(still same slide)}

\begin{theorem}
\texttt{halts()} can't be implemented.
\end{theorem}
\end{onslide}

It's unusual to be able to prove that a program is impossible to write.

\begin{writelive}
Proof.

For a contradiction, suppose halts() can be implemented.
\end{writelive}

\begin{lstlisting}
D_str = """
def D(x: str):
  if halts(x, x):
    while True:
      pass
  else:
    return
"""
\end{lstlisting}

\begin{writelive}
What does D(D\_str) do?

Case 1: Suppose halts(D\_str, D\_str) returns False. Then, from the code,
D(D\_str) runs forever. So halts must return True. Contradiction.

Case 2: Suppose halts(D\_str, D\_str) returns True. Then, from the code,
D(D\_str) returns. So halts must return False. Contradiction.

halts() cannot be implemented. (Proof by contradiction.) \qed
\end{writelive}

We can also view this as a table:

\begin{writelive}
\begin{tabular}{c|ccccc}
  halts & \(P_0\) & \(P_1\) & \(P_2\) & \ldots \\
  \hline
  \(P_0\) & T & F & F \\
  \(P_1\) & T & T & F \\
  \(P_2\) & T & F & T \\
  \vdots
\end{tabular}

\emph{(Arrow to rows and columns.)} All syntactically correct Python functions.

If halts(\(P_i, P_i\)) == False then D(\(P_i\)) halts, so halts(D, \(P_i\)) == True.

If halts(\(P_i, P_i\)) == True then D(\(P_i\)) runs forever, so halts(D, \(P_i\)) == False.

\emph{(Circle diagonal elements.)} D is the opposite of the diagonal, so it
cannot be in the table.
\end{writelive}

\begin{onslide}
We've shown:
\begin{itemize}
\item \(\powerset( \Nats )\) is uncountable.
\item The Halting Problem is uncomputable.
\end{itemize}
\end{onslide}

\begin{writelive}
Others:
\begin{itemize}
\item Russell's paradox
\end{itemize}
\end{writelive}

There's a story here. For a while, mathematicians were trying to come up with a
complete set of axioms that you could use to prove anything in mathematics.

At that time there was a mathematician named Frege. At one point, Frege put
together a great mathematical work, laying out a system for doing mathematics.
Unfortunately, just as Frege's work was being published, another mathematician,
named Bertrand Russell, pointed out a problem.

\begin{writelive}
Let \( D = \{ s \mid s \not\in s \} \) (the set of all sets that don't contain
themselves)

\( D \in D \IFF D \not \in D \): contradiction
\end{writelive}

This was a disaster for Frege's work. His mathematical system lets you define
this set \(D\), which leads to a contradiction. That means you can prove any
statement you want!

\begin{writelive}
\emph{(Arrow to \(D\).)} Any system that lets you define this is
\underline{inconsistent}.

Solution: set things up so you can't define \(D\).

Set-builder notation: \( \{ x \in A \mid \ldots \} \).

\emph{(Arrow to ``\( \in A \)''.)} This is important!
\end{writelive}

Requiring set-builder notation to start from a bigger set helps us avoid this
problem.

But let's go back to that fundamental goal, of finding a universal collection of
axioms for doing all of mathematics.

\begin{writelive}
\begin{itemize}
\item G\"odel incompleteness: every system of axioms is either inconsistent or
incomplete.
\end{itemize}

\emph{(Arrow to ``inconsistent''.)} Possible to prove a contradiction

\emph{(Arrow to ``incomplete''.)} Some things can't be proved true or false

\(D =\) ``There is no proof that \(D\) is true.''
\end{writelive}

\section{Preview: analyzing runtime}

(On a slide.)
\begin{lstlisting}
def contains_negative_pair(numbers: List[int]):
  """Returns True if there's some positive integer x such that
  x and -x are in numbers."""
  for a in numbers:
    if a == 0:
      continue
    for b in numbers:
      if a == -b:
        return True
  return False

>>> contains_negative_pair([1, 2, -1])
True
>>> contains_negative_pair([0, 2, -3])
False
\end{lstlisting}

\begin{onslide}
How long does \texttt{contains\_negative\_pair} take to run?
\end{onslide}

\begin{writelive}
Let \(n =\) \texttt{len(numbers)}.

\emph{(Annotate ``if a == 0'' line.)} Runs \(n\) times. A few nanoseconds each.

\emph{(Annotate ``if a == -b'' line.)} Runs \( \le n^2 \) times.

Runtime = \(a n^2 + b n + c\) nanoseconds??
\end{writelive}

You might be able to write the amount of time this takes as some function like
this in nanoseconds. But it will depend on your computer, and whether other
processes are running on the same computer, and so on.

All of that is important, but it's also really hard. So computer scientists
often do a simpler kind of analysis. The important thing about the runtime is
the n squared term.

\begin{writelive}
Forget about the details: write \( O( n^2 ) \).

Double \(n\) \(\rightarrow\) runtime x4.
\end{writelive}

The n squared term tells us that every time you double the length of the input,
the runtime gets multiplied by approximately four, at least when \(n\) is large.
That's a useful starting point.

Let's define what big oh of n squared means.

\section{Big Oh notation}

\subsection{Definition of Big Oh}

\begin{onslide}
\textbf{Big \(O\) Notation}

Let \( \mathcal {F} \) be the set of all functions from \( \Nats \) to
\( \Reals^+ \).

For any \( f \in \mathcal {F} \), let
\[ O(f) = \{ g \in \mathcal {F} \mid \exists c \in \Reals^+ .\> \exists b \in
\Nats .\> \forall n \in \Nats .\> (n \ge b \IMPLIES g(n) \le c f(n)) \} \]
\end{onslide}

In other words:

\begin{writelive}
\( f \in O(g) \) means for all sufficiently large \( n \), \( g(n) \) is at most
a constant factor times \( f(n) \).

Example:

\( 6n + 4 \in O( 3n ) \) because when \( n \ge 2 \), \( 6n + 4 \le 8n \le 3 \cdot 3n \).

Here, \( c = 3 \) and \( b = 2 \). Can also choose \( c = 4 , b = 1 \). Or
infinitely many other choices.

Conventionally, \( O( n^2 ) \) means \( O( f ) \) where \( f (n) = n^2 \).
\end{writelive}

\subsection{Properties of Big Oh}

\begin{onslide}
\textbf{Properties of big \(O\)}

Summary on ``Further reading'' page of course website.

\emph{(This slide has more stuff, but we didn't get to this slide in lecture.)}
\end{onslide}
\end{document}
