\documentclass{article}

\usepackage[hidelinks]{hyperref}  % For \autoref
\usepackage{parskip}  % Don't indent paragraphs; skip instead.

\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{listings} \lstset{language=Python}
\usepackage{mathtools}  % For the \mathclap command.
\usepackage{ragged2e}  % FlushLeft environment
\usepackage{thmtools}  % For restatable environment
\usepackage{tikz}
\usepackage{varwidth}
\usepackage{xcolor}

% Code style, based on one being used in 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\footnotesize,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    numbers=left,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2,
    escapechar=@
}
\lstset{style=mystyle}

% 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\FALSE{{\mathrm{F}}}
\newcommand\TRUE{{\mathrm{T}}}

\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\ComplexNums{{\mathbb{C}}}
\newcommand\Ints{{\mathbb{Z}}}
\newcommand\Nats{{\mathbb{N}}}
\newcommand\Reals{{\mathbb{R}}}
\newcommand\Rationals{{\mathbb{Q}}}

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

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

\newcommand\Todo[1]{{\fbox{\begin{varwidth}{\linewidth}TODO: {#1}\end{varwidth}}}}

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

% Useful in tests.
\newcommand{\marksminutes}[2]{[#1 marks; \( \sim #2 \) minutes] \par}
\newcommand{\doublenewpage}{\newpage\null\newpage}

\DeclareMathOperator\concat{+\!\!+}
\DeclareMathOperator\Exp{E}
\DeclareMathOperator\size{size}

\newcommand\emptyslide{\begin{frame}\end{frame}}


% 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*{corollary*}{Corollary}
\newtheorem{theorem}{Theorem}
\newtheorem*{theorem*}{Theorem}
\newtheorem*{definition*}{Definition}
\newtheorem*{lemma*}{Lemma}

% 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}}

\begin{document}

These are \emph{rough notes} based on my own notes I prepared for 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 was already on the slides is in a box, like this.

\emph{Descriptions of things that were already on the slides are formatted like
  this.}
\end{onslide}

\begin{writelive}
Text I planned to write during lecture is formatted like this, with a line to
the left.

\emph{Descriptions of other things I planned to draw or write or do are
  formatted like this.}
\end{writelive}

Anything else is text I planned to say, or things I planned to ask.

\section{Solving recurrences (continued)}

\begin{onslide}
Define \(M : \Ints^+ \to \Nats\) by
\begin{itemize}
\item \( M(1) = c \)
\item For \(n > 1\), \( M(n) = M( \lceil n/2 \rceil ) + M( \lfloor n/2 \rfloor ) + dn\)
\end{itemize}
where \(c, d \in \Nats\) are constants.

\bigskip

Last week:
\begin{itemize}
\item Closed form when \(n\) is a power of 2: \( M(n) = cn + d n \log_2 n \).
\item We found this using \emph{repeated substitution}.
\item For a recap, see Section 3.2.1 of \emph{Course notes for CSC
  B36/236/240}, linked from the \emph{Textbooks} section the course homepage.
\end{itemize}
\end{onslide}

\begin{onslide}
\emph{(Repeat definition of \(M\).)}

\begin{theorem}
\( M(n) \in O( n \log n ) \)
\end{theorem}
\end{onslide}
To prove this, we'll use the fact that \(M\) is a nondecreasing function.

\begin{onslide}
\begin{lemma*}
\( \forall a \in \Ints^+ .\> \forall b \in \Ints^+ .\> ( a \le b \IMPLIES M(a)
\le M(b) ) \)
\end{lemma*}
(Proof: see Lemma~3.6 on page~84 of \emph{Course notes for B36/236/240}
(Textbooks section of course website).)
\end{onslide}

\begin{writelive}
\emph{(Arrow to Lemma.)} Summary: use induction to prove \( M \) is
nondecreasing on \( \{ 1, 2, \dotsc, k \} \) for all \( k \in \Ints^+ \).

Proof of Theorem using Lemma:

Given any \(n \in \Nats\), let \(2^k\) be smallest power of 2 that is
\( \ge n \). So \( n \le 2^k < 2n \).
\begin{flalign*}
M(n)
\le& M(2^k) & \\
=& c 2^k + d k 2^k \\
<& 2 c n + 2 d n \log_2 (2n) \\
\in& O( n \log n ) & \qed
\end{flalign*}
\end{writelive}

\begin{onslide}
\textbf{Master Theorem (for ``divide and conquer'' recurrences)}

Suppose \(T : \Ints^+ \to \Reals^+\) is defined by:
\begin{itemize}
\item For \(n < B\), \(T(n) = c\)
\item For \(n \ge B\), \(T(n) = a_1 T( \lceil n/b \rceil ) + a_2 T( \lfloor n/b \rfloor ) + d n^{ \ell }\)
\end{itemize}
where \(a_1, a_2, B \in \Nats\), \(a = a_1 + a_2 \ge 1\), \(b > 1\), \(c, d, \ell \in \Reals^+\)

Then:
\[
T(n) \in
\begin{cases}
 & \text{if \(a = b^\ell\)} \\
 & \text{if \(a < b^\ell\)} \\
 & \text{ if \(a > b^\ell\)}
\end{cases}
\]

\emph{(Copy of the recurrence we just solved.)}
\end{onslide}

Which case does the recurrence we just solved fit into?

\begin{writelive}
\emph{(Beside the recurrence we just solved:)}

\( a = 2 \), \( b = 2 \), \( c = c \), \( d = d \), \( \ell = 1 \)

\( a = b^\ell \) \emph{(Arrow to \( a = b^\ell \) case.)}

\emph{Fill in the missing parts to get:}
\[
T(n) \in
\begin{cases}
O( n^\ell \log n ), & \text{if \(a = b^\ell\)} \\
O( n^\ell ), & \text{if \(a < b^\ell\)} \\
O( n^{ \log_b a } ), & \text{ if \(a > b^\ell\)}
\end{cases}
\]
\end{writelive}

\subsection{Transformations}

\textbf{Note: some students pointed out a mistake in the following slide: the
  definition \( G( n / 2 ) + \dotsb \) doesn't work when \( n \) is not even.
  So we should replace \( n / 2 \) with \( \lfloor n / 2 \rfloor \) (or state
  that the recurrence is only defined when \(n\) is a power of 2), and we
  should also say that our solution to the recurrence assumes \(n\) is a power
  of \(2\).}

\begin{onslide}
\textbf{Using transformations to solve recurrences}

Example:
\begin{itemize}
\item \( G(1) = 0 \)
\item \( G(n) = 2 G( n / 2 ) + \log_2 n \) when \( n \in \Ints^+ \)
\end{itemize}
\end{onslide}

\begin{writelive}
Let \( n = 2^k \).

Then \( G(2^k) = 2 G(2^{k-1}) + k \); \( G(2^0) = 0 \).

Let \( H(k) = G(2^k) \).

\( H(0) = 0 \)

\( H(k) = 2 H(k-1) + k \)

Solve using repeated substitution: \( H(k) = 2^{k+1} - k - 2 \)

So \( G(n) = 2 n - \log_2 n - 2 \).

\underline{Domain transformation} \emph{(arrow to work we just did)}
\end{writelive}

This is called a domain transformation, because we transformed the input to the
function.

We can also do range transformations, where we transform the output.

\begin{onslide}
Example:
\begin{itemize}
\item \(A(0) = 1\)
\item \(A(n) = 3 A(n-1)^2\) for \( n \in \Ints^+ \)
\end{itemize}
\end{onslide}

\begin{writelive}
Let \( B(n) = \log_2 A(n) \).

\( B(n) = \log_2 3 + 2 \log_2 A(n-1) = \log_2 3 + 2 B(n - 1) \)

\( B(0) = \log_2 1 = 0 \)

Repeated substitution:
\begin{align*}
  B(n)
  =& \log_2 3 + 2 \cdot \log_2 3 + 4 \cdot \log_2 3 + \cdots + 2^n B(0) \\
  =& (2^n - 1) \log_2 3
\end{align*}

So \( A(n) = 2^{ B(n) } = 2^{ (2^n - 1) \log_2 3 } = 3^{ 2^n - 1 } \)

\underline{Range transformation} \emph{(arrow to work we just did)}
\end{writelive}

\subsection{Linear recurrences using characteristic polynomials}

\begin{onslide}
Methods for solving recurrences:
\begin{enumerate}
\item Guess and verify
\item Repeated substitution and verify
\item Special techniques for ``divide and conquer'' recurrences / Master Theorem
\item Transformations
\item \textcolor{blue}{Solving linear recurrences using characteristic polynomials}
\end{enumerate}
\end{onslide}

Here's our last method for solving recurrences.

\begin{onslide}
\textbf{Linear recurrences using characteristic polynomials}

Fibonacci recurrence:
\begin{itemize}
\item \( F(0) = 0 \)
\item \( F(1) = 1 \)
\item \( F( n ) = F(n-1) + F(n-2) \) for \( n \in \Nats \), \( n >1 \)
\end{itemize}
\end{onslide}

\begin{writelive}
0, 1, 1, 2, 3, 5, 8, 13, \ldots
\end{writelive}

This is an example of a \emph{homogeneous linear recurrence}.

\begin{onslide}
\emph{(Repeat Fibonacci recurrence.)}

In general, the recursive case for a \emph{homogeneous linear recurrence} looks
like this:
\begin{align*}
  f(n) =& a_1 f(n-1) + a_2 f(n-2) + \dotsb + a_d f(n-d) \\
  =& \sum_{i=1}^d a_i f(n-i) 
\end{align*}
\( d, a_1, \dotsc, a_d \) are constants.
\end{onslide}

In a homogeneous linear recurrence, the recursive case is a fixed linear
combination of previous terms.

\begin{onslide}
\emph{(Repeat Fibonacci recurrence.)}

Find a closed form for \( F(n) \).
\end{onslide}

Let's try something easier first.

\begin{writelive}
\emph{Cross out \( F(n-1) + F(n-2) \). Replace it with \( 2 F(n-1) \). Cross out \( F(0) \).}

\( 1, 2, 4, 8, \dotsc \).

\( F(n) = 2^{n-1} \)
\end{writelive}

\begin{onslide}
\emph{(Repeat previous slide.)}
\end{onslide}

\begin{writelive}
To solve: guess \( f(n) = c x^n \). Need to find \( c, x \).

Plug in: \( c x^n = c x^{n-1} + c x^{n-1} \)

\( x^2 = x + 1 \)

\( x = (1 \pm \sqrt{5}) / 2 \)

\( F(n) = c [(1 + \sqrt{5}) / 2)]^n \) or
\( F(n) = c [(1 - \sqrt{5}) / 2)]^n \)
\end{writelive}

Okay, that's strange. And it doesn't even work.

\begin{writelive}
\emph{(Arrow to \( F(n) = c [(1 + \sqrt{5}) / 2)]^n \).)} This would mean
\( \frac {F(2)} {F(1)} = \frac {1 + \sqrt 5} {2} \). But
\( \frac {F(2)} {F(1)} = \frac11 = 1 \).
\end{writelive}

What if we combined them?

\begin{writelive}
Try this: \( F(n) = c_1 [(1 + \sqrt{5}) / 2)]^n + c_2 [(1 - \sqrt{5}) / 2)]^n \)
\end{writelive}

