\documentclass{article}

\newcommand{\steem}{\texttt{STEEM}}
\newcommand{\mytoken}{\texttt{MYTOKEN}}

\begin{document}

\section{Automated Market Makers for SMTs}

\section{Setup}

\subsection{Basic definitions}

In this article, we'll let $s$ represent a quantity of STEEM, let $t$
represent a quantity of some token (SMT), and let $p$ represent a price,
such that $pt$ is STEEM-valued (i.e. if MYTOKEN is trading at
$p = 0.05$ STEEM / MYTOKEN then $t = 120$ MYTOKEN has a value of
$pt = (0.05\ \mbox{STEEM / MYTOKEN}) \cdot (120\ \mbox{MYTOKEN}) = 6\ \mbox{STEEM}$.

Suppose we have a market maker (or any economic agent) with a two-asset
``portfolio'' (inventory) of $s$ STEEM and $t$ tokens.  If the price of
tokens is $t$, then we may measure of the value of this portfolio, in
units of STEEM, as $v(p, s, t) = s + pt$.

One common portfolio management policy is to require that STEEM
should be some constant fraction $r$ of the portfolio, i.e.
$s = r v(p, s, t)$ where $0 < r < 1$.  We call this policy the
\textit{constant portfolio ratio} or CPR policy, and the equation
$s = r v(p, s, t)$ is the \textit{CPR invariant}.

A different portfolio management policy, discussed by the Bancor
whitepaper, is called CRR or \textit{constant reserve ratio}.  To discuss CRR,
let us notate the total number of tokens in existence as $T$.  The
\textit{CRR invariant} is then defined as $s = r v(p, 0, T-t)$.

\subsection{Note on conventions}

We must discuss where our convention varies from the Bancor whitepaper.
At some times, when some user Alice interacts with the market maker,
Alice will remove some tokens from her balance to get STEEM from
the market maker's balance.  On the other hand, Bob may add some tokens
to his balance in exchange for sending STEEM to the market maker's balance.

Bancor takes the convention that in this example, the market maker \textit{destroys}
tokens in its interaction with Alice, and \textit{creates} tokens in its interaction
with Bob.  The Bancor convention suggests the market maker is not
an ordinary actor, but needs system-level ``special powers'' -- specifically,
the privilege to operate the token printing press -- in order to function.

In this paper, we adopt the convention that the tokens sent by Alice to the
market maker are not destroyed, but are instead added to the inventory (balance)
of the market maker.  Likewise, the tokens sent to Bob by the market maker
are not created out of thin air; they already exist and are merely
transferred from the inventory of the market maker to Bob.  Thus, we show
that the market maker is essentially an ordinary economic agent acting according
to a deterministic algorithm -- it doesn't actually need ``special powers''!

\section{Finite trades}

\subsection{Basic definitions}

A \textit{trade} is a change in the market maker's balance from
$(s, t) \to (s+\Delta s, t+\Delta t)$.  The \textit{price} at which the
trade occurs is defined as $p = {- \Delta s \over \Delta t}$.  We restrict
ourselves to \textit{well-formed trades} where either $\Delta s = \Delta t = 0$,
or $\Delta s$ and $\Delta t$ are both nonzero and have opposite sign.

Theorem:  A trade at price $p$ conserves value at price $p$.  More rigorously,
if $\Delta s, \Delta t$ represent a trade at price $p$, then
$v(p, s, t) = v(p, s + \Delta s, t + \Delta t)$.

Let us more rigorously define the market maker's \textit{state} as a tuple
$M = (s, t, T, r)$.  Given some price $p$, we may define the
\textit{restoring trade} at $p$ (also called a \textit{relaxing trade} or
a \textit{relaxation}) to be a trade which occurs at price $p$ and results in a
state that satisfies the CRR invariant.

\subsection{Computing the restoring trade}

The restoring trade consists of functions $\Delta s(M, p)$ and $\Delta t(M, p)$.
We may actually compute these functions from the definition of price and the
CRR invariant:

\begin{eqnarray*}
\Delta s & = & -p \Delta t \\
s + \Delta s & = & r v(p, 0, T-(t+\Delta t)) \\
\Rightarrow s - p \Delta t & = & r v(p, 0, T-t-\Delta t) \\
   & = & r p(T-t-\Delta t) \\
   & = & rpT - rpt - rp \Delta t \\
\Rightarrow r p \Delta t - p \Delta t & = & r p (T-t) - s \\
\Rightarrow \Delta t & = & {r p (T - t) - s \over rp - p } \\
                     & = & \left( {1 \over 1-r} \right) \left( {s \over p} - r (T-t) \right) \\
\Rightarrow \Delta s & = & - p \Delta t \\
   & = & \left( {1 \over 1-r} \right) \left( r p (T - t) - s \right)
\end{eqnarray*}

\subsection{Computing the equilibrium price}

Given a state $M$, there exists some price $p_{eq}(M)$ for which the restoring
trade is zero; call this price the \textit{equilibrium price}.  We may compute
the equilibrium price by setting $\Delta s = 0$:

\begin{eqnarray*}
\Delta s & = & 0 \\
& = & \left( {1 \over 1-r} \right) \left( r p_{eq} (T - t) - s \right) \\
\Rightarrow r p_{eq} (T - t) - s & = & 0 \\
\Rightarrow r p_{eq} (T - t) & = & s \\
\Rightarrow p_{eq} & = & {s \over r (T-t)}
\end{eqnarray*}

Theorem:  Relaxation is idempotent.  That is, after relaxing at price $p$,
the equilibrium price of the resulting state is $p$, and a second relaxation
at price $p$ will be a zero trade.

\subsection{Example}

Example:  Suppose $M = (1200, 3600, 12000, 0.25)$ and $p = 0.5$.  Then of the
$T = 12000$ TOKEN in existence, $t = 3600$ TOKEN is held by the MM, so
$T-t = 12000 - 3600 = 8400$ TOKEN are ``circulating'' (i.e. exist in balances
outside the MM).  These circulating tokens are worth $p(T-t) = 4200$ STEEM
total, so they ``should be'' backed by a target reserve level of
$rp(T-t) = 4200 * 0.25 = 1050$ STEEM.

In this example, there is ``too much'' STEEM in the reserve, so relaxation
will buy tokens in the market.  This sale will cause two effects:   It will
decrease the reserve STEEM, and also decrease circulating tokens.  The decrease
in circulating tokens, in turn, causes the target reserve level to decline.  For
every 1 STEEM used to buy tokens, the target reserve level declines by $r$ STEEM;
since $r < 1$ eventually the declining reserve will ``catch up'' to its more slowly
declining target level.

The above algebra shows that we will catch up at
$\Delta s = \left( {1 \over 1-r} \right) \left( r p (T - t) - s \right)$ and
$\Delta t = \left( {1 \over 1-r} \right) \left( {s \over p} - r (T - t) \right)$.
Running the calculations with the numbers defined in this example gives
$\Delta s = -200$ STEEM and $\Delta t = 400$ TOKEN.

Let's check that these computed values $\Delta s = -200, \Delta t = 400$
(a) represent a trade with price $0.5$, and (b) that the CRR invariant holds
for the new state $M_{new} = (s+\Delta s, t+\Delta t, T, r)$.  Calculating
$p = {- \Delta s \over \Delta t}$ we indeed get $p = 0.5$.  After this trade executes,
the market maker has $s_{new} = s + \Delta s = 1200 - 200 = 1000$ STEEM, and
$t_{new} = t + \Delta t = 3600 + 400 = 4000$ tokens.

To check condition (b), that the CRR invariant holds, we effectively repeat the
analysis in the initial paragraph of this example with the new numbers.  We know
$M_{new} = (1000, 4000, 12000, 0.25)$ and $p = 0.5$.  Then of the $T = 12000$
TOKEN in existence, $t_{new} = 4000$ TOKEN is now held by the MM, so
$T-t_{new} = 12000 - 4000 = 8000$ TOKEN are now circulating.  These circulating
tokens are worth $p(T-t_{new}) = 4000$ STEEM total, so they ``should be'' backed
by a target reserve level of $rp(T-t) = 4000 * 0.25 = 1000$ STEEM.  Since the
target reserve level indeed exactly matches the actual reserve level of
$s_{new} = 1000$ STEEM, we conclude that the CRR invariant is satisfied after
this relaxing trade.

\section{Infinitesimal trades}

This section is fairly technical; the reader will need a good grasp of
calculus and differential equations to follow the results.

\subsection{Setting up the problem}

Suppose we satisfy the invariant condition at some price
$p = p_eq$; by the CRR invariant $s = r v( p, 0, T-t) = r p (T-t)$.
Suppose the price then increases to $p + \Delta p$ and a relaxing trade
$\Delta s, \Delta t$ occurs at this new price.

In this section we consider the limiting situation where $\Delta p$ is
infintesimally small, so we will use Leibniz notation ($dp$ for a small change
in $p$, $ds$ for a small change in $s$, $dt$ for a small change in $t$).

\subsection{Solving the DE's}

By applying the substitution $p \gets p + dp$ to the expression for $\Delta s$
computed in the previous section, we obtain an expression which simplifies
to a separable DE which can be solved:

\begin{eqnarray*}
ds & = & {1 \over 1-r} \left( r (p + dp) (T - t) - s \right) \\
   & = & {1 \over 1-r} \left( r p (T - t) + r dp (T - t) - r p (T - t) \right) \\
   & = & {1 \over 1-r} r (T - t) dp \\
   & = & {1 \over 1-r} \left( {s \over p} \right) dp \\
\Rightarrow {1 \over p} dp & = & (1-r) \left( {1 \over s} ds \right) \\
\Rightarrow \int {1 \over p} dp & = & (1-r) \int {1 \over s} ds \\
\Rightarrow \ln(p) & = & (1-r) \ln(s) + C_0 \\
\Rightarrow p & = & k_0 s^{1-r}
\end{eqnarray*}

Similarly for $t$, we can start from $dt = -ds / p$ and again obtain and solve
a separable DE:

\begin{eqnarray*}
dt & = & -ds / p \\
   & = & -{r \over 1-r} (T - t) dp / p \\
\Rightarrow {1 \over T-t} dt & = & -{r \over 1-r} \left( {1 \over p} \right) dp \\
\Rightarrow {1 \over p} dp & = & {1-r \over r} \left( {1 \over t-T} \right) dt \\
\Rightarrow \int {1 \over p} dp & = & {1-r \over r} \int {1 \over t-T} dt \\
\Rightarrow \ln(p) & = & {1-r \over r} \ln | t-T | + C_1 \\
\Rightarrow p & = & k_1 (T-t)^{1-r \over r}
\end{eqnarray*}

\section{Qualitative discussion}

In a CRR market maker, where does the ``backing'' for newly emitted tokens come from?

One option is to lower the reserve ratio $r$.  This option results in no immediate market
activity, but will weaken the response of the market maker to any future price changes.
This is called the ``pay later'' option.

Another option is to change the dynamical system's initial conditions, i.e. edit the
constants of integration.  This option will cause the equilibrium price $p_{eq}$ to drop,
meaning the market maker will more aggressively sell tokens to replenish the reserve.
If order books are deep compared to the amount of emission, and there are adequate buyers
for the tokens, then the sales will be able to replenish the reserve and keep the equilibrium
price near its old value; the deep order books provide resistance to the price change being
driven by the market maker.  If order books are thin compared to the amount of emission,
and there are few/no buyers for the tokens, then the equilibrium price will fall, breaking
through the thin orders and lowering the market price.  Even though few/no many tokens were
sold, so even though the \textit{absolute} amount of STEEM in the reserve is still
nearly/exactly the same as before, the reserve's value \textit{relative} to the now-lower
market cap of the token has increased to the reserve ratio.  This option is the ``pay now''
option.

\section{FAQ}

FAQ

Q:  Where are the pretty pictures and graphs?

A:  They're being worked on right now, and will exist in a future version of this paper.

Q:  What is the relevance of constant portfolio ratio policy?

A:  It may become a supported market maker policy in the future.

Q:  Can the reserve ratio go over 100 percent?

A:  No.

Q:  Can the reserve ratio be exactly 100 percent?

A:  Not with the system described in this paper.  It might be possible to code as a special case.

Q:  In a CRR market maker, where does the ``backing'' for newly emitted tokens come from?

A:  As blockchain designers, we have two options for sourcing the ``backing''.  One option
is to lower the reserve ratio $r$.  This option results in no immediate market
activity, but will weaken the response of the market maker to any future price changes.
This is called the ``pay later'' option.

Another option is to change the dynamical system's initial conditions, i.e. edit the
constants of integration.  This option will cause the equilibrium price $p_{eq}$ to drop,
meaning the market maker will more aggressively sell tokens to replenish the reserve.
If order books are deep compared to the amount of emission, and there are adequate buyers
for the tokens, then the sales will be able to replenish the reserve to its target level while
keeping the equilibrium price near its old value.  The deep order books provide resistance to
the price change being driven by the market maker.

If order books are thin compared to the amount of emission, and there are few/no buyers for the
tokens, then the equilibrium price will fall, breaking through the thin orders and lowering
the market price.  Even though few/no many tokens were sold, so even though the
\textit{absolute} amount of STEEM in the reserve is still nearly/exactly the same as before,
the reserve's value \textit{relative} to the now-lower market cap of the token has increased
to the reserve ratio.  This option is the ``pay now'' option.

Q:  Where's the ``don't pay'' option?

A:  You have to come up with some answer to where the ``backing'' for newly emitted
tokens will come from.  Unless there's no emission.  Or unless there's no ``backing'' for
any tokens.  So the ``don't pay'' option would be to have an SMT with either no
emission, or no market maker.

Q:  Don't fractional exponents require floating point to implement?

A:  Only if you need fairly high precision (we don't), don't care about bit-for-bit
reproducibility across compilers, OS's, CPU's, etc (we do), and need to do massive
numbers of calculations quickly (we don't).  A fast, approximate, all-integer
implementation is possible.

Q:  Does this market maker interact with the order book through the existing limit order
system, or is it a separate set of operations?

A:  In theory, it could be implemented either way.  However, the likely outcome is that the market maker will be implemented outside of order-book markets to allow its code to be modularized.  In practice, if implemented as a completely separate subsystem, people will run arbitrage bots which will trade away any price differences
between the reserve system and the existing market system.

Q:  Where do the market maker's initial token balances come from?

A:  ICO units can specify the market maker as a destination.  An ICO creator
may direct a percentage of ICO's STEEM contributions to the MM by specifying
the market maker similarly to specifying a founder.  Or may use the soft cap
system to specify all STEEM above a pre-determined amount goes to the ICO.  Likewise,
a fixed or percentage amount of tokens can be added in the ICO to increase the MM's
token balance.

Q:  Can someone send STEEM or tokens to the market maker?

A:  Yes.

Q:  What are the side effects of sending STEEM or tokens to the market maker?

A:  The constants of integration are re-initialized, meaning the equilibrium price will
change.  The market maker will become more aggressive about selling the asset.

Q:  Can't this cause manipulation or appropriating the market maker's inventory
to private profit?

A:  Sending assets to the market maker does cause it to engage in trading activity
which affects the price.  However, dumping an identical amount on the market will
result in a larger amount of trading activity and a larger effect on the price.  If
Eve is willing to spend her tokens/STEEM to manipulate prices, she would prefer
the strategy of simply dumping tokens/STEEM on the market, as that strategy is more
cost-effective for her.

Q:  Does the market maker's activity generate profits (losses)?

A:  It depends on how you measure ``profits.'' If you measure the value of STEEM
and tokens in some external third currency such as US dollars or bitcoins, the
market maker's inventory, valued in that currency, can definitely increase or
decrease.  If people voluntarily send STEEM or tokens to the market maker,
such activity definitely increases the value of the market maker regardless of
your measurement.

Another way to define profits is by the constants of integration.  If both
of the constants of integration increase, or one increases while the other
remains the same, a tiny increase occurs with each trade when the market
maker is in ``taker'' mode.

Q:  What is ``taker'' mode?  How can a market maker be set to operate
in ``taker'' mode?

A:  When orders execute, the order used to set the price is called the maker;
the maker's counterparty is the taker.  In the STEEM on-chain market (and on
almost all trading platforms) the older order is always the maker.

When the market maker is in taker mode, its actions are always considered to be
taker orders, which execute at the price specified by the user acting as its
counterparty -- this price is always at least a little bit more favorable than
the market maker is willing to accept.  When the market maker is not in taker
mode, its actions are always considered to be maker orders, which don't
generate changes in the constants of integration.

Taker mode is a runtime parameter that can be set by the SMT's control account.

Q:  Who benefits from the profits of a market maker in taker mode?

A:  Maybe nobody, or maybe everybody.  It's decentralized.

Q:  OK, if my SMT reaches a steady price, the STEEM in the reserve is basically
locked up forever.  That seems not cool.  How do I set it up so that this STEEM
can be unlocked for the benefits of my SMT users?

A:  Set the DRR (decaying reserve ratio) setup parameter.  If you set DRR, then the
reserve ratio will slowly drop over time to a pre-set value, using its excess STEEM
reserves to buy excess tokens.  Setting DRR is an excellent, fair, decentralized
way to return excess capitalization to contributors in a more-popular-than-anticipated
ICO that raises more than the sponsor can effectively spend.

Q:  If the reserve ratio can change over time due to pay-later emissions or DRR,
it's not really a constant reserve ratio, is it?

A:  No, they're not.  The reserve ratio's called ``constant'' because it's constant
over the short-term, in normal conditions, or in the conditions in Bancor which is
where it was named.  But the name could be regarded as slightly misleading.

Q:  If the constants of integration can change over time, they're not really
constants either, are they?

A:  No, they're not.  They're called constants of integration because that's their
mathematical role in the calculation that introduces them.  Maybe they'll be
differently named in a future version of this paper.

Q:  Can I specify a contribution to a DRR to be a pay-later contribution, that
\textit{increases} its reserve ratio, the increase to be eventually negated over
time by future decay?  Why would I want to?

A:  Yes.  This is effectively contributing to the market maker,
subject to the condition that it's not allowed to immediately dump a portion
of the contribution.  It's useful if you want to make a large contribution
to a market maker without causing it to create a disturbance by immediately
dumping a significant fraction of your contribution onto the market.

Q:  Can I specify a DRR with emission to use pay-later for emissions when the
RR is decaying?

A:  Yes.

Q:  Is the market maker specified here equivalent to a Bancor token changer?

A:  No.  A Bancor token changer has multiple reserve ratios that must sum to
one hundred percent, and involves a third token that effectively represents
equity in the token changer.  This paper's market maker has none of these
features.

Q:  I want to have an initial ``price discovery'' period where people trade
without action from the market maker, then have tokens and STEEM from
the ICO gradually flow in over time to the market maker so it has a delayed,
slow start from zero to full power.  Can I do it?

A:  This is called ``gradual seeding'' and it may be supported.

Q:  What about numerical stability?

A:  A market maker will be restricted to only operate when its
balances exceed a certain minimum for both assets.  Also, reserve
ratios will be restricted to a certain range, all the mechanisms
that can set / increase / decrease a reserve ratio will be restricted
to not allow it to move outside the range.  Tentative numerical
experiments suggests these limits should be about 10,000 satoshis
of both assets, 5 percent and 50 percent, respectively.  These
values are subject to change based on future experimentation,
worst-case analysis, and testing.

\end{document}


% Preamble.
\documentclass[12pt]{article}
\usepackage[margin=1.25in]{geometry}
\usepackage[fleqn]{amsmath}
\usepackage{textcomp}
\usepackage{gensymb}
\usepackage{amsfonts}
\usepackage{enumitem}
\usepackage{siunitx}
%\usepackage{subfiles}  % Include for subfiles.
%\usepackage{tikz}  % Include for handmade figures.
\usepackage{graphicx}  % Include for external figures.

%% Title macros.
\newcommand{\HOMEWORKNUM}{1}
\newcommand{\NAME}{D. Choi}
\newcommand{\DATE}{2020-08-28}

\title{\vspace{-2\baselineskip}PHYS 225 - Written Homework \#\HOMEWORKNUM}
\author{\NAME}
\date{\DATE}

%% Formatting options.
%\pagenumbering{gobble}  % Include for single-page document.

% Document.
\begin{document}
\maketitle

\section*{1.}
\textit{Consider the vectors $\vec{A} = -2\hat{\imath} - 2\hat{\jmath}$ and
$\vec{B} = \hat{\imath} + 3\hat{\jmath}$.}
\begin{enumerate}[label=(\alph*)]
	\item \textit{Find the unit vectors
	$\hat{A} = \frac{\vec{A}}{\|\vec{A}\|}$ and
	$\hat{B} = \frac{\vec{B}}{\|\vec{B}\|}$.}
	\begin{gather*}
		\hat{A}
		= \frac{\vec{A}}{\|\vec{A}\|}
		= \frac{-2\hat{\imath} - 2\hat{\jmath}}{2\sqrt{2}}
		= \boxed{
			-\frac{1}{\sqrt{2}} \hat{\imath}
			- \frac{1}{\sqrt{2}} \hat{\jmath}
		}, \\
		\hat{B}
		= \frac{\vec{B}}{\|\vec{B}\|}
		= \frac{\hat{\imath} + 3\hat{\jmath}}{\sqrt{10}}
		= \boxed{
			\frac{1}{\sqrt{10}} \hat{\imath}
			+ \frac{3}{\sqrt{10}} \hat{\jmath}
		}.
	\end{gather*}
	\item \textit{Express the vector $\vec{C} = 5\hat{\imath} - 3\hat{\jmath}$
	in terms of $\hat{A}$ and $\hat{B}$.}
	\begin{gather*}
		\vec{C} = \hat{A}x + \hat{B}y, \\
		\begin{pmatrix} 5 \\ -3 \end{pmatrix}
		=
		\begin{pmatrix}
			-\frac{1}{\sqrt{2}} & \frac{1}{\sqrt{10}} \\
			-\frac{1}{\sqrt{2}} & \frac{3}{\sqrt{10}}
		\end{pmatrix}
		\begin{pmatrix} x \\ y \end{pmatrix}.
	\end{gather*}
	Solving for $x$ and $y$,
	\begin{equation*}
		\begin{pmatrix} x \\ y \end{pmatrix}
		= \begin{pmatrix} -9\sqrt{2} \\ -4\sqrt{10} \end{pmatrix}.
	\end{equation*}.
	Thus,
	\begin{equation*}
		\vec{C} = \boxed{
			-9\sqrt{2}\hat{A} - 4\sqrt{10}\hat{B}
		}.
	\end{equation*}
\end{enumerate}

\section*{2.}
\textit{A particle moves in one dimension with a velocity
$v(t) = v_0 + v_1 t^3$, where $v_0 = 2.00\si{\frac{m}{s}}$ and
$v_1 = 1.00\si{\frac{m}{s^4}}$.}
\begin{enumerate}[label=(\alph*)]
	\item \textit{Assuming that at $t_0 = 0$, the particle is at position
	$x_0 = 4.00\si{m}$ measured from the origin, derive a formula for the
	particle's position as a function of time.}
	\begin{gather*}
		v(t) = v_1 t^3 + v_0, \\
		x(t)
		= \int v(t) dt
		= \boxed{
			\frac{v_1}{4}t^4 + v_0 t + x_0
		}.
	\end{gather*}
	\item \textit{Obtain a formula for the acceleration of the particle.}
	\begin{equation*}
		a(t)
		= \frac{d}{dt} v(t)
		= \boxed{
			3v_1 t^2
		}.
	\end{equation*}
	\item \textit{Make quantitative plots of the particle's position,
	velocity, and acceleration as a function of time during the interval
	$t_0 = 0$ and $t_1 = 1.00\si{s}$.}
	\begin{center}
		\includegraphics[width=0.6\textwidth]{fig.pdf}
	\end{center}
	\newpage
	\item \textit{What is the particle's average velocity and acceleration in
	the time interval $t_0 = 0$ to $t_1 = 1.00\si{s}$?} \\[\baselineskip]
	For the interval $[0, 1.00\si{s}]$, let $v_{avg}$ and $a_{avg}$ be the
	particle's average velocity and average acceleration over the interval
	respectively.
	\begin{gather*}
		v_{avg}
		= \frac{x(1.00\si{s}) - x(0)}{1.00\si{s} - 0}
		= \frac{6.25\si{m} - 4.00\si{m}}{1.00\si{s} - 0}
		= \boxed{2.25\si{\frac{m}{s}}}, \\
		a_{avg}
		= \frac{v(1.00\si{s}) - v(0)}{1.00\si{s} - 0}
		= \frac{3.00\si{\frac{m}{s}} - 2.00\si{\frac{m}{s}}}{1.00\si{s} - 0}
		= \boxed{1.00\si{\frac{m}{s^2}}}.
	\end{gather*}
	\item \textit{What is the particle's position at the end of this time
	interval (at $t_1 = 1.00\si{s}$)?}
	\begin{align*}
		x(1.00\si{s})
		&= \frac{v_1}{4}(1.00\si{s})^4 + v_0 (1.00\si{s}) + x_0 \\
		&= {
			\frac{1.00\si{\frac{m}{s^4}}}{4}(1.00\si{s})^4
			+ 2.00\si{\frac{m}{s}} (1.00\si{s})
			+ 4.00\si{m}
		} \\
		&= \boxed{6.25\si{m}}
		.
	\end{align*}
\end{enumerate}

\end{document}


\section{Dijkstra's Algorithm as Framework}

%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Dijkstra's algorithm}
  \[
	\dist(v) = \min_{u \in N(v)} \set{\dist(u) + l(u,v)}
  \]

  \begin{columns}
	\column{0.35\textwidth}
	  \begin{algorithmic}
		\ForAll{$v \in V$}
		  \State $\dist[v] \gets \infty$
		\EndFor

		\State $\dist[s] \gets 0$
		\Statex
		\State $Q \gets \text{MinPQ}(V)$
	  \end{algorithmic}
	\column{0.60\textwidth}
	  \begin{algorithmic}
		\While{$Q \neq \emptyset$}
		  \State $u \gets \text{deleteMin}(Q)$
		  \ForAll{$(u,v) \in E \land v \in Q$}
			\If{$\dist[v] > \dist[u] + l(u,v)$}
			  \State $\dist[v] \gets \dist[u] + l(u,v)$
			  \State $\text{decreaseKey(Q, v)}$
			\EndIf
		  \EndFor
		\EndWhile
	  \end{algorithmic}
  \end{columns}

  \vspace{0.50cm}
  \[
	O(n + (n+m) \log n) \implies O((n + m) \log n) \implies O(m \log n)
  \]
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Dijkstra's algorithm}
  \begin{columns}
	\column{0.45\textwidth}
	  Prim's algorithm for MST:
	  \begin{algorithmic}
		\If{$\text{cost}[u] > w(u,v)$}
		  \State $\text{cost}[u] \gets w(u,v)$
		\EndIf
	  \end{algorithmic}
	\column{0.45\textwidth}
	  BFS:
	  \begin{algorithmic}
		\State $Q \gets \text{FIFO-Q}(s)$
		\Statex
		\If{$\dist[v] = \infty$}
		  \State $\dist[v] \gets \dist[u] + 1$
		\EndIf
	  \end{algorithmic}
  \end{columns}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Unique shortest paths}
  \begin{exampleblock}{Unique shortest paths (Problem 6.18)}
	  Undirected graph $G = (V, E), w(e) > 0, s \in V$:
	  \[
		\text{usp}[v] = T \iff \exists! s \leadsto^{\SP} v
	  \]
  \end{exampleblock}

  \begin{columns}[t]
	\column{0.30\textwidth}
	  \begin{algorithmic}
		\ForAll{$v \in V$}
		  \State $\text{usp}[v] \gets F$
		\EndFor
		\State $\text{usp}[s] \gets T$
	  \end{algorithmic}
	\column{0.60\textwidth}
	  \begin{algorithmic}
		\If{$\dist[v] > \dist[u] + l(u,v)$}
		  \State $\dist[v] \gets \dist[u] + l(u,v)$
		  \State $\text{usp}[v] \gets \text{usp}[u]$
		\ElsIf{$\dist[v] = \dist[u] + l(u,v)$}
		  \State $\text{usp}[v] \gets F$
		\EndIf
	  \end{algorithmic}
  \end{columns}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Number of shortest paths}
  \begin{exampleblock}{Number of shortest paths (Problem 6.31, 5.26)}
	\[
	  \# s \leadsto^{\SP} v
	\]

	\[
	  O(n + m) \text{\emph{ if }} w(e) = 1
	\]
  \end{exampleblock}

  \[
	\text{Num}[s] = 1 \text{\emph{ vs. }} \text{Num}[s] = 0
  \]
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Min-max path problem}
  \begin{exampleblock}{Min-max path problem (Problem 6.23)}
	\begin{itemize}
	  \item $G = (V, E)$: network of highways
	  \item $l_e$: road length; $L$: tank capacity
	  \item (1) Given $L$, $\exists? s \leadsto t$.
	  \item (2) Given $G$, compute $\min L$.
	\end{itemize}
  \end{exampleblock}

  \[
	L[v] = \min_{u \in N(v)} \max \set{L[u], l(u,v)}
  \]

  \begin{columns}
	\column{0.35\textwidth}
	  \begin{algorithmic}
		\ForAll{$v \in V$}
		  \State $L[v] \gets \infty$
		\EndFor
		\State $L[s] \gets 0$
	  \end{algorithmic}
	\column{0.65\textwidth}
	  \begin{algorithmic}
		\If{$L[v] > \max(L[u], l(u,v))$}
		  \State $L[v] \gets \max(L[u], l(u,v))$
		\EndIf
	  \end{algorithmic}
  \end{columns}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Min-max path problem}
  \begin{exampleblock}{Min-max path problem (Problem 6.23)}
	\begin{itemize}
	  \item $G = (V, E)$: network of highways
	  \item $l_e$: road length; $L$: tank capacity
	  \item (1) Given $L$, $\exists? s \leadsto t$.
	  \item (2) Given $G$, compute $\min L$.
	\end{itemize}
  \end{exampleblock}

  \vspace{0.50cm}
  \centerline{$O(\log m)$ binary searches for $\min L$}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Max-min path problem}
  \begin{exampleblock}{Max-min path problem (Problem 6.26)}
	\begin{itemize}
	  \item $G = (V, E)$: network of oil pipelines
	  \item $c(u,v)$: capacity of $(u,v)$
	  \item (1) Given $s$, compute $\text{cap}(s, v)$.
	  \item (2) Compute all-pair $\text{cap}(u,v)$.
	\end{itemize}
  \end{exampleblock}

  \[
	\text{cap}[v] = \max_{u \in N(v)} \min (\text{cap}[u], c(u,v))
  \]

  \[
	Q \gets \text{MaxPQ}(V)
  \]
\end{frame}
%%%%%%%%%%%%%%%%%%%%


%!TEX root = ../CombinatoricsNotes.tex

\section{Convexity}
We will consider finite collections of points in Euclidean space, $\R^d$. Let's recall some definitions.
\begin{itemize}[]
\item[Linear space:] $L\subset \R^d$ is a \defn{linear space}[linear!space] if it is closed under addition and multiplication by scalars.
\item[Linear dependence:] We say $v_1,v_2,\dotsc,v_n$ are \defn{linearly dependent}[linear!dependence] if there exist $\alpha_1,\alpha_2,\dotsc,\alpha_n\in \R$ not all zero such that
\[
\alpha_1 v_1 + \alpha_2v_2 + \dotsc + \alpha_n v_n = 0.
\]
\item[Linear hull:] The \defn{linear hull}[linear!hull] $\braket{S}$ of $S\subset \R^d$ is the smallest linear subspace containing $S$, i.e. $\braket{S}=\bigcap_{L\supset S} L$ where the intersection is taken over linear subspaces $L$. Equivalently,
\begin{align*}
\braket{S} = \{ \alpha_1 v_1 + \alpha_2 v_2 + \dotsc + \alpha_n v_n: \forall n,\, v_1,\dotsc,v_n\in S,\, \alpha_1,\dotsc,\alpha_n\in \R \}.
\end{align*}
Note that we may take $n = d$.
% , because any $d+1$ vectors in $S\subset \R^d$ will be linearly dependent.}

