\documentclass{article}
\usepackage{fullpage,amsmath,amssymb,amsthm,bbm}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{corollary}{Corollary}
\theoremstyle{definition}
\newtheorem{definition}{Definition}
\newtheorem{example}{Example}
\newtheorem{remark}{Remark}
\newtheorem{exercise}{Exercise}
\newtheorem{solution}{Solution}
\newcommand{\Geo}{\mathop{\text{\rm Geo}}}
\newcommand{\ER}{\mathop{\text{\rm ER}}}
\newcommand{\Pois}{\mathop{\text{\rm Pois}}}
\newcommand{\Bin}{\mathop{\text{\rm Bin}}}
\renewcommand{\P}{\mathbb P}
\newcommand{\E}{\mathbb E}
\newcommand{\1}{\mathbb I}
\newcommand{\Z}{\mathbb Z}
\newcommand{\R}{\mathbb R}
\newcommand{\Var}{\mathop{\text{\rm Var}}}
\newcommand{\Exp}{\mathop{\text{\rm Exp}}}
\newcommand{\Uni}{\mathop{\text{\rm Uni}}}
\newcommand{\BL}{\mathop{\text{\rm BL}}}
\usepackage{hyperref}
\ProvidesPackage{hyperrefextras}
\hypersetup{
  colorlinks=true,
}
\newcommand{\eqhref}[1]{\hyperref[#1]{Equation \eqref{#1}}}
\newcommand{\thmhref}[1]{\hyperref[#1]{Theorem \ref{#1}}}
\newcommand{\defnhref}[1]{\hyperref[#1]{Definition \ref{#1}}}
\newcommand{\lemhref}[1]{\hyperref[#1]{Lemma \ref{#1}}}
\newcommand{\corlhref}[1]{\hyperref[#1]{Corollary \ref{#1}}}
\newcommand{\sechref}[1]{\hyperref[#1]{Section \ref{#1}}}
\newcommand{\fighref}[1]{\hyperref[#1]{figure \ref{#1}}}
\newcommand{\alghref}[1]{\hyperref[#1]{Algorithm \ref{#1}}}
\newcommand{\tabhref}[1]{\hyperref[#1]{table \ref{#1}}}
\newcommand{\exerhref}[1]{\hyperref[#1]{Exercise \ref{#1}}}
\newcommand{\examhref}[1]{\hyperref[#1]{Example \ref{#1}}}
\begin{document}
\begin{flushright}
  Recitation 10--v3\\
  \today\\
  Ross Anderson
\end{flushright}

\section{Equality in Distribution and Convergence in Distribution}

\subsection{Review of Definitions}

In this section, we consider several perspectives on what it means for
two random variables to be ``equal'' or converging.  First we review
some definitions from class.
\begin{definition}
  Given two random variables $X$ and $Y$, we say that $X =_d Y$ or $X$
  is \emph{equal in distribution} to $Y$ if for every $t \in \R$, $P(X \leq
  t) = \P(Y \leq t)$.
\end{definition}
\begin{small}
\begin{example}
  Suppose that $X \sim \Bin(n,1/2)$ and $Y = n-X$.  We claim that $X =_d Y$.  For all $t$,
  letting $k = \lfloor t \rfloor$, we have
  \begin{align*}
    \P(X \leq k) = \sum_{i=0}^k \binom n i \left(\frac{1}{2}\right)^i
    \left(1 - \frac 12\right)^{n-i} =\left(\frac{1}{2}\right)^n
    \sum_{i=0}^k \binom n i
  \end{align*}
  and
  \begin{align*}
    \P(Y \leq k) &= \P(n-X \leq k) =\P(X \geq n- k) = \sum_{i=n-k}^n
    \binom n i \left(\frac{1}{2}\right)^i \left(1 - \frac
      12\right)^{n-i} =\left(\frac{1}{2}\right)^n \sum_{i=n-i}^n
    \binom n i\\
    &= \left(\frac{1}{2}\right)^n \sum_{i=n-i}^n \binom {n}
    {n-i} = \left(\frac{1}{2}\right)^n \sum_{j=0}^k \binom {n} {j} = \P(X \leq k).
  \end{align*}
  where we have used the identity $\binom{n}{i} = \binom{n}{n-i}$.
\end{example}
\end{small}
This can be generalized relatively easily:
\begin{exercise}
  For any random variable $Z$ that is symmetric about zero ($\P(Z \geq
  t) = \P(Z \leq -t)$ for all $t \geq 0$, prove that $Z =_d -Z$.  Then
  reprove the claim in the above example by considering the random
  variable $X - n/2$.
\end{exercise}
Note that while in these examples, it was the case that both $X$ and $Y$ lived on a common
probability space, this in general need not be the case.
\begin{definition}
  Given a sequence of random variables $X_n$ and a random variable $X$,
  we say that $X_n \to_d X$ or $X_n$ \emph{converges in distribution} to $X$
  if for every $t \in \R$ such that $\P(\{X = t\}) = 0$, we have that
  \begin{align*}
    \lim_{n \to \infty} \P(X_n \leq t) = \P(X \leq t).
  \end{align*}
\end{definition}
\begin{small}
\begin{example}
  Suppose that $X_n$ is a the discrete random variable that for $i =
  1,\ldots,n$, satisfies $\P(X = i/n) = 1/n$, and that $X \sim
  \Uni(0,1)$.  We claim that $X_n \to_d X$.  As $X$ is continuous (and
  thus $\P(X = t) = 0$ for all $t$), we must check convergence of the
  cdf at every $t$ in the support of $X$.  For $t \in (0,1)$, we have
  \begin{align*}
    \left|\left\{\frac{i}{n} \colon \frac i n \leq t\right\}\right| =
    \lfloor t n \rfloor
  \end{align*}
  thus
  \begin{align*}
    \lim_{n \to \infty} \P(X_n \leq t) = \lim_{n \to \infty}
    \left|\left\{\frac{i}{n} \colon \frac i n \leq t\right\}\right|
    \frac{1}{n} = \lim_{n \to \infty}\frac{\lfloor t n\rfloor}{n} = t
  \end{align*}
  showing the claim.
\end{example}
\end{small}

\subsection{Equivalent Notions}

For a random variable $X$, we let $\phi_X(t) = \E[\exp(itX)]$ be the
\emph{characteristic function}, $M_X(t) = \E[\exp(tX)]$ be the
\emph{moment generating function}, and $\mathcal L_X(t) =
\E[\exp(-tX)]$ be the \emph{Laplace transform}.  In lecture 18, we stated that $X =_d Y$ iff
$\phi_X(t) =\phi_Y(t)$ for $t \geq 0$, and that a sequence of random
variables $X_n \to_d X$ iff $\phi_{X_n}(t) \to \phi_X(t)$ as $n \to
\infty$ for all $t \geq 0$.  We have some analogous results for moment
generating functions and Laplace transforms, under the assumption that the moment generating
function was defined.

The notion that $\E[f(X)]$ for all $f \in F$ determines the
distribution of $X$ is actually quite general.  Taking $F =\{\1(x \leq
t) \mid t \in \R\}$, we can generalize our original definition of
equality in distribution.  However, for convergence in distribution to
$X$, we need the more carefully chosen set of $F =\{\1(x \leq t) \mid
t \in \R, F_X \text{ is continuous at } t\}$.  We can extend this idea
to other sets $F$ as follows.

\begin{theorem}
  \label{thm:equality}
  Given random variables $X$ and $Y$, the following are equivalent:
  \begin{enumerate}
  \item [a.] $X =_d Y$
  \item [b.] For all $t \geq 0$, $\phi_X(t) = \phi_Y(t)$
  \item [c.] For a sequence $t_n \to 0$, for all $n$, $\phi_X(t_n) = \phi_Y(t_n)$
  \item [d.] For every bounded continuous function $f$, $\E[f(X)] = \E[f(Y)]$.
  \item [e.] For every bounded Lipschitz function $f$, $\E[f(X)] = \E[f(Y)]$.
  \item [f.] For every function $f$ with $\|f\|_{\BL} \leq 1$, $\E[f(X)] = \E[f(Y)]$.\footnote{
  {\bf Aside: Bounded Lipschitz Functions.} A function $f \colon \R \to \R$ is $\alpha$-Lipschitz if for all $x, y \in \R$,
\begin{align*}
  |x - y| \leq \alpha |f(x) - f(y)|.
\end{align*}
We let
\begin{align*}
  \|f\|_L = \inf\{\alpha \geq 0 \mid f_L = \inf\{\alpha \geq 0 \mid
  \forall x,y \in \R,\, |x - y| \leq \alpha |f(x) - f(y)|\}.
\end{align*}
be the smallest Lipschitz constant a function satisfies, or $\infty$
if $f$ satisfies no Lipschitz constant.  We let
\begin{align*}
  \|f\|_\infty = \sup_{x \in \R} |f(x)|,
\end{align*}
and
\begin{align*}
  \|f\|_{\BL} = \|f\|_L + \|f\|_\infty.
\end{align*}
It turns out that $\|\cdot\|_{\BL}$ is in fact a
\href{http://en.wikipedia.org/wiki/Metric_(mathematics)}{metric}, \cite{dudley2002real}, although the proof of this is
beyond the scope of our discussion.
}
  \end{enumerate}
  If in addition, $X \geq 0$ a.s. and $M_X(t) < \infty$ for $t > 0$, then the following
  are also equivalent:
  \begin{enumerate}
  \item [g.] For all $0 \leq s \leq t$, $\mathcal L_X(s) = \mathcal L_Y(s)$
  \item [h.] For a sequence $s_n \to 0$, for all $n$, $\mathcal L_X(s_n) = \mathcal L_Y(s_n)$
  \item [i.] For all $n \in \Z_+$, $\E[X^n] = \E[Y^n]$ (existence is implied by the finiteness of the m.g.f., see pset 7).
    \item [j.] For all $0 \leq s \leq t$, $M_X(s) = M_Y(s)$
  \item [k.] For a sequence $s_n \to 0$, for all $n$, $M_X(s_n) = M_Y(s_n)$
  \end{enumerate}
\end{theorem}
Note that the assumption $X \geq 0$ for (g)-(k) is essential,
otherwise for any random variable $Z$ with $\E[Z] = \infty$ but $Z
\geq 0$, by taking $X = -Z$ we could violate the theorem.  We give an
intermediate lemma that is of general use before giving the proof.
\begin{lemma}
  \label{lem:expectation}
  If $X =_d Y$, then for any measurable function $f$ where $\E[f(X)]$
  exists, $\E[f(X)] = \E[f(Y)]$.
\end{lemma}
\begin{proof}
  For convenience, we show this for $f \geq 0$ and leave the general
  case to the reader (consider the positive and negative parts
  separately).  For any interval $[a,b)$, we have $\P(f(X) \in [a,b))
  = \P(f(Y) \in [a,b))$ as $X =_d Y$.  Recalling the canonical
  construction for computing the expectation of a nonnegative quantity
  through simple functions (or instead by the MCT), we see that
  \begin{align*}
    \E[f(X)] = \lim_{n \to \infty} \sum_{i=0}^{n2^{n}}
    \frac i {2^n} \cdot\P\left(\frac{i}{2^n} \leq f(X) < \frac{i+1}{2^n}\right) = \lim_{n
      \to \infty} \sum_{i=0}^{n2^n} \frac i {2^n} \cdot\P\left(\frac{i}{2^n} \leq f(Y) <
      \frac{i+1}{2^n}\right) = \E[f(Y)],
  \end{align*}
  showing the claim.
\end{proof}
\begin{proof}[Proof of \thmhref{thm:equality}]
  By \lemhref{lem:expectation}, it is immediate that (a) implies (b)-(k).

  We have (a) implies (b), it is immediate that (b) implies (c),
  and that (c) implies (a) requires the use of complex analysis and is
  beyond the scope of this course (see \cite{durrett2010probability}
  and also lecture 16, the inversion theorem).  The proof that (c)
  implies (a) uses the concept of \emph{analytic continuation}, that a
  complex differentiable function is determined entirely by its values
  on a convergence sequence.

  Similarly, under the assumption that $M_X(t) < \infty$ for $t > 0$,
  we have that (a) implies (j), (j) immediately implies (k), and that
  (k) implies (a) requires the same tools from complex analysis.
  Again (a) implies (g) implies (h), and that (h) implies (a) requires
  complex analysis.

  Next, (a) implies (i) as $M_X(t) < \infty$ for $t > 0$ implies finiteness of moments.  We now claim that
  (i) implies (j).  For any $0 \leq s \leq t$ where $M_X(t) < \infty$,
  let
  \begin{align*}
    S_n &= \sum_{k=0}^n \frac{(sX)^k}{k!}& S &= \exp(sX),\\
    T_n &= \sum_{k=0}^n \frac{(sY)^k}{k!}& T &= \exp(sY).\\
  \end{align*}
  As $\E[S] = M_X(s)$ and $\E[T] = M_Y(s)$, it suffices to show that $\E[S] = \E[T]$.
  By linearity of expectation, we have
  \begin{align}
    \label{eq:sameSeq}
    \E[S_n] &= \sum_{k=0}^n \frac{s^k\E[X^k]}{k!} = \sum_{k=0}^n \frac{s^k\E[Y^k]}{k!} = \E[T_n],
  \end{align}
  where the middle equality follows from (i).  Thus if $\E[S_n] \to
  \E[S]$ and $\E[T_n] \to \E[T]$ as $n \to \infty$, then we have shown
  the claim.  By construction, $S_n \to S$ a.s. and $T_n \to T$ a.s.
  As $X,Y \geq 0$, the convergence is monotone from below, thus by the
  MCT, $\E[S_n] \to \E[S]$ and $\E[T_n] \to \E[T]$.  As by
  \eqref{eq:sameSeq}, these sequences are the same, this implies that
  $\E[S] = \E[T]$.

  We already have (a) implies (d), and (d) implies (e) implies (f) as
  every Lipschitz function is continuous.  To obtain (f) implies (a),
  we must show that for every $a \in \R$ that $\P(X > a) = \P(Y
  > a)$.  For $n = 1,2,\ldots,$ let $f_n$ be the continuous piecewise linear function
  \begin{align*}
    f_n(x) =
    \begin{cases}
      0& x < a \\
      \text{linear}& a \leq x \leq a + \frac 1n\\
      1 & x > a+\frac 1n.
    \end{cases}
  \end{align*}
  Observe that $f_n \to \1(x > a)$ pointwise and monotonically from
  below, thus $\E[f_n(X)] \to \E[\1(X > a)] = \P(X > a)$ as $n \to
  \infty$, and likewise $\E[f_n(Y)] \to \P(Y > a)$ as $n \to \infty$.
  Thus it suffices to show that $\E[f_n(X)] = \E[f_n(Y)]$ for all $n$.
  Note that $\|f_n\|_{\sup} = 1$ and $\|f\|_L = n$, so $\|f\|_{\BL} =
  n+1$, thus had we assumed (e), we would be done.  However, letting
  $g_n(x) = f_n(x)/(n+1)$, we have that $\|g_n\|_{\BL} \leq 1$, so
  \begin{align*}
    \E[f_n(X)] = \E\left[(n+1) g_n(X)\right] = (n+1)\E\left[ g_n(X)\right] = (n+1)\E\left[ g_n(Y)\right] = \E[f_n(Y)],
  \end{align*}
  giving the result.
\end{proof}
\begin{small}
  \begin{example}
  Recall from your homework the following problem: if $X_1,\ldots,X_n$
  are i.i.d. $\Exp(\lambda)$, show that $S_n = X_1 + \cdots + X_n$
  (the Erlang distribution) has density for $x \geq 0$ of
  \begin{align*}
    f_{S_n}(x) = \frac{\lambda^n x^{n-1} \exp(-\lambda x)}{(n-1)!}.
  \end{align*}
  In your homework, we asked you to compute the $n$-fold convolution,
  but it is much easier to just use the equivalence of (a) and (g).
  We have that for all $s < \lambda$,
  \begin{align*}
    \E\left[\exp\left(s \sum_{i=1}^n X_i\right)\right] = \E[\exp(s
    X_1)]^n = \left(1 - \frac t \lambda\right)^{-n} = \int_0^\infty
    \exp(sx)\frac{\lambda^n x^{n-1} \exp(-\lambda x)}{(n-1)!}dx
  \end{align*}
  (integration by parts $n$ times yields that $\int_0^\infty x^n \exp(-x)dx = n!$).
\end{example}
\end{small}
This result extends nicely to convergence in distribution.  In fact,
we see that (b)-(f) extend more intuitively than (a) does.
\begin{theorem}
  \label{thm:convergence}
 Given a sequence of random variables $X_n$ and a random variable $X$, the following are equivalent:
  \begin{enumerate}
  \item [a.] $X_n \to_d X$
  \item [b.] For all $t \geq 0$, $\lim_{n \to \infty} \phi_{X_n}(t) = \phi_X(t)$
  \item [c.] For a sequence $t_m \to 0$, for all $m$, $\lim_{n \to \infty} \phi_{X_n}(t_m) = \phi_X(t_m)$
  \item [d.] For every bounded continuous function $f$, $\lim_{n \to \infty}\E[f(X_n)] = \E[f(X)]$.
  \item [e.] For every bounded Lipschitz function $f$, $\lim_{n \to \infty}\E[f(X_n)] = \E[f(X)]$.
  \item [f.] For every function $f$ with $\|f\|_{\BL} \leq 1$, $\lim_{n \to \infty}\E[f(X_n)] = \E[f(X)]$.
  \end{enumerate}
  If in addition, we have {\bf Assumption A1:} $X_n \geq 0$ a.s. and there exists $t > 0$ such that for all $n$, $M_{X_n}(t), M_X(t) < \infty$, then the following
  are also equivalent:
  \begin{enumerate}
    \item [g.] For all $0 \leq s \leq t$, $\lim_{n \to \infty} \mathcal L_{X_n}(s) = \mathcal L_{X}(s)$
  \item [h.] For a sequence $s_m \to 0$, for all $m$, $\lim_{n \to \infty} \mathcal L_{X_n}(s_m) = \mathcal L_X(s_m)$
  \end{enumerate}
  If in addition to Assumption A1, we have {\bf Assumption A2:} $\sup_n \E[X_n^p] < \infty$ for all $p \in \Z_+$, then the following is also equivalent:
  \begin{enumerate}
  \item [i.] For all $m \in \Z_+$, $\lim_{n \to \infty} \E[X_n^m] =
    \E[X^m]$.
  \end{enumerate}
  If in addition to Assumption A1, we have {\bf Assumption A3:} that $\sup_n \E[\exp(tX_n)] < \infty$, then the following is also equivalent:
  \begin{enumerate}
\item [j.] For all $0 \leq s < t$, $\lim_{n \to \infty} M_{X_n}(s) = M_{X}(s)$
  \item [k.] For a sequence $s_m \to 0$, for all $m$, $\lim_{n \to \infty} M_{X_n}(s_m) = M_X(s_m)$
  \end{enumerate}
\end{theorem}
Note that Assumptions A2 and A3 cannot be weakened if we want a
necessary and sufficient condition, as (i) alone implies A2 and (j)
implies A3.  In particular, we do not need to verify A2 if we are only
using the direction that (i) implies (a), and likewise for A3 and (j)
implies (a).  This idea will be further explored in
\lemhref{lem:tech}.

The equivalence of (a) with (d)-(f) is part of the
\href{http://en.wikipedia.org/wiki/Portmanteau_theorem}{Portmanteau
  Theorem}, which adds several more equivalences.  The word
Portmanteau is French and has an
\href{http://en.wikipedia.org/wiki/Portmanteau#Origin}{interesting
  etymology}.  We only prove the equivalence of (a) with (d)-(f), but
show that (a) implies (b)-(h).  In the appendix we show
that (a) implies (i)-(k) and that any of (b)-(c) or (g)-(k) imply (a) using
more sophisticated tools.
\begin{proof}[Proof of (a) implies (b)-(h), (d)-(f) implies (a)]
  First, we claim that for any bounded continuous function $f$, $X_n
  \to_d X$ implies \mbox{$\lim_{n \to \infty} \E[f(X_n)] = \E[f(X)]$,} thus
  (a) implies (b)-(h), noting that $f(x) = \exp(itx) = \sin(tx) +
  i\cos(tx)$ is a bounded continuous function.  Recall the Skorokhod
  representation theorem (Theorem 1 of Lecture 18), that $X_n \to_d X$
  implies that there exists $Y_n =_d X_n$ and $Y =_d X$ such that $Y_n
  \to Y$ a.s.  For any bounded function $f$, by the Bounded
  Convergence Theorem (BCT), $\lim_{n \to \infty} \E[f(Y_n)] =
  \E[f(Y)]$.  Finally, as from \thmhref{thm:equality} where (a)
  implies (d), we have that $\E[f(X_n)] = \E[f(Y_n)]$ and $\E[f(X)] =
  \E[f(Y)]$, thus
  \begin{align*}
    \lim_{n \to \infty} \E[f(X_n)] =\lim_{n \to \infty} \E[f(Y_n)] = \E[f(Y)] =  \E[f(X)]
  \end{align*}
  showing the claim.

  We have that (a) implies (d), and it is immediate that (d) implies
  (e) implies (f), so it suffices to show that (f) implies (a).
  Instead we will show that (e) implies (a).  A similar trick to the
  proof from \thmhref{thm:equality} allows us to show (f) implies (a)
  instead.  Fixing $a \in \R$, we need to prove that $\lim_{n \to
    \infty} \P(X_n \leq a) = \P(X \leq a)$.  Following
  \cite{durrett2010probability}, for every $\epsilon > 0$, we define
  the piecewise linear continuous functions
  \begin{align*}
    g_{t,\epsilon}(x) =
    \begin{cases}
      1 & x \leq t\\
      \text{linear} &t < x < t + \epsilon\\
      0 & x \geq t + \epsilon
    \end{cases}
  \end{align*}
  To show the desired limit, we can show that both $\limsup$ and
  $\liminf$ equal the limit.  For every $\epsilon > 0$, we have
  \begin{align*}
    \limsup_{n \to \infty} \P(X_n \leq a) = \limsup_{n \to \infty} \E[\1(X_n \leq a)] \leq  \limsup_{n \to \infty} \E[g_{a,\epsilon}(X_n)] = \E[g_{a,\epsilon}(X)] \leq \P(X \leq a + \epsilon),
  \end{align*}
  where the final equality follows as $g_{a,\epsilon}$ is a bounded
  Lipschitz function, so by (f) the $\limsup$ is just the
  limit. Taking $\epsilon \to 0$, we have $\limsup_{n \to \infty}
  \P(X_n \leq a) \leq \P(X \leq a + \epsilon)$ as the cdf is always right continuous.  Similarly,
  \begin{align*}
    \liminf_{n \to \infty} \P(X_n \leq a) \geq \liminf_{n \to \infty} \E[g_{a-\epsilon,\epsilon}(X_n)] = \E[g_{a-\epsilon,\epsilon}(X)] \geq \P(X \leq a - \epsilon).
  \end{align*}
  Finally, as the cdf is left continuous at $a$ iff $\P(X = a) =0$, we
  have that when $a$ is a continuity point, then by taking $\epsilon
  \to 0$, we have that $\liminf_{n \to \infty} \P(X_n \leq a) \geq
  \P(X \leq a)$.  This shows the result.
\end{proof}
You may have noticed that the proof that (f) implies (a) in
\thmhref{thm:convergence} requires a construction which is very
similar to the proof that (f) implies (a) in \thmhref{thm:equality}.
In the appendix, more sophisticated and general tools are introduced
that allow us to eliminate this redundancy with an alternate proof.

Note that under slightly strong assumptions there is a simple proof that (i) implies (j)
much like that proof that (i) implies (j) from \thmhref{thm:equality}.
\begin{exercise}
  If $\lim_{n \to \infty} \sup_m | \E[X_n^m] - \E[X^m]| = 0$
  (i.e. $\E[X_n^m] \to \E[X^m]$ as $n \to \infty$ uniformly in $m$),
  then show that $0 \leq s \leq t$, $\lim_{n \to \infty} M_{X_n}(s) = M_{X}(s)$.
\end{exercise}
\begin{exercise}
  \label{exer:moreDomination}
  Without further qualification, (j) seems to be at least as strong as (i).
  \begin{enumerate}
  \item [a.]If $Z_n \to Z$ a.s., $\E[Z_n] \to \E[Z]$, $Y_n \leq Z_n$ a.s., and $Y_n \to Y$ a.s., show that $\E[Y_n] \to \E[Y]$.  \emph{Hint:} Uniform integrability (defined in the next section) may be helpful, see \cite{resnick1998probability} ``More domination'' page 183 for a solution.
  \item [b.] Assume only that $X_n \to_d X$ and that there exists $s>0$
    such that $\lim_{n \to \infty} M_{X_n}(s) = M_{X}(s)$.  Show that
    for all $p \in \Z_+$, $\E[X_n^p] \to \E[X^p]$.  \emph{Hint:}
    Equivalently, show that $\E[s^pX_n^p/p!] \to \E[s^pX^p/p!]$ for all $p$.
  \end{enumerate}
\end{exercise}
\begin{exercise}
  This exercise shows that without Assumptions A2 and A3, we would not have (a) implies (i)-(k).
  Consider the following probability space $(\Omega,\mathcal F, \P)$,
  $\Omega = (0,1)$, $\mathcal F$ is the Borel sets, $\P$ is the
  Lebesgue measure.  Let $X_n(\omega) = n$ for $\omega < 1/n$ and $0$
  otherwise.  Let $X = 0$.  Show that $X_n \to X$ a.s., $\mathcal
  L_{X_n}(s) \to L_{X}(s)$ for all $s$, but that $M_{X_n}(s) \not \to
  M_X(s)$ for any $s > 0$ and $\E[X_n^p] \not \to \E[X^p]$ for any $p
  \geq 1$.
\end{exercise}
\begin{exercise}
  What is the relationship between assumptions A2 and A3?
  \begin{enumerate}
  \item [a.] Show that Assumption A3 implies assumption A2 (very similar \exerhref{exer:moreDomination} part b).
  \item [b.] (Open question, for the author) Does Assumption A2 imply A3?
    Contact me if you have a solution.
  \end{enumerate}
\end{exercise}
\begin{exercise}[Continuous Mapping]
  \label{exer:continuousMapping}
  If $X_n \to_d X$, show that for any continuous function $f$, $f(X_n)
  \to f(X)$.  \emph{Hint:} use the equivalence of (a) and (d), or
  Skorokhod's Representation Theorem and \thmhref{thm:equality}.  Then
  improve your result: if $A \subset \R$ is the set of points in the
  domain of $f$ such that $f$ is continuous, show that the claim still
  holds if $\P(X \in A) = 1$.
\end{exercise}

\section{Appendix--Convergence in Distribution and  Moments}

This section uses slightly more sophisticated tools than what we
covered in this course, and is only intended as a reference for the
curious.

\subsection{Uniform Integrability, \thmhref{thm:convergence} (a) implies (i)-(k)}

In this section, we prove some of the missing parts of
\thmhref{thm:convergence}, that (a) implies (i)-(k).  Looking back at
the proof that (a) implies any of the other parts of
\thmhref{thm:convergence}, we used the following technique: upgrade
convergence in distribution to almost sure convergence using the
Skorokhod Representation Theorem, and then apply a convergence theorem
to interchange the expectation and the limit.  In the parts proven
thus far, we were able to use the bounded convergence theorem, but for
$X_n^p$ and $\exp(sX_n)$, this of course will not apply.  As a
dominating function is not easily identified here, we need to first
introduce a stronger convergence theorem than the DCT, the Vitali
Convergence Theorem.
\begin{definition}
  A set of random variables $\mathcal X$ is $p$-uniformly integrable if
  \begin{align}
    \label{eq:uniformIntegrability}
    \lim_{t \to \infty} \sup_{X \in \mathcal X} \E[|X|^p\1(|X|^p > t)] = 0.
  \end{align}
\end{definition}
\begin{theorem}[Vitali]
  \label{thm:vitali}
  Suppose that $X_n$ is a sequence of random variables and $X$ is a random variable.  Then the following are equivalent:
  \begin{enumerate}
  \item [a.] $X_n$ is $p$-uniformly integrable and $X_n \to X$ i.p.
  \item [b.] $\lim_{n \to \infty} \E[|X_n - X|^p] = 0$.
  \item [c.] $\E[X_n^p] \to \E[X^p]$ and $X_n \to X$ i.p.
  \end{enumerate}
\end{theorem}
The equivalence between (b) and (c) is much easier to show and not
typically attributed to Vitali.  The convergence in b., that $\lim_{n
  \to \infty} \E[|X_n - X|^p] = 0$ is usually called $L_p$.
\begin{lemma}[Crystal Ball Condition]
  \label{lem:crystal}
  For $X_n,X \geq 0$, if $X_n \to_d X$ and there exists $t>0$ such that
  $\sup_{n} \E[X_n^t] < \infty$, then for all $s < t$, $\lim_{n \to
    \infty} \E[X_n^s] = \E[X^s]$.
\end{lemma}
While a proof of this result can be found in any text, I have only ever seen it by this name in \cite{resnick1998probability}.
\begin{proof}
  Instead, we will show the result for all $s \leq t/2$.  A proof in
  the general case is almost the same, but H\"older's Inequality is
  substituted for Cauchy-Schwarz.

  First, as $X_n \to_d X$ iff $X_{n}+1 \to_d X+1$ and $\sup_{n}
  \E[X_n^t] < \infty$ iff $\sup_{n} \E[(X_n+1)^t] < \infty$, WLOG we can
  assume that $X_n \geq 1$.  By the Skorokhod Representation Theorem,
  there exists $Y_{n}$ and $Y$ with $Y_{n} =_d X_{n}$ for all $i$ and
  $Y =_d X$ such that $Y_{n} \to Y$ a.s., and thus in probability as
  well.  By \lemhref{lem:expectation}, it suffices to instead prove
  that for all $f \in F$, $\lim_{n \to \infty} \E[Y_{n}^s] = \E[Y^s]$.
  By \thmhref{thm:vitali}, it suffices to show that $Y_{n}$ are
  $s$-uniformly integrable for $s \leq t/2$.  Recalling that $Y_{n}
  \geq 1$, we verify \eqref{eq:uniformIntegrability} as
  \begin{align}
    \label{eq:cauchy}
    \lim_{t \to \infty} \sup_n \E[Y_{n}^s \1(Y_{n}^s \geq t)] &\leq \lim_{t \to \infty} \sup_n \sqrt{\E[Y_{n}^{2s}]\E[\1(Y_{n}^s \geq t)^2]}\\
    \nonumber
    &\leq \lim_{t \to \infty}\sqrt{\sup_n \E[Y_{n}^{2s}]}\sqrt{\sup_n\P(Y_{n}^s > t)}\\
    \label{eq:markov}
    &\leq \lim_{t \to \infty}\sqrt{\sup_n \E[Y_{n}^{2s}]}\sqrt{\sup_n\frac{\E[Y_{n}^s]}{t}}\\
    \label{eq:st}
    &\leq \sqrt{\sup_n \E[Y_{n}^{t}]} \sqrt{\sup_n\E[Y_{n}^t]}\lim_{t \to \infty} \frac{1}{\sqrt{t}}\\
    \label{eq:bounded}
    & =0,
  \end{align}
  showing the claim.  In \eqref{eq:cauchy} we used Cauchy-Schwarz and
  in \eqref{eq:markov} we used Markov.  We have \eqref{eq:st} as
 $X_n =_d Y_n$ and \lemhref{lem:expectation} imply that
  $\E[Y_{n}^{2s}] = \E[X_{n}^{2s}] \leq \E[X_n^t] = \E[Y_n^t]$ and $\E[Y_n^{s}] = \E[X_n^s] \leq \E[X_n^t] = \E[Y_n^t]$ (as $s \leq t/2$ and $X_n \geq 1$).
  Finally, to justify \eqref{eq:bounded}, recall that we assumed
  $\sup_n \E[X_n^t] < \infty$.
\end{proof}

\begin{proof}[Proof of \thmhref{thm:convergence} (a) implies (i),(j)]
  First we prove that (a), $X_n \to_d X$, Assumption A1, and
  Assumption A2, imply (i), that for all $m$, $\lim_{n \to \infty}
  \E[X_n^m] = \E[X^m]$.  For each $m$ and apply \lemhref{lem:crystal} with
  $t= m+1$ and $s = m$, where $\sup_n \E[X_{n}^{m+1}]$ is finite by
  Assumption A2.

  Similarly, we now show that (a), Assumption A1, and Assumption A3
  imply (j), that for $0 \leq s < t$, $\lim_{n \to \infty}
  \E[\exp(sX_n)] \to \E[\exp(sX)]$.  Let $Y_n = \exp(sX_n)$ and $Y =
  \exp(sX)$.  By \exerhref{exer:continuousMapping}, we have that $Y_n
  \to_d Y$.  As $Y_n^{1+\epsilon} = \exp(s(1+\epsilon)X_n)$ and $s < t$, we have by Assumption A3 that
  \begin{align*}
    \sup_n \E[Y_n^{1+\epsilon}] \leq \sup_n \E[\exp(tX_n)] < \infty,
  \end{align*}
  thus by \lemhref{lem:crystal}, $\E[Y_n] \to \E[Y]$, showing the claim.
\end{proof}

\subsection{Tightness, \thmhref{thm:convergence} (b)-(k) imply (a)}

In this section we complete the missing parts of the proof of
\thmhref{thm:convergence}, showing that any of (b)-(k) each imply (a).  First, we
define a condition on a set of random variables called
\emph{tightness}, and then prove that tightness implies that every
sequence of random variables drawn from the set has a (weakly)
convergent subsequence.  Thus tightness for a set of random variables
is analogous to compactness for a set of real numbers.

Next, we give a result suggested by \cite{billingsley2009convergence} that we
then use repeatedly to prove the desired implications in
\thmhref{thm:convergence}. Informally, the result is as
follows: Suppose that for a class of functions $F$, we have already
shown that
\begin{enumerate}
\item {\it Uniqueness:} $X =_d Y$ iff for all $f \in F$, $\E[f(X)] = \E[f(Y)]$ (as in
  \thmhref{thm:equality}).
\item {\it Forward Result:} If $X_n \to_d X$, then for all $f\in F$, $\E[f(X_n)] \to
  \E[f(X)]$ (the ``forward half'' of \thmhref{thm:convergence}).
\item {\it Tightness:} $\E[f(X_n)] \to \E[f(X)]$ for all $f \in F$ implies that the random variables $X_n$ are \emph{tight}.
\end{enumerate}
Then the ``backward result'' (the converse to the second point above)
also holds.  Namely, if for all $f \in F$, $\E[f(X_n)] \to \E[f(X)]$,
then $X_n \to_d X$.  A precise statement is given later. In addition
to filling in missing gaps in the proof, we this technique also gives
alternate proof that any of (d)-(f) imply (a).
\begin{definition}
  A set of random variables $\mathcal X$ is \emph{tight} if for every
  $\epsilon$ there exists $t$ such for all $X \in \mathcal X$, $\P(|X| >
  t) < \epsilon$, i.e.
  \begin{align*}
    \lim_{t \to \infty} \sup_{X \in \mathcal X} \P(|X| > t) = 0.
  \end{align*}
\end{definition}
\begin{theorem}[Prokhorov]
  \label{thm:prokhorov}
  A set of random variables $\mathcal X$ is sequentially (or relatively) compact
  w.r.t. weak convergence iff it is tight.  Namely, $\mathcal X$
  is tight iff for any sequence of random variables $X_n \in
  \mathcal X$, there exists a subsequence $X_{n_i}$ and a random
  variable $X$ such that as $i \to \infty$, $X_{n_i} \to_d X$.
\end{theorem}
Generally speaking, the result that tightness implies sequential
compactness will be the half of the theorem of interest.  For a short
proof, see \cite{billingsley2012probability}.  For a proof of a
significantly more general version of the theorem, see Section 5 of
\cite{billingsley2009convergence}; however, the proof is quite
complicated.  \thmhref{thm:prokhorov} provides an existence result,
that there is some limiting random variable.  The following two
exercises are often used to turn existence into uniqueness.
\begin{exercise}
  Prove that $X_n \to_d X$ iff for every subsequence $X_{n_i}$, there
  exists a further subsubsequence $X_{n_{i_j}}$ such that as $j \to
  \infty$, $X_{n_{i_j}} \to_d X$.  Hint: use the
  \thmhref{thm:convergence} (a) and (d).
\end{exercise}
\begin{exercise}
  \label{ex:limit}
  First, if $Y_n \to_d Y$ and $Y =_d Z$, prove that $Y_n \to_d Z$ as
  well (easy).  Then, let $X_n$ be a sequence of random variables that
  is tight and let $X$ be a random variable.  Suppose that for any
  convergent subsequence $n_i$ with $X_{n_i} \to_d \bar X$, we have
  $\bar X =_d X$ (and thus $X_{n_i} \to_d X$).  Prove that $X_n \to_d
  X$ as well.
\end{exercise}
A solution can be found in the proofs of Theorem 2.6 and the Corollary
to Theorem 5.1 in \cite{billingsley2009convergence}.  We our now ready
to return to the problem of interest, but first we give a technical
lemma suggested by the discussion following Example 5.5 from
\cite{billingsley2009convergence}, as described at the start of this
section.
\begin{lemma}
  \label{lem:tech}
  Given a set of random variables $\mathcal X$ and a set of measurable functions $F$,
  suppose that
  \begin{itemize}
  \item {\bf Uniqueness:} For a random variables $X \in \mathcal X$ and any random variable $Y$, if for all $f \in F$, $\E[f(X)] =\E[f(Y)]$, then $X =_d Y$,
  \item {\bf Forward Result:} For any random variable $Y$ and a
    sequence $\{X_n\}\subset \mathcal X$ with $X_n \to_d Y$, for all
    $f \in F$, $\lim_{n \to \infty} \E[f(X_n)] = \E[f(Y)]$,
  \item {\bf Tightness:} For any $X \in \mathcal X$ and a sequence
    $\{X_n\} \subset \mathcal X$, if for all $f \in F$, $\lim_{n \to
      \infty} \E[f(X_n)] = \E[f(X)]$, then it is implied that that
    $\{X_n\}$ is tight.
  \end{itemize}
  Then for any $X \in \mathcal X$ and any sequence of random variables
  $\{X_n\}\subset \mathcal X$, we have $\lim_{n \to \infty} \E[f(X_n)]
  = \E[f(X)]$ for all $f \in F$ implies $X_n \to_d X$.
\end{lemma}
Note that the uniqueness assumption will be true whenever $F$ is one
of the sets of functions considered in \thmhref{thm:equality}, and
likewise ``forward result'' assumption is part of that we have already
shown \thmhref{thm:convergence}, thus only the tightness assumption
will need to be verified for our purposes.  Often, $\mathcal X$ will
simply be the set of all random variables, but in some instances it
must be chosen carefully.
\begin{proof}
  Suppose that $X \in \mathcal X$ and $\{X_n\} \subset \mathcal X$ is
  a sequence such that for all $f \in F$, $\lim_{n \to \infty}
  \E[f(X_n)] = \E[f(X)]$.  Then by the tightness assumption, $\{X_n\}$
  is tight.  Thus by \thmhref{thm:prokhorov}, there exists a random
  variable $Y$ (not necessarily in $\mathcal X$) and a subsequence
  $n_i$ such that as $i \to \infty$, $X_{n_i} \to_d Y$.  Fix any such
  subsequence and $Y$.  For all $f \in F$,
  \begin{align}
    \label{eq:equalDist}
    \E[f(Y)] =  \lim_{i \to \infty}
    \E[f(X_{n_i})] = \lim_{n \to \infty} \E[f(X_n)] = \E[f(X)],
  \end{align}
  where the first equality follows by the ``forward result'' assumption, the second
  equality follows as the sequence $\E[f(X_n)]$ is convergent (by our initial
  assumption) and thus has the same limit as any subsequence, and
  the final equality follows again by our initial assumption.  Thus by
  \eqref{eq:equalDist} and the uniqueness assumption, we must have that $Y =_d
  X$.  Finally, as our choice of convergent
  subsequence was arbitrary, by \exerhref{ex:limit}, $X_{n} \to_d X$,
  giving the result.
\end{proof}
We can now prove the main results of the section:
\begin{proof}[Proof of \thmhref{thm:convergence}--(i)-(k) imply (a)]
  First we show that (i) implies (a).  Given $X_n$ and $X$, let
  \begin{align*}
    \mathcal X = \left\{Y \mathrel{\bigg \vert} \forall p \in \Z_+,\, \E[Y^p] \leq \sup_n\E[X_n^p]\right\}.
  \end{align*}
  Note that by Assumption A2, $\sup_n\E[X_n^p] < \infty$ for all $p$.
  Let $F = \{f(x) = x^p \mid p \in \Z_+\}$.  Let $X_n$ and $X$ be such
  that A1 and A2 hold and for every $f \in F$, $\lim_{n \to \infty}
  \E[f(X_n)] \to \E[f(X)]$.  We now want apply \lemhref{lem:tech} for
  $\mathcal X$ and $F$, which clearly would imply the result.  The
  uniqueness assumption holds from \thmhref{thm:equality} where (i)
  implies (a).  The forward result holds as for any sequence $\{X_n\}
  \subset \mathcal X$, we will have assumption A2 satisfied, so we can
  apply the result that (a) implies (i) as shown in the previous
  section.  Thus we need only check tightness.

  As $f(x) = x$ is in $F$, we have that $\E[X_n] \to \E[X] < \infty$.
  As argued previously, this implies that $\sup_n \E[X_n] < \infty$ as
  well.  Thus for all $n$, by Markov's inequality (recall that $X_n
  \geq 0$),
  \begin{align}
    \label{eq:applyMarkov}
    \P(X_n > t) \leq \frac{\E[X_n]}{t} \leq \frac{\sup_m \E[X_m]}{t}.
  \end{align}
  Thus $\{X_n\}$ is tight. Thus by \lemhref{lem:tech}, $X_n \to_d X$,
  showing (i) implies (a) as claimed.

  The argument that (k) implies (a) is essentially the same, expect
  that \eqref{eq:applyMarkov} is replaced by (for $s$ such that $\sup_n \E[\exp(sX_n)] <\infty$)
  \begin{align*}
    \P(X_n \geq t) = \P(\exp(s X_n) \geq \exp(s t)) \leq \frac{\E[\exp(sX_n)]}{\exp(s t)} \leq \frac{\sup_m \E[\exp(s X_m)]}{\exp(s t)}.
  \end{align*}
\end{proof}
This approach of showing existence through tightness then uniqueness
through \thmhref{thm:equality} is quite versatile.  While showing
tightness in these cases was quite easy, it is not always this way.
Now we apply the Lemma in a few interesting cases.
\begin{exercise}
  Assuming that (a) implies (f) from \thmhref{thm:convergence}, use
  \lemhref{lem:tech} to show that (f) implies (a).  \emph{Hint:} To
  show tightness, start by proving that (g) implies (a) by bounding
  $\P(X_n > t) \leq \E[f(X_n)]$ for some bounded Lipschitz function
  $f$.
\end{exercise}
In both the proofs of \thmhref{thm:equality} and
\thmhref{thm:convergence}, when showing (f) implies (a), we are
\emph{essentially} approximating $g_a(t) = \1(t<a)$ with bounded
Lipschitz functions $g_{a,\epsilon}(t)$.  This exercise is interesting
as we eliminate this redundancy (at the expense of using some heavier
tools) in this exercise.
\begin{exercise}
  Assuming (a) implies (b) and (g) from \thmhref{thm:convergence}, use
  \lemhref{lem:tech} to show that (b) and (g) each imply (a). \emph{Hint:} To
  show tightness, first prove the inequalities
  \begin{align*}
    \P\left(|X_n| \geq \frac 2 u\right) \leq \frac{1}{u} \int_{-u}^u (1-  \phi_{X_n}(t))dt
  \end{align*}
  and
  \begin{align*}
    \P\left(|X_n| \geq \frac 2 u\right) \leq \frac{1}{u} \int_{0}^u (1-  \mathcal L_{X_n}(t))dt
  \end{align*}
  This completes the proof of \thmhref{thm:convergence} (assuming we have \thmhref{thm:equality}).
\end{exercise}
To show tightness in (g) implies (a), see \cite{billingsley2009convergence} example 5.5.  To show tightness for (b) implies (a), see \cite{billingsley2012probability}, Theorem 26.3.
\begin{exercise}
  Suppose that $X_n$ and $X$ both have support on the integers.  Let
  $F = \{f(x) = \1(x = z) \mid z \in \Z\}$.  Prove that $X_n \to_d X$
  iff $\lim_{n \to \infty} \E[f(X_n)] = \E[f(X)]$ for all $f \in F$.
  (This problem was also on your homework, but now you have \lemhref{lem:tech}.)
\end{exercise}

\subsection{A slightly stronger ``backward'' result}

The Lemma below is exactly the same as \lemhref{lem:tech} except the assumption of the ``Forward Result'' has been made weaker.
\begin{lemma}
  \label{lem:techHard}
  Given a set of random variables $\mathcal X$ and a set of measurable functions $F$,
  suppose that
  \begin{itemize}
  \item {\bf Uniqueness:} For a random variables $X \in \mathcal X$
    and any random variable $Y$, if for all $f \in F$, $\E[f(X)]
    =\E[f(Y)]$, then $X =_d Y$,
  \item {\bf Restricted Forward Result:} For any $X \in \mathcal X$
    and a sequence $\{X_n\} \subset \mathcal X$ such that for all $f
    \in F$, $\E[f(X_n)] \to \E[f(X)]$, if $n_{i}$ is a subsequence and
    $Y$ is a random variable is such that $X_{n_i} \to_d Y$, then we
    have that for all $f \in F$, $\lim_{i \to \infty} \E[f(X_{n_i})] =
    \E[f(Y)]$,
  \item {\bf Tightness:} For any $X \in \mathcal X$ and a sequence
    $\{X_n\}\subset \mathcal X$, if for all $f \in F$, $\lim_{n \to
      \infty} \E[f(X_n)] = \E[f(X)]$, then it is implied that that
    $\{X_n\}$ is tight.
  \end{itemize}
  Then for any $X \in \mathcal X$ and any sequence of random variables
  $\{X_n\}\subset \mathcal X$, we have $\lim_{n \to \infty} \E[f(X_n)]
  = \E[f(X)]$ for all $f \in F$ implies $X_n \to_d X$.
\end{lemma}
The key idea is that since in the conclusion of the lemma, we will
assume that $\lim_{n \to \infty} \E[f(X_n)] = \E[f(X)]$ for all $f \in
F$, and since we were only applying the forward result to subsequences
of the sequence $X_n$, we can use this additional information to help
prove the forward result in the relevant special case.  This can be
very helpful if we are not exactly sure what we should take $\mathcal
X$ to be.  For example, we can apply Lemma 4 with $F = \{f(x) = x^p
\mid p \in \Z_+\}$ and $\mathcal X$ to be the set of random variables
satisfying Assumption A1, i.e. we can drop Assumption A2.  Note that
we do not get a stronger result, we just can be less careful in
choosing $\mathcal X$ when we apply the Lemma.

The proof is omitted as it is exactly the same as the proof
\lemhref{lem:tech}, since in the proof we only apply the ``forward
result'' when the ``restricted forward result'' would be sufficient.

\subsection{Notes and References}

The class of functions $F = \{f \colon \R \to \R \mid \|f\|_{\BL}\}$
is of particular interest because the quantity
\begin{align*}
  \rho(X,Y) = \sup_{f \in F} |\E[f(X)] - \E[f(Y)]|
\end{align*}
is a \href{http://en.wikipedia.org/wiki/Metric_(mathematics)#Definition}{metric} on the space of random variables.  See
\cite{dudley2002real} for more details.

The moment problem, one
of the most studied problems in probability, asks (1.) given a sequence of moments, does there
exist a random variable with these moments, and (2.) is this random
variable unique (often within a class of random variables).  In
\thmhref{thm:equality}, we simply gave the sufficient condition for
uniqueness that $\E[\exp(sX)]$ is finite, but much more can be said
here.

A closely related question asks: given a sequence of random variables
$X_n$ and a random variable $X$ with $\lim_{n \to \infty} \E[X_n^p] =
\E[X^p]$ for all $p \in \Z_+$, does $X_n \to_d X$?  This problem was
first addressed by \cite{frechet1931proof} (and potentially
\cite{wintner1928konvergenzbegriff}, German) under the name ``the
generalized second limit theorem,'' presumably where the ``second
limit theorem'' is the CLT, as this result was used when $X$ was the
standard normal and $X_n$ was an i.i.d. sum of random variables to
prove restricted versions of the CLT.  This problem has attracted
considerably less attention than the moment problem, and is often not
included in books, as lamented in \cite{rao1950generalized}.  In  \cite{rao1950generalized},
they obtain the same result as \thmhref{thm:convergence}, (a) iff (i),
although with a different approach.

An alternative approach is taken \cite{chung2000course} to prove that
(i) implies (a) using the weaker notion of \emph{vague convergence}
and
\href{http://www.math.ust.hk/~makchen/Math541/Chap2Sec1.pdf}{Helly's
  Selection Theorem} instead of tightness, see Theorem 4.5.5. and
``proof,'' (an anonymous ``standard theorem on the inversion of
repeated limits'' is heavily relied upon).  The proof of tightness for
$\R$ valued random variables given in
\cite{billingsley2012probability} relies on Helly's Selection Theorem
on $\R$, and the more general proof of \thmhref{thm:prokhorov} given
in \cite{billingsley2009convergence} is similar to the proof of a more
general version of Helly's Selection Theorem.  While we are using
slightly heavier machinery, tightness is a powerful tool that is frequently
used applied probability, while vague convergence lies somewhat below
our (``Applied Probability's'') radar.

In Section 5 of \cite{billingsley2009convergence}, Example 5.5
considers \lemhref{lem:tech} in the special case for $F = \{f(x) =
\exp(-sx)\mid s \geq 0\}$, the Laplace transform. Then, the key
assumptions of \lemhref{lem:tech} are identified, and the general
result of \lemhref{lem:tech} is suggested in high level terms.

\phantomsection
\bibliography{bibliography}{}
\bibliographystyle{plain}

\end{document}


\documentclass[bigger]{beamer}

\usepackage{tikz}
\usetikzlibrary{graphs, arrows, shapes}

\input{header-beam} % change to header-handout for handouts

% ====================
\title[Lecture 3]{Logic I F13 Lecture 3}
\date{September 17, 2013}
% ====================

\input{header}

\setlength{\fitchargwidth}{10cm}

\section{Arguments and Validity}

\subsec{What are Arguments?}{

\begin{block}{Arguments in Logic}

\fitcharg{
$\left.\begin{array}{@{}c@{}}
\formula{P}_1 \\
\vdots\\
\formula{P}_n
\end{array}
\right\} \quad \text{Premises}$}{
\formula{C\qquad  \text{Conclusion}}}
\end{block}
\bit
\item Argument is \emph{valid} if conclusion follows from (is a consequence of) the premises
\item Otherwise it is \emph{invalid}
\eit

}

\subsec{Logical Consequence}{

\bit
\item $C$ is a \emph{logical consequence} of $P_1$, \dots, $P_n$ iff $C$ is true in every possible world in which $P_1$, \dots, $P_n$ are all true together
\item NB: This does \emph{not} say that $P_1$, \dots, $P_n$ and $C$
  have to be true in every possible world (or even one)!
\item $C$ is a logical consequence of $P_1$, \dots, $P_n$ iff there is no possible world in which $P_1$, \dots, $P_n$ are all true together while $C$ is false
\item $C$ is \emph{not} a \emph{consequence} of $P_1$, \dots, $P_n$ iff there is a world in which $P_1$, \dots, $P_n$ are all true but $C$ is false.
\eit
}

\subsec{Validity}{

\bit
\item An argument is \emph{logically valid} iff its conclusion is true in every possible world in which the premises are all true
\item An argument is \emph{logically invalid} iff there is a world in which its premises all true but its conclusion is false.\\[2ex]
\item Determine invalidity by finding a possible world in which premises are true and conclusion is false.
\item No such world can be found: argument is (probably) valid
\item To establish validity, give a proof
\eit
}

\subsec{Soundness}{

\bit
\item An argument is \emph{sound} (with respect to a given world) iff it is valid \emph{and} its premises are true in the world
\item An argument is \emph{unsound} (with respect to a given world) iff it is invalid \emph{or} at least one premise is false in the world (or both)
\eit

}

\subsec{Examples}{

\fitcharg{
  \formula{LeftOf(a, b)} \\
  \formula{RightOf(c, b)}}{\formula{Between(b, a, c)}}

}

\subsec{Examples}{

\fitcharg{
\formula{LeftOf(a, b)} \\
\formula{RightOf(d, c)}\\
\formula{b = d}}{
\formula{LeftOf(a, c)}}

}

\subsec{Examples}{

\fitcharg{
\formula{LeftOf(a, b)} \\
\formula{RightOf(c, d)} \\
\formula{b = d}}{
\formula{LeftOf(a, c)}}

}

\subsec{First-order Consequence}{

\bit
\item $C$ is a logical consequence of $P_1$, \dots, $P_n$ iff there is no possible world in which $P_1$, \dots, $P_n$ are all true together while $C$ is false
\item $C$ is a \emph{first-order consequence} of $P_1$, \dots, $P_n$ iff there is no \emph{first-order structure} in which $P_1$, \dots, $P_n$ are all true together while $C$ is false
\eit
\begin{columns}
\begin{column}{5cm}\fitcharg{\formula{LeftOf(a, b)} \\
\formula{RightOf(c, b)}}{\formula{LeftOf(a, c)}}
\end{column}
\begin{column}{5cm}
\tikz [x=2cm] \graph {
        "$\bullet_a$" ->[label=LeftOf] "$\bullet_b$" ;
        "$\bullet_b$" <-[dashed] "$\bullet_c$";
};
\end{column}
\end{columns}

}

\section{Properties of Predicates}

\subsec{Reflexive Predicates}{

\bits
\item $P(\_,\_)$ is \emph{reflexive} iff $P(a, a)$ is always true
\item $=$, SameSize, SameShape, SameCol, SameRow
\item Extensions of reflexive predicates:
\[
\tikz [x=2cm] \graph {
        "$\bullet_1$" ->[in=60, out=120, loop] "$\bullet_1$" -!-
        "$\bullet_2$" ->[in=60, out=120, loop] "$\bullet_2$";
};
\]
\eit
}

\subsec{Symmetric Predicates}{

\bits
\item $P(\_,\_)$ is \emph{symmetric} iff: whenever $P(a, b)$ is true, $P(b, a)$ is also true, and vice versa
\item $=$, SameSize, SameShape, SameCol, SameRow
\item Adjoins
\item Extensions of symmetric predicates:
\[\tikz [x=2cm] \graph {
        "$\bullet_1$" ->[bend left] "$\bullet_2$";
        "$\bullet_1$" <-[bend right] "$\bullet_2$";
};
\]
\eit
}

\subsec{Transitive Predicates}{

\bits
\item $P(\_,\_)$ is \emph{transitive} iff: whenever $P(a, b)$ and $P(b, c)$ are true, $P(a, c)$ is also true
\item $=$, SameSize, SameShape, SameCol, SameRow
\item Larger, Smaller, LeftOf, RightOf, FrontOf, BackOf
\item \emph{not} Adjoins
\item Extensions of transitive predicates:
\[\tikz [x=2cm] \graph {
        "$\bullet_1$" ->[bend left] "$\bullet_2$" ->[bend left] "$\bullet_3$";
        "$\bullet_1$" ->[bend right] "$\bullet_3$";
};
\]
\eit
}

\subsec{Inverse Predicates}{

\bits
\item $P(\_,\_)$ and $Q(\_, \_)$ are \emph{inverses} iff: whenever $P(a, b)$ is true, $Q(b, a)$ is also true, and vice versa
\item $=$, SameSize, SameShape, SameCol, SameRow, Adjoins are inverses of themselves
\item Larger---Smaller, LeftOf---RightOf, FrontOf---BackOf
\item Extensions of inverse predicates:
\[
\tikz [x=2cm] \graph {
        "$\bullet_1$" ->[bend left] "$\bullet_2$";
        "$\bullet_1$" <-[bend right,
         dashed] "$\bullet_2$";
};
\]
\eit
}

\subsec{Indiscernibility of Identicals}{

If $P(a)$ is true, and $a = b$ is also true, then $P(b)$ is true

}

\section{Informal Proofs}

\subsec{Proofs}{

\begin{block}{Definition}

A \emph{proof} of conclusion $C$ from premises $P_1$, \dots, $P_n$ is
a demonstration that $C$ is a consequence of $P_1$, \dots, $P_n$ which
\bit
\item proceeds \emph{step-by-step}
\item each step is \emph{justified}
\item is \emph{rigorous} (no gaps)
\eit
\end{block}
}

\subsec{Informal vs Formal Proof}{

\bit
\item Informal proofs in English
\item Formal proofs
\bit
\item in first-order logic,
\item using system Fitch,
\item steps justified by \emph{inference rules}
\eit
\eit

}

\subsec{Example}{

\begin{columns}
\begin{column}{3cm}
\fitcharg{\formula{LeftOf(a, b)} \\
\formula{RightOf(c, d)}\\
\formula{b = d}}{\formula{LeftOf(a, c)}}
\end{column}
\begin{column}{7cm}
\bens
\item We are told that $c$ is right of $d$ (premise 2)
\item We are told that $b$ is identical to $d$ (premise 3)
\item Thus, $c$ is right of $b$ (from 1, 2, by the
  indiscernibility of identicals)
\item So $b$ is left of $c$ (from 3, as ``left of'' and ``right of'' are inverses)
\item We are told that $a$ is left of $b$ (premise 1)
\item So $a$ is left of $c$ (from 5, 4 as ``left of'' is transitive)\\
\qquad QED
\een
\end{column}
\end{columns}

}

\subsec{Example: Formal Proof}{

\setlength{\fitchprfwidth}{10em}

\fitchprf{\pline[1.]{LeftOf(a, b)}\\
\pline[2.]{RightOf(c, d)}\\
\pline[3.]{b = d}}{
\pline[4.]{RightOf(c, b)}{\eqe{2}{3}}\\
\pline[5.]{LeftOf(b, c)}{{\bf AnaCon:} 4}\\
\pline[6.]{LeftOf(a, c)}{{\bf AnaCon:} 1, 5}
}

}

\end{document}


\documentclass[a4paper]{article}
\input{head}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\newcommand{\algrule}[1][.2pt]{\par\vskip.5\baselineskip\hrule height #1\par\vskip.5\baselineskip}
\begin{document}

%-------------------------------
%	TITLE SECTION
%-------------------------------

\title{\textbf{COL351 : Analysis and Design of Algorithm \\ Assignment 1 }}
\author{Gaurav Jain (2019CS10349) \& T Abishek (2019CS10407)}
\date{}
%-------------------------------
%	CONTENTS
%-------------------------------

\maketitle
\section{Minimum Spanning Tree}
Let G be an edge-weighted graph with n vertices and m edges satisfying the condition that all the edge weights in G are distinct.
\begin{enumerate}[label=(\alph*)]

    \item Prove that G has a unique MST.\\
    \newline Solution: \underline{Proof by Contradiction:}\\\\
    Let T and T' be two different MSTs of G. Since both T and T' have same number of vertices and are different, there exists at least one edge that belongs to one and not the other. Let the set of such edges be called P.\\\\
    Let the edge with minimum weight among these edges be $e_1$. This edge is unique since all edge weights are distinct. Without loss of generality, assume $e_1$ is in T. If $e_1$ is added to MST T', it will form a cycle C with $e_1$ as one of the edges due to the property of tree. Since, T is a tree (acyclic), there must be at least one edge $e_2$ present in cycle C but not in T because if only edges from tree T is present in cycle C, a cycle wouldn't form.\\
    This $e_2$ belongs to T' and not T and thus also belongs to set P. As $e_1$ is the minimum weight edge from set P, wt($e_1$) \textless \ wt($e_2$), as all edge weights are distinct. If $e_2$ is removed from T' U \{$e_1$\}, (T' U \{$e_1$\}) \textbackslash \{$e_2$\} will still be a spanning tree as all paths passing through $e_2$ can be redirected through $e_1$. \\
    Thus, (T' U \{$e_1$\}) \textbackslash \{$e_2$\} is a spanning tree with cost wt(T') + wt($e_1$) - wt($e_2$). Since wt($e_1$) \textless \ wt($e_2$), the new weight of this newly formed tree is less than T'. This is a \textbf{contradiction}, as we assumed that T' is a MST.\\\\
    Thus, G has a unique MST. {\hfill\qedsymbol}

    \newpage
    \item If it is given that G has at most n + 8 edges, then design an algorithm that returns a MST of G in O(n) running time.

    \textbf{Observations:}
    \begin{enumerate}[1)]
        \item Since $m \leq n+8$ and number of edges in a MST is $n-1$, we need to remove atmost 9 edges without disconnecting the graph.
        \item Cycles can be detected using DFS which taken $O(m+n)$ time but since $m \leq n+8$ so DFS will taken $O(n)$ time.
    \end{enumerate}

    \textbf{Algorithm:}
    \begin{algorithm}
    \caption{MST Algorithm}\label{alg:mst}
    \begin{algorithmic}[1]
    \Procedure{MST}{$G$}\Comment{Returns the MST of graph G}
    \State $n \to$ number of vertices.
    \State $m \to$ number of edges and $m \leq n+8$.
    \While{$m \not= n-1$}
    \State $S \gets \textsc{DetectCycle}(G)$
    \State $e \gets$ Maximum edge weight in S
    \State $G\gets G \setminus \{e\}$
    \State $m \gets m-1$
    \EndWhile\label{mstendwhile}
    \State \textbf{return} $G$\Comment{G is the MST of the original graph}
    \EndProcedure
    \algrule
    \Procedure{DetectCycle}{$G$}\Comment{Returns a set of edges that form a cycle in G}
    \State $S \gets \{\}$
    \State $visited \gets false$ for all vertices.
    \State $parent \gets -1$ for all vertices.
    \State $start \gets -1$
    \State $end \gets -1$
    \For{all $v \in V$}
    \If{$visited(v) = false$}
    \State $\textsc{DFS}(v,parent(v))$
    \EndIf
    \EndFor
    \State $v \gets end$
    \While{$v \not= start$}
        \State Add $v$ to $S$
        \State $v \gets parent(v)$
    \EndWhile
    \State \Return $S$ \Comment{$S$ is the set of edges of a cycle}
    \EndProcedure
    \algrule
    \Procedure{DFS}{$v,parent$}\Comment{Recursive function to visit vertices}
    \State $visited(v) \gets true$
    \For{all $u$, neighbours of $v$}
    \If{$u=parent$} \textbf{continue}
    \EndIf
    \If{$visited(u)=true$}\Comment{Cycle Detected}
    \State $end \gets u$
    \State $start \gets v$
    \State \Return
    \EndIf
    \State $parent(u) \gets v$
    \State \textsc{DFS}($u,parent(u)$)
    \EndFor
    \EndProcedure
    \end{algorithmic}
    \end{algorithm}

    \textbf{Correctness of Algorithm:}

    \underline{\textbf{Claim 1:}} Let $C$ be any cycle in $G$, and let edge $e = (v, w)$ be the most expensive edge belonging to $C$. Then $e$ does not belong to any minimum spanning tree of $G$.

    \underline{\textbf{Proof:}} Proof by Contradiction \\
    Delete $e$ from $T$, the MST of $G$; this partitions the vertices into two components: $S$, containing node $v$; and $V \setminus S$, containing node $w$. Now, the edge we use in place of $e$ should have one end in $S$ and the other in $V \setminus S$, so as to stitch the tree back together.

    We can ﬁnd such an edge by following the cycle $C$. The edges of $C$ other than $e$ form, by deﬁnition, a path $P$ with one end at $v$ and the other at $w$. If we follow $P$ from $v$ to $w$, we begin in $S$ and end up in $V \setminus S$, so there is some edge $e' = (v',w')$ on $P$ that crosses from $S$ to $V \setminus S$.

    Now consider the set of edges $T'$ = $T \setminus \{e\} \cup \{e'\}$. We claim that $T'$ is a spanning tree. Clearly $(V,T')$ is connected, since $(V,T)$ is connected, and any path in $(V,T)$ that used the edge $e = (v,w)$ can now be “rerouted” in $(V,T')$ to follow the portion of $P$ from $v$ to $v'$, then the edge $e'$, and then the portion of $P$ from $w$ to $w'$.

    To see that $(V,T')$ is also acyclic, note that the only cycle in $(V,T\cup\{e'\})$ is the one composed of $e'$ and the path $P$, and this cycle is not present in $(V,T')$ due to the deletion of $e$.
    Moreover, since $e$ is the most expensive edge on the cycle $C$, and $e'$ belongs to $C$, it must be that $e'$ is cheaper than $e$, and hence $T'$ is cheaper than $T$, a contradiction. {\hfill\qedsymbol}

    \bigskip
    \underline{\textbf{Proof of Correctness:}}\\
    Consider any edge $e = (v, w)$ removed by the algorithm. At the time that $e$ is removed it is the most expensive edge on $C$. Thus by claim 1, $e$ does not belong to any minimum spanning tree.

    So if we show that the output $(V,T)$ of the algorithm is a spanning tree of $G$, we will be done. Clearly $(V,T)$ is connected as only edges present in a cycle are removed by the algorithm. Since number of edges in $(V,T)$ is $n-1$ as the algorithm terminates here. Thus, $(V,T)$ is acyclic and hence it is a spanning tree. {\hfill\qedsymbol}
\end{enumerate}

\newpage
\section{Huffman Encoding}

\begin{enumerate}[(a)]
    \item What is the optimal binary Huffman encoding for n letters whose frequencies are the first n Fibonacci numbers? What will be the encoding of the two letters with frequency 1, in the optimal binary Huffman encoding? \\ \\
    Sketch of Huffman encoding algorithm (taken from notes)
    \begin{enumerate}[(1)]
        \item Replace the two letters with least frequency by a new symbol $\tilde{a}$.
        \item Set freq($\tilde{a}$) = $\tilde{f}$ := $f_0$ + $f_1$.
        \item Solve $\tilde{F}$ = $(F \cup \tilde{f}) \setminus \{f_0, f_1\}$ and find optimal binary tree $\tilde{T}$.
        \item If $\tilde{a}$ is node for $\tilde{f}$, then add children $a_0$ and $a_1$ to $\tilde{a}$.

    \end{enumerate}
    Let the frequency vector be $F$ where $F[i]$ represents the $i^{th}$ Fibonacci number.\hfill \\ \\
    \underline{\textbf{Claim 1:}} $ \forall n \leq N, F[n] \leq \sum_{i=1}^{n-1} F[i] < F[n+1]$ \\ \\
    \underline{\textbf{Proof:}} Proof by Induction on $n$ \\
    {\textit{Base Case:} \par}
    {\hspace{10mm}    $n = 2$ \par}
    {\hspace{10mm}    $F[2] = 1$ \par}
    {\hspace{10mm}    $\sum_{i=1}^{1} F[i] = F[1] = 1$ \par}
    {\hspace{10mm}    $F[3] = 2$ \par}
    {\hspace{10mm} Thus, \par}
    {\hspace{10mm}    $F[2] \leq \sum_{i=1}^{1} F[i] < F[3]$ \par}
    {\textit{Induction Hypothesis:}
    \par}
    {\hspace{10mm} $F[n-1] \leq \sum_{i=1}^{n-2} F[i] < F[n]$ \par}
    {\textit{Induction Step:} \par}
    {\hspace{10mm} $F[n-1] \leq \sum_{i=1}^{n-2} F[i]$   \hfill (Induction Hypothesis)\par}
    {\hspace{10mm}    $F[n-1] + F[n-1] \leq \sum_{i=1}^{n-2} F[i] + F[n-1]$ \par}
    {\hspace{10mm}    $\sum_{i=1}^{n-1} F[i] \geq F[n-1] + F[n-1] \geq F[n-1] + F[n-2]$ \par}
    {\hspace{10mm}    $\sum_{i=1}^{n-1} F[i] \geq F[n]$  \hfill (A) \par}
    {\par}
    {\hspace{10mm} $\sum_{i=1}^{n-2} F[i] < F[n]$   \hfill (Induction Hypothesis)\par}
    {\hspace{10mm}    $\sum_{i=1}^{n-2} F[i] + F[n-1] < F[n] + F[n-1]$ \par}
    {\hspace{10mm}    $\sum_{i=1}^{n-1} F[i] < F[n+1]$ \hfill (B)\par}
    {\hspace{10mm} From (A) and (B), $F[n] \leq \sum_{i=1}^{n-1} F[i] < F[n+1]$.\par}
    By PMI, $ \forall n \leq N, F[n] \leq \sum_{i=1}^{n-1} F[i] < F[n+1]$.
    {\hfill$\qedsymbol$}\\

    \underline{\textbf{Claim 2:}} In $k^{th}$ recursive call, the least two frequency symbols are $\sum_{i=1}^{k} F[i]$ and $F[k+1]$.\\ \\
    \underline{\textbf{Proof:}} Proof by Induction on $k$ \\
    {\textit{Base Case:} \par}
    {\hspace{10mm}    $k = 1$ \par}
    {\hspace{10mm}  Initially, the least two frequencies in $F$ are $F[1]$ and $F[2]$.\par}
    {\hspace{10mm}  So, $\sum_{i=1}^{1} F[i]$ and $F[2]$ are least two frequency symbols.\par}
    {\textit{Induction Hypothesis:}
    \par}
    {\hspace{10mm} $\sum_{i=1}^{k-1} F[i]$ and $F[k]$ are least two frequencies in $(k-1)^{th}$ call.\par}
    {\textit{Induction Step:} \par}
    {\hspace{10mm} Applying the algorithm on $(k-1)^{th}$ call, we get a new symbol $\tilde{a}$ with frequency $\tilde{f}$.\par}
    {\hspace{10mm} Freq $\tilde{f} = \sum_{i=1}^{k-1} F[i] + F[k]$ as these two are least two frequencies (by induction hypothesis).\par}
    {\hspace{10mm} So, the new frequency vector for next call $\tilde{F_k}$ is \par}
    {\begin{equation}
        \tilde{F_k} = [ \sum_{i=1}^{k} F[i], F[k+1], F[k+2] \dots F[N]]
    \end{equation}}
    {\hspace{10mm} Using the first claim, \par}
    {\begin{equation}
        F[k+1] \leq \sum_{i=1}^{k} F[i] < F[k+2] < F[k+3] \dots < F[n]
    \end{equation}}
    {\hspace{10mm}Thus when the algorithm is called using this frequency vector. \par}
    {\hspace{10mm} The least two frequencies are $\sum_{i=1}^{k} F[i]$ and $F[k+1]$.\par}
    Hence, by PMI, the above claim is true for all recursive calls. {\hfill$\qedsymbol$}\\

    Binary Encoding of all symbols is determined using claim 2. So, in the last recursive call, the only two frequencies are $\sum_{i=1}^{n-1} F[i]$ and $F[n]$ and they are encoded as `0' and `1' respectively.

    $\sum_{i=1}^{n-1} F[i]$ is formed by combining $\sum_{i=1}^{n-2} F[i]$ and $F[n-1]$. Thus, encoding of $\sum_{i=1}^{n-2} F[i]$ and $F[n-1]$ are `00' and `01' respectively.

    After expanding all the merged symbols, the final encoding of symbols are:

    {\hspace{10mm} $F[n] \to$ `1'\par}
    {\hspace{10mm} $F[n-1] \to$ `01'\par}
    {\hspace{10mm} $F[n-2] \to$ `001'\par}
    {\hspace{10mm} $\dots$\par}
    {\hspace{10mm} $\dots$\par}
    {\hspace{10mm} $\dots$\par}
    {\hspace{10mm} $\dots$\par}
    {\hspace{10mm} $F[3] \to$ `00....001' (n-3 zeros)\par}
    {\hspace{10mm} $F[2] \to$ `00....001' (n-2 zeros)\par}
    {\hspace{10mm} $F[1] \to$ `00....000' (n-1 zeros)\par}
    \newpage
     \item Suppose you aim to compress a file with 16-bit characters such that the maximum character frequency is strictly less than twice the minimum character frequency. Prove that the compression obtained by Huffman encoding, in this case, is same as that of the ordinary fixed-length encoding.\\

     \underline{\textbf{Property P:}} maximum frequency \textless \ 2*min frequency\\

     \underline{\textbf{Given:}} 16 bit characters with maximum frequency \textless \ 2*min frequency\\

     \underline{\textbf{Claim:}} The Huffman encoding with n bit characters is reducible to n-1 bit characters by merging 2 characters and representing it as a single character using n-1 bits, following the same property i.e, maximum frequency \textless \ 2*min frequency\\

     \underline{\textbf{Proof:}}\\
     Let N = $2^n$ and F = [$f_1$, $f_2$ ....., $f_N$] be the frequencies sorted in ascending order.\\
     Merging the least frequencies according to Huffman algorithm.\\

     $\Rightarrow \tilde{f_1}$ = $f_1$ + $f_2$\\
     {Since $f_N < 2*f_1 $\par}
     {\hspace{10mm} $\Rightarrow f_1 + f_2 \geq 2*f_1 > f_N$ \par}
     {\hspace{10mm} $\Rightarrow f_1 + f_2 > f_i \ \ \forall i = 1, 2.... N$ \par}
     {Thus the new sorted frequency vector is [$f_3, f_4,......f_N, \tilde{f_1}$]\\
     Recursively merge the least two frequency characters to form a single character of the combined frequency, N/2 times. We get the new frequency vector as,\par}
    {\hspace{10mm} $\tilde{F} = [\tilde{f_1}, \tilde{f_2},.....\tilde{f_{N/2}}]$\par}
    {\hspace{10mm} where $\tilde{f_i} = f_{i*2-1} + f_{i*2}$\par}
    {\hspace{10mm} max($\tilde{F}$) = $f_{N-1} + f_N$ \par}
    {\hspace{10mm} min($\tilde{F}$) = $f_{1} + f_2$ \par}
    {\hspace{10mm} Since $f_N < 2*f_1 \ and \ f_{N-1} \leq f_N < 2*f_1 \leq 2*f_2$\par}
    {\hspace{10mm} So, $f_N + f_{N-1} < 2f_1 + 2f_2$ \par}
    {\hspace{10mm} $\Rightarrow$ max($\tilde{F}$)  $<$ 2min($\tilde{F}$) \par }
Thus, $\tilde{F}$ represents frequency vector of $2^{N-1}$ characters or n-1 bit characters with same property.{\hfill$\qedsymbol$}\\

\underline{\textbf{Proof:}} Proof by Induction on number of bits \\
    {\textit{Base Case:} \par}
    {\hspace{10mm}    $n = 1$ \par}
    {\hspace{10mm}  Number of characters = $2^1$\par}
    {\hspace{10mm}  Huffman encoding is `0' and `1'\par}
    {\hspace{10mm}  Huffman encoding is `0' and `1'\par}

{\textit{Induction Hypothesis:}}\\
Huffman encoding for n-1 bit characters which follow property P is same as fixed length encoding of length n-1.\\

{\textit{Induction Step:}}\\
Using the claim proved above, n bit characters are merged to form n-1 bit characters with same property P.
By induction hypothesis, these n-1 bit characters has fixed length encoding and forms a complete binary tree.\\
Expand the merged symbols in n-1 bit characters i.e, expand each leaf node present at height n-1 to get 2 children which is present at height h. These resulting nodes represent the n bit characters that were merged to form the n-1 bit Huffman tree and thus the resulting tree is the Huffman tree for n bit characters.\\
As the height of all leaf nodes in the resulting tree is n, it too forms a complete binary tree. Thus, they have fixed length encoding as all the characters require n bits to represent them.\\

Hence, By PMI, compression obtained by Huffman encoding of 16 bit characters is same as fixed length encoding of length 16.{\hfill$\qedsymbol$}

\end{enumerate}

\newpage
\section{Graduation Party of Alice}
\begin{enumerate}[(a)]
    \item Alice wants to throw a graduation party and is deciding whom to call. She has n people to choose from, and she has made up a list of which pairs of these people know each other. She wants to pick a largest subset of n people, subject to two constraints: at the party, each person should have at least five other people whom they know and five other people whom they don’t know. Present an efficient algorithm that takes as input the list of n people along with the list of pairs who know each other and outputs the best choice of party invitees. Give the running time in terms of n.

    \textbf{Algorithm:}
    \begin{algorithm}
    \caption{Selection Algorithm}\label{alg:sa}
    \begin{algorithmic}[1]
    \Procedure{Select}{list of people, list of pairs}\Comment{Returns the maximal set of people possible}
    \State $G \to$ Undirected Graph produced using every person as a vertex
    \State $n \to$ number of vertices/people
    \State $m \to$ number of edges. An edge is added between two people if they know each other.
    \State $change \gets true$
    \State $current \gets n$
    \State $D \gets$ array of buckets where vertices are arranged by their degree
    \While{$change = true$}
    \State $change \gets false$
    \For{$v \in D$ with $deg(v) <5$ and $current > deg(v) > current-5$}
    %\If{$v$ is marked} \textbf{continue}
    %\EndIf
    %\If{$deg(v) < 5$}
    \State $change \gets true$
    \State $current \gets current-1$
    \State \textsc{RemoveVertex}($G,v$)
    %\EndIf

    %\If{$current - deg(v) < 5$}
    %\State $change \gets true$
    %\State $current \gets current-1$
    %\State \textsc{RemoveVertex}($G,v$)
    %\EndIf

    \EndFor
    \EndWhile\label{saendwhile}
    \State \textbf{return} the set of unmarked vertices
    \EndProcedure
    \algrule
    \Procedure{RemoveVertex}{$G,v$}\Comment{Reduces degrees of vertices connected to $v$}
    \State Mark $v$ \Comment{$v$ is removed from invitees list}
    \State $D[deg(v)].\textsc{remove}(v)$
    \For{$j$ from $1$ to $deg(v)$}
    \State $u \gets adj(v)(j)$
    \If{$u$ is marked}
    \State \textbf{continue}
    \EndIf
    %\State Delete $v$ from $adj(u)$
    \State $deg(u) \gets deg(u)-1$
    \State $D[deg(u)+1].\textsc{remove}(u)$
    \State $D[deg(u)].\textsc{insert}(u)$
    \EndFor
    \EndProcedure
    \end{algorithmic}
    \end{algorithm}

    \textbf{Runtime Analysis:}\\
    The graph is stored in adjacency list as a HashMap mapping each vertex to an unordered set of connected vertices. The time taken to make the graph is $O(m)$.
    Degree of all vertices is also stored separately as a HashMap to give $O(1)$ access to degrees of each vertex and updated accordingly.

    The degree of all the vertices is then sorted using bucket sort with each bucket being an unordered set(Hash) of vertices which allows us to insert and remove in  $O(1)$ time. The buckets themselves are stored as an array of buckets of size N, with the index of the bucket representing the degree of the vertices stored in them. The initial sorting takes $O(m)$ as it is done through a single pass.

    The inner FOR loop inside the WHILE loop in the Select procedure, is also run only $O(n)$ times as each iteration, a vertex is removed, and the maximum no. of vertices that can be removed is n.

    Since the number of edges are $m$ so the procedure \textsc{RemoveVertex}'s FOR loop runs atmost $2*m$  over all the calls as the procedure is called for each vertex only once, and the for loop is run degree(v) times. Sum of the degrees of all the vertices is $2*m$. So the overall running time of the algorithm is $O(m+n)$.
    Since $m \leq n^2$ so the time complexity is $O(n^2)$.

    \textbf{Correctness of Algorithm:}\\
    In each iteration of the algorithm, vertices or people which can never be part of the optimal invitation list are removed as such people know less than 5 or such people don't know less than 5 people. At each iteration, we must remove at least one person, so this algorithm terminates.

    When this algorithm terminates, by definition the subset of invitees is valid. Since we only remove people we have deduced could not possibly be invited, we always produce the largest possible number of invitees. {\hfill\qedsymbol}

    \newpage
    \item Suppose finally Alice invited $n_0$ out of her n friends to the party. Her next task is to set a minimum number of dinner tables for her friends under the constraint that each table has a capacity of ten people and the age difference between members of each dining group should be at most ten years. Present a greedy algorithm to solve this problem in $O(n_0)$ time assuming the age of each person is an integer in the range [10, 99].

    \begin{algorithm}
    \caption{Minimum tables Algorithm}\label{alg:arr}
    \begin{algorithmic}[1]
    \Procedure{Minimum}{$G$}
    \State $n_0 \gets$ number of selected friends.
    \State $Tables \gets \{\}$
    \State $S \gets \textsc{sort}(G)$\Comment{sorted in ascending order of Age}
    \While{$\textsc{size}(S) \not= 0$}
        \State $start \gets \textsc{front}(S)$.Age
        \State $T \gets NULL$
        \State $T$.\textsc{insert}($S$.\textsc{pop\_front})
        \State $k \gets 1$
        \While{($\textsc{size}(S) \not= 0$ and $k < 10$ and $\textsc{front}(S).age \leq start + 10$)}
            \State $T$.\textsc{insert}($S$.\textsc{pop\_front})
            \State $k \gets k+1$
        \EndWhile
        \State $Tables$.\textsc{insert}($T$)
    \EndWhile
    \State \Return $Tables$
    \EndProcedure
    \end{algorithmic}
    \end{algorithm}

    \textbf{Runtime Analysis:}\\
    Sorting of people by age is possible in $O(n_0)$ time complexity using bucket sort as the range of age is fixed between $10$ and $99$. Every person present in the sorted list is only visited once because as soon as a vertex is visited, it is popped out of the list. Thus, the overall time complexity of the above algorithm is $O(n_0)$.

    \textbf{Correctness of Algorithm:}\\
    \underline{\textbf{Claim:}} Let P be the set of
    people that have to be assigned tables. Let $a_1$ be the age of the youngest person. Let T be a table that seats the youngest person in P in optimal arrangement. Let T' be a table that seats the youngest person in P by applying the greedy algorithm. Then $size(T') \geq size(T)$ and $youngest((P \setminus T')) \geq youngest((P \setminus T))$\\

    \underline{\textbf{Proof:}} \\
    \underline{Case 1:} The no. of people with age $a_1$ is more than 10.\\
    In this case, our greedy algorithm would form a table with all the 10 people of the same youngest age $a_1$.\\
    $Size(T') = 10$ which is the maximum allowed. $youngest((P \setminus T')) = a_1$ as there is more than 10 people with that age. No matter what arrangement T has, $youngest((P \setminus T)) = a_1$ as there is more than 10 people with that age. So, the claim is proved.\\

    \underline{Case 2:} The no. of people with age $a_1$ is less than or equal to 10.\\
    \phantom{Case 2:} \underline{Case 2.1:} $T = T'$\\
    \phantom{Case 2:Case 2.1:} Claim is proved.

    \phantom{Case 2:} \underline{Case 2.2:} $T \not= T'$\\
    \phantom{Case 2:Case 2.1:} Proof by contradiction\\

    \phantom{Case 2:Case 2.1:} Assume $size(T') < size(T)$\\
    \phantom{Case 2:Case 2.1:} $=> \exists \ p_1$ present in T but not present in T' and size(T') $<$ 10.\\
    \phantom{Case 2:Case 2.1:} But because $p_1 \nexists \ T' => p_1 > a_1 + 10$\\
    \phantom{Case 2:Case 2.1:} $=> p_1 \nexists \ T$ as a person with age $a_1$ is present in T.\\
    \phantom{Case 2:Case 2.1:} Contradiction arises. So, Assumption is false.\\

    \phantom{Case 2:Case 2.1:} Assume $youngest((P \setminus T')) < youngest((P \setminus T))$\\
    \phantom{Case 2:Case 2.1:} All persons with age less than  $youngest((P \setminus T))$ is present in T, but only persons with \\
    \phantom{Case 2:Case 2.1:} age less than $youngest((P \setminus T'))$ are present in T'.\\
    \phantom{Case 2:Case 2.1:} $=> size(T') < size(T)$, due to the assumption, which is false as proved above.\\
    \phantom{Case 2:Case 2.1:} Contradiction arises. So, Assumption is false.\\

    \phantom{Case 2:Case 2.1:} Claim is proved. {\hfill\qedsymbol}

    \bigskip
    \underline{\textbf{Proof of Correctness:}}\\
    At each step of the algorithm, where a table is assigned for the youngest person of the remaining people, it is shown by the above claim that both size of the assigned table by the algorithm is equivalent to optimal arrangement and the youngest person left to be assigned also is equivalent.

    As, the youngest person's age not assigned is greater than or equal to the optimal arrangement, this implies that the people not assigned a table at each step is minimised optimally in the greedy algorithm, as all the people below that age have been assigned a table. This shows that the greedy algorithm is working equivalent to an optimal algorithm, and in fact stays ahead of the optimal solution always. As this algorithm continues executing until all people are assigned a table, and at each step, at least 1 person is assigned a table, the algorithm terminates.\\

    The correctness of the above greedy algorithm is thus proved. {\hfill\qedsymbol}

    %\phantom{Case 2:Case 2.1:} \underline{Case 2.2.1:} There exists a person $p_1$ is present in T but not in T'.\\
    %\phantom{Case 2:Case 2.1:Case 2.2.1:}  \\
    %\phantom{Case 2:Case 2.1:Case 2.2.1:} This implies, there exists a person $p_2$ present in T' but not in T.\\
    %\phantom{Case 2:Case 2.1:Case 2.2.1:} $=> age(p_1) \geq youngest((P \setminus T'))$\\ %\geq(p_2) $\\
    %\phantom{Case 2:Case 2.1:Case 2.2.1:} $=> age(p_2) \geq youngest((P \setminus T)) $\\
    %\phantom{Case 2:Case 2.1:Case 2.2.1:} $=> youngest((P \setminus T')) \geq youngest((P \setminus T))$\\
    %\phantom{Case 2:Case 2.1:Case 2.2.1:}Claim is proved.\\
    %\phantom{Case 2:Case 2.1:} \underline{Case 2.2.2:} $Size(T) \not= Size(T')$\\
    %\phantom{Case 2:Case 2.1:Case 2.2.1:}
\end{enumerate}
%------------------------------------------------

\end{document}


\documentclass[epsfig,10pt,fullpage]{article}

\newcommand{\LabNum}{1}
\newcommand{\CommonDocsPath}{../../../common/docs}
\input{\CommonDocsPath/preamble.tex}

\begin{document}

\centerline{\huge Digital Logic}
~\\
\centerline{\huge Laboratory Exercise \LabNum}
~\\
\centerline{\large Switches, Lights, and Multiplexers}
~\\

The purpose of this exercise is to learn how to connect simple input and
output devices to an FPGA chip and implement a circuit that uses these devices.
We will use the switches on the DE-series boards as inputs to the circuit.
We will use light emitting diodes (LEDs) and 7-segment displays as output
devices.

\section*{Part I}
\addcontentsline{toc}{1}{Part I}
The DE10-Lite, DE0-CV, and DE1-SoC boards provide ten switches and lights, called
{\it SW}$_{9-0}$ and {\it LEDR}$_{9-0}$. Similarly, the DE2-115 provides eighteen switches
and lights. The switches can be used to provide inputs, and the lights can be used as
output devices. Figure~\ref{fig:1} shows a simple Verilog module that uses ten switches
and shows their states on the LEDs. Since there are multiple switches and lights
it is convenient to represent them as vectors in the Verilog code, as shown. We have used a
single assignment statement for all {\it LEDR} outputs, which is equivalent to the
individual assignments:

\begin{center}
\begin{minipage}[t]{12.5 cm}
\begin{tabbing}
$\ldots$\\
{\bf assign} LEDR[2] = SW[2];\\
{\bf assign} LEDR[1] = SW[1];\\
{\bf assign} LEDR[0] = SW[0];
\end{tabbing}
\end{minipage}
\end{center}

The DE-series boards have hardwired connections between its FPGA chip and the switches and
lights. To use the switches and lights it is necessary to include in
your Quartus\textsuperscript{\textregistered} project the correct pin assignments, which are
given in your board's user manual. For example, the DE1-SoC manual specifies that
{\it SW}$_0$ is connected to the FPGA pin {\it AB12} and {\it LEDR}$_0$ is connected to
pin {\it V16}. A good way to make the required pin assignments is to import into the
Quartus software the pin assignment file for your board, which is provided on the
Boards section of FPGAcademic.org web site. The procedure for making pin assignments is
described in the tutorial {\it Quartus Introduction using Verilog Design}, which is
also available from FPGAcademic.org.
~\\

It is important to realize that the pin assignments in the
file are useful only if the pin names that appear in this file
are exactly the same as the port names used in your Verilog module.
For example, if the pin assignment file uses the names {\it SW}[0], $\ldots$, {\it SW}[9]
and {\it LEDR}[0], $\ldots$, {\it LEDR}[9], then these are the names that must be used
for input and output ports in the Verilog code, as we have done in Figure~\ref{fig:1}.

~\\
~\\
\begin{figure}[H]
\begin{center}
\begin{minipage}[t]{12.5 cm}
\begin{tabbing}
ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\kill
// Module that connects ten switches and lights\\
{\bf module} ~part1 (SW, LEDR);\\
\>{\bf input} [9:0] SW;\>\>\>\>\>\>\>\>// slide switches\\
\>{\bf output} [9:0] LEDR;\>\>\>\>\>\>\>\>// red LEDs\\
~\\
\>{\bf assign} LEDR = SW;\\
{\bf endmodule}
\end{tabbing}
\end{minipage}
	\caption{Verilog code that uses ten switches and lights.}
	\label {fig:1}
\end{center}
\end{figure}

\newpage
Perform the following steps to implement a circuit corresponding to the code
in  Figure~\ref{fig:1} on the DE-series boards.
\begin{enumerate}
\item Create a new Quartus project for your circuit. Select the target chip that corresponds to your DE-series board.
Refer to Table \ref{tab:device} for a list of devices.
\item Create a Verilog module for the code in  Figure~\ref{fig:1} and include it in your project.
\item Include in your project the required pin assignments for your DE-series board, as discussed
above. Compile the project.
\item Download the compiled circuit into the FPGA chip by using the Quartus Programmer
tool (the procedure for using the Programmer tool is described in
the tutorial {\it Quartus Introduction}). Test the functionality of the
circuit by toggling the switches and observing the LEDs.
\end{enumerate}

\begin{table}[H]
	\begin{center}
	\begin{tabular}{l | l}
   \bf{Board} & \bf{Device Name} \\
	\hline
	\rule{0pt}{3ex}DE10-Lite & MAX\textsuperscript{\textregistered} 10 10M50DAF484C6GES \\
	DE0-CV & Cyclone\textsuperscript{\textregistered} V 5CEBA4F23C7 \\
	DE1-SoC & Cyclone\textsuperscript{\textregistered} V SoC 5CSEMA5F31C6 \\
	DE2-115 & Cyclone\textsuperscript{\textregistered} IVE EP4CE115F29C7 \\
	\end{tabular}
	\end{center}
	\caption{DE-series FPGA device names}
	\label{tab:device}
\end{table}

\section*{Part II}
\addcontentsline{toc}{2}{Part II}
Figure~\ref{fig:2}$a$ shows a sum-of-products circuit that implements
a 2-to-1 {\it multiplexer} with a select input $s$.
If $s = 0$ the multiplexer's output $m$ is equal to the input $x$, and if $s=1$ the
output is equal to $y$. Part $b$ of the figure gives a truth table for this
multiplexer, and part $c$ shows its circuit symbol.

\begin{figure}[H]
	\begin{center}
		\includegraphics[scale=.8]{figures/figure2.pdf}
	\end{center}
	\caption{A 2-to-1 multiplexer.}
	\label{fig:2}
\end{figure}

The multiplexer can be described by the following Verilog statement:

\begin{center}
\begin{minipage}[t]{12.5 cm}
\begin{tabbing}
{\bf assign} m = ($\sim$s \& x) $\mid$ (s \& y);
\end{tabbing}
\end{minipage}
\end{center}

\newpage
You are to write a Verilog module that includes four
assignment statements like the one shown above to describe the circuit given
in Figure~\ref{fig:3}$a$.
This circuit has two four-bit inputs, $X$ and $Y$, and produces the four-bit output
$M$. If $s=0$ then $M = X$, while if $s=1$ then $M=Y$. We refer to this circuit as a four-bit
wide 2-to-1 multiplexer. It has the circuit symbol shown in  Figure~\ref{fig:3}$b$, in which $X$,
$Y$, and $M$ are depicted as four-bit wires.

\begin{figure}[H]
	\begin{center}
		\includegraphics[scale=.8]{figures/figure3.pdf}
	\end{center}
\caption{A four-bit wide 2-to-1 multiplexer.}
\label{fig:3}
\end{figure}

Perform the steps listed below.
\begin{enumerate}
\item Create a new Quartus project for your circuit.
\item Include your Verilog file for the four-bit wide 2-to-1 multiplexer
in your project. Use switch {\it SW}$_{9}$ as the $s$ input, switches
{\it SW}$_{3-0}$ as the $X$ input and
{\it SW}$_{7-4}$ as the $Y$ input. Display the value of the input $s$ on {\it LEDR}$_9$,
connect the output $M$ to {\it LEDR}$_{3-0}$, and connect the unused LEDR lights to the constant value 0.
\item Include in your project the required pin assignments for your DE-series board. As discussed
in Part I, these assignments ensure that the ports of your Verilog code will use the pins
on the FPGA chip that are connected to the {\it SW} switches and {\it LEDR} lights.
\item Compile the project, and then download the resulting circuit into the FPGA chip.
Test the functionality of the
four-bit wide 2-to-1 multiplexer by toggling the switches and observing the LEDs.
\end{enumerate}

\section*{Part III}
\addcontentsline{toc}{3}{Part III}
In Figure~\ref{fig:2} we showed a 2-to-1 multiplexer that selects between the
two inputs {\it x} and {\it
y}. For this part consider a circuit in which the output {\it m} has to be selected from
four inputs $u$, $v$, $w$, and $x$. Part $a$ of  Figure~\ref{fig:4} shows how we can build the
required 4-to-1 multiplexer by using three 2-to-1 multiplexers. The circuit uses a 2-bit
select input $s_1 s_0$ and implements the truth table shown in Figure~\ref{fig:4}$b$. A circuit
symbol for this multiplexer is given in part $c$ of the figure.

~\\
Recall from  Figure~\ref{fig:3} that a four-bit wide 2-to-1 multiplexer can be built
by using four instances of a 2-to-1
multiplexer. Figure~\ref{fig:5} applies this concept to define a two-bit wide 4-to-1
multiplexer. It contains two instances of the circuit in  Figure~\ref{fig:4}$a$.

\begin{figure}[H]
	\begin{center}
		\includegraphics[scale=.8]{figures/figure4.pdf}
	\end{center}
\caption{A 4-to-1 multiplexer.}
\label{fig:4}
\end{figure}

\begin{figure}[H]
	\begin{center}
		\includegraphics[]{figures/figure5.pdf}
	\end{center}
\caption{A two-bit wide 4-to-1 multiplexer.}
\label{fig:5}
\end{figure}

Perform the following steps to implement the two-bit wide 4-to-1 multiplexer.
\begin{enumerate}
\item Create a new Quartus project for your circuit.
\item Create a Verilog module for the two-bit wide 4-to-1 multiplexer. Connect its select
inputs to switches {\it SW}$_{9-8}$, and use switches SW$_{7-0}$ to
provide the four 2-bit inputs $U$ to $X$. Connect the
output $M$ to the red lights {\it LEDR}$_{1-0}$.
\item Include in your project the required pin assignments for your DE-series board.
Compile the project.
\item Download the compiled circuit into the FPGA chip. Test the functionality of the
two-bit wide 4-to-1 multiplexer by toggling the switches and observing the LEDs. Ensure
that each of the inputs $U$ to $X$ can be properly selected as the output $M$.
\end{enumerate}

\section*{Part IV}
\addcontentsline{toc}{4}{Part IV}
The objective of this part is to display a character on a 7-segment display.
The specific character displayed depends on a two-bit input.
Figure~\ref{fig:6} shows a {\it 7-segment decoder} module that has the two-bit input $c_1
c_0$. This decoder produces seven outputs that are used to display a character on a
7-segment display. Table~\ref{tab:codes} lists the characters that should be displayed for each
valuation of $c_1 c_0$ for your DE-series board. Note that in some cases the `blank'
character is selected for code $11$.
~\\

The seven segments in the display are identified by the indices 0 to 6 shown in the
figure. Each segment is illuminated by driving it to the logic value 0. You are to write a
Verilog module that implements logic functions to activate
each of the seven segments.
Use only simple Verilog {\bf assign} statements in your code to specify each logic function using a
Boolean expression.

\begin{figure}[H]
	\begin{center}
		\includegraphics[]{figures/figure6.pdf}
	\end{center}
\caption{A 7-segment decoder.}
\label{fig:6}
\end{figure}

\begin{table}[H]
	\begin{center}
	\begin{tabular}{l | c | c | c | c}
	$c_1 c_0$ & DE10-Lite & DE0-CV & DE1-SoC & DE2-115 \\
	\hline
	{\rule[0mm]{0mm}{5mm}\hspace{0.75 mm} 00} & d & d & d & d \\
	\hspace{0.75 mm}
	01 & E & E & E & E \\
	\hspace{0.75 mm}
	10 & 1 & 0 & 1 & 2 \\
	\hspace{0.75 mm}
	11 & 0 & & & \\
	\end{tabular}
	\caption{Character codes for the DE-series boards.}
	\label{tab:codes}
	\end{center}
\end{table}

Perform the following steps:

\begin{enumerate}
\item Create a new Quartus project for your circuit.
\item Create a Verilog module for the 7-segment decoder. Connect the $c_1 c_0$ inputs
to switches {\it SW}$_{1-0}$, and connect the outputs of the decoder to the {\it HEX0}
display on your DE-series board. The segments in this display are called
{\it HEX0}$_0$, {\it HEX0}$_1$, $\ldots$, {\it HEX0}$_6$, corresponding to  Figure~\ref{fig:6}.
You should declare the 7-bit port

\begin{center}
\begin{minipage}[t]{12.5 cm}
\begin{tabbing}
{\bf output} [0:6] HEX0;
\end{tabbing}
\end{minipage}
\end{center}

in your Verilog code so that the
names of these outputs match the corresponding names in your board's user manual and pin assignment file.
\item After making the required pin assignments, compile the project.
\item Download the compiled circuit into the FPGA chip. Test the functionality of the
circuit by toggling the {\it SW}$_{1-0}$ switches and observing the 7-segment display.
\end{enumerate}

\newpage
\section*{Part V}
\addcontentsline{toc}{5}{Part V}
Consider the circuit shown in  Figure~\ref{fig:7}. It uses a two-bit wide 4-to-1 multiplexer to
enable the selection of four characters that are displayed on a 7-segment display. Using the
7-segment decoder from Part IV this circuit can display the characters d, E, 0, 1, 2, or `blank'
depending on your DE-series board.
The character codes are set according to Table~\ref{tab:codes} by using the switches
{\it SW}$_{7-0}$, and a specific character is selected for display by setting the
switches {\it SW}$_{9-8}$.
~\\

An outline of the Verilog code that represents this circuit is
provided in  Figure~\ref{fig:8}. Note that we have
used the circuits from Parts III and IV as subcircuits in this code. You are to
extend the code in  Figure~\ref{fig:8} so that it uses four 7-segment displays rather
than just one.  You will need to use four instances of each of the subcircuits. The
purpose of your circuit is to display any word on the four 7-segment displays that is
composed of the characters in Table~\ref{tab:codes}, and be able to rotate this word in a
circular fashion across the
displays when the switches {\it SW}$_{9-8}$ are toggled. As an example,
if the displayed word is dE10, then your circuit should produce the output patterns
illustrated in Table~\ref{tab:rotate}.

\begin{figure}[H]
	\begin{center}
		\includegraphics[]{figures/figure7.pdf}
	\end{center}
\caption{A circuit that can select and display one of four characters.}
\label{fig:7}
\end{figure}

\begin{figure}[ht]
\begin{center}
\begin{minipage}[t]{12.5 cm}
\begin{tabbing}
ZZ\={\bf output} [0:6] HEX3, HEX2, HEX1, HEX0;ZZ\=\kill
{\bf module} ~part5 (SW, LEDR, HEX0);\\
\>{\bf input} [9:0] SW;	\>// slide switches\\
\>{\bf output} [9:0] LEDR;	\>// red lights\\
\>{\bf output} [0:6] HEX0;	\>// 7-seg display\\
ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\=ZZ\kill
~\\
\>{\bf wire} [1:0] M0;\\
~\\
\>mux\_2bit\_4to1 U0 (SW[9:8], SW[7:6], SW[5:4], SW[3:2], SW[1:0], M0);\\
\>char\_7seg H0 (M0, HEX0);\\
\>$\ldots$\\
{\bf endmodule}\\
~\\
// implements a 2-bit wide 4-to-1 multiplexer\\
{\bf module} mux\_2bit\_3to1 (S, U, V, W, X, M);\\
\>{\bf input} [1:0] S, U, V, W, X;\\
\>{\bf output} [1:0] M;\\
\>$\ldots$ code not shown\\
~\\
{\bf endmodule}	\\
~\\
// implements a 7-segment decoder for d, E, 1 and 0\\
{\bf module} char\_7seg (C, Display);\\
\>{\bf input} [1:0] C;	\>\>\>\>\>\>\>\>// input code\\
\>{\bf output} [0:6] Display;	\>\>\>\>\>\>\>\>// output 7-seg code\\
\>$\ldots$ code not shown\\
~\\
{\bf endmodule}
\end{tabbing}
\end{minipage}
\end{center}
\caption{Verilog code for the circuit in  Figure~\ref{fig:7}.}.
\label{fig:8}
\end{figure}

\begin{table}[H]
\begin{minipage}[t]{12.5 cm}
\begin{center}
\begin{tabular}{c|cccc}
{\it SW}$_{9-8}$ & \multicolumn{4}{c}{Characters} \\
\hline
{\rule[0mm]{0mm}{5mm}00} & d & E & 1 & 0\\
01 & E & 1 & 0 & d\\
10 & 1 & 0 & d & E\\
11 & 0 & d & E & 1\\
\end{tabular}
\end{center}
\end{minipage}
\caption{Rotating the word dE10 on four displays.}
\label{tab:rotate}
\end{table}

Perform the following steps.
\begin{enumerate}
\item Create a new Quartus project for your circuit.
\item Include your Verilog module in the Quartus project. Connect the switches
{\it SW}$_{9-8}$ to the select inputs of each of the four instances of the two-bit
wide 4-to-1 multiplexers. Also connect {\it SW}$_{7-0}$ to each instance of the
multiplexers as required to produce the patterns of characters shown in Table~\ref{tab:codes}.
Connect the SW switches to the red lights LEDR, and connect the outputs of the four
multiplexers to the 7-segment displays {\it HEX3}, {\it HEX2}, {\it HEX1}, and {\it HEX0}.
\item Include the required pin assignments for your DE-series board for all switches, LEDs,
and 7-segment displays. Compile the project.
\item Download the compiled circuit into the FPGA chip. Test the functionality of the
circuit by setting the proper character codes on the switches SW$_{7-0}$ and then
toggling {\it SW}$_{9-8}$ to observe the rotation of the characters.
\end{enumerate}

\section*{Part VI}
\addcontentsline{toc}{6}{Part VI}
Extend your design from Part V so that is uses all 7-segment displays on your DE-series board.
Your circuit needs to display a three- or four-letter word, corresponding to Table 2,
using 'blank' characters for unused displays. Implement rotation of this word from right-to-left
as indicated in Table~\ref{tab:rotate2} and Table~\ref{tab:rotate3}.
To do this, you will need to connect 6-to-1 multiplexers to each of six 7-segment display
decoders for the DE10-Lite, DE0-CV and DE1-SoC. Note that for the DE10-Lite you will need
to use 3-bit codes for your characters, because five characters are needed when including
the 'blank' character (your 7-segment decoder will have to use 3-bit codes, and you will
need to use 3-bit wide 6-to-1 multiplexers).  For the DE2-115, you will need to connect
8-to-1 multiplexers to each of the eight 7-segment display decoders.
You will need to use three select lines for each of the multiplexers: connect the select lines
to switches {\it SW}$_{9-7}$. In your Verilog code connect constants to the 6-to-1 (or
8-to-1) multiplexers that select each character, because there are not enough {\it SW} switches.

\begin{table}[h]
\begin{minipage}[t]{12.5 cm}
\begin{center}
\begin{tabular}{c|cccccc}
{\it SW}$_{9-7}$ & \multicolumn{6}{c}{Character pattern} \\
\hline
{\rule[0mm]{0mm}{5mm}000 } & & & d & E & 1 & 0 \\
001 & & d & E & 1 & 0 & \\
010 & d & E & 1 & 0 & & \\
011 & E & 1 & 0 & & & d\\
100 & 1 & 0 & & & d & E \\
101 & 0 & & & d  & E & 1 \\
\end{tabular}
\end{center}
\end{minipage}
\caption{Rotating the word dE10 on six displays.}
\label{tab:rotate2}
\end{table}

\clearpage

\begin{table}[ht]
\begin{minipage}[t]{12.5 cm}
\begin{center}
\begin{tabular}{c|cccccccc}
{\it SW}$_{9-7}$ & \multicolumn{6}{c}{Character pattern} \\
\hline
{\rule[0mm]{0mm}{5mm}000 } & & & & & & d & E & 2 \\
001 & & & & & d & E & 2 & \\
010 & & & & d & E & 2 & & \\
011 & & & d & E & 2 & & & \\
100 & & d & E & 2 & & & & \\
101 & d & E & 2 & & & & &  \\
110 & E & 2 & & & & & & d \\
111 & 2 & & & & & & d & E \\
\end{tabular}
\end{center}
\end{minipage}
\caption{Rotating the word dE2 on eight displays.}
\label{tab:rotate3}
\end{table}

Perform the following steps:
\begin{enumerate}
\item Create a new Quartus project for your circuit.
\item Include your Verilog module in the Quartus project. Connect the switches
{\it SW}$_{9-7}$ to the select inputs of each instance of the
multiplexers in your circuit. Connect constants in your Verilog code to the multiplexers
as required to produce the patterns of characters shown in Table~\ref{tab:rotate2} or
Table~\ref{tab:rotate3} depending on your DE-series board.
Connect the outputs of your multiplexers to the 7-segment displays {\it HEX5}, $\ldots$,
{\it HEX0} of the DE10-Lite, DE0-CV and DE1-SoC or {\it HEX7}, $\ldots$,
{\it HEX0} for the DE2-115.
\item Include the required pin assignments for your DE-series board for all switches, LEDs,
and 7-segment displays. Compile the project.
\item Download the compiled circuit into the FPGA chip. Test the functionality of the
circuit by toggling {\it SW}$_{9-7}$ to observe the rotation of the characters.
\end{enumerate}

\input{\CommonDocsPath/copyright.tex}
\end{document}