\begin{onslide}
\begin{theorem}
Consider any homogeneous linear recurrence, ignoring the base case(s):
\[ f(n) = \sum_{i=1}^d a_i f(n-i) \] If \( x_1^n \) and \( x_2^n \) are both
solutions, then \( c_1 x_1^n + c_2 x_2^n \) is a solution too, for any
\( c_1, c_2 \in \Reals \).
\end{theorem}
\end{onslide}

\begin{writelive}
Proof:

\( x_1^n \) is a solution means
\begin{flalign*}
\forall n \in \Nats .\, x_1^n
=& \sum_{i=1}^d x_1^{n-i} & \\
c_1 x_1^n
=& c_1 \sum_{i=1}^d x_1^{n-i} \\
=& \sum_{i=1}^d c_1 x_1^{n-i}
\end{flalign*}

Similarly \( c_2 x_2^n = \sum_{i=1}^d c_2 x_1^{n-i} \).

Add:
\[ c_1 x_1^n + c_2 x_2^n = \sum_{i=1}^d (c_1 x_1^{n-i} + c_2 x_2^{n-i} \]
So \( f(n) = c_1 x_1^n + c_2 x_2^n \) is a solution.
\end{writelive}

Back to the Fibonacci recurrence

\begin{onslide}
\emph{(Repeat Fibonacci recurrence.})
\end{onslide}

\begin{writelive}
Try \( F(n) = c_1 [(1 + \sqrt{5}) / 2)]^n + c_2 [(1 - \sqrt{5}) / 2)]^n \).