\item[Affine subspace:] An \defn{affine subspace}[affine!subspace] is a subset of $\R^d$ of the form $v+ L$ where $L$ is a linear subspace and $v\in \R^d$.

\item[Affine hull:] The \defn{affine hull}[affine!hull] $\aff(S)$ of $S\subset \R^d$ is the intersection of all affine subspaces containing $S$. Equivalently,
\[
\aff(S) = \{\alpha_1 v_1 + \alpha_2 v_2 + \dotsb + \alpha_n v_n : \sum_i \alpha_i= 1,\, v_1,\dotsc,v_n\in S\}.
\]
Note: if $w\in v+L$, then $w-v\in L$, so $w+L = v+L$.

\item[Affine dependence:]A set of vectors $\{v_1,\dotsc,v_n\}$ are \defn{affinely dependent}[affine!dependence] if
\[
\alpha_1 v_1 + \dotsb + \alpha_n v_n = 0
\]
for some $\sum_{i=1}^n\alpha_i=0$ with some $\alpha_i \neq 0$. This is equivalent to one of the vectors belongs to the affine hull of the other vectors.
\begin{remark}
The maximum number of affinely independent vectors in $\R^d$ is $d+1$. Why? Any $d+2$ vectors in $\R^d$ are affinely dependent. Suppose we have $v_1,\dotsc,v_{d+2}$. Then WLOG $v_1,\dotsc,v_d$ form a basis of $\R^d$. Then $\alpha_1 v_1 + \dotsb + \alpha_d v_d = v_{d+1}$ for some choice of $\alpha_i$'s. Likewise, $\beta_1 v_1  +\dotsb + \beta_d v_d = v_{d+2}$. Let $\alpha = \sum_i \alpha_i$ and $\beta = \sum_i \beta_i$. So
\begin{align*}
\alpha_1 v_1 + \dotsb + \alpha_d v_d - v_{d+1} &= 0\\
\beta_1 v_1 + \dotsb + \beta_d v_d - v_{d+2} &= 0.
\end{align*}If $\alpha=1$ or $\beta=1$, then we are done. Otherwise,
\begin{align*}
(\beta-1)(\alpha_1 v_1 + \dotsb + \alpha_d v_d - v_{d+1}) - (\alpha-1)(\beta_1 v_1 + \dotsb + \beta_d v_d - v_{d+2} ) &= 0
\end{align*}
which demonstrates affine dependence, as the sum of coefficients is $(\beta-1)(\alpha-1) - (\alpha-1)(\beta-1)=0$.
\end{remark}