\( F(0) = c_1 \cdot 1 + c_2 \cdot 1 = 0 \) so \( c_2 = - c_1 \).

\begin{flalign*}
F(1) =& c_1 (1 + \sqrt {5}) / 2 - c_1 (1 - \sqrt {5}) / 2 = 1 & \\
1 =& c_1 \sqrt {5} \\
c_1 =& 1 / \sqrt {5}
\end{flalign*}

Solution:
\[ F(n) = \frac {1} {\sqrt {5}} \left( \frac {1 + \sqrt{5}} {2} \right)^n - \frac
{1} {\sqrt {5}} \left( \frac {1 - \sqrt{5}} {2} \right)^n \]

Looks wrong (\( \sqrt{5} \)??) but can be proved by induction.
\end{writelive}

\section{Analysis of algorithms}

\begin{onslide}
\textbf{Analysis of algorithms}

How long does this algorithm take to run?
\end{onslide}

(Still on slide...)
\begin{lstlisting}
def linear_search(l : List[int], x : int):
  # If x occurs in l, returns an index of l at which x occurs.
  # Otherwise, returns None.
  i = 0
  while i < len(l):
    if l[i] == x:
      return x
    i += 1
  return None
\end{lstlisting}

\begin{writelive}
Why does it matter?
\begin{itemize}
\item Predict how long it will take.
\item Decide what inputs are reasonable.
\item Compare different algorithms.
\end{itemize}
\end{writelive}

The actual runtime depends on lots of things.

\begin{writelive}
Depends on:
\begin{itemize}
\item Programming language / compiler / interpreter
\item The computer running it
\item What other processes are running
\item \ldots
\end{itemize}
So: instead of exact time, estimate to within a constant factor, and write \(O(\cdot)\).
\end{writelive}

\begin{onslide}
\emph{(Repeat code.)}
\end{onslide}

\begin{writelive}
Running time also depends on input. For algorithm \(A\) and input \(I\), let \( t_A (I) \) denote \#
steps algorithm performs on input \(I\).

What is a step?

Since we're only estimating within a constant factor, we can choose! Make sure
\# steps is within a constant factor of total \# operations.

\emph{(Circle \texttt{i < len(l)}, draw arrow to that.)} 1 step.

\begin{itemize}
\item \( t_{LS} ((\texttt{[2, 4, 6, 8]}, 2)) = 1\)
\item \( t_{LS} ((\texttt{[2, 4, 6, 8]}, 8)) = 4\)
\item \( t_{LS} ((\texttt{[2, 4, 6, 8]}, 1)) = 4\)
\end{itemize}

Runtime usually longer for bigger inputs.

\( T_A (n) = \) running time on input of size \(n\)? Not well-defined.

Worst-case running time \( T_A (n) = \max \{ t_A (I) \mid \size(I) = n \} \).

E.g.\ \( T_{LS} (n) \in O(n) \).
\end{writelive}

Remember, we write big O because we're only estimating to within a constant
factor.
\end{document}