\item[Convex:] A set $C\subset \R^d$ is \defn{convex} if for any two points $v_1,v_2\in C$, the interval joining these two points lies in $C$. That is, if $v_1,v_2\in C$, then $\{\alpha v_1 + (1- \alpha)v_2: 0\leq \alpha \leq 1\} \subset C$.
\item[Convex hull:]  For $X\subset \R^d$, the \defn{convex hull}[convex!hull] $\conv(X)$ is the intersection of all convex sets containing $X$. Then
\[
\conv(X) = \{\alpha_1 v_1 + \dotsc + \alpha_n v_n:n\in \N,\, v_1,\dotsc,v_n\in X, \sum_i \alpha_i =1, \alpha_i\geq 0 \}
\]
Note that $\alpha_1 v_1 + \dotsc + \alpha_n v_n$ where $\alpha_i\geq 0$ and $\sum_i \alpha_i=1$ is called a \defn{convex combination}[convex!combination] of $v_1,\dotsc,v_n$.

To prove this formula,  we note by induction on $n$ we see that any convex combination of $n$ vectors in $X$ is in $\conv(X)$: wlog $\alpha_n\neq 0$, $\alpha_n\neq 1$. Then
\[
\alpha_1 v_1 + \dotsc + \alpha_n v_n = (1- \alpha_n) \underbrace{\left[ \frac{\alpha_1}{1- \alpha_n}v_1 + \dotsc + \frac{\alpha_{n-1}}{1 - \alpha_n} v_{n-1} \right]}_{\in \conv(X)} + \alpha_n \underbrace{ v_n.}_{\in \conv(X)}
\]
So this set of convex combinations is a subset of $\conv(X)$. But since the set of convex combinations is itself convex, as it is easy to check, we have equality.

\item[Convex dependence:]We say $X\subset \R^d$  is \defn{convex dependent}[convex!dependence] if for some $x\in X$ we have $x\in \conv(X\setminus \{x\})$. \marginnote{That is, one vector in $X$ is a convex combination of some of the others.}

We say $X$ is \defn{convex independent}[convex!independence] if it is not convex dependent.

\begin{remark}
No point on the boundary of a circle is a convex combination of the other points, so there is no bound on the size of convex independent sets.
\end{remark}

\end{itemize}

\begin{theorem}[\cite{Radon1921}]
Let $A\subset \R^d$ be finite, with $|A| \geq d+2$. Then there exist disjoint $A_1,A_2\subset A$ such that $\conv(A_1)\cap \conv(A_2)\neq 0$.

\end{theorem}
\begin{remark}
Let's first consider the case $d=2$, $|A| =4$. If three points do not fall on a line (a degenerate case), then there are two cases:
\begin{figure}
\hfill
\begin{tikzpicture}[scale=1.5]
\coordinate (a) at (0,0);
\coordinate (b) at (1.5,0);
\coordinate (c) at (.75,1);

\coordinate (d) at (0.5,.3);
% \draw[green] (a) -- (d) --;
% \draw[blue] (b) -- (c);
\filldraw[LightBlue] (a) -- (b) -- (c) -- cycle;
\filldraw[black] (a) circle (1pt);
\filldraw[black] (b) circle (1pt);
\filldraw[black] (c) circle (1pt);
\filldraw[black] (d) circle (1pt);

\end{tikzpicture}
\hfill
\begin{tikzpicture}[scale=1.5]
\coordinate (a) at (0,0);
\coordinate (b) at (0,1);
\coordinate (c) at (1.25,0);
\coordinate (d) at (1.25,1);
\draw[green] (a) -- (d);
\draw[blue] (b) -- (c);

\filldraw[black] (a) circle (1pt);
\filldraw[black] (b) circle (1pt);
\filldraw[black] (c) circle (1pt);
\filldraw[black] (d) circle (1pt);

% \node at (a) {\textbullet};

\end{tikzpicture}
\hfill
\caption{Four points in $\R^2$, when no three form a line. Either one is in the convex hull of the other three (left), or we may divide into pairs so that the convex hulls intersect (right).}\label{fig:Radon_example_d2}
\end{figure}
In either case, we may verify Radon's theorem.
% \missingfigure{BLue Triangle with dot in middle}
\end{remark}

\begin{proof}
WLOG, we may take $|A|=d+2$; any surplus points we could put in $A_1,A_2$, or neither, without changing the result. Set $A=  \{v_1,v_2,\dotsc,v_{d+2}\}$. Since $v_1,v_2,\dotsc,v_{d+2}$ are affinely dependent\sidenote{since there are more than $d+1$ of them}, there exist $\alpha_1,\dotsc,\alpha_{d+2}$ with $\sum_i \alpha_i=0$ not all zero such that
\[
\alpha_1v_1 + \dotsb + \alpha_{d+2} v_{d+2} = 0.
\]
Let $A_1 = \{v_i : \alpha_i > 0\}$ and $A_2 = \{ v_i: \alpha_i < 0\}$. WLOG, $\alpha_1,\dotsc,\alpha_k > 0$ and $\alpha_{k+2},\dotsc,\alpha_{d+2} < 0$. Let $s = \alpha_1 + \dotsb + \alpha_k = - \alpha_{k+1} - \dotsb - \alpha_{d+2}$.
Then
\begin{align*}
\left( \frac{\alpha_1}{s} \right) v_1 + \dotsb + \left( \frac{\alpha_k}{s} \right) v_{k+1} = \left( - \frac{\alpha_{k+1}}{s} \right)v_{k+1} + \dotsb + \left( - \frac{\alpha_{d+2}}{s} \right)v_{d+2}.
\end{align*}
But as shown by the LHS, this quantity lies in $\conv(A_1)$, and as shown by the RHS, the quantity lies in $\conv(A_2)$.
\end{proof}

\begin{theorem}[\cite{Caratheodory1911}]
Every point in $\conv(X)$ for $X\subset \R^d$ is a convex combination of at most $d+1$ points in $X$.
\end{theorem}
\begin{proof}
Let $x\in \conv(X)$ be written $x = \alpha_1 v_1 + \dotsb + \alpha_n v_n$ for $v_1,\dotsc,v_n\in X$ and $\sum_i \alpha_i=0$ with $\alpha_i\geq 0$ not all zero. Suppose is $n$ is chosen minimally such that a convex combination exists. Suppose $n\geq d+2$ for the sake of contradiction. Then by Radon's theorem, wlog
\[
\conv(\{v_1,\dotsc,v_k\})\cap \conv(\{v_{k+1},\dotsc, v_n \}).
\]
So, for some $\beta_1,\dotsc,\beta_n\geq 0$,
\[
\beta_1 v_1 + \dotsb \beta_k v_k = \beta_{k+1} v_{k+1} + \dotsb + \beta_n v_n
\]
with $\sum_{i=1}^k \beta_i = \sum_{i=k+1}^n \beta_i = 1$. Then
\vspace{-\baselineskip}
\begin{fullwidth}
\begin{align}
x &= (\alpha_1 + \epsilon \beta_1) v_1 + \dotsb + (\alpha_k + \epsilon \beta_k)v_k + (\alpha_{k+1} - \epsilon \beta_{k+1})v_{k+1}+ \dotsb +  (\alpha_n - \epsilon \beta_n)v_n \tag{$\star$} \label{eq:Cara_proof}
\end{align}
\end{fullwidth}
has the sum of coefficients one for every $\epsilon$. If $\epsilon>0$ is minimally such that $\alpha_i - \epsilon \beta_i=0$ for some $i$, then the expression \eqref{eq:Cara_proof} is a convex combination of $\{v_1,\dotsc,v_n\}\setminus \{v_i\}$, giving a contradiction.
\end{proof}
\begin{theorem}[\cite{Helly1923}]
Let $C_1,C_2\dotsc,C_n$ be a collection of convex sets in $\R^d$. If $\bigcap_{i\in I} C_i \neq \emptyset$ for every $I\subset [n]$ with $|I| = d+1$, then $\bigcap_{i=1}^n C_i \neq \emptyset$.
\end{theorem}
\begin{remark}
For $\R^1$, if we consider \marginnote{Convex sets in $\R^1$ may be open or closed intervals or rays, but we will take closed intervals for simplicity.}$C_i = [a_i,b_i]$, then if each $C_i\cap C_j\neq \emptyset$, then we need $a_i \leq b_j$ for each $i,j$. Then $[\max_i a_i, \min_i b_i] \subset C_k$ for all $k$.
\end{remark}
\begin{proof}[Proof by induction on $n$.]
Let us postpone the base case, $n=d+2$.
For the induction step, let $C_1' = C_1 \cap C_n$, \ldots, $C_{n-1}' = C_{n-1} \cap C_n$. Then by the base case, any $(d+1)$ sets in $\{C_1',\dotsc,C_{n-1}'\}$ have a non-empty intersection. Therefore they all intersect by the induction hypothesis, hence
\[
\bigcap_{i=1}^n C_i = \bigcap_{i=1}^{n-1}C_i' \neq \emptyset.
\]
So let us show the base case. Let $v_i \in \bigcap_{j\neq i} C_j$; by the assumption. Let $A = \{v_1,\dotsc,v_{d+2}\}$. Then by Radon's theorem, there exist disjoint $A_1,A_2\subset A$ such that $\conv(A_1)\cap \conv(A_2)\neq \emptyset$. Consider $v\in \conv(A_1)\cap \conv(A_2)$. We will show that $v\in C_i$ for every $i$.

Suppose wlog that $v_i \in A_2$. Then $A_1 \subset C_i$. Since $C_i$ is convex, then $\conv(A_1) \subset C_i$. So $v\in C_i$, as desired. See \cref{fig:Helly_proof} for an example of this process in $\R^2$. \qedhere
% \missingfigure{Draw four disks in $\R^2$, $C_1,\dotsc,C_4$. Draw the $v_i$ in all the intersections but $v_i$. Connect $v_1,v_3$ in red $v_2,v_4$ in blue. Then let $v$ be the intersection. $v$ is in all four; it is in $C_1$ because it's in the interval between $v_2,v_4 \in C_1$. Etc.}
\begin{figure}
\begin{center}
\begin{tikzpicture}[circ/.style={circle,minimum size=\circdiam cm,draw,opacity=.7}]

\def\circposrad{1}
\def\circdiam{3}

\node[circ,label={right:$C_1$}] at (0:\circposrad cm) {};
\node[circ,label={above:$C_2$}] at (90:\circposrad cm) {};
\node[circ,label={left:$C_3$}] at (180:\circposrad cm) {};
\node[circ,label={below:$C_4$}] at (270:\circposrad cm) {};

\coordinate (v3) at (0:.7cm);
\coordinate (v4) at (90:.7cm);
\coordinate (v1) at (180:.7cm);
\coordinate (v2) at (270:.7cm);

\draw[red] (-.7,0) -- (.7,0);
\draw[blue] (v2) -- (v4);

\filldraw[black] (v1) circle (1.65pt) node[label=$v_1$]{};
\filldraw[black] (v2) circle (1.65pt) node[label=right:$v_2$]{};
\filldraw[black] (v3) circle (1.65pt) node[label=$v_3$]{};
\filldraw[black] (v4) circle (1.65pt) node[label=above:$v_4$]{};

\draw (0,0) circle (1.75pt);% node[label={[label distance=.02cm]north east:$v$}]{};
\end{tikzpicture}
\end{center}
\caption{Helly's theorem in $\R^2$. Given four convex sets $C_1,\dots,C_4$ such that every triple has a non-empty intersection $v_i \in \bigcap_{j\neq i} C_j$, we can use Radon's theorem to divide $\{v_1,v_2,v_3,v_4\}$ into two disjoint sets with non-empty convex hull. Here, $A_1 = \{v_1,v_3\}$ and $A_2 = \{v_2,v_4\}$. The point in the intersection $\conv(A_1)\cap \conv(A_2)$ is labelled here with an open circle.} \label{fig:Helly_proof}
\end{figure}
\end{proof}

\begin{remark}
Helly's theorem may not apply to infinite collections of sets; consider $\{[n,+\infty):n\in \N\}$.
\end{remark}

For any set of $n$ points on the line, we can find a point such that there are at least $n/2$ points above it and below it. This is the median. How do we find an analog for $\R^d$? We'd like to find a point that in any direction away from this point, there are still many points in our set.

Let us generalize to $\R^d$: Given $X\subset \R^d$, $|X|=n$, the \defn{centerpoint} of $x$ is a point $m\in \R^d$ such that for every closed halfspace $H\subset \R^d$ such that $m\in H$, then $H$ contains at least $\frac{n}{d+1}$ points in $X$.

\begin{theorem}
For every finite set $X\subset \R^d$, there exists a centerpoint.
\end{theorem}
\begin{proof}
Note the following are all equivalent:
\begin{itemize}
\item $m$ is a centerpoint
	\item  For every closed halfspace $H\subset \R^d$, if $m\in H$ then $|H\cap X| \geq \frac{n}{d+1}$.
	\item For every closed halfspace $H\subset \R^d$, if  $|X\cap H| < \frac{n}{d+1}$, then $m\not \in H$.
	\item For every closed halfspace $H\subset \R^d$, if $|X\cap H^c| > n - \frac{n}{d+1} = \frac{dn}{d+1}$, then $m\in H^c$.
	\item For every open halfspace $H\subset \R^d$, if $|X\cap H| >  \frac{dn}{d+1}$, then $m\in H$.
\end{itemize}
% Alternatively, every open half space which does not contain $m$ must contain at most $$
Consider the family
\begin{align*}
\H = \{H: H \text{ is an open halfspace of }\R^d, |H\cap X| >  \frac{dn}{d+1}\}.
\end{align*}
We will show for some $m \in \R^d$, we have $m\in H$ for every $H\in \H$, proving that $m$ is a centerpoint.
First, if $H_1,H_2,\dotsc,H_{d+1}\in \H$, then
\begin{align*}
H_1\cap H_2\cap \dotsm \cap H_{d+1}\neq \emptyset.
\end{align*}
Otherwise, every point of $X$ belongs to $\leq d$ out of $d+1$ of these half spaces, and thus
\begin{align*}
dn = \sum_{i=1}^{d+1} \frac{dn}{d+1} < \sum_{i=1}^{d+1} |H_i \cap X| \leq d n
\end{align*}
which is a contradiction.

We would like to now apply Helly's theorem, but we have an infinite collection $\H$ instead of a finite one.

Let us instead set $\H' = \{ \conv(Y): Y\subset X, |Y| > \frac{dn}{d+1}\}$. By the previous argument and Helly's theorem, there exists $m\in \R^d$ such that $m\in C$ for every $C\in \H'$. But this suffices. For any $H\in \H$  there is $Y\subset X$ with $|Y| > \frac{dn}{d+1}$ and $Y\subset H$. Then $\conv(Y) \subset H$ since $H$ is convex. But since $m\in \conv(Y)$, we have $m\in H$, as desired.%if $m\not \in H$, then $m\not \in \conv(Y)$ which is a contradiction.
% If $m\not \in H$, then we want that $H$ contains
\end{proof}

\begin{theorem}[Birch]
Let $X\subset \R^2$ be a collection of $3n$ points. Then $X$ can be partitioned into $n$ triples such that the corresponding triangles all share a point in common.
\end{theorem}

\begin{figure}
\begin{center}
\begin{tikzpicture}
\begin{scope}[scale=.6]
\coordinate (v1) at (0:3cm);
\coordinate (v9) at (20:2cm);
\coordinate (v6) at (130:1cm);
\coordinate (v3) at (240:2.3cm);
\coordinate (v5) at (160:3.3cm);
\coordinate (v4) at (290:.3cm);
\coordinate (v7) at (80:1.3cm);
\coordinate (v2) at (340:1.23cm);
\coordinate (v8) at (45:3cm);
\foreach \n in {1,2,3,4,5,6,7,8,9}
{
	\filldraw (v\n) circle (1.65pt);
}
\end{scope}
\node at (3cm,0){$\leadsto$};
\begin{scope}[scale=.6, xshift=10cm]
\coordinate (v1) at (0:3cm);
\coordinate (v9) at (20:2cm);
\coordinate (v6) at (130:1cm);
\coordinate (v3) at (240:2.3cm);
\coordinate (v5) at (160:3.3cm);
\coordinate (v4) at (290:.3cm);
\coordinate (v7) at (80:1.3cm);
\coordinate (v2) at (340:1.23cm);
\coordinate (v8) at (45:3cm);
\foreach \n in {1,2,3,4,5,6,7,8,9}
{
	\filldraw (v\n) circle (1.65pt);
}

\filldraw[blue,opacity=.4] (v7) -- (v4) -- (v1) -- cycle;

\filldraw[green,opacity=.4] (v2) -- (v5) -- (v8) -- cycle;

\filldraw[red,opacity=.4] (v3) -- (v6) -- (v9) -- cycle;
\end{scope}

\end{tikzpicture}
\end{center}
\caption{An example of Birch's theorem with nine points. On the left, the nine points are depicted; on the right, intersecting triangles chosen.}\label{fig:birch_ex}
\end{figure}
% \missingfigure{Draw 9 points and three triangles in different colors so that there is a common area. Label a point of intersection.}
\begin{remark}
See \cref{fig:birch_ex} for an example.
\end{remark}\begin{proof}
% \missingfigure{Draw rays from centerpoint to each vertex}

Let $m$ be a centerpoint of $X$. Label the points of $X$ by $x_1$, $x_2$, \ldots, $x_{3n}$ so that the rays $mx_1$, $mx_2$, \ldots, $mx_{3n}$ are arranged around $m$ in clockwise order, as shown in \cref{fig:birch_proof1}.
\begin{figure}[ht]
\begin{center}
\begin{tikzpicture}
\begin{scope}[scale=1]
\coordinate (m) at (.7cm,.3cm);
\coordinate (v1) at (0:3cm);
\coordinate (v9) at (20:2cm);
\coordinate (v6) at (130:1cm);
\coordinate (v3) at (240:2.3cm);
\coordinate (v5) at (160:3.3cm);
\coordinate (v4) at (290:.3cm);
\coordinate (v7) at (80:1.3cm);
\coordinate (v2) at (340:1.23cm);
\coordinate (v8) at (45:3cm);
\foreach \n in {6,7}
{
	\filldraw (v\n) circle (1.65pt) node[above]{$x_\n$};
}
\foreach \n in {4,5}
{
	\filldraw (v\n) circle (1.65pt) node[left]{$x_\n$};
}
\foreach \n in {1,8,9}
{
	\filldraw (v\n) circle (1.65pt) node[right]{$x_\n$};
}
\foreach \n in {2,3}
{
	\filldraw (v\n) circle (1.65pt) node[below]{$x_\n$};
}

% \filldraw[blue,opacity=.4] (v5) -- (v4) -- (v1) -- cycle;

% \filldraw[green,opacity=.4] (v3) -- (v9) -- (v8) -- cycle;

% \filldraw[red,opacity=.4] (v6) -- (v7) -- (v2) -- cycle;

	\foreach \n in {1,2,...,9}
{
	\draw[opacity=.7] (m) -- (v\n);
}
	\filldraw[red] (m) circle (1.75pt) node[black,right]{$m$};

\end{scope}
\end{tikzpicture}
\end{center}
\caption{Nine points $\{x_1,\dotsc,x_9\}$ in $\R^2$ and their centerpoint $m$, in red. Continuing the example from \cref{fig:birch_ex}, the points are labelled clockwise in order from the centerpoint. }\label{fig:birch_proof1}
\end{figure}
Let $X_i = \{ x_i, x_{i+n}, x_{i+2n}\}$ for $i=1,\dotsc, n$. Then $m\in \conv(X_i)$ for each $i$.

Assume $m\not \in \conv(X_i)$. Then\sidenote{We are appealing to a separating hyperplane theorem, such as \cite[Theorem 1.24]{matouvsek2002lectures}, although the result seems clear geometrically in this case.} some closed halfplane containing $m$ separates $m$ from the three points of $X_i$; see \cref{fig:birch_proof2}. But then the $2n$ rays between $x_i$ and $x_{i+2n}$ fall in the other half of the halfplane, so there are at least $2n+1$ points in $X$ on the other side, contradicting that $m$ is a centerpoint. \qedhere
\begin{marginfigure}
\begin{center}
\begin{tikzpicture}
\coordinate (v3) at (240:2.3cm);
\coordinate (v2) at (100:1.5cm);
\coordinate (v1) at (130:2cm);
\coordinate (m) at (.7cm,.3cm);
\foreach \n in {1,2,3}
{
	\draw[opacity=.7] (m) -- (v\n);
}
	\filldraw (v3) circle (1.65pt) node[left]{$x_i$};
	\filldraw (v2) circle (1.65pt) node[above]{$x_{i+2n}$};
	\filldraw (v1) circle (1.65pt) node[above]{$x_{i+n}$};

\filldraw[blue,opacity=.4] (v1) -- (v2) -- (v3) -- cycle;
	\filldraw[red] (m) circle (1.75pt) node[black,right]{$m$};

\end{tikzpicture}

\end{center}
\caption{The case where the centerpoint $m$, in red, is not in the convex hull of $X_i$, shown in blue. Then the $2n$ points $\{x_i,x_{i+1},\dotsc,x_{i+2n}$ all lie between $x_i$ and $x_{i+2n}$ (angularly) and may be separated from $m$ by a closed hyperplane, contradicting the definition of centerpoint.} \label{fig:birch_proof2}
\end{marginfigure}
% \missingfigure{$m$, then line, and three points $x_i$, $x_{i+n}$, $x_{i+2n}$. Draw outline convex hull in red  around those three. They are all on the other side of a halfplane from $m$.}

\end{proof}

\begin{theorem}[Colorful Carath\'eodory theorem, \cite{barany1982generalization}]
Let $S_1,S_2,\dotsc,S_{d+1}\subset \R^d$ and suppose that $x\in \bigcap_{i=1}^{d+1} \conv(S_i)$. Then there exist $x_1\in S_1$, $x_2\in S_2$, \ldots, $x_{d+1}\in S_{d+1}$ such that $x\in \conv(\{x_1,x_2,\dotsc,x_{d+1}\})$.
\end{theorem}
\begin{remark}
The different sets correspond to different colors. Then $x$ belongs to a convex combination of points each a different color. Taking each set $S_i \equiv X$, we recover Carath\'eodory's theorem.
\end{remark}
\begin{proof}
We will say $C$ is a \defn{colorful simplex} if $C = \conv(\{ x_1,x_2,\dotsc,x_{d+1} \})$ for $x_i\in S_i$ for $i\in[d+1]$. Suppose that no colorful simplex contains $x$.

Choose a colorful simplex $C$ such that $\dist(C,x) = \min_{c\in C} \|c-x\|$ is minimal.\marginnote{$\|c-x\| = \dist(c,x)$ is the 2-norm (Euclidean norm).}

Let $z\in C$ be the closest point to $x$: $\dist(z,x) = \dist(C,x)$.

Let $H$ be a hyperplane orthogonal to $zx$ through $z$. Let $H^+$ and $H^-$ be closed halfspaces with respect to $H$ such that $x\in H^-$.

\begin{claim}
$C\subset H^+$.
\end{claim}
\begin{subproof}
Suppose not: $z' \in C\cap (H^- \setminus H^+)$\marginnote{So $z'\not \in H^+$}. Then angle $z' z x$ is acute and points on $zz'$ near $z$ are closer to $x$ than $z$, a contradiction.
\end{subproof}

\begin{claim}
$z\in \conv(\{x_1,\dotsc,x_{d+1}\}\cap H)$.
\end{claim}
\begin{subproof}
$z = \sum_{i=1}^{d+1} \lambda_i x_i$ with $\lambda_i\geq 0$, $\sum_i \lambda_i =1$, since $z\in C$. Let $f: \R^d \to \R$ linear such that if $f(p)> 0$ for $p\in H^+\setminus H$, and $f(p) = 0$ for $p\in H$.

Then $0 = f(z) = \sum_{x_i \in H^+ \setminus H} \lambda_i \underbrace{f(x_i)}_{>0} + \sum_{x_i \in H} \lambda_i \underbrace{f(x_i)}_{=0}$. So we must have $\lambda_i=0$ when $x_i \in H^+ \setminus H$.
\end{subproof}

By Caratheordy theorem applied to $z$ and $\{x_i,\dotsc,x_{d+1}\}\cap H$, there exists $j$ such that \begin{align*}
z\in \conv((\{x_1,x_2,\dotsc,x_{d+1}\}\setminus \{j\})\cap H).
\end{align*}
We know $x\in \conv(S_j)$, so there exists $x'_j \in S_j \cap (H^-\setminus H^+)$. Let $C' = \conv( \{x_1,\dotsc,x_{j-1},x_j',x_{j+1},\dotsc,x_{d+1}\})$.

\begin{claim}
Then $\dist(C',x) < \dist(C,x)$.
\end{claim}
\begin{subproof}
We know $z\in C'$. Then, as before, points on $x_j' z\subset C'$ are closer to $x$ than $z$.
\end{subproof}
This last claim yields a contradiction to minimality of $C$.

Note: we assumed the $S_i$ were finite when assuming there exists a minimal colorful simplex $C$. But in fact, we only need to consider $\conv(S_i)$, which only depends on $d+1$ points, by (the original) Carath\'eodory points. Thus, we could take $|S_i|=d+1$ for each $i$.
\end{proof}
\begin{remark}
This proof yields an algorithm to improve our convex set one point at a time to the optimal one. Yet, it is unknown if this algorithm or any other can find the optimal $\conv(\{x_1,\dotsc,x_{d+1}\})$ in polynomial time.
\end{remark}

\begin{theorem}[\cite{Tverberg1966}]
Let $A\subset \R^d$ with $|A| \geq (r-1)(d+1)+1$. Then there exist $A_1,A_2,\dotsc,A_r\subset A$ pairwise disjoint such that $\bigcap_{i=1}^r \conv(A_i)\neq \emptyset$.
\end{theorem}
\begin{remark}
Radon's theorem is the case $r=2$. If $d=1$, then we have $2r-1$ points in $\R$ which we wish to write as $r$ groups with intersecting convex hulls.

For $d=2$, $3r-2$ points in $\R^2$ can be partitioned into $r$ groups with intersecting convex hulls. This in fact implies Birch's theorem.
\end{remark}

If $u\in \R^n$ and $v\in \R^m$, then $u\otimes v \in \R^{nm}$. Suppose $u = (u_1,\dotsc,u_n)$ and $v = (v_1,\dotsc,v_m)$; i.e., we have chosen bases for $\R^n$ and $\R^m$. Then $u\otimes v $ can be thought of as a $m\times n$ matrix with components $(u\otimes v)_{ij} = u_i v_j$.
\begin{align*}
u = \begin{pmatrix}
u_1\\ u_2\\ u_3
\end{pmatrix}, \qquad v = \begin{pmatrix}
v_1 \\ v_2 \\ v_3
\end{pmatrix}, \qquad u\otimes v = \begin{pmatrix}
u_1 v_1 & u_1 v_2 & u_1 v_3\\
u_2 v_1 & u_2 v_2 & u_2 v_3 \\
u_3 v_1 & u_3 v_2 & u_3 v_3
\end{pmatrix}.
\end{align*}

\begin{proposition}
~\begin{enumerate}
\item $(\alpha_1 u_1 + \alpha_2 u_2) \otimes v = \alpha_1 (u_1\otimes v) + \alpha_2 (u_2\otimes v)$
\item  Suppose $v_1,\dotsc,v_k$ are linearly independent and $u_1\otimes v_1 + u_2\otimes v_2 + \dotsm + u_k \otimes v_k = 0$. Then $u_1 = u_2 = \dotsm = u_k = 0$.
\end{enumerate}
\end{proposition}
We will leave the proof of the proposition as an exercise, and proceed to the proof of Tverberg's theorem.
\lect{3}{23}
% \marginnote{Lecture X: March 23, 2016.}

\begin{proof}[Proof (\cite{sarkaria1992tverberg}).]
Let $m = (r-1)(d+1)+1$.

\begin{enumerate}
	\item Instead of the orginal setting, consider $A = \{v_1,v_2,\dotsc,v_m\} \subset \R^{d+1}$ such that the vectors of $A$ lie in an affine hyperplane not passing through the origin. I.e., there exists $f: \R^{d+1}\to \R$ linear such that $f(v_i) = 1$ for every $i$.
	\item Let $w_1,w_2,\dotsc,w_r$ be vectors in $\R^{r-1}$ such that $w_1+w_2+\dotsc+w_r=0$ and this is essentially the only relation between these vectors. For instance, we could choose $w_i = (\underbrace{0,\dotsc,0}_{i-1},1,0,\dotsc,0)$ for $i=1,\dotsc,r-1$ and $w_r = (-1,-1,\dotsc,-1)$.
	\item Consider $\{v_i \otimes w_j: 1\leq i \leq m, 1\leq j \leq r\}\subset \R^{d+1}\otimes \R^{r-1}\cong \R^{m-1}$. For each $i$, we may think of $S_i:=\{v_i\otimes w_j:1\leq j\leq r\}$ as a copy of the $w_j$ embedded in a hyperplane. Note $0\in \conv(S_i)$ for each $i$, since
	\begin{align*}
	0 &= \frac{1}{r}v_i \otimes (w_1+w_2+\dotsm+ w_r) = \frac{1}{r} v_i \otimes w_1 + \frac{1}{r}v_i\otimes w_2  +\dotsm + \frac{1}{r}v_i\otimes w_r.
	\end{align*}
	\item Apply the Colorful Carath\'eodory theorem to $S_1,\dotsc,S_m$ and the point $0$. We get that for $1\leq i\leq m$ there exist $j_i$ and $\lambda_i\geq 0$ such that
	\begin{align}
	\sum_{i=1}^m \lambda_i (v_i\otimes w_{j_i})=0 \label{eq:Tv_conv_comb}
	\end{align}
	with $\sum_i \lambda_i=1$.

	Let $A_j = \{ v_i : j_i = j \}$ for $j=1,\dotsc,r$. Let us rewrite \cref{eq:Tv_conv_comb} as follows.
	\begin{align*}
	\sum_{j=1}^r \left( \sum_{v_i \in A_j} \lambda_i v_i \right)\otimes w_j = 0.
	\end{align*}
	Let us write $u_j =  \sum_{v_i \in A_j} \lambda_i v_i$. Then we have
	\begin{align*}
	u_1\otimes w_1 + u_2\otimes w_2 + \dotsm + u_r\otimes w_r &= 0\\
	(u_1- u_r)\otimes w_1 + (u_2-u_r)\otimes w_2 + \dotsm + (u_{r-1} - u_r)\otimes w_{r-1} &= 0
	\end{align*}
	using $w_r = -w_1 - w_2 - \dotsm - w_{r-1}$. But since $\{w_1,\dotsc,w_{r-1}\}$ are linearly independent, we must have $u_1-u_r = u_2 - u_r = \dotsm = u_{r-1}- u_r = 0$. That is,
	\[
	u_1 = u_2 = \dotsm = u_r.
	\]
	Substituting the definition of $u_j$,
	\begin{align}
	\sum_{v_i \in A_1} \lambda_i v_i = \sum_{v_i\in A_2} \lambda_i v_i = \dotsm = \sum_{v_i\in A_r}\lambda_i v_i. \label{eq:Tv_lambda_vs_equal}
	\end{align}
	Suppose the sum of coefficients $\lambda_i$ in each expression is the same and is equal to $s$.\marginnote{In fact, $s= \frac{1}{r}$.} Then
	\begin{align*}
	\sum_{v_i \in A_1} \frac{\lambda_i}{s}v_i = \dotsm = \sum_{v_i\in A_r} \frac{\lambda_i}{s}v_i = p
	\end{align*}
	and hence $p\in \conv(A_k)$.

	But if we apply $f$ to \cref{eq:Tv_lambda_vs_equal}, by linearity we obtain
	\begin{align*}
	\sum_{v_i \in A_1} \lambda_i f(v_i) = \sum_{v_i\in A_2} \lambda_i f(v_i) = \dotsm = \sum_{v_i\in A_r}\lambda_i f(v_i).
	\end{align*}
	Since $f(v_i)=1$ for all $i$, we find $\sum_{v_i\in A_j} \lambda_i \equiv s$. \qedhere
\end{enumerate}
\end{proof}
\flavor{A magical proof. 30 years after the original.}

\begin{remark}
In Helly's theorem, we consider $C_1,\dotsc,C_n\subset \R^d$ convex such that every $(d+1)$-tuple of $C$'s has non-empty intersection. Then we obtain that there is a common point in all of the sets. Suppose instead $\sim \frac{1}{2}$ of the $(d+1)$-tuples have a non-empty intersection. What can we say about large intersections?
\end{remark}
\begin{theorem}[Fractional Helly's theorem]
For every $d\in \N$ and $0< \alpha \leq 1$ there exists a $\beta = \beta(\alpha,d)$ such that the following holds. Let $C_1, C_2,\dotsc,C_n\subset \R^d$ be convex and suppose $\bigcap_{i\in I} C_i\neq \emptyset$ for at least $\alpha {n\choose d+1}$ sets $I\subset[n]$ with $|I| = d+1$. Then there exists $X \subset [n]$ with $|X|\geq \beta n$ such that $\bigcap_{i\in X} C_i \neq \emptyset$.

\end{theorem}
\begin{proof}
We need to assume that $C_1,C_2,\dotsc,C_n$ are compact; we may do this as follows. For each set $I$ as in the statment, select $p_I \in \bigcap_{i\in I} C_i$. Then replace $C_i$ by $\conv(\{p_I:  I \text{ s.t. } I\ni i\})$.

Let $F_I = \bigcap_{i\in I} C_i$. Let $<$ be a linear lexicographic order on $\R^d$. Let $p_I= \min (F_I)$ in this order, if $F_I\neq \emptyset$. Since $F_I$ is compact, $p_I$ exists and is unique.

\begin{claim}
For every $I\subset[n]$ with $|I|=d+1$, s.t. $F_I\neq \emptyset$, there exists $J\subset I$, $|J| = d$ such that $p_I = p_J$.
\end{claim}
\begin{remark}
For any $J\subset I$,  we have $p_J \leq P_I$.
\end{remark}
\begin{subproof}[Proof of claim.]
Let $C =  \{q \in \R^d: q< p_I\}$. Then $C$ is convex. Then $C\cap \left( \bigcap_{i\in I} C_i \right) = C\cap F_I = \emptyset$. Since this $(d+2)$-tuple intersection is empty, then by the contrapositive of Helly's theorem, not all $(d+1)$-tuples can have empty intersection. But since $F_I\neq \emptyset$, the $(d+1)$-tuple with empty intersection must include $C$. So there exists $J\subset I$ with $|J| = d$ such that
\begin{align*}
C \cap \left( \bigcap_{i\in J}C_i \right) = \emptyset.
\end{align*}
Then $p\geq p_I$ for every $p\in F_J$.
\end{subproof}

Now for every $I\subset[n]$ with $|I| = d+1$, $F_I\neq \emptyset$, select $J\subset I$ with $|J| = d$ such that $P_J = P_I$. Then there are $\alpha{n \choose d+1}$ sets $I$ which we consider, and  ${n\choose d}$ sets $J$ of size $d$. So some set $J_0$ is associated with at least $\frac{\alpha {n\choose d+1}}{{n\choose d}}$ sets $I$. Such sets are of the form $J_0 \cup \{ i\}$ for some $i$, and $p_{J_0} \in C_i$ for such $i$. So $p_{J_0}$ belongs to  at least
\begin{align*}
\frac{\alpha{ n\choose d+1}}{{n\choose d}} + d = \frac{\alpha(n-d)}{d+1}+d \geq \frac{\alpha}{d+1}n
\end{align*}
sets $C_i$ (counting the $d$ sets in $J_0$).
Therefore, we may take $\beta = \frac{\alpha}{d+1}$.
\end{proof}
\begin{remark}
This constant is not optimal; for instance, when $\alpha=1$, we would like $\beta=1$ to recover Helly's theorem. The optimal constant is known, however.
\end{remark}

For every $k$, we'd like to prove existence (and estimate the value) of the number $g(k)$ such that in every set of $g(k)$ points in $\R^2$, with no three lying on a line\sidenote[][-1cm]{``in general position''} there exist $k$ points which are convexly independent\sidenote[][-1cm]{``in convex position,'' or are the verticies of a convex polygon}.

Let us consider small $k$. Then $g(1)=1$, $g(2)=2$, $g(3)=3$. But $g(4) > 4$, as shown in \cref{fig:g4}.
\begin{marginfigure}[-1cm]
\begin{center}
\begin{tikzpicture}[scale=1.5]
\coordinate (a) at (0,0);
\coordinate (b) at (1.5,0);
\coordinate (c) at (.75,1);

\coordinate (d) at (0.5,.3);
% \draw[green] (a) -- (d) --;
% \draw[blue] (b) -- (c);
\filldraw[LightBlue] (a) -- (b) -- (c) -- cycle;
\filldraw[black] (a) circle (1pt);
\filldraw[black] (b) circle (1pt);
\filldraw[black] (c) circle (1pt);
\filldraw[black] (d) circle (1pt);

\end{tikzpicture}
\end{center}
\caption{An illustration of the fact that $g(4) > 4$. We have four points with no three lying on a line such that not all four are convexly independent: one point is in the convex hull of the other three.} \label{fig:g4}
\end{marginfigure}

\begin{lemma} \label{lem:g4_is_5}
$g(4)=5$.
\end{lemma}
\begin{proof}
Let $X\subset \R^2$ be a set of five points in general position. Consider $\conv(X)$: this is a polygon with 3, 4, or 5 vertices. If there are 4 or 5 vertices we are done, so, assume $\conv(X)$ is a triangle with vertices $\{a,b,c\}$. Let $\{d,e\}$ be the remaining two points of $X$, as shown in \cref{fig:lem89convX}.
\begin{figure}
\begin{center}
\begin{tikzpicture}[scale=3]
\coordinate (a) at (0,0);
\coordinate (b) at (1.5,0);
\coordinate (c) at (.75,1);

\coordinate (e) at (0.9,.2);
\coordinate (d) at (0.7,.4);

% \draw[green] (a) -- (d) --;
% \draw[blue] (b) -- (c);
\filldraw[LightBlue] (a) -- (b) -- (c) -- cycle;
	\filldraw[black] (a) circle (1pt) node[label=left:$a$]{};
\filldraw[black] (b) circle (1pt)node[label=right:$b$]{};
\filldraw[black] (c) circle (1pt)node[label=above:$c$]{};
\filldraw[black] (d) circle (1pt)node[label=below:$d$]{};
\filldraw[black] (e) circle (1pt)node[label=below:$e$]{};

\end{tikzpicture}
\end{center}
\caption{We assume $\conv(X) = \conv(\{a,b,c\})$; then $\{d,e\}:= X\setminus \{a,b,c\}$ lie inside the triangle formed by $\{a,b,c\}$.} \label{fig:lem89convX}
\end{figure}

Now assume the line $de$ intersects $ab$ and $ac$, as shown in \cref{fig:lem89de_line}.
\begin{marginfigure}
\begin{center}
\usetkzobj{all}
\begin{tikzpicture}[scale=1.5]
\coordinate (a) at (0,0);
\coordinate (b) at (1.5,0);
\coordinate (c) at (.75,1);

\coordinate (e) at (0.9,.2);
\coordinate (d) at (0.6,.4);

% \draw[green] (a) -- (d) --;
% \draw[blue] (b) -- (c);
\filldraw[LightBlue] (a) -- (b) -- (c) -- cycle;
	\filldraw[black] (a) circle (1pt) node[label=left:$a$]{};
\filldraw[black] (b) circle (1pt)node[label=right:$b$]{};
\filldraw[black] (c) circle (1pt)node[label=above:$c$]{};
\filldraw[black] (d) circle (1pt)node[label=below:$d$]{};
\filldraw[black] (e) circle (1pt)node[label=below:$e$]{};

  % \tkzDefLine[through d](d,e)
  \tkzDrawLine[add = 2 and 2](d,e)
\end{tikzpicture}
\end{center}
\caption{We assume the line passing through $d$ and $e$ intersects the sides $ac$ and $ab$ of the triangle. Since no three points in $X$ are colinear, the line passing through $d$ and $e$ must intersect two of the sides of the triangle, so we can always relabel the verticies so that this is the case.} \label{fig:lem89de_line}
\end{marginfigure} Then $bcde$ form a convex quadrangle. We may see this by taking the convex hull of any three of $\{b,c,d,e\}$ to form a triangle $T$, and calling the omitted point $z$. Then there are two boundary line segments of the quadrilateral $bcde$ which end in $z$, but each of these line segments only intersects $T$ at the opposite end from $z$. If $z\in T$, then the whole line between $z$ and those endpoints would have to be included in $T$. Thus, $z\not \in T$, and we truly have a convex quadrilateteral $bcde$. This is illustrated in \cref{fig:lem89T}.
\begin{figure}
\begin{center}
\begin{tikzpicture}[scale=3]
\coordinate (a) at (0,0);
\coordinate (b) at (1.5,0);
\coordinate (c) at (.75,1);

\coordinate (e) at (0.9,.2);
\coordinate (d) at (0.7,.4);

% \draw[green] (a) -- (d) --;
% \draw[blue] (b) -- (c);
\filldraw[LightBlue] (a) -- (b) -- (c) -- cycle;
	\filldraw[black] (a) circle (1pt) node[label=left:$a$]{};
\filldraw[black] (b) circle (1pt)node[label=right:$b$]{};
\filldraw[black] (c) circle (1pt)node[label=above:$c$]{};
\filldraw[black] (d) circle (1pt)node[label=below:$d$]{};
\filldraw[black] (e) circle (1pt)node[label=below:$e$]{};

\filldraw[pattern=north west lines] (c) -- (d) -- (b) -- cycle;

\draw[dashed] (d)--(e);
\draw[dashed] (e)--(b);

\end{tikzpicture}
\end{center}
\caption{We will take $T= \conv(\{c,d,b\})$, the crosshatched region. Then the two dashed lines only intersect $T$ at the verticies $d,b$. We see if $e$ were moved up into $T$, then the line passing through $d$ and $e$ would no longer intersect the segment $ab$.} \label{fig:lem89T}
\end{figure}
\end{proof}
\begin{theorem}[\cite{erdosszekeres1935combinatorial}]
$g(k)$ exists for all $k$.
\end{theorem}
\begin{proof}
Let $n=g(k)$ be such that in every coloring of $[n]^{(4)}$ in colors red and blue, one can find either a set of $5$ with all quadruples in red, or a set of size $k$ with all quadruples blue. This exists by the Hypergraph Ramsey theorem\sidenote{For all positive integers $r,k_1$, and $k_2$ there exists a positive integer $n= R^{(r)}(k_1,k_2)$ so that the following holds. If elements of $[n]^{(r)}$ are colored in colors red and blue then there is a set $Z\subset [n]$ such that either $|Z| = k_1$ and all elements of $Z^{(r)}$ are red, or $|Z| = k_2$ and all elements of $Z^{(r)}$ are blue.}.

Color quadruples of points in red color if it is not in convex position, and otherwise in blue. By  \cref{lem:g4_is_5}, there exists a set of $k$ points such that every four of them are in convex position.

Then these $k$ points are in convex position by Carath\'eodory's theorem.
\end{proof}
\begin{remark}
We may obtain much better bounds by longer proofs.
\end{remark}


% !TEX root = Main.tex
\section{Non-Negative Matrix Factorization}
$\mathbf{X} \in \mathbb{Z}^{N \times M}_{\geq 0}$, NMF: $\mathbf{X} \approx \mathbf{U^\top V}, x_{ij}=\sum_z{u_{zi}v_{zj}}=\langle\mathbf{u}_i \mathbf{v}_j\rangle$
Decompose object into features: topics, face parts, etc.. $\mathbf{u}$ weights on parts, $\mathbf{v}$ parts (bases). More interpretable (PCA: holistic repre.).

\subsection*{EM for MLE for pLSA (NO global opt guarantee)}
\textbf{Context Model:} $p(w | d) = \sum_{z=1}^K p(w | z) p(z | d)$\\
\textbf{Conditional independence assumption ($*$):}\\
$p(w|d) = \sum_z p(w,z|d) = \sum_z p(w|d,z)p(z|d) \stackrel{*}{=} \sum_z p(w|z)p(z|d)$\\
\textbf{Symmetric parameterization:}\\
$p(w, d) = \sum_z p(z)p(w | z) p(d | z)$ \\
Log-Likelihood: $L(\mathbf{U}, \mathbf{V}) = \sum_{i,j} x_{i,j}\log p(w_j|d_i) \\
= \sum_{(i,j) \in X} \log \sum_{z=1}^K p(w_j|z)p(z|d_i)$ \\
$ p(w_j|z) = v_{zj}$, $p(z|d_i) = u_{zi}$, $\sum_j^N v_{zj} = \sum_z^K u_{zi} = 1$\\
E-Step (optimal q: posterior of z over $(d_i, w_j)$):\\
$q_{zij} = \frac{p(w_j|z)p(z|d_i)}{\sum_{k=1}^K p(w_j|k)p(k|d_i)} := \frac{v_{zj}u_{zi}}{\sum_{k=1}^K v_{kj}u_{ki}}$, $\sum_z q_{zij}=1$\\
M-Steps:\\
$p(z|d_i) = \frac{\sum_j x_{ij}q_{zij}}{\sum_j x_{ij}}, p(w_j|z) = \frac{\sum_i x_{ij}q_{zij}}{\sum_{i,l}x_{il}q_{zil}}$\\

\subsection*{Latent Dirichlet Allocation}
To sample a new document, we need to extend $X$ and $U^T$ with a new row, s.t. $X=U^T V$. (While pLSA fixes both dimensions)\\
For each $d_i$ sample topic weights $\mathbf{u}_i$\textasciitilde Dirichlet($\alpha$): $p(u_i|\alpha) = \prod_{z=1}^K u_{zi}^{\alpha_k-1}$, then topic $z^t$\textasciitilde Multi($u_i$), word $w^t$\textasciitilde Multi($v_{z^t}$)\\
Multinom. obsv. model on wc vec: $p(\mathbf{x}|V,u) = \frac{l!}{\prod_j \mathbf{x}_j!}\prod_j \pi_j^{\mathbf{x}_j}$
where $\pi_j=\sum_z v_{zj} u_z$, $l=\sum_j x_j$ \\
Bayesian averaging over $\mathbf{u}$: $p(\mathbf{x}|\mathbf{V},\alpha)=\int p(\mathbf{x}|\mathbf{V},\mathbf{u})p(\mathbf{u}|\alpha)d\mathbf{u}$

\subsection*{NMF Algorithm for quadratic cost function}

$\min_{\mathbf{U}, \mathbf{V}} J(\mathbf{U}, \mathbf{V}) = \frac{1}{2} \|\mathbf{X} - \mathbf{U}^\top\mathbf{V}\|_F^2$ (non-negativity)
s.t. $\forall i,j,z:u_{zi},v_{zj} \geq 0 $ \\
Comparison with pLSA:\\
1. sampling model: Gaussian vs multinomial
2. objective: quadratic vs KL divergence
3. constraints: not normalized \\
Alternating least squares:\\
1. init: $\mathbf{U}, \mathbf{V} = rand()$\\ 2. repeat 3\textasciitilde4 for $\mathit{maxIters}$:\\
3. upd. $(\mathbf{VV}^\top)\mathbf{U} = \mathbf{VX}^\top$, proj. $u_{zi} = \max \{ 0, u_{zi} \}$\\
4. update $(\mathbf{UU}^\top)\mathbf{V} = \mathbf{UX}$, proj. $v_{zj} = \max \{ 0, v_{zj} \}$


\section{Homology Calculations and Applications}
\begin{example}
    If $S^n\simeq S^m$, then $m=n$.
    This follows immediately from our calculation of the homology groups of simplices which are the triangulations of the spheres.
\end{example}
\begin{theorem}
    If $\mathbb R^n\cong\mathbb R^m$, then $m=n$.
\end{theorem}
\begin{proof}
    Suppose we are given a homeomorphism $\phi:\mathbb R^m\to\mathbb R^n$, then by a translation we can assume WLOG that $\phi(0)=0$.
    Then we can restrict $\phi$ so that $\mathbb R^n\setminus\{0\}$ and $\mathbb R^m\setminus\{0\}$, which are homotopic to $S^{n-1}$ and $S^{m-1}$ respectively, consequently $S^{n-1}\simeq S^{m-1}$, so $n=m$.
\end{proof}
\begin{proposition}
    Any map $\phi:D^n\to D^n$ has a fixed point.
\end{proposition}
\begin{proof}
    Repeat the argument as in the $D^2$ case and finish the argument by homology.
\end{proof}
\subsection{Mayer-Vietoris Theorem}
The Mayor-Vietoris theorem is an analog of Seifert-van Kampen theorem as in both deals with the algebraic invariants of spaces that are constructed by gluing.
\begin{definition}
    A sequence of homomorphisms of abelian groups
    \[
        \begin{tikzcd}
            \cdots\arrow{r}&A_{i+1}\arrow{r}{f_i}&A_i\arrow{r}{f_{i-1}}&A_{i-1}\arrow{r}&\cdots
        \end{tikzcd}
    \]
    is exact at $A_i$ if $\operatorname{Im}f_i=\ker f_{i-1}$.
    We say the sequence is exact if it is exact at every $A_i$.
\end{definition}
Consequenly, any exact sequence is a chain complex with all homology groups trivial.
\begin{definition}
    A short exact sequence is an exact sequence in the form
    \[
        \begin{tikzcd}
            0\arrow{r}&A\arrow{r}&B\arrow{r}&C\arrow{r}&0
        \end{tikzcd}
    \]
\end{definition}
So necessarily $A\to B$ is injective, $B\to C$ is surjective and $C\cong B/A$.
An exact sequence that is not short is of course called a long exact sequence.\\
Suppose $K$ is a simplicial complex with $K=L\cup M$ where $L,M$ are subcomplexes of $K$.
Then $N=L\cap M$ is also a subcomplex.
We usually write $K=L\cup_NM$.
Analogous to what we did in Seifert-van Kampen theorem, we want to relate the homology groups of $L,M,N$ to the homology group of $K$.
Of course we immediately get the natual inclusion maps
$$i:N\to L,j:N\to M,l:L\to K,m:M\to K$$
\begin{theorem}[Mayer-Vietoris]\label{mayer-vietoris}
    There exists a map (known as the connecting homomorphism) $\delta_\ast:H_n(K)\to H_{n-1}(N)$ for each $n$ such that the sequence
    \[
        \begin{tikzcd}
            \cdots\arrow{r}{\delta_\ast}&H_n(N)\arrow{r}{i_\ast\oplus j_\ast}&H_n(L)\oplus H_n(M)\arrow{r}{l_\ast-m_\ast}&H_n(K)\arrow[swap,overlay,out=0,in=180]{dll}{\delta_\ast}&\\
            &H_{n-1}(N)\arrow[swap]{r}{i_\ast\oplus j_\ast}&H_{n-1}(L)\oplus H_{n-1}(M)\arrow[swap]{r}{l_\ast-m_\ast}&H_{n-1}(K)\arrow[swap]{r}{\delta_\ast}&\cdots
        \end{tikzcd}
    \]
    is exact.
\end{theorem}
We will prove this after further development on homological algebra.
\begin{definition}
    We say a sequence of chain complexes
    \[
        \begin{tikzcd}
            \cdots\arrow{r}&A_\bullet\arrow{r}{f_\bullet}&B_\bullet\arrow{r}{g_\bullet}&C_\bullet\arrow{r}&\cdots
        \end{tikzcd}
    \]
    is exact at $B_\bullet$ if
    \[
        \begin{tikzcd}
            \cdots\arrow{r}&A_n\arrow{r}{f_n}&B_n\arrow{r}{g_n}&C_n\arrow{r}&\cdots
        \end{tikzcd}
    \]
    is exact for every $n$.
\end{definition}
So we can analogously define (short) exact sequences of chain complexes.
\begin{lemma}[Snake Lemma]
    Let
    \[
        \begin{tikzcd}
            0\arrow{r}&A_\bullet\arrow{r}{f_\bullet}&B_\bullet\arrow{r}{g_\bullet}&C_\bullet\arrow{r}&0
        \end{tikzcd}
    \]
    be a short exact sequence of chain complexes.
    Then for any $n$ there is a homomorphism $\delta_\ast:H_{n+1}(C_\bullet)\to H_n(A_\bullet)$ such that
    \[
        \begin{tikzcd}
            \cdots\arrow{r}{\delta_\ast}&H_{n+1}(A_\bullet)\arrow{r}{f_\ast}&H_{n+1}(B_\bullet)\arrow{r}{g_\ast}&H_{n+1}(C_\bullet)\arrow[swap,overlay,out=0,in=180]{dll}{\delta_\ast}&\\
            &H_n(A_\bullet)\arrow[swap]{r}{f_\ast}&H_n(B_\bullet)\arrow[swap]{r}{g_\ast}&H_n(C_\bullet)\arrow[swap]{r}{\delta_\ast}&\cdots
        \end{tikzcd}
    \]
    is exact.
\end{lemma}
\begin{proof}
    We already have this huge commutative diagram with exact rows
    \[
        \begin{tikzcd}
            &\vdots\arrow{d}&\vdots\arrow{d}&\vdots\arrow{d}&\\
            0\arrow{r}&A_{n+1}\arrow{r}{f_{n+1}}\arrow{d}{\partial_{n+1}}&B_{n+1}\arrow{r}{f_{n+1}}\arrow{d}{\partial_{n+1}}&C_{n+1}\arrow{r}\arrow{d}{\partial_{n+1}}&0\\
            0\arrow{r}&A_n\arrow{r}{f_n}\arrow{d}{\partial_n}&B_n\arrow{r}{f_n}\arrow{d}{\partial_n}&C_n\arrow{r}\arrow{d}{\partial_n}&0\\
            0\arrow{r}&A_{n-1}\arrow{r}{f_{n-1}}\arrow{d}&B_{n-1}\arrow{r}{g_{n-1}}\arrow{d}&C_{n-1}\arrow{r}\arrow{d}&0\\
            &\vdots&\vdots&\vdots&
        \end{tikzcd}
    \]
    We now construct $\delta_\ast:H_{n+1}(C_\bullet)\to H_n(A_\bullet)$.
    Take $[x]\in H_{n+1}(C_\bullet)$ for $x\in Z_{n+1}(C_\bullet)$.
    As $g_{n+1}$ is surjective, there is $x\in B_{n+1}$ such that $g_{n+1}(y)=x$.
    Now $g_n\circ\partial_{n+1}(y)=\partial_{n+1}\circ g_{n+1}(y)=\partial_{n+1}(x)=0$.
    So by exactness, there exists $z\in A_n$ such that $f_n(z)=\partial_{n+1}(y)$.
    Note that $f_{n-1}\circ\partial_n(z)=\partial_n\circ f_n(z)=\partial_n\circ\partial_{n+1}(y)=0$, so $\partial_n(z)=0$ since $f_{n-1}$ is injective.
    This means that $z\in Z_n(A_\bullet)$, so we define $\delta_\ast([x])=[z]$.
    To see it is well-defined, we have two issues to deal with:\\
    First, if we replace $x$ by $x+\partial_{n+2}(x')$ and $g_{n+2}(y')=x'$ , then replace $y$ with $y+\partial_{n+2}(y')$.
    Then $g_{n+1}(y+\partial_{n+2}(y'))=g_{n+1}(y)+\partial_{n+2}\circ g_{n+2}(y')=x+\partial_{n+2}(x')$.
    But $\partial_{n+1}(y+\partial_{n+2}(y'))=\partial_{n+1}(y)$, so $z$ does not change.\\
    Secondly, if we have made up our mind on $x$ and $y,y'$ are chosen with $g_{n+1}(y')=g_{n+1}(y)=x$, then $g_{n+1}(y'-y)=0$, therefore by exactness there is some $z'$ such that $f_{n+1}(z')=y'-y$, thus $y'=y+f_{n+1}(z')$.
    So $\partial_{n+1}(y')=\partial_{n+1}(y)+\partial_{n+1}\circ f_{n+1}(z')=\partial_{n+1}(y)+f_n\circ\partial_{n+1}(z')$, which means $f_n(z+\partial_{n+1}(z'))=\partial_{n+1}(y)+f_n\circ\partial_{n+1}(z')=\partial_{n+1}(y')$.
    This is saying that replacing $y$ by $y'$ modifies $z$ by adding $\partial_{n+1}(z')$.
    But $[z]=[z+\partial_{n+1}(z')]$, so $\delta_\ast$ is well-defined.\\
    The proof of the sequence being exact is tedious routine work.\\
    At $H_n(B_\bullet)$, if $[a]\in H_n(A_\bullet)$, then by definition $g_\ast\circ f_\ast([a])=[g_n\circ f_n(a)]=0$ by the exactness of our original short exact sequence.
    So $\operatorname{Im}f_\ast\subset \ker g_\ast$.
    Now if $g_\ast([b])=0$, then $[g_n(b)]=g_\ast([b])=0$, thus there is some $c\in C_{n+1}$ with $g_n(b)=\partial_{n+1}(c)$.
    But then choosing $b'\in B_{n+1}$ with $g_{n+1}(b')=c$ by exactness gives $g_n(b-\partial_{n+1}(b'))=0$, hence $b-\partial_{n+1}(b')\in\ker g_n=\operatorname{Im}f_n$, so there is some $a\in A_n$ such that $f_n(a)=b-\partial_{n+1}(b')$.
    In particular, $f_\ast([a])=[f_n(a)]=[b-\partial_{n+1}(b')]=[b]$, therefore $\operatorname{Im}f_\ast\supset\ker g_\ast$.
    Combining these gives $\operatorname{Im}f_\ast=\ker g_\ast$ which implies the exactness at $H_n(B_\bullet)$.\\
    At $H_n(A_\bullet)$, suppose $[z]=\delta_\ast([x])$, then $f_\ast([z])=[f_n(z)]=[\partial_{n+1}(y)]=0$ where $y$ is as in the construction of $\delta_\ast$, so $\operatorname{Im}\delta_\ast\subset\ker f_\ast$.
    Suppose now that $f_\ast([z])=0$, then $f_n(z)=\partial_{n+1}(y)$ for some $y\in B_{n+1}$.
    Take $x=g_{n+1}(y)$, then $\partial_{n+1}(x)=\partial_{n+1}\circ g_{n+1}(y)=g_n\circ\partial_{n+1}(y)=g_n\circ f_n(z)=0$, so $x$ is a cycle, so $[x]$ is a homology class.
    But $\delta_\ast([x])=[z]$ by construction, hence $\operatorname{Im}\delta_\ast\supset\ker f_\ast$, therefore $\operatorname{Im}\delta_\ast=\ker f_\ast$ which gives the exactness at $H_n(A_\bullet)$.\\
    At $H_n(C_\bullet)$, if $[x]\in\operatorname{Im}g_\ast$, then (ater replacing $x$ with another representative if necessary) WLOG there exists a cycle $y\in B_n$ such that $g_n(y)=x$.
    But then immediately $\delta_\ast([x])=0$, so $\operatorname{Im}g_\ast\subset\ker\delta_\ast$.
    Conversely, suppose $\delta_\ast([x])=0$, then let $y,z$ be the corresponding element in the construction of $\delta_\ast$.
    By hypothesis there exists $a\in A_n$ such that $\partial_n(a)=z$.
    Then $\partial_n\circ f_n(a)=f_{n-1}\circ\partial_n(a)=f_{n-1}(z)=\partial_n(y)$.
    Thus $\partial_n(y-f_n(a))=0$, so $y-f_n(a)$ is a cycle.
    Also $g_n(y-f_n(a))=x$, so $[x]=g_\ast([y-f_n(a)])$.
    Consequently $\operatorname{Im}g_\ast\supset\ker\delta_\ast$, so $\operatorname{Im}g_\ast=\ker\delta_\ast$.
    This finished the proof.
\end{proof}
\begin{proof}[Proof of Theorem \ref{mayer-vietoris}]
    Easy to check that $C_\bullet\oplus D_\bullet$ is a chain complex with all data obtained from direct sums of data of $C_\bullet,D_\bullet$.
    Also $H_n(C_\bullet\oplus D_\bullet)=H_n(C_\bullet)\oplus H_n(D_\bullet)$.
    It then suffices to check that
    \[
        \begin{tikzcd}
            0\arrow{r}&C_\bullet(N)\arrow{r}{i_\bullet\oplus j_\bullet}&C_\bullet(L)\oplus C_\bullet(M)\arrow{r}{l_\bullet-m_\bullet}&C_\bullet(K)\arrow{r}&0
        \end{tikzcd}
    \]
    is exact.
    The proof is then done by snake lemma.\\
    Note that $C_\bullet(N)$ naturally embeds into $C_\bullet (L)$ and $C_\bullet(M)$ via $i_\bullet\oplus j_\bullet$, so in particular $i_\bullet\oplus j_\bullet$ is a injective.
    Also since $K=L\cup M$, for any $c\in C_n(K)$ we can write $c=c_L+c_M$ where $c_L$ is a linear combination of simplices in $L$ and $c_M$ is that in $M$.
    Let $b_L,b_M$ be the respective copies of $c_L,c_M$ in $C_n(L),C_n(M)$ respectively, then $l_\bullet(b_L)=c_L,m_\bullet(b_M)=c_M$, and hence $c=(l_\bullet-m_\bullet)(b_L,-b_M)$, so $l_\bullet-m_\bullet$ is surjective.\\
    It remains to show the exactness in the middle.
    For $(b_L,b_M)\in C_n(L)\oplus C_n(M)$, note that $l_\bullet(b_L)-m_\bullet(b_M)=0$ iff every simplex which occurs in $b_L$ also occurs in $b_M$ with the same coefficient.
    This is just saying that $b_L,b_M$ are linear combinations of simplices in $L\cap M=N$.
    Therefore $\operatorname{Im}(i_\bullet\oplus j_\bullet)=\ker (l_\bullet-m_\bullet)$, hence the sequence is indeed exact.
\end{proof}
\begin{lemma}[Five Lemma]
    Given a commutative diagram
    \[
        \begin{tikzcd}
            A\arrow{r}\arrow{d}{\alpha}&B\arrow{r}\arrow{d}{\beta}&C\arrow{r}\arrow{d}{\gamma}&D\arrow{r}\arrow{d}{\delta}&E\arrow{d}{\epsilon}\\
            A'\arrow{r}&B'\arrow{r}&C'\arrow{r}&D'\arrow{r}&E'
        \end{tikzcd}
    \]
    If the rows are exact and $\alpha,\beta,\delta,\epsilon$ are isomorphisms, then $\gamma$ is also an isomorphism.
\end{lemma}
\begin{proof}
    Exercise.
\end{proof}
We can now prove Proposition \ref{barycentric_iso_homol}.
Recall that we want to show that $s_\ast:H_n(K')\to H_n(K)$ is an isomorphism where $s:K'\to K$ is a simplicial approximation to the identity map on $|K|=|K'|$ which sends each $\hat\sigma$ to a vertex of $\sigma$.
\begin{proof}[Proof of Proposition \ref{barycentric_iso_homol}]
    Induction on the number of simplices of $K$.
    If $K$ has only one simplex, then it is just a vertex, so the proposition is trivial.
    For the induction step, let $\sigma\in K$ be of maximal dimension, then $L=K\setminus \{\sigma\}$ is also a simplicial complex.
    Let $M$ be the simplicial complex consists of $\sigma$ and its faces.
    Then $N=M\cap L$ is just the proper faces of $\sigma$.
    By construction of $s$ we can restrict $s$ to $L',M',N'$ to get the maps
    $$s_\ast:H_n(L')\to H_n(L),s_\ast:H_n(M')\to H_n(M),s_\ast:H_n(N')\to H_n(N)$$
    By the induction hypothesis, these are all isomorphisms.
    Then by Mayer-Vietoris, we have the diagram
    \[
        \begin{tikzcd}[column sep=0.34em]
            H_n(N')\arrow{r}\arrow{d}{s_\ast}&H_n(L')\oplus H_n(M')\arrow{r}\arrow{d}{s_\ast\oplus s_\ast}&H_n(K')\arrow{r}\arrow{d}{s_\ast}&H_{n-1}(N')\arrow{r}\arrow{d}{s_\ast}&H_{n-1}(L')\oplus H_{n-1}(M')\arrow{d}{s_\ast\oplus s_\ast}\\
            H_n(N)\arrow{r}&H_n(L)\oplus H_n(M)\arrow{r}&H_n(K)\arrow{r}&H_{n-1}(N)\arrow{r}&H_{n-1}(L)\oplus H_{n-1}(M)
        \end{tikzcd}
    \]
    with exact rows.
    Easy to check that it commutes.
    Also, by the induction hypothesis, all vertical arrows except $s_\ast:H_n(K')\to H_n(K)$ are isomorphisms.
    Hence $s_\ast:H_n(K')\to H_n(K)$ is an isomorphism by five lemma.
\end{proof}
\subsection{Homology of Compact Surfaces}
Recall that we constructed the oriented surfaces of genus $g$ as $\Sigma_g=\Gamma_{2g}\cup_{\rho_g}D^2$ where $\Gamma_{2g}$ is a bouquet of $2g$ copies of $S^1$ with generators $\alpha_1,\ldots,\alpha_g,\beta_1,\ldots,\beta_g$ and $\rho_g=\alpha_1\beta_1\alpha_1^{-1}\beta_1^{-1}\cdots \alpha_g\beta_g\alpha_g^{-1}\beta_g^{-1}$.
\begin{example}
    $\Gamma_r$ can be triangulated in the obvious way by taking it as $r$ hollow triangles joined by a common vertex.
    We know that $H_n(\Gamma_1)$ is $\mathbb Z$ when $n=0,1$ and $0$ otherwise.
    We claim that
    $$H_i(\Gamma_r)=\begin{cases}
        \mathbb Z\text{, if $i=0$}\\
        \mathbb Z^r\text{, if $i=1$}\\
        0\text{, otherwise}
    \end{cases}$$
    which we shall show inductively.
    The cases except for $i=1$ are all trivial, so it suffices to show that.
    Suppose we have shown the case for $r-1$, then take $K=\Gamma_r$, $L$ be a natural copy of $\Gamma_{r-1}$ in $\Gamma_r$ and $M$ be the remaining triangle.
    Then $N=L\cap M=\{\ast\}$ and hence Mayer-Vietoris gives the exact sequence
    \[
        \begin{tikzcd}
            0\arrow{r}&H_1(N)\arrow{r}&H_1(\Gamma_{r-1})\oplus H_1(S^1)\arrow{r}& H_1(\Gamma_r)\arrow[overlay, out=0, in=180]{dll}&\\
            &H_0(N)\arrow{r}&H_0(\Gamma_{r-1})\oplus H_0(S^1)\arrow{r}& H_0(\Gamma_r)\arrow{r}&0
        \end{tikzcd}
    \]
    which, after putting in everything we already know,
    \[
        \begin{tikzcd}
            0\arrow{r}&0\arrow{r}&\mathbb Z^{r-1}\oplus \mathbb Z\arrow{r}& H_1(\Gamma_r)\arrow[overlay, out=0, in=180]{dll}&\\
            &\mathbb Z\arrow{r}&\mathbb Z\oplus \mathbb Z\arrow{r}& \mathbb Z\arrow{r}&0
        \end{tikzcd}
    \]
    By exactness of the bottom row, the map $\mathbb Z\to\mathbb Z\oplus\mathbb Z$ is injective (alternatively this can also be easily seen from the definition of that map), hence the connecting homomophism is the zero map.
    Therefore the top row reduces to the exact sequence
    \[
        \begin{tikzcd}
            0\arrow{r}&\mathbb Z^{r-1}\oplus \mathbb Z\arrow{r}& H_1(\Gamma_r)\arrow{r}&0
        \end{tikzcd}
    \]
    which precisely means that $H_1(\Gamma_r)\cong\mathbb Z^{r-1}\oplus \mathbb Z\cong\mathbb Z^r$.
\end{example}
Worth noting that $H_1(\Gamma_r)$ is the free abelian group on $r$ letters while $\pi_1(\Gamma_r)$ is the free group on $r$ letters.
Also, if $\alpha_1,\ldots,\alpha_r$ are the generating paths of the circles, then $H_1(\Gamma_r)$ is generated by $\alpha_1,\ldots,\alpha_r$.
\begin{remark}
    Like in the preceding exacmple, whenever $N$ is connected, the map $H_0(N)\to H_0(L)\oplus H_0(M)$ is injective, and hence $H_1(L)\oplus H_1(M)\to H_1(K)$ is surjective.
\end{remark}
Now we want to attach our $2$-cell to get $\Sigma_g$.
We'll do this in two steps.\\
First, we attach a cylinder $S^1\times I$ via the same $\rho_g$ but on $S^1\times\{0\}$.
Write $\Sigma_g^\star=\Gamma_{2g}\cup_{\rho_g} (S^1\times I)$
By shrinking $I$ to a point we can obtain the deformation retraction of $\Sigma_g^\star$ onto $\Gamma_{2g}$, therefore they have the same homology groups.\\
Now $\Sigma_g=\Sigma_g^\star\cup_\alpha D^2$ where $\alpha:\partial D^2\to S^1\times \{1\}$ is the natural identity.
We can choose triangulations of $\Sigma_g^\star$ and $D^2$ so that they are compatible under this gluing (so we are essentially removing a $D^2$ from $\Sigma_g$ to get $\Sigma_g^\star$).
\footnote{There are of course some technical details regarding why we can do this, but we are not going into this part of details here. It is not too hard though.}
Let $L$ be the triangulation of $\Sigma_g^\star$ and $M$ be the triangulation of $D^2$ such that this holds.
Then $N=L\cap M$ is a triangulation of $S^1\times\{1\}\cong S^1$.
Majer-Vietoris then gives the exact sequence
\[
    \begin{tikzcd}
        &H_2(L)\oplus H_2(M)\arrow{r}&H_2(\Sigma_g)\arrow[overlay, out=0,in=180]{dll}&\\
        H_1(N)\arrow[swap]{r}{i_\ast}&H_1(L)\oplus H_1(M)\arrow{r}&H_1(\Sigma_g)\arrow{r}&0
    \end{tikzcd}
\]
Note that the zero at the end is due to the preceding remark.
Again putting everything we already know into it gives
\[
    \begin{tikzcd}
        0\arrow{r}&H_2(\Sigma_g)\arrow{r}&\mathbb Z\arrow{r}{i_\ast}&\mathbb Z^{2g}\arrow{r}&H_1(\Sigma_g)\arrow{r}&0
    \end{tikzcd}
\]
The exactness means that $H_2(\Sigma_g)\cong\ker i_\ast$ and $H_1(\Sigma_g)\cong\operatorname{coker}i_\ast=\mathbb Z^{2g}/\operatorname{Im}i_\ast$.
So we just need to understand $i_\ast:\mathbb Z\to\mathbb Z^{2g}$, or in other words $i_\ast:H_1(S^1)\to H_1(\Sigma_g^\star)$.
Note that the generator of $H_1(S^1)$ is the cycle runs through it (which is basically the one corresponding to the generator of $\pi_1(S^1)$).
The deformation retract of $\Sigma_g^\star$ to $\Gamma_{2g}$ identifies $S^1$ with the image of the cycle under $\rho_g$.
Therefore $(\rho_g)_\ast$ maps the cycle to $[\alpha_1]+[\beta_1]-[\alpha_1]-[\beta_1]+\cdots+[\alpha_g]+[\beta_g]-[\alpha_g]-[\beta_g]=0$.
Therefore $i_\ast=0$ and hence $H_2(\Sigma_g)\cong\mathbb Z$ and $H_1(\Sigma_g)\cong\mathbb Z^{2g}$.
To conclude,
$$H_n(\Sigma_g)\cong\begin{cases}
    \mathbb Z\text{, if $n=0,2$}\\
    \mathbb Z^{2g}\text{, if $n=1$}\\
    0\text{, otherwise}
\end{cases}$$
which in particular allows us to know $g$ given the homology group of some $\Sigma_g$.\\
We also have the non-orientable surfaces $S_g$ which is given by $\Gamma_{g+1}\cup_\alpha D^2$ where $\alpha=\alpha_0^2\alpha_1^2\cdots\alpha_g^2$ where $\alpha_i$ is the generator of the $i^{th}$ circle in the bouquet.
Repeating the same process gives the exact sequence
\[
    \begin{tikzcd}[row sep=tiny]
        0\arrow{r}&H_2(\Sigma_g)\arrow{r}&H_1(S^1)\arrow{r}{i_\ast}&H_1(\Gamma_{g+1})\arrow{r}&H_1(\Sigma_g)\arrow{r}&0\\
        &&\mathbb Z\arrow[equal]{u}&\mathbb Z^{g+1}\arrow[equal]{u}&
    \end{tikzcd}
\]
So again $H_2(S_g)\cong\ker i_\ast$ and $H_1(S_g)\cong\operatorname{coker}i_\ast$.
Note that $i_\ast$ maps the generator of $H_1(S^1)$ to the nonzero element $2[\alpha_0]+\cdots+2[\alpha_g]$.
Hence $\ker i_\ast=0$ and $\operatorname{coker}i_\ast\cong\mathbb Z^{g+1}/(2,\ldots,2)\mathbb Z\cong\mathbb Z^g\oplus\mathbb Z/2\mathbb Z$.
In conclusion
$$H_n(S_g)\cong\begin{cases}
    \mathbb Z\text{, if $n=0$}\\
    \mathbb Z^g\oplus\mathbb Z/2\mathbb Z\text{, if $n=1$}\\
    0\text{, otherwise}
\end{cases}$$
In particular we can tell apart $S_g$ and $\Sigma_g$ as well.


\section{Diatomic gas}
The heat capacity is
\begin{align}
    c_v &= (6N + N)\frac{1}{2}k_B = \frac{7}{2}k_B N
\end{align}
Harmonic oscillator.
For $k_B T \gg \hbar\omega$, $\bar{E} = k_B T$.

Back to solids.
Debye model.
\begin{itemize}
    \item Normal modes have $\omega_k=v k$ for all 3 modes.
        2 transverse and 1 longitudinal.
    \item Brilluoin zone is a sphere
        \begin{align}
            3N = 3V \int^{k_D} \frac{d^3 k}{(2\pi)^3}
            =
            \frac{V}{2\pi^2} k_D^3
        \end{align}
        i.e. number of normal modes $3N$ equals number of fourier modes.
\end{itemize}
We used that
\begin{align}
    \sum_{\vec{k}} f(\vec{k}) \approx
    V \int \frac{d^3k}{(2\pi)^3} f(k)
\end{align}
If you don't know this,
we'll discuss it in detail soon.

The partition function is
\begin{align}
    Z &= \Tr e^{-\beta H}\\
    &=
    \prod_{k,\alpha}\sum_{n_k=0}^{\infty} e^{-\beta\hbar\omega\left( n_k +
    \frac{1}{2}
    \right)}
\end{align}
where each term is partition function for one oscillator for one oscillator of
frequency $\omega_k$.
Here $\alpha = 1,2,3$
are the polarizations.
So
\begin{align}
    Z &=
    \prod_{k,\alpha}
    \frac{e^{-\beta\hbar\omega/2}}{1 - e^{-\beta\hbar\omega_k}}
\end{align}
The free energy is
\begin{align}
    F &= -k_B T\ln Z\\
    &=
    k_B T
    \sum_{\vec{k},\alpha}
    \left[
    \ln\left( 1 - e^{-\beta\hbar\omega_k} \right)
    +
    \underbrace{\frac{\beta\hbar\omega_k}{2}}_{\textrm{negelect zero-point}}
    \right]\\
    &\approx
    3 k_B T V \int^{k_D} \frac{d^3k}{{(2\pi)}^3}
    \ln\left( 1 - e^{-\beta\hbar vk} \right)
\end{align}
We neglect the zero-point energy because it's independent of $T$.
The factor of 3 comes from the 3 polarizations.

The entropy is then
\begin{align}
    S &= \left.\frac{-\partial F}{\partial T}\right|_{V}\\
    &=
    -3k_B V \int^{k_D} d^3k\, \ln(\cdots)
    +
    \frac{3k_B T}{k_B T^2} V
    \int^{k_D} d^3k
    \frac{\hbar v k e^{-\beta\hbar v k}}{1 - e^{-\beta\hbar v k}}
\end{align}

And finally the energy is
\begin{align}
    E &= F + TS\\
    &=
    3V \int^{k_D} \frac{d^3k}{(2\pi)^3}
    \frac{\hbar vk}{e^{\beta\hbar vk} - 1}.
\end{align}
Then the heat capacity is
\begin{align}
    C_v &=
    \left.\frac{1}{N} \frac{\partial E}{\partial T}\right|_{V,N}\\
    &=
    \frac{1}{N} V
    \frac{\partial}{\partial T}
    \int_{0}^{k_D} \frac{d^3k}{(2\pi)^3}
    \frac{3\hbar\omega_k}{e^{\beta\hbar\omega_k} - 1}\\
    &=
    \frac{3V}{N} \frac{1}{k_B T^2}
    \int_{0}^{k_D} \frac{d^3k}{(2\pi)^3}
    \frac{(\hbar\omega_k)^2}{e^{\beta\hbar\omega_k} - 1}
    e^{\beta\hbar\omega_k}\\
    &=
    \frac{3V}{N} \frac{1}{2\pi^2}
    \frac{(\hbar v)^2}{k_B T^2}
    \left( \frac{k_B T}{\hbar v} \right)^5
    \int_{0}^{\beta\hbar vk} dx\,
    \frac{x^4}{\left( e^x - 1 \right)^2} e^x
\end{align}
which for $k_B T \ll \hbar v k_D$
gives
\begin{align}
    C_V &\approx
    3 \frac{V}{N}
    \left( \frac{k_B T^}{\hbar v} \right)^3
    \frac{1}{2\pi^2}
    \int_{0}^{\infty} dx\,
    \underbrace{\frac{x^4 e^{x}}{\left( e^x - 1 \right)^2}}_{4\pi^4/15}\\
    &=
    \frac{V}{N}
    k_B
    \frac{2\pi^2}{5}
    \left( \frac{k_B T}{\hbar v} \right)^3\\
    &\sim T^3
\end{align}
which is the $C_v \sim T^3$ law for low temperatures.


\section{Introduction}
\label{sec:intro}

We consider a rectangular area of dimension $W \times L$ subdivided into countries of dimension $w \times l$ each. We consider a cartesian reference system with $x$ pointing to the East and $y$ to the North as shown in figure~\ref{fig:area_subdivision}. The areas are indexed in left-to-right and bottom-up order.
For simplicity, we assume that these dimensions are given as integers, that $l$ divides $L$ and $w$ divides $W$ (the input will be checked).
We use MPI as the implementation technology, so we decide to assign each country to a separate MPI process to improve the regularity of the program, while still not preventing multiple processes to be assigned to the same node.
\begin{figure}[h]
    \centering
    \begin{subfigure}[c]{0.7\textwidth}
    \begin{tikzpicture}
        % grid
        \draw[xstep=2, ystep=1] (0,0) grid (8,4);
        % axes
        \draw[->] (0,0) -- (8.25,0) node[right] {$x$};
        \draw[->] (0,0) -- (0,4.25) node[above] {$y$};
        \node[below left] at (0,0) {$0$};
        % country dimensions
        \draw[<->] (0,-.25) -- node[below] {$w$} (2,-.25);
        \draw[<->] (-.25,0) -- node[left] {$l$} (-.25, 1);
        % world dimensions
        \draw[<->] (0,-.75) -- node[below] {$W$} (8,-.75);
        \draw[<->] (-.75,0) -- node[left] {$L$} (-.75, 4);
        % country indices
        \matrix[matrix of nodes, inner sep=0, anchor=south west,
        nodes={minimum width=2cm, minimum height=1cm, inner sep=0, align=center}]{
            12 & 13 & 14 & 15 \\
                8 &  9 & 10 & 11 \\
                4 &  5 &  6 &  7 \\
                0 &  1 &  2 &  3 \\
        };
    \end{tikzpicture}
    \end{subfigure}
    \begin{subfigure}[c]{0.29\textwidth}
    \begin{tikzpicture}
        \draw[step=2] (-.25,-.25) grid (2.25,2.25);
        \node           at (1,1) {$c$}; % center
        \node[above]    at (1,2) {$c + \left\lfloor \frac{W}{w} \right\rfloor$}; % north
        \node[right]    at (2,1) {$c + 1$}; % east
        \node[below]    at (1,0) {$c - \left\lfloor \frac{W}{w} \right\rfloor$}; % south
        \node[left]     at (0,1) {$c - 1$}; % west
    \end{tikzpicture}
    \end{subfigure}
    \caption{World subdivision into countries.}
    \label{fig:area_subdivision}
\end{figure}

\noindent
Our program then considers a population $N$ and an initial number of infected individuals $I$ which are uniformly distributed among the countries.

\paragraph{Motion}
The individuals will follow a linear motion with velocity $v$ (given in \si{m/s}), whose direction is randomly generated for each individual at the beginning of the simulation, and is kept also when moving from one country to another. When an individual is detected reaching (or surpassing) the world boundary it “bounces” onto it: the components of its speed perpendicular to the boundary will be inverted, as in figure~\ref{fig:boundary_bounce}.

\begin{figure}[h]
    \centering
    \begin{tikzpicture}[
        dot/.style={circle,fill,inner sep=1.25pt},
        >=stealth
    ]
        \node[dot, label=below:$p_s$] (p1) at (210:2) {};
        \node[dot,                  ] (p2) at (30:0) {};
        \node[dot,                  ] (p3) at (30:1) {};
        \node[dot, label=below:$p_d$] (p4) at (-30:1) {};
        \draw[-] (p1) -- (p2);
        \draw[->, dashed] (p2) -- (p3);
        \draw[->] (p2) -- (p4);
        \draw[-, very thick] (-2.5,0) -- (2.5,0);
        \draw[draw=none, pattern=north east lines] (-2.5,0) rectangle (2.5,.1);
    \end{tikzpicture}
    \caption{An individual bouncing on the world boundary}
    \label{fig:boundary_bounce}
\end{figure}

\paragraph{Infection and recovery}
If a susceptible individual $i$ spends at least $t_{infection} = \SI{10}{min}$ continuously at a distance $d_{ij}\leq d$ from an infected person $j$, it gets \emph{infected} too.
After being infected for $t_{recovery} = \SI{10}{day}$ a person will become \emph{immune}, so it can't be infected or infect others. It will become susceptible again after further $t_{immunity} = \SI{90}{days}$.

We only consider individuals in the same country when computing infections, in order to reduce the communication between processes. This means that if two individuals are closer than $d$ but in different countries, they won't be able to infect each other. This approximation should be acceptable in a realistic scenario where there are physical borders between countries and where $d$ is much smaller than the size of a country. However, the accumulated exposure time of an individual is not reset when passing the border.

\paragraph{Simulation step}
The position and status of each individual is computed with a granularity of $t_{step}$ seconds, with no interpolation in between.
We compute periodic logs of the status of the population as soon as the end of each day has been surpassed, therefore we assume that $t_{step} \ll \SI{1}{day}$. Also, in a realistic scenario we should have $t_{step} \cdot v \ll w,l$ so that an individual cannot span a whole country in few steps. However, we only enforce $t_{step} \cdot v \le w,l$, so that each country only needs to send individuals out to its neighbors.
The simulation ends when
\begin{enumerate*}[label=(\roman*)]
    \item $t \geq t_{target}$, or
    \item there are no infected people left
\end{enumerate*}.