\documentclass{beamer}
\input{../util/beamerpreamble.tex}

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

\title[GB13604]{GB13604 - Maths for Computer Science}
\subtitle[]{Lecture 7 -- Counting Part II}
\author[Claus Aranha]{Claus Aranha\\{\footnotesize caranha@cs.tsukuba.ac.jp}}
\institute[COINS]{College of Information Science}
\date[2018-11-21]{2018-11-21\\{\tiny Last updated \today}}

\tikzstyle{vertex}=[circle,fill=black!25,minimum size=10pt,inner sep=0pt]
\tikzstyle{blue vertex}=[circle,fill=blue!100,minimum size=10pt,inner sep=0pt]
\tikzstyle{red vertex}=[circle,fill=red!100,minimum size=10pt,inner sep=0pt]
\tikzstyle{yellow vertex}=[circle,fill=yellow!100,minimum size=10pt,inner sep=0pt]
\tikzstyle{edge} = [draw,thick,-]
\tikzstyle{pedge} = [draw,thick,.]
\tikzstyle{red edge} = [draw, thick,-,red!50]
\tikzstyle{black edge} = [draw, line width=2pt,-,black!20]
\tikzstyle{weight} = [font=\smaller]

\begin{document}

\begin{frame}
  \maketitle

  \begin{center}
    {\smaller This course is based on Mathematics for Computer Science, Spring
    2015, by Albert Meyer and Adam Chlipala, Massachusetts Institute
    of Technology OpenCourseWare.}

    \includegraphics[width=0.2\textwidth]{../img/by-nc-sa}
  \end{center}
\end{frame}

\section{Introduction}

\begin{frame}
  \frametitle{Week 6 and 7 summary}

  {\larger
    {\bf Counting}

    \bigskip

    \begin{itemize}
    \item \structure{Sums and Products}
    \item \structure{Asymptotics}
    \item \alert{Counting with Bijections}
    \item \alert{Repetitions and Binomial Theorem}
    \item \alert{Pigeonhole Principle}
    \end{itemize}
  }
\end{frame}

\begin{frame}
  \frametitle{Counting Rules}

  {\larger

    \structure{How do you count things} and \structure{Why is it important}?

    \begin{itemize}

    \item Count the number of people by counting heads;
    \item Count the number of people by counting \alert{tables};
    \item Count the number of cards in a deck;
    \item Count the number of \structure{possible card combinations};
    \item Count the number of \alert{steps in an algorithm};
    \item count the number of \alert{passwords};
    \item count the number of {\bf \alert{problem configurations}}

    \end{itemize}
  }
\end{frame}

\subsection{The Bijection Rule}

\begin{frame}
  \frametitle{The Bijection Rule}

  {\larger

    The {\bf Bijection Rule} states that if there is a bijection $A
    \to B$, then $|A| = |B|$.

    \bigskip

    This means that if there is a \alert{set $A$ that we don't know
      the size}, but we make a bijection to a \structure{set $B$ that
      we know the size}, then we can discover the size of $A$.

    \bigskip

    We will use this rule a lot in this class.
  }
\end{frame}

\begin{frame}
  \frametitle{The Bijection Rule: Example}

  {\larger

    A card game has five colors of cards: \structure{Blue, Green, Red,
      Black and White.}

    \bigskip

    How many different decks can you build of \structure{12 cards}?

    \vfill

    \begin{center}
      \begin{tabular}{ccccc}
        000 & (none) & 0000 & 00 & 000\\
        blue & green & red & black & white\\
      \end{tabular}
    \end{center}
  }
\end{frame}

\begin{frame}
  \frametitle{Bijection Rule: Example}

  {\larger
    \begin{itemize}
    \item {\bf Set $A$}: Number of 12-card deck with 5 colors.
    \item {\bf Set $B$}: Number of 16-bit strings with 4 ``1''.
    \end{itemize}

    \bigskip

    \begin{center}
      \begin{tabular}{ccccccccc}
        000 & 1 & & 1 & 0000 & 1 & 00 & 1 & 000\\
        blue & & green & & red & & black & & white\\
      \end{tabular}
    \end{center}

    \bigskip

    If we can count set $B$, we know the size of set $A$.

    \bigskip

    {\bf Strategy:} Get really good at counting a few things, and use
    bijections to count everything else!  }
\end{frame}

\subsection{Counting Sequences}

\begin{frame}
  \frametitle{Counting Sequences}

  {\larger

    Our strategy is to create rules for \structure{couting sequences}.

    \bigskip

    We define a \structure{sequence} as ``choosing something from set
    A'' then ``choosing something from set B'' then ``choosing
    something from set C'', etc.

    \bigskip

    If we can define rules for counting sequences, and define rules
    for representing other objects as sequences, we can solve many
    complex counting problems!
  }
\end{frame}

\begin{frame}
  \frametitle{Starting from the basics: Sum rule and product rule}

  {\larger

    {\bf The Sum Rule:} If Two sets (A,B) are disjoint, the size of
    the set that combines $A$ and $B$ is:
    \begin{equation}
      |A \cup B| = |A| + |B|
    \end{equation}

    \bigskip

    {\bf The Product Rule:} If $|A| = m$ and $|B| = n$. The size of
    the sequence of choosing one thing from $A$ and one thing from $B$
    is:
    \begin{equation}
      |A \times B| = |A| \times |B|
    \end{equation}

  }
\end{frame}

\begin{frame}
  \frametitle{Sum Rule/Product Rule Example}

  {\larger

    You have \structure{3 blue shirts}, \alert{5 black shirts},
    \structure{4 pants} and \alert{3 skirts}.  If you wear one top and one
    bottom, {\bf how amany different cloth sets do you have}?

    \vfill

    \begin{itemize}
    \item {\bf TOP}: $|A| = |\text{Blu} \cup \text{Bla}| = 3 + 5 = 8$
    \item {\bf BOTTOM}: $|B| = |P \cup S| = 4 + 3 = 7$
    \item {\bf TxB Set}: $|A \times B| = 7 \times 8 = 56$
    \end{itemize}

    \bigskip

    You have 56 combinations of tops and bottoms.
  }
\end{frame}

\begin{frame}
  \frametitle{Product Rule and Bit Strings}

  {\larger

    How many bit strings exist of size \structure{n}?

    \bigskip

    We can think of this problem as a \alert{sequence of choices}:
    \begin{itemize}
    \item We choose 1st bit from $\{0,1\}$
    \item We choose 2nd bit from $\{0,1\}$
    \item $\ldots$
    \item We choose nth bit from $\{0,1\}$
    \end{itemize}

    \begin{equation}
      \{0,1\}^n ::=
      \{0,1\}\times\{0,1\}\times\{0,1\}\times\ldots\times\{0,1\}
    \end{equation}
    So by the product rule:
    \begin{equation}
      |\{0,1\}^n| = |\{0,1\}|^n = 2^n
    \end{equation}
  }
\end{frame}

\subsection{Counting Passwords}

\begin{frame}
  \frametitle{Password Counting}

  {\larger

    How many different passwords exist with these rules?

    \bigskip

    \begin{itemize}
    \item Digits and letters are acceptable;
    \item From 6 to 8 characters;
    \item Starts with a letter;
    \item Case sensitive;
    \end{itemize}
  }
\end{frame}

\begin{frame}
  \frametitle{Password Counting}

  {\larger
    \begin{itemize}
    \item $L ::= \{a,b,c,\ldots,y,z,A,B,C,\ldots,Y,Z\}, |L| = 52$
    \item $D ::= \{0,1,2,3,4,5,6,7,8,9\}, |D| = 10$
    \item $P_n ::=$ Passwords with $n$ characters.
    \item $P_n ::= L \times (L\cup D)^{n-1}$

      \bigskip

    \item We want to know: $P_6 \cup P_7 \cup P_8$
    \end{itemize}
  }
\end{frame}

\begin{frame}
  \frametitle{Password Counting}

  {\larger

    \begin{itemize}
    \item Size of the password space: $|L\times(L\cup D)^5 \cup
      L\times(L\cup D)^6 \cup L\times(L\cup D)^7|$

      \bigskip

    \item Sum Rule:\\
      $|L\times(L + D)^5| + |L\times(L + D)^6| +  |L\times(L + D)^7|$

      \bigskip

    \item Product Rule:\\
      $|L|\cdot(|L| + |D|)^5 + |L|\cdot(|L| + |D|)^6 +  |L|\cdot(|L| + |D|)^7$

      \bigskip

    \item Replacing sizes:\\
      $52\times62^5+52\times62^6+52\times62^7$ \\
      \hfill$= 1.8\times10^{14}$ different passwords.
    \end{itemize}

  }
\end{frame}

\subsection{Another Example}

\begin{frame}
  \frametitle{Counting Sevens}

  {\larger
    How many numbers with 4 digits have {\bf at least one 7}?

    \bigskip

    \begin{onlyenv}<2>
    Count cases based on \structure{first occurence of seven}

    \bigskip

    \structure{o}: digit \structure{with} 7 (10 choices) \hfill \alert{x}:
    digit $\neq$ 7 (9 choices)

    \bigskip

    So we define four possible sequences:\\
    7\structure{ooo}\hfill\alert{x}7\structure{oo}\hfill\alert{xx}7\structure{o}\hfill\alert{xxx}7

$10^3$ \hfill $9\cdot10^2$ \hfill $9^2\cdot10$\hfill $9^3$

    \bigskip

    \begin{center}
      3439
    \end{center}
    \end{onlyenv}
  }
\end{frame}

\section{Generalized Rules}

\begin{frame}
  \frametitle{Generalized Product Rule}

  {\larger

    How can I choose a \alert{group of 5 students} from a class of 25?

    \bigskip

    {\bf Thinking with a sequence of choices:}
    \begin{itemize}
    \item Select 1 student from 25
    \item Select 1 student from 24
    \item Select 1 student from 23
    \item Select 1 student from 22
    \item Select 1 student from 21
    \end{itemize}

    \bigskip

    $|G| = 25\cdot24\cdot23\cdot22\cdot21$
  }
\end{frame}

\begin{frame}
  \frametitle{Generalized Product Rule}

  {\larger

    If we have to choose $k$ elements from a set of size $n$,
    \alert{without repetition}:

    \begin{equation}
      n\cdot n-1\cdot\ldots\cdot n-k \text{ OR } \frac{n!}{(n-k)!}
    \end{equation}

  }

\end{frame}

\begin{frame}
  \frametitle{Generalized Product Rule: Chess Example}

  {\larger
  How many different ways can we put \structure{a bishop, a knight and
    a pawn} in a chessboard \alert{in different rows and columns}?

  \bigskip

  \begin{center}
    \includegraphics[width=0.8\textwidth]{../img/chess_count1}
  \end{center}
  }

\end{frame}

\begin{frame}
  \frametitle{Generalized Product Rule: Chess Example}

  {\larger
  Let's represent this problem as a \structure{Sequence of Choices}:
  \begin{itemize}
  \item Column for Knight: $c_n$ (from 8)
  \item Column for Bishop: $c_b$ (from 7)
  \item Column for Pawn: $c_p$ (from 6)
  \item Row for Knight: $r_n$ (from 8)
  \item Row for Bishop: $r_b$ (from 7)
  \item Row for Pawn: $r_p$ (from 6)
  \end{itemize}

  \bigskip

  Total Positions: $c_nc_bc_pr_nr_br_p = 8\cdot7\cdot6\cdot8\cdot7\cdot6$
  }
\end{frame}

\subsection{The Division Rule}

\begin{frame}
  \frametitle{The Division Rule}

  {\larger

  I could count the number of students in this room by \alert{counting
    the number of {\bf hands} in the room}.

  \bigskip

  \begin{itemize}
  \item $A$ - number of students in the room
  \item $B$ - number of hands in the room
  \end{itemize}

  Since every student \structure{maps} to two hands, then:

  \begin{equation}
    |B| = 2|A|
  \end{equation}

  \vfill

  The \structure{Division Rule} says that if there is a \alert{k-to-1}
  relationship between $A$ and $B$, then $|B| = k|A|$.

  }
\end{frame}

\begin{frame}
  \frametitle{Chess Example 2}

  {\larger How many different ways can we put \structure{two towers}
    in a chessboard \alert{in different rows and columns}?

  \bigskip

  \begin{center}
    \includegraphics[width=0.8\textwidth]{../img/chess_count1}
  \end{center}
  }

\end{frame}

\begin{frame}
  \frametitle{Chess Example 2}

  {\larger
  Let's represent this problem as a \structure{Sequence of Choices}:
  \begin{itemize}
  \item Column for Tower 1: $c_1$ (from 8)
  \item Column for Tower 2: $c_2$ (from 7)
  \item Row for Tower 1: $r_1$ (from 8)
  \item Row for Tower 2: $r_2$ (from 7)
  \end{itemize}

  \bigskip

  Total Positions: $c_1c_2r_1r_2 = (8\cdot7)^2$ \hfill \alert{WRONG!}
  }
\end{frame}

\begin{frame}
  \frametitle{Chess Example 2}

  {\larger
    Total Positions: $c_1c_2r_1r_2 = (8\cdot7)^2$ \hfill \alert{WRONG!}

    \bigskip

    (2,7,3,4) is the same as (7,2,4,3) -- \alert{The two towers are equal!}

    \bigskip

    Every position can be ``doubled'' by switching the towers, so we
    have a 2-1 relationship.

    \bigskip

    Therefore, the number of positions is $\frac{(8\cdot7)^2}{2}$
  }
\end{frame}

\subsection{Binomial Theorem}
\begin{frame}
  \frametitle{Counting Subsets}

  {\larger
    How many size 4 subsets of 1 to 13 can we make?
    \begin{itemize}
    \item Set $A ::=$ total permutations of 1 to 13
    \item Set $B ::=$ size 4 subsets
    \end{itemize}

    \bigskip

    Map: \structure{$a_1a_2a_3a_4a_5\ldots a_{11}a_{12}a_{13} \in A$}\\
    To: \alert{$\{a_1a_2a_3a_4\} \in B$}

  }
\end{frame}

\begin{frame}
  \frametitle{Counting Subsets}

  {\larger
    Map: \structure{$a_1a_2a_3a_4a_5\ldots a_{11}a_{12}a_{13} \in A$}\\
    To: \alert{$\{a_1a_2a_3a_4\} \in B$}

    \bigskip

    \begin{itemize}
    \item \structure{$a_1a_3a_4a_2$}$a_5\ldots a_{11}a_{12}a_{13}$ also maps.
    \item \structure{$a_1a_2a_3a_4$}$a_{12}\ldots a_{7}a_{9}a_{5}$ also maps.
    \end{itemize}

    \bigskip

    For \alert{one subset in $B$}, we can map $4!$
    \structure{permutations of the first four elements, and $9!$
      permutations of the others}.

    \vfill

    So the relation between $B$ and $A$ is $4!\cdot9!$-to-1!

  }
\end{frame}

\begin{frame}
  \frametitle{Counting Subsets: Binomial Coefficient}

  {\larger
    So to choose a 4 subset out of 13 elements:
    \begin{equation}
      13! = |A| = 9!4!|B|, |B| = \frac{13!}{9!4!}
    \end{equation}

    \vfill

    More generally, to choose $k$ subset from $n$ elements:

    \begin{equation}
      \frac{n!}{n!(n-k)!} = \binom{n}{k}
    \end{equation}

    \hfill ($n$ choose $k$)
  }
\end{frame}

\subsection{2-pair Poker}

\begin{frame}
  \frametitle{Everything Together: 2-pair poker hand}

  {\larger

    In the game of \structure{Poker}, you draw {\bf 5 cards} of a
    52-card deck with \structure{13 ranks} and \structure{4 suits}.

    \bigskip

    \begin{center}
      \includegraphics[width=0.6\textwidth]{../img/poker1}
    \end{center}

    A \structure{2 pair} hand has
    \begin{itemize}
    \item 2 cards of one rank;
    \item 2 cards of another rank;
    \item 1 card of a third rank;
    \end{itemize}
  }
\end{frame}

\begin{frame}
  \frametitle{2-pair poker hand}

  {\larger
    What is the probability of a 2-pair hand?

    \bigskip

    {\bf Total number of hands:} $\binom{52}{5}$

    \bigskip

    {\bf Total number of 2-pair:}
    \begin{itemize}
    \item Select first rank (1 of 13)
    \item Select first suits (2 of 4)
    \item Select second rank (1 of 12)
    \item Select second suits (2 of 4)
    \item Select third rank (1 of 11)
    \item Select third suit (1 of 4)
    \end{itemize}
  }
\end{frame}

\begin{frame}
  \frametitle{2-pair Poker Hand}

  {\larger
    1st Rank - 1st Suit - 2nd Rank - 2nd Suit - 3rd rank - 3rd suit\\

    \begin{equation*}
      13\cdot12\cdot11\cdot\binom{4}{2}\cdot\binom{4}{2}\cdot4
    \end{equation*}

    \begin{center}
      \alert{Problem!}

      \includegraphics<2>[width=0.7\textwidth]{../img/poker2}
    \end{center}
  }
\end{frame}

\begin{frame}
  \frametitle{2-pair poker hand -- Problem}

  {\larger
    {\bf Total number of 2-pair:}
    \begin{itemize}
    \item Select \alert{first rank} (1 of 13)
    \item Select first suits (2 of 4)
    \item Select \alert{second rank} (1 of 12)
    \item Select second suits (2 of 4)
    \item Select third rank (1 of 11)
    \item Select third suit (1 of 4)
    \end{itemize}

    \bigskip

    First and Second rank may be switched: \alert{2-to-1} relationship!
  }
\end{frame}

\begin{frame}
  \frametitle{2-pair Poker Hand -- Fixed!}

  {\larger
    1st Rank - 1st Suit - 2nd Rank - 2nd Suit - 3rd rank - 3rd suit\\

    \bigskip

    2 Sequences to 1 hand: 2-to-1 relationship: $|A| = 2|B|$

    \bigskip

    \begin{equation*}
      \frac{1}{2}\cdot13\cdot12\cdot11\cdot\binom{4}{2}\cdot\binom{4}{2}\cdot4
    \end{equation*}

  }
\end{frame}

\section{Bookkeeper Principle}

\begin{frame}
  \frametitle{The BOOKKEEPER Principle}

  {\larger
    How many permutations has the word \alert{BOOKKEEPER}?

    \bigskip

    \begin{itemize}
    \item Total permutations: $bo_1o_2k_1k_2e_1e_2pe_3r = 10!$

      \bigskip

    \item But how many mappings do we have?\\
      $po_1k_1e_1ro_2e_2k_2e_3b$ and\\
      $po_2k_2e_2ro_1e_1k_1e_3b$ and many others...

      \bigskip

    \item 2! permutations of $o_1o_2$, 2! of $k_1k_2$ and 3! of $e_1e_2e_3$
    \item 2!2!3!-to-1 mapping of permutations.
    \end{itemize}

    \bigskip

    Total Permutations:
    \begin{equation*}
      \frac{10!}{2!2!3!}
    \end{equation*}
  }
\end{frame}

\begin{frame}
  \frametitle{Generalized Multinomial Coefficient}

  {\larger

    Permutation of length $n$ word with $n_1$ a's, $n_2$ b's, $n_3$ c's...

    \bigskip

    \begin{equation}%
      \binom{n}{n_1,n_2,n_3,\ldots,n_k} = \frac{n!}{n_1!n_2!n_3!\ldots n_k!}
    \end{equation}

    \vfill

    What is the number of ways to rearrange the word \alert{SYSTEMS}?

    \begin{equation*}
      \binom{6}{1,1,3,1,1} = \frac{6!}{3!1!1!1!1!} = 6\cdot5\cdot4
    \end{equation*}

  }
\end{frame}

\subsection{Binomial Theorem}
\begin{frame}
  \frametitle{The Binomial Theorem}

  {\larger
  What is the value of $(a+b)^n$?

  \begin{equation}
    (a+b)^n = \sum^n_{k = 0}\binom{n}{k}a^{n-k}b^k
  \end{equation}

  \vfill

  The expansion of $(a+b)^n$ is the sum of all permutations of ``n''
  a's, ``n-1'' a's and 1 b's, ``n-2'' a's and ``2'' b's, ... etc.
  }
\end{frame}

\begin{frame}
  \frametitle{The Binomial Theorem: Examples}

  {\larger

  {\bf Example 1}: The coefficient of $a^3b^5$ from $(a+b)^8$ is the
  number of permutations of $a_1a_2a_3b_1b_2b_3b_4b_5 =
  \frac{8!}{3!5!}$

  \bigskip

  {\bf Example 2}: The coefficient of $bn^2a^3$ from $(b+n+a)^6$ is
  the number of permutations of \structure{banana} = $\frac{6!}{1!2!3!}$

  \bigskip

  {\bf Example 3}: The coefficient of
  $x_1^{k_1}x_2^{k_2}x_3^{k_3}\ldots x_n^{k_n}$ from $(x_1 + x_2 +
  \ldots + x_n)^{k_1+k_2+\ldots+k_n}$ is
  \begin{equation}
    \binom{k_1+k_2+\ldots+k_n}{k_1,k_2,\ldots,k_n}
  \end{equation}

  }
\end{frame}

\section{Pigeonhole Principle}

\begin{frame}
  \frametitle{The Pigeonhole Principle}

  {\larger

    {\bf Puzzle:} A drawer has \alert{red socks}, \structure{blue
      socks}, and {\bf black socks}. How many socks do you need to
    pick in the dark to be sure that you have \structure{at least one
      matching pair}?

    \vfill

    \begin{block}{The Pigeonhole Principle}
      If there are more pigeons then pigeonholes, there is at least
      one hole with 2 pigeons.
    \end{block}
  }
\end{frame}

\begin{frame}
  \frametitle{Pigeonhole Example: Same number of hairs in Tokyo!}

  {\larger

    {\bf Claim:} There is a group of \alert{at least 40 people} with
    \structure{the exact number of hairs} in Tokyo.

    \bigskip

    \begin{itemize}
    \item The number of hairs in a person is between 0 and 200.000 (Set $B$)
    \item The number of people in Tokyo is 9.000.000 (Set $A$)
    \item By the \structure{Pigeonhole Principle}, there are at least
      $k$ ($|A|= k|B|$) people in the same group.
    \end{itemize}

    \bigskip

    Therefore, there is \alert{at least one group of 40 people} with
    the exact same number of hairs in Tokyo.

    \vfill

    \begin{center}
      Super Useful!
    \end{center}
  }
\end{frame}

\begin{frame}
  \frametitle{Pigeonhole Principle: Pitfalls}

  {\larger

    {\bf Correct Example:} If you draw 5 cards from a normal deck,
    then \structure{at least 2 cards} have the same suit.

    \bigskip

    {\bf Incorrect Example:} If you draw 5 cards from a normal deck
    then \structure{at least 2 cards} have the {\bf Hearts} suit.\\
    \hfill(\alert{WRONG!})\\
    \hfill(Example: Clubs, Clubs, Clubs, Clubs, Clubs)

    \vfill

    The Pigeonhole principle says that {\bf a group} will have size k,
    but it does not say \alert{which group} will have size k.
  }
\end{frame}

\begin{frame}
  \frametitle{Pigeonhole Principle: Subset Sums}
  \begin{columns}
    \column{0.7\textwidth}

    {\larger
      \begin{itemize}
      \item The image to the right has 90 numbers of 25 digits each;
      \item Are there 2 subsets with the exact same sum?
        \bigskip

      \item<2> Maximum sum: $90\times10^{25}$
      \item<2> Maximum subsets: $2^{90} > 1.237 \times 10^{27}$
        \bigskip

      \item<2> By the \structure{pigeonhole principle}, at least two
        subsets have the same sum!

      \end{itemize}
    }

    \column{0.2\textwidth}
    \hspace{-1.3cm}\includegraphics[height=0.8\textheight]{../img/90numbers}
  \end{columns}
\end{frame}

%% TODO: Use Inclusion-Exclusion Principle to prove Euler's Theorem

\section{Conclusion}
\begin{frame}
  \frametitle{Summary}

  \begin{itemize}
  \item How to count permutations
  \item Division Principle: k-to-1 relations
  \item Binomial Theorem
  \item Pigeonhole Principle
  \end{itemize}
\end{frame}

\end{document}


\documentclass{tufte-handout}
\usepackage{amsmath}
\pagestyle{empty}
\usepackage[utf8]{inputenc}
\usepackage{mathpazo}
\usepackage{microtype}

\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{chains}
\usetikzlibrary{decorations}

\title{Spanning USA}
\author{Thore Husfeldt}

\begin{document}

\maketitle

\subsection{Description}
Find a minimum spanning tree of highway distances between 128 American cities.

\begin{marginfigure}
\includegraphics[width=2in]{us66.png}
\end{marginfigure}

\subsection{Requirements}

You algorithm returns a list of edges that constitutes a minimum spanning tree, as well as its total length.
For the graph described in \verb!USA-highway-miles.in!, the length is 16598.

\paragraph{Minimal solution:} Your algorithm has to work correctly (not only on this input -- otherwise you could just write \verb!print "16598"!) and run in polynomial time.
You can decide yourself if you want to use Prim's or Kruskal's algorithm.
Note that I don’t require you to implement fancy tricks like a decrease-key priority queue and or a clever union–find data structures for connected components.
The graph isn’t that big, and quadratic time will do just fine.

\paragraph{Good solution:}
Solve the problem in $O(m\log n)$ and optimise as much as you can.
This involved either writing a good priority queue for Prim's algorithm, or a union–find data structure for Kruskal's.

\subsection{Tip}

Be careful with reading the input file, names of cities don't necessarily behave as you expect. Especially, if you get the answer 16394, your error is probably in the parsing stage, not in the algorithm.

\subsection{Deliverables}

\begin{enumerate}
  \item The source code for your implementation
  \item A report in PDF, including the MST for the graph given by \verb!tinyEWG-alpha.txt!.
  Use the report skeleton in the {\tt doc} directory.
  \end{enumerate}

\end{document}


\documentclass[8pt]{article}

\usepackage{fullpage}
\usepackage[margin=.7in]{geometry}
\usepackage{epic}
\usepackage{eepic}
\usepackage{graphicx}
\usepackage{mathtools}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{ragged2e}
\usepackage[parfill]{parskip}

\newcommand{\proof}[1]{
{\noindent {\it Proof.} {#1} \rule{2mm}{2mm} \vskip \belowdisplayskip}
}

\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}

\newtheorem{lemma}{Lemma}[section]
\newtheorem{theorem}[lemma]{Theorem}
\newtheorem{claim}[lemma]{Claim}
\newtheorem{definition}[lemma]{Definition}
\newtheorem{corollary}[lemma]{Corollary}

\begin{document}
\hfill \small{\today} \\
\setlength{\fboxrule}{.5mm}\setlength{\fboxsep}{1.2mm}
\newlength{\boxlength}\setlength{\boxlength}{\textwidth}
\addtolength{\boxlength}{-4mm}
\begin{center}\framebox{\parbox{\boxlength}{\bf
\center{CS 577 - Homework 5}
\center{Sejal Chauhan, Vinothkumar Siddharth, Mihir Shete}
}}\end{center}
\vspace{5mm}

\section{Graded written problem}

\textbf{Input:} In a city there are $n$ bus drivers. There are also $n$ morning bus routes and $n$ afternoon bus routes, each with various lengths. Each driver is assigned one morning route
and one evening route. For any driver, if his total route length for a day exceeds $d$, he has to
be paid overtime for every hour after the first $d$ hours at a fixed rate per hour.
\\ \\
\textbf{Output:} Assign one morning route and one evening route to each bus driver so that the total overtime amount that the city authority has to pay is minimized.

\subsection{Algorithm}
Our greedy algorithm begins by sorting the $n$ morning bus routes in reverse order (Longest route first) and the $n$ afternoon bus routes in order. To find a set $G\{(m,a): \forall m \in M \land \forall a \in A \}$, where M is set of all morning routes and A is the set of all evening routes, we will use the following \textit{strategy}:
\begin{enumerate}
    \item Starting from the first morning route we try to find the \textit{largest} afternoon route such that their sum is less than or equal to $d$.
    \item If there is no such afternoon route such that the sum of the morning and afternoon route is less than or equal to $d$ then we will pair the smallest available afternoon route with the given morning route.
    \item Rule \textit{2.} Also implies that if morning route is greater than $d$ we will pair it with the smallest available afternoon route.
\end{enumerate}

Consider the following morning and evening routes ordered as per our algorithm: \\ \\
$Morning:$ \{$7,4,3,1$\} $Afternoon:$ \{$2,3,6,9$\} $d:8$ \\ \\
In the above example we will start from the largest morning route $7$, we can see that the smallest available afternoon route and the largest morning route add to a value greater than $d$, since there is no available route in the afternoon with which we can pair the largest route in the morning to get the total duration less than $d$ we will pair $7$ and $2$ (i.e the smallest available afternoon route). Similarly, we will pair ($4$, $3$), ($3$, $6$) and ($1$, $9$) according to our greedy algorithm.

If we do time complexity analysis of the \textit{strategy} of pairing the routes we can see that in the worst case we will have to compare the $1^{st}$ route in the morning to $n$ routes in the afternoon, the $2^{nd}$ route in the morning to $n - 1$ routes in the afternoon and so on. Hence, our time complexity will be $O(n^2)$. To better the running time of our strategy we will use the following algorithm (a variant of binary search). This algorithm will take $key$ as one of the inputs where $key = d - m_i$, $m_i$ is the route duratio of the $i^{th}$ morning trip and will return the best match (as decribed in our strategy) from the array $A$ of size $\left\vert{A}\right\vert$ which is the array of ordered Afternoon trips.

\begin{algorithm}
\caption{Find-Pair}\label{euclid}
\begin{algorithmic}[1]
\Procedure{Find-Pair}{$key$, $A$, $left$, $right$}

\If{$left \textgreater right$}
    \Return left
\EndIf

\State $mid \leftarrow \floor{(right + left) / 2}$
    \If{$key \textless A[mid]$}
        \State \Return \Call{Find-Pair}{$key$, $A$, $left$, $mid - 1$}
    \ElsIf{$key \textgreater A[mid]$}
        \State \Return \Call{Find-Pair}{$key$, $A$, $mid + 1$, $right$}
    \ElsIf{$key = A[k]$}
        \State \Return k
    \EndIf
\EndProcedure
\end{algorithmic}
\end{algorithm}

If $key = d - m_i$ is present in the array $A$ then \textbf{Find-Pair} will return an index $k$ such that $A[k] = key$ and we can directly pair this trip of length $A[k]$ with trip $m_i$. if $key$ is not present and $A[k]$ \textless $key$ then we will also pair $k$ with $m_i$, but if $A[k] \textgreater m_i$ then we will pair $m_1$ with $A[k-1]$ if $k \textgreater 0$. The runtime of \textbf{Find-Pair} is $log(n)$ because we are divind the array in half for every iteration and using this algorithm we can complete our strategy on ordered arrays in $O(nlog(n))$ time.

Let $M$ be the array of all ordered morning trips and $A$ be the array reresenting all ordered afternoon trips, our \textit{strategy} will be represented by the following Pseudocode:

\begin{algorithm}
\caption{Pseudocode of our greedy stratgey}\label{euclid}
\begin{algorithmic}[1]
\Procedure{Find-All-Pairs}{$M$, $A$}

\State $i \leftarrow 0$

\While{$i \textless \left\vert{M}\right\vert$}
    \If {$M[i] \geq d$}
        \State Pair $M[i]$, $A[0]$
        \State Delete $A[0]$ from $A$
        \State $i \leftarrow i + 1$
        \State continue
    \EndIf
    \State $key \leftarrow d - M[i]$
    \State $index \leftarrow \Call{Find-Pair}{key, A, 0, \left\vert{A}\right\vert}$
    \If {$A[index] \leq key$}
        \State Pair $M[i]$, $A[index]$
        \State Delete $A[index]$ from $A$
    \Else
        \If{$index = 0$}
            \State Pair $M[i]$, $A[index]$
            \State Delete $A[index]$ from $A$
        \Else
            \State Pair $M[i]$, $A[index - 1]$
            \State Delete $A[index - 1]$ from $A$
        \EndIf
    \EndIf
    \State $i \leftarrow i + 1$
\EndWhile
\EndProcedure
\end{algorithmic}
\end{algorithm}

\subsection{Exchange Argument}
Let $r$ be all the route-pairs in a solution that are greater than $d$ and let $l_i$ be the length of the route-pair . $p(G) = \sum_{r}(l_i - d)$ for our greedy solution $G$, similarly for an optimal Solution $S$ $p(S) = \sum_{r}(l_i - d)$

If, $G = S$ then clearly $p(G) = p(S)$.

Otherwise, $G \neq S$. So, there must be some 2 routes in $G$ and $S$, such that the morning routes are the same but the paired afternoon routes are different. More formally, if ($M_G(i)$, $A_G(i)$), ($M_G(j)$, $A_G(j)$) are 2 pairs in G and ($M_S(k)$, $A_S(k)$), ($M_S(l)$, $A_S(l)$) are 2 pairs in S, then
$$M_G(i) = M_S(k) \And M_G(j) = M_S(l)$$
but,
$$A_G(i) \neq A_S(k) \And A_G(j) \neq A_S(l)$$ \\
Let, $M_{ik} = M_G(i) = M_S(k)$ and $M_{jl} = M_G(j) = M_S(l)$
Now, consider the following scenarios in $S$:
\begin{description}
    \item[$M_{ik} \textgreater M_{jl}$] \hfill \\
        Let $S'$ denote a solution which would have selected the ($M_{ik}, A_S(l)$) and ($M_{jl}, A_S(k)$) pair contrary to $S$. $S'$ is similar to $G$ in a way that $G$ would also have chosen the same pairing as $S'$. So, given the constraint on morning routes $S'$ will choose the afternoon route first for $M_{ik}$ and it will pair with $A_S(l)$ if:

    \begin{enumerate}
        \item \textbf{$A_S(l) \textgreater A_S(k)$ and $M_{ik} + A_S(l) \leq d$} \hfill \\
            In this scenario, it is trivial to see that $M_{jl} + A_S(k) \leq d$ as both morning and afternoon trips are lesser than their counterparts i.e $M_{jl} \textless M_{ik}$ and $A_S(k) \textless A_S(l)$. So, the choice of pair for $S'$ does not yield any overtime. Hence, $p(S') \leq p(S)$ \\

        \item \textbf{$A_S(l) \textless A_S(k)$ and $M_{ik} + A_S(l) \textgreater d$} \hfill \\
            For the above constraints $M_{jl} + A_S(k)$ can be less than, more than or equal to d, so lets aanalyze all the conditions:
            Here, if $M_{jl} + A_S(k) \textgreater d$ it is trivial to see that $p(S') = p(S)$ since the extra hours for both the selections will be $2d - M_{ik} - M_{jl} - A_S(k) - A_S(l)$. \\ \\
            If, $M_{jl} + A_S(k) \leq d$ then it implies that $M_{jl} + A_S(l) \leq d$ from the constraints. $$p(S') =  M_{ik} + A_S(l) - d$$ And $$p(S) = M_{ik} + A_S(k) - d$$
            And since $A_S(l) \textless A_S(k)$ it is clear that $p(S') \textless p(S)$
    \end{enumerate}
\end{description}

From the above analysis it is clear that $p(S') \leq p(S)$ whenever the order is changed according to greedy strategy. Hence, our greedy strategy will provide an optimal solution.

\subsection{Running Time Analysis}
To sort get the routes in correct order for our greedy algorithm to process we will have to spend $O(n\log{n})$ time. The \textbf{Find-All-Pairs} procedure will iterate over all $n$ elements in $M$ and all the operations in these iterations are constant time except \textbf{Find-Pair}.

\textbf{Find-Pair} will take $O(\log{n})$ time in the worst case for input of size $n$. In the \textbf{Find-All-Pairs} procedure we can see that the input size to \textbf{Find-Pair} decreases by $1$ each iteration. So the total running time for \textbf{Find-All-Pairs} will be:

$$\log{n} + c + log{n - 1} + c ...(n times) = n*c + \log{n!}$$

So the total running time of \textit{ordering} and running the \textit{strategy} will be:

$$O(n*c + \log{n!} + n\log{n})$$

Since, $\log{n!} \leq n\log{n}$. The total running time is $O(n\log{n})$ in worst case.

\end{document}


%!TEX root =  ../main.tex
% !TEX useTabs

\mychapters{Introduction}{introduction}{\chapdir/pics/pano-5.jpg}

\section{Motivation}

This textbook exists to facilitate a class coming after Algebra II and before Calculus II.  It is meant
to be a coherent mixture of Paul A Foerster's \textit{Precalculus with Trigonometry} and his
\textit{Calculus: Concepts and Applications}.  Some students can handle moving at a much
faster pace through Precalculus topics, and hence cover the first four chapters of \textit{Calculus}.
A student should not be expected to
do well on the AP exam after this class (without additional side work), because of inadequate
time spent on integrals.

\personfeature[-1.6in]{\chapdir/pics/foerster}{Paul
    Foerster}{1936-, U.S.A.}{taught mathematics at Alamo Heights
    High School in San Antonio, Texas, from 1961 to 2011\cite{BuckleyFoerster}
    . After earning
    a BS in chemical engineering, he served four years as an engineering
    duty officer with the Navy's Nuclear Propulsion Program. Tutoring
    high school students while on active duty, he found his true passion
    --- seeing students get the ``Aha!'' reaction as they finally grasp new
    concepts. After completing his Navy service, he went back to college
    for a spring and a summer to get his teaching certification. His teaching
    has been interrupted only by a year's leave of absence to earn a master's
    degree in mathematics through an academic year institute awarded by
    the National Science Foundation.}

Once a school is over a certain size, and the pool of students to draw from is sufficient, there may
be the critical mass needed to create an accelerated class for those who can proceed quickly through
the skills and understanding needed to appreciate calculus.  While it may be possible in such
situations to promote kids sooner, it may also make sense to wait until their junior year, in which case
an analysis class is a possible solution.  Students are introduced to categories of
functions, with an eye towards their uses in calculus, at the same time.  At best, a student would proceed
from this class to a Calculus II class, which would move quickly through derivatives and
spend most of its time on integrals.  At worst, if a student (barely) made it through this class, they would
no doubt do well in an AB class, and have time to go back and master derivatives.

\section{Prerequisites}

Students are expected to have performed well in a strong Algebra II class, and retain excellent
arithmetic, graphing, and factoring skills.  This textbook contains an extensive set of appendices,
some of which should
be assigned as summer work.  We recommend thorough consultation with the sophomore teacher
to see which areas need more work and require additional assignments.  It is burdensome, but we
also highly recommend making assignments due at regular intervals throughout the summer, perhaps
via electronic submission.
American summer breaks are too long, and students forget a great
deal over three months.  Because of the exploratory nature of assignments in the appendix, it is
possible to assign exercises which were not covered at all in Algebra II, with little or no instruction.

This textbook contains many calculator lessons for the Texas Instrument 83 or 84 (hereafter \textbf{TI-8*}).
This antiquated device may be taken into the AP, ACT, and SAT exams, and hence
should be thoroughly mastered by a student who wishes to maximize his or her tools.  Other
devices may be used, but are unwise, since the TI-8* is the most powerful tool allowed in the
standardized tests.

\section{Worldview}
It is highly remarkable how many  mathematics textbooks begin without
prolegomena of any kind.  Such an oversight belies all of mathematical work, which seeks
to establish foundations and ascertain unassailable truths.  We will attempt to counter this trend
in the shortest space.

\personfeature[-1.0in]{\chapdir/pics/Immanuel_Kant_001}{Immanuel
    Kant}{1724-1804, Prussian}{was a German philosopher, and is
    considered a central figure in philosophy. He argued that the
    human mind creates the structure of experience, that reason
    is the source of morality, and that the world as it is ``in-itself'' is
    independent of our concepts of it. He is sometimes credited with
    being the greatest Modernist philosopher, and yet laying the
    ground from which Post-Modernism sprung.
    \href{https://en.wikipedia.org/wiki/Immanuel_Kant}{(Wikipedia)}}

The universe in which we find ourselves doing mathematics is a
reasonable and orderly one.  The Ancient Greek distinction between `cosmos'
and `chaos', or the Biblical separation of the watery abyss from the peaceable land are
metaphors in recognition of this fact.  The ethical and personal disorder which we bring to the world
does not belie its inherent predictability.
God has not absconded from his creation, but has
chosen to mediate himself, leaving us with the ability and responsibility to discover
what is hidden in the material world (Proverbs 25:2).  It is only by suppressing this truth that one could
pronounce as Einstein did that, ``The most incomprehensible thing about the universe
is that it is comprehensible.''\footnote{Albert Einstein,
\emph{Journal of the Franklin Institute}, Physics and Reality, p221, 1936.}
Only by \textit{a priori} rejecting the Judeo-Christian worldview -- that a reasonable and rational God
has made human beings in his image, and that we are therefore capable of thinking his thoughts
after him, and making true statements about the world -- can we understand comments such
as Kant's ``[W]ir auch, gleich als ob es ein glücklicher unsre Absicht begünstigender Zufall wäre,
erfreuet (eigentlich eines Bedürfnisses entledigt) werden, wenn wir eine solche systematische
Einheit unter bloß empirischen Gesetzen antreffen.''\footnote{
[We rejoice when, just as if it were a lucky chance favoring
our aim, we do find such systematic unity among merely empirical laws.] \textit{Critique of Judgment}, 1790.}
Rationality in human discourse and orderliness in the natural world are not things
which need to be proven but which must be presupposed for any proofs to be made.

Mathematics is not a construct of the human mind.  It is entirely foreseeable and
understandable that advances in number theory would later yield results in cryptography.
Esoteric processes such as analytic continuation produce useful dividends in
quantum physics, long after they are discovered.  Mathematics is the representation
and conceptualization of the invisible, created world.  Human beings are the representation
of the maximally personal God in the impersonal media of time, energy, and matter.
`Number' and other mathematical features are part of human language:
doing math is part of what it means to be human.  It is part of \emph{what} we are,
it is good, and it is imperative --- an integral component of our purpose.

\section{Method}

\personfeature[-0.6in]{\chapdir/pics/Albert_Einstein_(Nobel)}{Albert
    Einstein}{1879-1955, German}{was a theoretical physicist, and
    TIME's person of the century.  He is considered to be one of the
    smartest men of the the modern era for his revolutionary Special
    Theory of Relativity, and again for General Relativity.  He stated
    on numerous occasions that he could not ``imagine some will or
    goal outside the human sphere,'' and hence it is a happy
    coincidence that mathematics and science are so
    mutually affirming.
    \href{https://en.wikipedia.org/wiki/Albert_Einstein}{(Wikipedia)}}

Human beings are not mere containers of information.  Teaching is not plugging-in
a download wire from the side of the teacher's head into that of the students.  Most
things of value are \emph{caught}, not taught.  It is expected that a teacher will show the
lessons in this book to their students in the context of a safe, loving relationship, which
is of necessity personal, and therefore costly.  Eventually, students should feel safe to
fail in this class, because only failing produces change and ultimately learning.  Success
only confirms us in our biases.
Of course, no one likes to fail, so every class must balance a three-legged stool: structure,
support, and challenge.

If at all possible, teachers should present the material from a given lesson from a different
perspective than it is shown herein.  This gives the student the opportunity to read the book,
should they wish for another angle on the matter.  If students know how to learn, they should
be able to do the \textbf{problems} on their own, and spend class time presenting their
thoughts to others, or working through the \textbf{exercises} and discussing the book.
The first section of each chapter (after the first) is somewhat informal and self-explanatory,
and should be assigned as homework after the previous test without instruction.

Math assignments should never consist of 10 easy exercises, rotely completing the task defined
in the lesson, followed by 10 harder exercises, followed by 5 word-problems.  Problem sets
should be a journey, that is, they should possess a narrative arc.  Class periods should do the same.
Students must learn to ask themselves questions, a process they should see modeled in the
teacher.
``What did we just learn?  How can I summarize it in my own words?  How do I think it
will be applied?  How does it relate to what I already know?  When was this encountered
for the first time, and in what context (history)? How can it help me
understand more of the myriad things above my level?  How do I solve difficult problems
involving this principle? What might this become in my life and in the lives of my
neighbors?  How can I communicate this to others?  What are some common misunderstandings
people have when encountering this material?''

Finally, while we do have a volume of material we wish students to learn and retain,
it is irresponsible to allow students to think they have ``mastered'' mathematics.  Anyone
with a Masters degree in any field has been dislodged from his or her notion that one
can know \emph{everything} about anything.  When the Ph.Ds tell us they do not know all
there is to know about even a sliver of their discipline, we did not believe them ... at first.
But slowly, over years of paper-writing and drilling down into the subject, we see even they
can not know how much there is to know about one topic within their discipline! This is
only discouraging if we are delusional enough to think that our achievement is the
\textit{summum bonum}, the highest good.
We can indeed learn true things and asymptotically approach full understanding, but no
amateur should be so deluded as to think they know all there is to know about some
field within mathematics.  Hence, many lessons should include a taste of just how much
more there is to know, including problems that cannot be fully solved.

\section{To the Student}
\marginfig[-0in]{\chapdir/pics/TI-84_Plus}{The TI-84+.\label{fig:TU84}}

This is both a textbook and a workbook.  ``Math is not a spectator sport,'' and that means
you do not understand something until you've tried it.  Don't shortchange yourself by reading a
section before you've attempted the problems.  If you want to read ahead, that's fine, but
try the assignment before the reading.  Even if you don't get it all, consider it part of the reading.
Unless specifically asked to, we recommend you do not Google the answers.
Do you want to understand
the world?  Do you know that you are in a protected space, a safe time to explore and
make mistakes, a time that you will not be afforded later?  Take the time to learn as you go,
and you will learn how to learn, a lesson you can use for the rest of your existence.  If you get
desperate, cultivate relationships with human beings who can ask you questions that
allow you to discover the answer yourself, not be spoon-fed.

Always look out for \textbf{bold} words, in the problems, in the readings, and in the exercises.
Copy them down into your notes and try to define them for yourself, before you look in the
glossary or online. We
encourage you to take notes in the Cornell style.  This simply involves you drawing a large,
left-of-center capital `I' over your entire note page before you begin, taking notes as usual
in the right section, adding subject and paragraph headings on the left, and making
succinct summaries at the bottom of the page, perhaps a few before the test.  Headings,
your name, and the date should go across the top.
There are many videos on the internet about `Cornell Notes.'

\section{To the Teacher}

All the material in the prerequisites needs to be covered, ideally before the year begins.
Chapters 1-12 and 14 are the minimum.  Chapter 16 and 18-20 exist for the teacher's sake,
to allow variety over the years.  Ideally, that next class would use Foerster's excellent
\textit{Calculus} textbook, taking a month to review chapters 1-4, and then moving
on to the rest of the book, perhaps augmented the material with some lessons from
multivariable calculus and/or differential equations.

In a perfect world, every capable student would take Calculus II and Statistics
before graduating high school, but we don't live in that world.  Time permitting, discrete
topics should be broached, especially with students who will not take Stats.  The other discrete topics
are here as aides for those who have never seen them before.
Appendices B, C, and D exist for the teachers who love those topics and wish
to expand or vary the chapters from year to year.  There is always one student who can tell
they are not getting the whole story and will not let it rest!

Classes should consist of a brief introduction or grabber, the ``problem'' set, a succinct lesson, and then
discussion as much as possible.  Ideally, every class period will end with students summarizing what
they have learned and speculating about what it could become, or what they will do next.  Homework
should consist of careful note-taking and selected exercises.


\documentclass[jou]{apa6}

\usepackage[american]{babel}

\usepackage{csquotes}
\usepackage[style=apa,sortcites=true,sorting=nyt,backend=biber]{biblatex}
\DeclareLanguageMapping{american}{american-apa}
\addbibresource{bibliography.bib}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Discrete Structures
%% The start of RBS stuff
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Working internal and external links in PDF
\usepackage{hyperref}
% Extra math symbols in LaTeX
\usepackage{amsmath}
\usepackage{gensymb}
\usepackage{amssymb}
% Enumerations with (a), (b), etc.
\usepackage{enumerate}
\usepackage{xcolor}

\let\OLDitemize\itemize
\renewcommand\itemize{\OLDitemize\addtolength{\itemsep}{-6pt}}

\usepackage{etoolbox}
\makeatletter
\preto{\@verbatim}{\topsep=3pt \partopsep=3pt }
\makeatother

% These sizes redefine APA for A4 paper size
\oddsidemargin 0.0in
\evensidemargin 0.0in
\textwidth 6.27in
\headheight 1.0in
\topmargin -24pt
\headheight 12pt
\headsep 12pt
\textheight 9.19in

\setlength\parindent{0pt}

\title{Sample Quiz 5}
\author{Discrete Structures, Spring 2020}
\affiliation{RBS}

\leftheader{Discrete Sample Quiz 5}

\abstract{%
}

%\keywords{}

\begin{document}

%\thispagestyle{empty}

\twocolumn
\section{Worksheet 5: Number Theory}

\vspace{4pt}
{\bf Question 1.} Write at least one divisor (not equal to $1$ and to $N$) for the following numbers:\\
{\bf (A)} $N = 2^{48} + 1$\\
{\bf (B)} $N = 2^{77} - 1$\\
{\bf (C)} $N = 41^4 + 4$.

Write your answer as three comma-separated numbers arithmetic expressions.

{\em Note.} You may want to use various algebraic identities to factorize:
\begin{align}
 & a^n - b^n = \nonumber \\
= & (a-b)\left(a^{n-1} + a^{n-2}b + \ldots + ab^{n-2} + b^{n-1}\right) \nonumber \\
 & a^{2n+1} + b^{2n+1} = \nonumber \\
= & (a+b)\left(a^{2n} - a^{2n-1}b + \ldots - ab^{2n-1} + b^{2n}\right) \nonumber \\
 & a^4 + 4b^4 = \nonumber \\
= & (a^2 + 2b^2 - 2ab) (a^2 + 2b^2 + 2ab) \nonumber
\end{align}

\vspace{10pt}
{\bf Question 2.} Are these statements true or false (`all integers'' include also negative numbers):
\begin{enumerate}[(A)]
\item For all integers $a,b,c$, if $a\,\mid\,b$ and $a\,\mid\,c$, then $a\,\mid\,\text{gcd}(b,c)$.
\item For all integers $a,b,c$, if $a\,\mid\,\text{gcd}(b,c)$, then $a|b$ and $a|c$.
\item For all integers $a,b,c,d$, if $a\,\mid\,b$ and $c\,\mid\,d$, then $ac\,\mid\,\text{lcm}(b,d)$.
\item For all integers $a,b,c$, $\text{gcd}(\text{gcd}(a,b),c) = \text{gcd}(a,\text{gcd}(b,c))$.
\item For all integers $a,b,c$, $\text{lcm}(\text{gcd}(a,b),c) = \text{gcd}(\text{lcm}(a,c), \text{lcm}(b,c))$.
\item For all primes $p>2$, $2^p +1$ is not a prime.
\end{enumerate}

Write your answer as a comma-separated string of T/F. For example, {\tt T,T,T,T,T,T}.\\
{\em Note.} Even though you only write the answers,
make sure that you are able to justify your answer. For true statements you should be able to
find a reasoning; for false ones \textendash{} a counterexample.

\vspace{10pt}
{\bf Question 3.} Find $\text{gcd}(2160^{20},150^{30})$

Write your answer as a product of prime powers {\tt p\^{}a*q\^{}b*r\^{}c} or similar.
Numbers $p,q,r$ etc. should be in increasing order. All exponents (even those equal to $1$) should be written explicitly.

\vspace{10pt}
{\bf Question 4.}
Convert $(101\,0110\,0111)_2$ to base $16$, base $8$ and base $4$.

Write your answer as $3$ comma-separated numbers.
For the hexadecimal notation use all digits and also capital letters
A,B,C,D,E,F.

\vspace{10pt}
{\bf Question 5.} Find the sum and the product of these two integers written in ternary:
$(110112)_3$, $(1000221)_3$.\\

Write your answer as two comma-separated numbers (both written in binary).

{\em Note.} You may want to try the addition and multiplication algorithm directly in
ternary system (without converting them into the decimal and back).

\vspace{10pt}
{\bf Question 6.}
Write the fraction $1/7$ as an infinite periodic binary fraction.

{\em Note.} One method to get, say, the first 16 digits of this fraction, you can
multiply $1/7$ by $2^{16} = 65536$ and then express $65536/7 = 9362,\ldots$ in binary.
A more efficient way is to use the regular division algorithm (``long division'',
``dal\={\i}\v{s}ana stabi\c{n}\={a}''); this allows to generate a sequence of
binary digits of unlimited length.

Write your answer as $\textcolor{blue}{\mathtt{0.(}\ldots\mathtt{)}}$ or
$\textcolor{blue}{\mathtt{0.}\ldots\mathtt{(}\ldots\mathtt{)}}$.\\
(I.e. you start by the integer part, then write all digits preceding the period,
then the perdiod itself in round parentheses.)

\vspace{10pt}
{\bf Question 7.} Write the first eight powers (with non-negative exponents)
of number $5$ modulo $21$: $5^0,\,5^1,\,5^2,\,5^3,\,\ldots,5^{7}$.

Write your answer as a comma-separated list of eight remainders (mod $21$), \textendash{} all are numbers between $0$ and $20$.

\vspace{10pt}
{\bf Question 8.} Find the inverse values $1^{-1},\ldots,10^{-1}$ modulo $11$. (The inverse number of $x$ modulo $11$
is $x^{-1}$ such that $x^{-1}x \equiv 1\;(\text{mod}\,11)$.)

Write your answer as 10 comma-separated numbers.

\vspace{10pt}
{\bf Question 9.} Find the smallest three positive integer values of $x$ that
are solutions of the equation $55x + 21y=1$.

Write your answer as three comma-separated integers.

\newpage
\subsection{Answers}

\vspace{6pt}
{\bf Question 1.} Answer: {\tt 2\^{}16+1,2\^{}7-1,5}\\

{\bf (A)} $N = 2^{48} + 1 = \left( 2^{16} + 1 \right) \left( 2^{32} - 2^{16} + 1 \right)$.\\
{\bf (B)} $N = 2^{77} - 1 = \left( 2^{7} - 1 \right) \left( 2^{70} + 2^{63} + \ldots + 1 \right)$.\\
{\bf (C)} $N = 41^4 + 4$ ends with $5$, so it is divisible by $5$.
Also $N = \left( 41^2 + 2 \cdot 41 + 2 \right) \left( 41^2 - 2 \cdot 41 + 2 \right)$.

Expressions in form $x^4 + 4y^4$ can be factorized using Sophie Germain identity.
See \url{https://bit.ly/2xkJeq1}.

\vspace{6pt}
{\bf Question 2.} Answer: {\tt T,T,F,T,T,T}\\
{\bf (A)} $((a\,\mid\,b) \wedge (a\,\mid\,c)) \;\rightarrow\; a\,\mid\,\text{gcd}(b,c)$\\
{\tt True}. That's the definition of GCD: Any common divisor $a$ also divides $\text{gcd}(b,c)$.\\
{\bf (B)} $(a\,\mid\,\text{gcd}(b,c)) \;\rightarrow\; (a|b\,\wedge\,a|c)$\\
{\tt True}. Obviously $\text{gcd}(b,c)$ divides both $b$ and $c$.\\
{\bf (C)} $(a\,\mid\,b\;\wedge\;c\,\mid\,d) \;\rightarrow\; (ac\,\mid\,\text{lcm}(b,d))$.\\
{\tt False}. We can take $a = 2^3$, $b = 2^4$, $c = 2^5$, $d = 2^6$. Then $\text{lcm}(b,d) = 2^6$.\\
{\bf (D)} $\text{gcd}(\text{gcd}(a,b),c) = \text{gcd}(a,\text{gcd}(b,c))$.\\
{\tt True}. Both sides represent the GCD of all three numbers.\\
{\bf (E)} $\text{lcm}(\text{gcd}(a,b),c) = \text{gcd}(\text{lcm}(a,c), \text{lcm}(b,c))$.\\
{\tt True}. Take any prime factor that participates in the numbers
$$\left\{ \begin{array}{l}
a = p^i \cdot \ldots \\
b = p^j \cdot \ldots \\
c = p^k \cdot \ldots \\
\end{array} \right.$$
Then both sides are divisible by $p^m$, where
$$m = \max(\min(a,b),c) = \min(\max(a,c),\max(b,c)).$$
{\bf (F)} For all primes $p>2$, $2^p +1$ is not a prime.\\
{\tt True}. All such numbers are divisible by $3$, if $p$ is an odd number.

\vspace{6pt}
{\bf Question 3.} Answer: {\tt 2\^{}30*3\^{}30*5\^{}20}\\
Express both numbers as a product of their prime factors.
\begin{align}
 & \text{gcd}\left( (2^4 \cdot 3^3 \cdot 5)^{20},\;\; (2^1 \cdot 3^1 \cdot 5^2)^{30} \right) = \nonumber \\
= & \text{gcd}\left( 2^{80} \cdot 3^{60} \cdot 5^{20},\;\; 2^{30} \cdot 3^{30} \cdot 5^{60} \right) = \nonumber \\
= & 2^{30} \cdot  3^{30} \cdot 5^{20} \nonumber
\end{align}

\vspace{6pt}
{\bf Question 4.} Answer: TBD\\

\vspace{6pt}
{\bf Question 5.} Answer: TBD\\

\vspace{6pt}
{\bf Question 6.} Answer: TBD\\

\vspace{6pt}
{\bf Question 7.} Answer: TBD\\

\vspace{6pt}
{\bf Question 8.} Answer: TBD\\

\vspace{6pt}
{\bf Question 9.} Answer: TBD\\

\end{document}


\documentclass[12pt,letterpaper]{article}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{pdfsync}
\usepackage{url}
\usepackage{hyperref}
\usepackage{microtype}
\usepackage{siunitx}

\hypersetup{colorlinks=true, urlcolor=blue, citecolor=black, linkcolor=black}
\urlstyle{same}

\renewcommand{\d}{\mathrm{d}}

\title{Learning to Use Latex}
\author{Used with permission from Randy McDermott}
\date{\today}

\begin{document}

\maketitle

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

The purpose of this document is to provide most of the examples you will need to create a mathematical writeup using \LaTeX.  Simply copy and delete as needed.  The next section gives a quick overview of the document layout and shows how to reference sections of the document.  If you are new to \LaTeX, you will quickly come to appreciate the power of the referencing system.  Indeed, this is the primary reason---together with the quality of the mathematical formulae---that people come to like the \LaTeX~type-setting environment so much more than the wysiwyg (what you see is what you get) environment of Word.  You can easily shuffle sections, equations, figures, and bibliographic references without giving a nanosecond of thought to rearranging the numbers.  It's all done automatically.

% Notice the ~ between \LaTeX and type-setting.  Sometimes this is needed if the spacing looks off in the pdf.  You can also use this after abbreviations within a sentence, because the period makes LaTeX think a new sentence is coming and it will space things differently.  So, for example, Prof.~Trouv\'e would be better than Prof. Trouv\'e.

While this template will hopefully answer most of your questions, you should be aware that the online support for \LaTeX~is amazing.  A quick Google search with \verb=latex= as one of the arguments will return a wealth of information.  At this point, just about anything you can dream up has been done.

\section{Frontmatter}
\label{sec:front}

In \LaTeX~parlance, ``frontmatter'' refers to values set before the \verb=\begin{Document}= command.  For our purposes, this is pretty much self expanatory: change the title and the author name and you should be good.

\section{Section Headings}
\label{sec:layout}
This section shows the various section levels available for the article class.  Each section or subsection can be ``labeled'' and referenced later in the document.  For example, this section is created using the following lines:
\begin{verbatim}
\section{Section Headings}
\label{sec:layout}
\end{verbatim}
To reference this section, use Section \verb=\ref{sec:layout}=, which produces Section \ref{sec:layout}.

Paragraphs are separated by skipping a line in your text editor.  For example,
\begin{verbatim}
...which produces Section \ref{sec:layout}.

Paragraphs are separated by...
\end{verbatim}

If you want to skip lines in your text document but do not want a paragraph indention, use \verb=\noindent= at the beginning of the line.

\subsection{Subsection}

This is a subsection.

\subsubsection{Subsubsection}

This is a subsubsection.

\paragraph{Paragraph}

The lowest level is called a paragraph.

\section{Figures}

\subsection{Adding Images}

Inserting figures is a common task.  Make sure you add a label for referencing, as is done in Fig.~\ref{fig:universe}.

\begin{figure}[h!]
\centering
\includegraphics[scale=1.7]{universe.jpg}
\caption{The Universe}
\label{fig:universe}
\end{figure}

Many types of graphics images can be included, e.g., .png, .pdf, etc.  Make sure you upload your image to your project directory using the little arrow-to-cloud icon in the lower left of the ShareLaTeX site.

Another issue to be aware of with figures is the positioning argument.  In the universe example, we used \verb=\begin{figure}[h!]=, which forces the image to be located where inserted in the text.  Alternatively, the position argument can be omitted (leave off the square brackets) or you can use \verb=[t]= for top or \verb=[b]= for bottom of the page.  Keep in mind, however, that usually \LaTeX~is much better than you at figuring out where the figures should go and it is probably better to omit the position argument unless absolutely necessary.

Finally, note the scale optional argument in the \verb=\includegraphics= command.  A couple of useful options here are to directly specify the width and/or height of the image, e.g., \verb/[width=3.2in]/, or to specify the image width as a multiple of the page width, \verb/[width=.2\textwidth]/.  In Fig.~\ref{fig:smalluniverse} we show the result for the universe image using .2 scaled textwidth.

\begin{figure}
\centering
\includegraphics[width=0.2\textwidth]{universe.jpg}
\caption{The Small Universe}
\label{fig:smalluniverse}
\end{figure}

\subsection{Drawing Vector Graphics}

With the \href{http://www.texample.net/media/pgf/builds/pgfmanualCVS2012-11-04.pdf}{Ti\emph{k}Z drawing package} you can create your own vectorized (as opposed to rasterized) graphics.  This is especially helpful when the document may be resized (e.g., a poster) or used in a talk (with Beamer).  Below are two examples of the same image, one added with the \verb/\includegraphics/ command (Fig.~\ref{fig:raster}) from the previous section and one drawn with Ti\emph{k}Z (Fig.~\ref{fig:vector}).  View the PDF of this document and zoom in to high resolution to see the value of using vector graphics.

\begin{figure}[ht]
\begin{minipage}[c]{0.45\linewidth}
\centering
\includegraphics[width=.83\textwidth]{circle.png}
\caption{Raster image}
\label{fig:raster}
\end{minipage}
\hspace{0.5cm}
\begin{minipage}[c]{0.45\linewidth}
\centering
\begin{tikzpicture}
\draw[line width=1mm,fill=lightgray] (1.8,1.8) circle [radius=1.5];
\draw[help lines] (0,0) grid (5,5);
\end{tikzpicture}
\caption{Vector image}
\label{fig:vector}
\end{minipage}
\end{figure}

\section{Equations}

In this section, we provide examples for most of the equation constructs you will need in the class.  We start with symbols and work up to matrices.

\subsection{Symbols and Inline Math}

Most of the fancy symbols you will need are simply Greek names prefaced by a backslash, e.g., $\alpha$ is given by \verb=\alpha=.  If the equation is needed within the text, surround it by dollar signs, e.g., $y = mx + b$ is generated by \verb/$y=mx+b$/.  For the most part you do not need to worry about spacing in equations, though some fine-tuning may be required on occasion.  To slightly increase the space between symbols, add a \verb/\,/ \,. For example, $fg$ is produced by \verb/$fg$/, while $f\,g$ is produced by \verb/$f\,g$/.  To remove space use \verb/\!/ between symbols.

\subsection{Math Fonts in Figure Captions}

You want all math fonts to be consistent in your document whether they show up in an equation or in the main body of text or, as we will discuss here, in the text of a figure caption.  In Fig.~\ref{fig:example}, we plot an imaginary model comparison with data (the agreement is never this good!).  There are a few of points to make.  First, note that the fonts used for temperature, $T$, are consistent in this text, in the plot $y$-axis label, \emph{and} in the figure caption.  Next, notice that the font for the units, Kelvins, is K, not $K$ (this is discussed further in the next subsection). Last, note that you can (and should) use the figure caption to say something important about the figure.  If the figure does not convey an important point, why is it there to begin with?  Often, readers simply scan a paper and look at the plots or figures.  So, use this opportunity to communicate with your reader.  Draw them in and get them to read more detail in the text.

\begin{figure}
\centering
\includegraphics[width=0.75\textwidth]{example.pdf}
\caption{Model comparison with experimental data of axial profile of temperature, $T(x)$.  The agreement is remarkably good because this is a made up example.  This caption is a good place to tell the reader something important about the figure.  Often readers simply scan documents and look at the figures.  This is your chance to communicate the key result to the reader.  Don't waste the opportunity by being lazy and simply writing ``$T$ vs.~$x$''!}
\label{fig:example}
\end{figure}

\subsection{Units}

Units are generally not italicized.  The formal rules for what should and should not be italicized are documented in the \href{http://www.nist.gov/pml/pubs/sp811/index.cfm}{NIST Guide to SI Units} \cite{Thompson:2008}.  It is helpful to add \verb/usepackage{siunitx}/ to the preamble.  This allows you to easily include symbols, superscripts, etc., to units in the proper SI fonts, whether in math mode or not.

For example, if you want to write $T=20\,\si{\degree C}$, you would do this by \verb/$T=20\,\si{\degree C}$/. Below is a table of common unit problems with solutions using \verb/siunitx/.

\begin{center}
\begin{tabular}{ll}
unit & \verb/\si syntax/ \\
\hline
\si{m^2} & \verb/\si{m^2}/ \\
\si{kg m/s^2} & \verb=\si{kg m/s^2}= \\
\si{J/(m^2.s)} & \verb=\si{J/(m^2.s)}= \\
\si{J/(kg.\degree C)} & \verb=\si{J/(kg.\degree C)}=
\end{tabular}
\end{center}

\subsection{Numbering Equations}

As with sections, one of the most powerful features of \LaTeX~is the ability to reference equations.  Most of the time you will create equations using the \emph{equation environment} as follows:
\begin{equation}
\label{eq:line}
y = mx + b
\end{equation}
Now you can reference Eq.~(\ref{eq:line}) any time using \verb/\ref{eq:line}/.

You can also omit the equation number by using the \emph{displaymath envinronment}:
\begin{displaymath}
x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}
\end{displaymath}

\subsection{Differential Equations and Integrals}

Here is an example of an ODE:
\begin{equation}
\label{eq:ode}
\frac{\d \mathbf{u}_p}{\d t} = \frac{1}{2}\rho\,C_d A_p (\mathbf{U}-\mathbf{u}_p)|\mathbf{U}-\mathbf{u}_p|
\end{equation}
Note that the differential operator is written as a Roman d, because $d$ is too often needed as a variable name, as in $diameter$.

Here is an example PDE:
\begin{equation}
\label{eq:pde}
\frac{\partial \rho}{\partial t} + \nabla\!\cdot(\rho \mathbf{u}) = 0
\end{equation}

Here is an example integral:
\begin{equation}
\label{eq:filter}
\int_a^b f(x) \,\d x = F(a) - F(b)
\end{equation}

\subsection{Equation Arrays}
In homework assignments it may often be necessary to show the development of an equation.  The \emph{align environment} is useful for this.  For example:
\begin{align}
f(x,y,z) &= (a + x)(b + y)(c + z) \notag\\
         &= (ab + ay + xb + xy)(c + z) \notag\\
         &= abc + ayc + xbc + xyc + abz + ayz + xbz + xyz
\end{align}
Notice that the equation number for the first two lines is suppressed using \verb/\notag/.  The anpersand \verb/&/ is used to set the alignment.  The \verb/\\/ is a carriage return.

\subsection{Matrices}
Matrices are created using the \verb/array/ environment within an \verb/equation/ environment.  The key trick here is to also use the \verb/\left/ and \verb/\right/ bracket commands which automatically size themselves to handle multiple rows or fractions.

\begin{equation}
\label{eq:matrix}
\mathsf{T} = \left( \begin{array}{ccc} \tau_{11} & \tau_{12} & \tau_{13} \\
                                       \tau_{21} & \tau_{22} & \tau_{23} \\
                                       \tau_{31} & \tau_{32} & \tau_{33} \end{array} \right)
\end{equation}

\section{Lists}
\label{sec:lists}

To make numbered or bulleted lists, use the \verb/enumerate/ or \verb/itemize/ environments.  Steps in an algorithm might be nicely described using
\begin{enumerate}
\item Compute the density
\item Compute the velocity
\item Solve the Poisson equation
\end{enumerate}
The ingredients in a recipe might be listed as
\begin{itemize}
\item 2 eggs
\item 1 cup of flour
\item 2 Tsp.~of cinnamon
\end{itemize}

\section{Bibliographic References}
\label{sec:bib_ref}

As with figure and equation references, \LaTeX provides a powerful means of citing references that makes life incredibly easy.  A database of references is stored in one or more \verb/.bib/ files.  Near the end of the document, the bib style and bib file are called (see the end of this \verb/main.tex/ file). The file \verb/references.bib/ is included with this template and contains a few examples.  Within the text, citations are refenced by the name assigned in the \verb/.bib/ file, for example, \cite{FDS_Math_Guide}.  More than one citation can be referenced at a time \cite{Beyler:hood,FDS_Math_Guide}.

You may add as many references to the \verb/references.bib/ file as you like.  Only the references specifically called out with a \verb/\cite{}/ command will be added to the {\bf References} section at the end of the document.  I encourage you to grow your own \verb/.bib/ database for use throughout your career.  As a good starting point, the database for the FDS manuals contains most of the papers relevant to fire dynamics:

\vskip\baselineskip
\noindent{\small\tt\url{http://code.google.com/p/fds-smv/source/browse/trunk/FDS/trunk/Manuals/Bibliography/FDS_general.bib}}

\section{Compiling Outside ShareLaTeX}

If you choose not to use the ShareLaTeX site, you need to know how to compile the document.  In order to resolve all the cross-referencing in the \verb/\cite{}/ and \verb/\ref{}/ calls, you must run \verb/pdflatex/ and \verb/bibtex/ in the following order:
\begin{enumerate}
\item \verb/pdflatex/
\item \verb/bibtex/
\item \verb/pdflatex/
\item \verb/pdflatex/
\end{enumerate}

In the process, \LaTeX will create several inermediate files that it uses to get itself from one stage to the next.  If you are using Windows, I recommend using the MikTeX distribution and also using the trial version of WinEdt.  Once installed, you can simply click a few icons to compile the document.  On a Mac, TeXShop contains pretty much everything you need.

\section{Spell Checking}

Most editors have a spell checker.  But it is not a prerequisite for \LaTeX, so be careful.

\section{Conclusion}

Congratulations. If you have made it this far, you are on your way to becoming a \LaTeX~guru and watching sympathetically as your colleagues waste hours rearranging figures and references and search through ``objects'' to ``insert'' into equations that ultimately look amateurish.  You may now focus on content, and leave the formatting to the pros.

\begin{quotation}
``I always thought something was fundamentally wrong with the universe'' \cite{adams1995hitchhiker}
\end{quotation}

\section*{Acknowledgments}

I'd like to thank The Academy.

\appendix

\section{Appendices}

The appendices come before the references.  Use the \verb/\section/ command to start a new appendix.

\bibliographystyle{plain}
\bibliography{references}
\end{document}


\clearpage
\section{Concepts Related to Managing Multiple Values} % (fold)
\label{sec:managing_multiple_values_concepts}

Data is an important part of any program. Earlier chapters have shown how to store and work with data. These chapters covered both the \nameref{sub:type}s of data you can work with, and means of storing and exchanging data using \nameref{sub:variable}s. So far each Variable has stored only a single value, making it difficult to work with multiple values. This chapter introduces the concepts needed start working more effectively with multiple values.

The chapter introduces the following \textbf{artefacts}:
\begin{itemize}
  \item \nameref{sub:array}: A kind of variable that is used to store multiple values. The individual values in the array are called \emph{elements}.
  \item \nameref{sub:string}: An existing \nameref{sub:type} that stores textual data.
\end{itemize}

The following \textbf{actions} are then needed to work with Arrays:
\begin{itemize}
  \item \nameref{sub:for_loop}: A \nameref{sub:pre_test_loop} that can be used to easily repeat a block of code for each element of an array.
  \item \nameref{sub:expressions_with_arrays_}: Expressions can read the value from the element of an array.
  \item \nameref{sub:assignment_statement_with_arrays_}: The assignment statement can be used to assign a value to an element in an array.
\end{itemize}

\bigskip

You may need to revise the following programming artefacts:
\begin{itemize}
  \item \nameref{sub:variable}: The idea of storing data within your code.
  \item \nameref{sub:local_variable}: Storing data in a \nameref{sub:function} or \nameref{sub:procedure}.
  \item \nameref{sub:parameter}: Passing data to a Function or Procedure.
\end{itemize}

The following programming terminology will also be used in this Chapter:
\begin{itemize}
  \item \nameref{sub:expression}: A value used in a statement.
  \item \nameref{sub:type}: A kind of data used in your code.
\end{itemize}

The example for this chapter is a statistics calculator, where the user enters 10 values, and the program calculates some statistics. An example of this program executing is shown in Figure \ref{fig:simple-stats}.

\begin{figure}[h]
   \centering
   \includegraphics[width=0.6\textwidth]{./topics/arrays/images/SimpleStats}
   \caption[Statistics Calculator Terminal]{Statistics Calculator run from the Terminal}
   \label{fig:simple-stats}
\end{figure}

\input{topics/arrays/concepts/array}
\input{topics/arrays/concepts/assignment-with-arrays}
\input{topics/arrays/concepts/expressions-with-arrays}
\input{topics/arrays/concepts/pass-by-ref}
\input{topics/arrays/concepts/for}
\input{topics/arrays/concepts/string}
\input{topics/arrays/concepts/summary}

% section data_type_concepts (end)
\clearpage
\section{Using these Concepts} % (fold)
\label{sec:arrays_using_these_concepts}

Arrays make it easier to work with multiple values, allowing you to have a single variable that stores multiple values. With arrays you can start to create programs that process larger quantities of data.

\input{topics/arrays/application/using-arrays}

% section using_these_concepts (end)


\documentclass[11pt]{article}

\usepackage{alltt,fullpage,graphics,color,epsfig,amsmath, amssymb}
\usepackage{hyperref}
\usepackage{boxedminipage}
\usepackage[ruled,vlined]{algorithm2e}
\usepackage{amsmath}
\usepackage{commath}
\usepackage{graphicx}
\graphicspath{ {.} }
\newcommand{\floor}[1]{\lfloor #1 \rfloor}
\newcommand{\ceil}[1]{\lceil #1 \rceil}
\title{CS 512 Midterm}
\author{Daniel Campos}
\date{March 15th,2021}
\begin{document}
\maketitle
\section{Short Questions}
\subsection{If we train a classifier with few training samples then the classifier is less likely to overfit}
\textbf{False}. A smaller dataset is more easily skewed from the true data distribution than a larger one and as a result a classifier trained on a smaller sample of data is \textit{more} likely to overfit
\subsection{Self-training does not require labeled data}
\textbf{False}. Labeled data is required to train the initial classifier which then can perform self-training on the larger unlabeled data sample.
\subsection{Transfer learning is effective when the source and target task share many common grounds}
\textbf{True}. Take for example NLP or computer vision, effective transfer learning happens when the pretraining corpus resembles the fine tuning corpus. For computer vision is the structure of images, for NLP its the structure of language.
\subsection{The results of two runs of K-means algorithm on the same dataset are expected to be the same regardless of different initialization.}
\textbf{False}. Initialization will completely skew what kind of results are found, popular methods like Forgy choose random points to be the centroids while Random Partition tends to chose points close to the data centroid.
\subsection{In a transaction database, if the absolute support of the itemset X is larger than the absolute support of the itemset Y, then the relative support of the itemset X must be larger than the relative support of the itemset Y}
\textbf{True}. $support_{relative}(X) = (|X|/|T|)*100 ?= support_{relative}(Y) = (|Y|/|T|)*100$? Since $support_{abs}(X) = |X|,  support_{abs}(Y) = |Y|$ and $support_{abs}(X) > support_{abs}(Y)$ it implies $|X| > |Y|$ which since the transaction database size, $|T|$, is constant implies $support_{relative}(X) > support_{relative}(Y)$
\subsection{Explain why random walk with restart (RWR) is a good measure for node proximity in terms of catching information from both short and long distance?}
RWR is a good measure because it essentially measure the probability of reaching a given node from a start node. Since the algorithm has restarts we can discover long distance connections which can only be found by restarting and walking down a different path.
\subsection{In frequent graph pattern mining, what is major computational cost for pattern growth approach? List one solution for this challenge.}
Since the algorithm finds patterns by building from the bottom up for long sequences this can have major overhead in memory. For example, if the target pattern is of length 100 then we must generate and store about $10^30$ candidates. Additionally, at each step of the algorithm we must scan the entire database which can be quite computationally expensive. To avoid the cost of scanning the entire database each time strategies like partitioning the database and using database projections.
\subsection{What is the key difference between active learning and transductive learning?}
In transductive learning the amount of labeled data points does not change while in active learning the most salient points are sampled for human labeling.
\subsection{After training for an SVM classifier is done, how is the testing/evaluation performed given a new data sample $x'$}
First, when we get the new data sample we should split it into equal portions of evaluation/validation to ensure we do not overfit based on our validation. Then, for each class in our classifier we rewrite each class specific MMH using the Lagrangian formulation to rewrite the decision boundary of our SVM as $d(X) = \sum_{i=1}^l y_i \alpha_i X'x_i+b$. We plug our test dataset and check to see the sign of the result, if its negative it is not in the class and if it is positive its in the class. We run this on the entire validation/evaluation set and we check what the precision and recall of correct class is across tasks.
\subsection{In Gaussian mixture model, how to alleviate the problem of local optima}
The problem of local optima can be overcome by running the algorithm multiple times with random initialization.
\section{Frequent Pattern Mining}
\subsection{Transnational}
\subsubsection{Frequentset}
frequent set with support: A(0.6), B(0.6), D(1), E(0.8), F(0.6), AD(0.6), BD(0.6), BE(0.6), DE(0.8), DF(0.6),BED(0.6),
\subsubsection{Strong Association Rules}
The only strong association rules are BD $\rightarrow$ E, BE $\rightarrow$ D.
\subsection{Sequence}
\subsubsection{Projected database for <b> and <j>}
for $<b>$:(bde)(be)gj(em), (\_g)e(de)j(bj), (\_dg)gmed, EMPTY, (\_em)(edj)bd . \\
for $<j>$: (em), (bj), (\_m)(bdg)gmed, EMPTY, (\_)bd
\subsubsection{Support}
$<bej>=0.4, <mdg>=0.2, <mbj>=0.0$
\subsection{Graph}
\subsubsection{Let min\_support = 0.6, find all all frequent patterns with size $K > 2$}
First, we start with all patterns k=1 where we get: H:5, O:5, C:5, S:2 and we drop s. \\
Next, we find all patterns k=2 and we get: H-O:5, O-H:5, H-H:1, H-C:4, C-H:4, C=O:4, O=C:4, O-C:5, C-O:5 and we drop H-H \\
Moving to patterns of k> 2 and confidence >= 0.6: \textbf{O-C=O(Support 0.8), O-C-H(0.6), H-C-O(0.6), H-C-H(0.6), H-O-C(0.8)} and all re orderings of the mentioned examples.
\subsubsection{Let min\_confidence = 0.7 and min\_support = 0.6. List one strong association rule and the corresponding confidence. }
The association rule we find is O=C $\longrightarrow$ O=C-O as both O=C and O=C-O have a confidence of 0.8 and thus the confidence in association = 1
\section{SVM}
\subsection{If there is a mapping of $R^1 \rightarrow R^2$ by which the mapped positive and negative data samples can be linearly separable? Plot the data points after mapping in 2-d space}
Yes There does exist see figure \ref{fig:svm1}
\begin{figure}[]
\centering
\includegraphics[width=12cm]{Assignments/MidTerm/svmpart1.png}
\caption{Liberally separating data part 1}
\label{fig:svm1}
\end{figure}
\subsection{If we implement a hard margin linear SVM on the mapped data samples from problem A what is the decision boundary? Draw it on your figure and mark the corresponding support vectors.}
As shown in figure \ref{fig:svm2} our separation runs right along the two classes and is a the blue line is the support vector for $-$ and the orange line is the support vector for $+$
\begin{figure}[]
\centering
\includegraphics[width=12cm]{Assignments/MidTerm/svmpart2.png}
\caption{Liberally separating data part 1}
\label{fig:svm2}
\end{figure}
\subsection{What is the corresponding kernel $K(x_1, x_2)$}
The kernel I implemented was $K(x_1, x_2) = x, x^3$
\subsection{Does there exist a mapping $R^1 \rightarrow R^2$ such that mapped points can be linearly separable? Plot the points after mapping to a 2-d space}
Yes there does exist a linearly separable space as shown by figure \ref{fig:svm3}. For this data my kernel was $k(x_1, x_2) = x, (\sin(x_1)+4)$
\begin{figure}[]
\centering
\includegraphics[width=12cm]{Assignments/MidTerm/svmpart3.png}
\caption{Liberally separating data part 3}
\label{fig:svm3}
\end{figure}
\section{Random Walk with Restart}
\subsection{Adjacency matrix A of Network G}
See table \ref{tab:adj1}
\begin{table}[]
\begin{tabular}{|l|l|l|l|l|l|} \hline
1 & 1 & 1 & 0 & 0 & 0 \\ \hline
1 & 1 & 1 & 1 & 0 & 1 \\ \hline
1 & 1 & 1 & 1 & 0 & 0 \\ \hline
0 & 0 & 0 & 1 & 1 & 1 \\ \hline
0 & 0 & 0 & 1 & 1 & 1 \\ \hline
0 & 1 & 0 & 1 & 1 & 1  \\\hline
\end{tabular}
\caption{adjacency matrix A of network G}
\label{tab:adj1}
\end{table}
\subsection{Row normalized adjacency matrix}
See table \ref{tab:adj2}
\begin{table}[]
\begin{tabular}{|l|l|l|l|l|l|}
\hline
.33 & .33 & .33 & 0   & 0   & 0   \\ \hline
.2  & .2  & .2  & .2  & 0   & .2  \\ \hline
.25 & .25 & .25 & .25 & 0   & 0   \\ \hline
0   & 0   & 0   & .33 & .33 & .33 \\ \hline
0   & 0   & 0   & .33 & .33 & .33 \\ \hline
0   & .25 & 0   & .25 & .25 & .25 \\ \hline
\end{tabular}
\caption{row normalized adjacency matrix A of network G}
\label{tab:adj2}
\end{table}
\subsection{What node is the most relevant to node 2 using the RWR formula. Report Results of the first three iterations.}
The first iteration gives us node 0 with a value of 0.40. The second iteration gives us node 0 with the value 0.22. The third iteration gives us node 0 with a value of 0.17.
\section{K-Means}
\subsection{What is the Manhattan distance between A and E}
The distance is 7.
\subsection{Run K means for 2 iterations using 2 centroid with initialization of (0, -3) and (2, -2)}
For iteration 1 centroid 1 is: (0,-3) and centroid 2 is: (2,-2) and A,C, D belong to centroid 1 while B and E belong to centroid 2. \\
For iteration 2 centroid 1 is: (-1.33, 0) and centroid 2 is: (1.5, -0.5) and A,C belong to centroid 1 while B, D, E belong to centroid 2. \\
For iteration 3 centroid 1 is : (-2.0, 1.5) and centroid 2 is: (1.0, -1.33) and A, C belong to centroid 1 while B,D, E belong to centroid 2. After this iteration the algorithm converges.
\subsection{Has the K-means converged in the first 3 iterations on the provided data? Why? }
Yes it has. It has converged because after iteration 2 the data points in each cluster do not change which means the centroids chosen for iteration 3 do not change no matter how many more iterations we run.
\subsection{How many possible different clustering are there}
Since K means is so heavily dependent on initialization point there are theoretically infinite combinations of possible centroid(assuming centroids do not have to be integers). If we limit the question to how many ways can our 5 points be arranged into 2 clusters by k means there are a few essential combinations which to combine: 4 in cluster 1 and 1 in cluster 2, 3 in cluster 1 and 2 in cluster 2, 2 in cluster 1 and 3 in cluster 2, 1 in cluster 1 and 4 in cluster two. We can rewrite this as a combinatorial problem $number-clustering=\frac{5!}{2!}{3!}*2+\frac{5!}{4!}{1!}*2= \frac{2*120}{2*6} + \frac{2*120}{24}= \frac{240}{12} + \frac{240}{24} = 20 + 10 = 30$. There are 30 possible clusters.
\section{2-way spectral graph partitioning}
\subsection{Explain why the mincut loss function can be formulated as $J = \frac{1}{4}\sum_{i,j} (q[i] - q[j])^2 A[i,j]$}
The loss function is formulated by the formula above because it is the most efficient way to represent how far off the connected graph is from the associated class. The $\frac{1}{4}$ is to normalize for when classes disagree since if $q_i = 1$ and $q_j=-1$ $(q_i-q_j)^2 = (1-(-1))^2= 2^2 = 4$ so we divide the summation by 4. We sum over all i, j because we want to compare every node in the graph. The main loss function $(q_i - q_j)^2$ is standard to loss(error squared). Finally we multiply by the adjacency matrix because we only care about any error if the nodes are connected in our adjacency matrix. If nodes are not connected but disagree on class this does not effect out loss.
\subsection{Prove the loss function of MinCut can be rewritten as $J= \frac{1}{2}q^T(D-A)q$}
Since the original formulation is NP hard we relax $q_i$ from being either -1 or 1 to being any real number between -1 and 1. We then impose the constraint $\sum_{i=1}^n q_i^2= n$ as we must make the total membership vector equal to the amount of nodes in the graph.\\
First we simplify, $J = \frac{1}{4}\sum_{i,j} (q[i] - q[j])^2 A[i,j] = \frac{1}{4}\sum_{i,j} q[i]^2 + q[j]^2 - 2q_i q_j A[i,j]$. //
Since $d_i = \sum{j}w_{i,j}$ we then further simplify $J = \frac{1}{4}\sum_i 2q_i^2 (d_i) - \frac{1}{4} \sum_{i,j}2q_i*q_j*w_{i,j}$ //
Then $J=\frac{1}{2}\sum q_i^2*d_i - \frac{1}{2}\sum_{i,j}q_i * q_j* w_{i,j} = \frac{1}{2} \sum_i q_i(d_i, \gamma_{i,j}  - w_{i,j}q_j$. Which since we know that $D= [d_i \gamma_{i,j}$ we arrive at our intended $J= \frac{1}{2} *q^\textbf{T}(D-W)q$
\subsection{Explain why we need to relax the constrain from $q \in {-1, 1}^N$ to $\sum (q_i)^2 = n$}
We need to relax this because when values in q can only be -1 or 1 the problem is NP hard as we must explore the entire space. Additionally by moving away from this 2 class distribution we must impose the total vertex constraint to prevent from having too many nodes that w
\subsection{Why can we not select the eigenvector corresponding to the minimum eigenvalue to do the 2-way cut}
We cannot select the smallest eigenvector because when we have a graph laplacian multiplication the smallest eigenvalue will be 0 which will mean the smallest eigen vector will have all vector values be set to $\frac{1}{n}$. To avoid this problem we choose the second smallest eigenvalue and related the second smallest eigenvector.
\subsection{What happens if we change the mincut constraint from n to 10n}
If we increase the N value we are increasing the size of the minimum cuts we have to do to separate the graph. A larger constant will mean that more items that are connected need to be in one of the two classes which in turn will cut the graph further. This will make the solution less optimal because we are not minimizing the amount of cuts we need to make. We are essentially increasing the amount of cuts required by $\sqrt{10}$ since we are requiring the membership vector to have that many more non zero values in it.
\end{document}


% Proposal for Convex Latent Variable
% Author:
%   Jimmy Lin
%   Ian Yen

\documentclass{article} % For LaTeX2e
\usepackage{nips14submit_e,times}
\usepackage{hyperref}
\usepackage{url}
\usepackage{amssymb,amsmath,amsfonts,latexsym,mathtext}
%\documentstyle[nips14submit_09,times,art10]{article} % For LaTeX 2.09

\title{Project Proposal: Convex Latent Variable}

%{{{ Authors
\author{
Hippocampus\thanks{ Use footnote for providing further information
about author (webpage, alternative address)---\emph{not} for acknowledging
funding agencies.} \\
Department of Computer Science\\
Cranberry-Lemon University\\
Pittsburgh, PA 15213 \\
\texttt{hippo@cs.cranberry-lemon.edu} \\
\And
Coauthor \\
Affiliation \\
Address \\
\texttt{email} \\
\AND
Coauthor \\
Affiliation \\
Address \\
\texttt{email} \\
\And
Coauthor \\
Affiliation \\
Address \\
\texttt{email} \\
\And
Coauthor \\
Affiliation \\
Address \\
\texttt{email} \\
(if needed)\\
}
%}}}

% The \author macro works with any number of authors. There are two commands
% used to separate the names and addresses of multiple authors: \And and \AND.
%
% Using \And between authors leaves it to \LaTeX{} to determine where to break
% the lines. Using \AND forces a linebreak at that point. So, if \LaTeX{}
% puts 3 of 4 authors names on the first line, and the last on the second
% line, try using \AND instead of \And before the third author name.

\newcommand{\fix}{\marginpar{FIX}}
\newcommand{\new}{\marginpar{NEW}}

%{{{ Macros
\DeclareMathOperator*{\argmin}{arg\,min}

\newcommand{\argmax}[1]{\underset{#1}{\text{argmax} }}

\newcommand{\indicator}[1]{\mathbf{1}_{#1}}
\newcommand{\LTwoNorm}[1]{||#1||^{2}}
\newcommand{\sumn}{\sum_{n}}
\newcommand{\sumk}{\sum_{k}}

\newcommand{\wnk}{w_{nk}}
\newcommand{\wn}{\mathbf{w}_n}
\newcommand{\wone}{\mathbf{w}_1}
\newcommand{\wtwo}{\mathbf{w}_2}
\newcommand{\wthree}{\mathbf{w}_3}
\newcommand{\wfour}{\mathbf{w}_4}
\newcommand{\w}{\mathbf{w}}
\newcommand{\wbar}{\overline{\w}}
\newcommand{\wopt}{\mathbf{w^{*}}}
\newcommand{\wnbyn}{\mathbf{w}_{n\times n}}

\newcommand{\x}[1]{\mathbf{x}_{#1}}
\newcommand{\xn}{\mathbf{x}_n}
\newcommand{\muk}{\boldsymbol{\mu}_k}
\newcommand{\maxn}{ \underset{n}{\text{max}} }
\newcommand{\maxk}{ \underset{k}{\text{max}} }

\newcommand{\yone}{\mathbf{y}_1}
\newcommand{\ytwo}{\mathbf{y}_2}
\newcommand{\ythree}{\mathbf{y}_3}
\newcommand{\yfour}{\mathbf{y}_4}
\newcommand{\z}{\mathbf{z}}
\newcommand{\quadraterm}[1]{\frac{\rho}{2}\LTwoNorm{\w_{#1}-\z}}
\newcommand{\dualterm}[1]{\mathbf{y}_{#1}^{T}(\w_{#1}-\z)}

\newcommand{\minimize}[1]{ \underset{#1}{\text{Minimize}} }
\newcommand{\maximize}[1]{ \underset{#1}{\text{Maximize}} }
\newcommand{\subjectto}{ \text{subject to} }
\newcommand{\hs}{\hspace*{0.6cm}}

%}}}

%\nipsfinalcopy % Uncomment for camera-ready version

%%% TODO list:
%% 1. reason of applying group-lasso (one or zero)
%% 2.

\begin{document}

\maketitle

\begin{abstract}
    Add abstract here..
\end{abstract}

\section{Problem Formulation}
\subsection{Motivation}
Given a set of data entities $\x{1}, ..., \xn, ..., \x{N} $, we wish to
automatically cluster these entities without specifying the number of cluster
centroids. One possible approach is to regard these input entities as
potential cluster candidates and evaluate the "belonging matrix" $\wnbyn$, consisting
of latent variables $\wnk$ indicating the extent of one entity $\xn$ explained
by one potential cluster centroid candidate $\muk$. In this project, we
propose a method to compute the sparse latent variable with faster
convergence by decomposing the objective function derived by ADMM into two
separate optimization task.

\subsection{Overall Optimization Goal}
Formally, our goal is to achieve clustering with group-lasso regularization.
Hence, we formulate the target problem in terms of notations introduced above
as follows:

 \begin{align}
  \minimize{\w}
  \hs & \frac{1}{2} \sumn \sumk \wnk \LTwoNorm{\xn - \muk}  % clustering loss
        + \lambda \sumk \maxn | \wnk |  \\ % group-lasso
  \subjectto
  \hs & \forall n,\ \sumk \wnk \leq 1 \\
  \hs & \forall n,\ k,\ \wnk \geq 0
 \end{align}

The limiting conditions listed above are jointly called simplex constraint.
Every entity must be assigned to some centroid with prob 1.
Every entity must have non-negative belonging index to each centroid candidate.

Note that the group-lasso can either be one or zero in the context that we
pick up the most promising centroid candidates from provided entities.

\subsection{ADMM}

We can rewrite the initial optimization goal as the following primal problem:

 \begin{align}
  \minimize{\wone, \wtwo, \z}
  \hs & \frac{1}{2} \sumn \sumk \wnk \LTwoNorm{\xn - \muk}  % clustering loss
    + \quadraterm{1} \nonumber \\
    & \hs + \lambda \sumk \maxn | \wnk |
    + \quadraterm{2}
        \\ % group-lasso
  \subjectto
  \hs & \w_{1} = \z,\ \w_{2} = \z
 \end{align}

 Note that we separate $\w$ into two variables, $\wone$ and $\wtwo$
 respectively. This is to prepare it for latter optimization decomposition.

By dual decomposition, the problem can be further transformed as follows:

 \begin{align}
     \minimize{\wone, \wtwo, \z} \hs  \maximize{\yone, \ytwo}
   \hs & \frac{1}{2} \sumn \sumk \wnk \LTwoNorm{\xn - \muk}  % clustering loss
   + \dualterm{1} + \quadraterm{1} \nonumber \\
    & \hs + \lambda \sumk \maxn | \wnk |  % group-lasso
   + \dualterm{2} + \quadraterm{2}
 \end{align}

 We continue by incorporting duality and derive the following optimization
 task:

  \begin{align}
     \maximize{\yone, \ytwo} \hs  \minimize{\wone, \wtwo, \z}
   \hs & \frac{1}{2} \sumn \sumk \wnk \LTwoNorm{\xn - \muk}  % clustering loss
   + \dualterm{1} + \quadraterm{1} \nonumber \\
    & \hs + \lambda \sumk \maxn | \wnk |  % group-lasso
   + \dualterm{2} + \quadraterm{2}
 \end{align}

 \subsection{Optimization Decomposition} \label{separate}
\newcommand{\mutw}[1]{\mathbf{y}_{#1}^{T} \w_{#1}}

The first optimization task:
  \begin{align}
   \minimize{\wone}
   \hs & \frac{1}{2} \sumn \sumk \wnk \LTwoNorm{\xn - \muk}  % clustering loss
   + \mutw{1} + \quadraterm{1} \\
   \subjectto
   \hs & \forall n,\ \sumk \wnk \leq 1 \\
   \hs & \forall n,\ k,\ \wnk \geq 0
 \end{align}
 This subproblem can be solved by Frank-Wolf Algorithm.

 The second optimization task:
 \begin{align} \label{second}
   \minimize{\wtwo}
   \hs & \lambda \sumk \maxn | \wnk |  % group-lasso
   + \mutw{2} + \quadraterm{2}
 \end{align}

 The closed-form solution of second subproblem can be found through Blockwise
 Coordinate Descent Procedures (Han Liu.).

\subsection{Overall Solution Procedure}
Repeat

1. Resolve $\wone$ and $\wtwo$ in separate subproblem formulated in section
\ref{separate}.

2. Update the combination indicator $\z$ by $\z = \frac{1}{2} (\wone + \wtwo)$

3. Refine the multiplier $\yone$ and $\ytwo$ with gradient update rule:
\begin{align}
\yone = \yone - \alpha \cdot (\wone - \z) \\
\ytwo = \ytwo - \alpha \cdot (\wtwo - \z)
\end{align}

Until Convergence.

\subsection{Resolve first subproblem: Frank-Wolf Algorithm}

\subsection{Resolve second subproblem: Blockwise Coordinate Descent Procedures}
According to Blockwise Coordinate Descent Procedures (Han Liu.), the way to
resolve the \eqref{second} optimization problem is first to seek solution $\wbar$ without
group-lasso \eqref{noglasso} as shown below.

\begin{align} \label{noglasso}
   \minimize{\wtwo}
   \hs & \mutw{2} + \quadraterm{2}
 \end{align}

\newcommand{\ms}{m^{*}}
Then ultimately find the closed-form solution in terms of $\wbar$. In
practice, we resolve it column by column. The formula for closed-form
solution $\wopt$ is given as follows:

\begin{align}
    \wopt = ...
\end{align}

where $\ms = \argmax{m} \frac{1}{m} \big(\sum_{i'}^{m} |\wopt^{(k_{i'})}| -
\lambda \big)$.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage
\section{Application on Topic Model}
\newcommand{\lambdag}{\lambda_g}
\newcommand{\lambdal}{\lambda_l}
\newcommand{\lambdab}{\lambda_b}

\newcommand{\dummyterm}{r \big| 1 - \sumk \wnk \big|}
\newcommand{\globalReg}{\lambdag \sumk \maxn |\wnk|}
\newcommand{\localReg}{\lambdal \sum_d \sum_k \underset{n \in d}{\text{max}} |\wnk|}
\newcommand{\coverReg}{\lambdab \sum_k \sum_{w \in voc(k)} \underset{word(n) = w}{\text{max}} |\wnk|  }

\subsection{Problem Formulation}
In this topic model, there are four main semantics supposed to be captured.

First key point is that all words in any document should be related to one
topic. This can be regarded as resources (words) given out to all receivers
(topics). And intuitively, this term support the value of components within
$w$. Formally, this is represented by equality constraints.

Secondly, too many hidden topics are supposed to be penalized. Intuitively,
this objective aimed at reducing total number of topics. Formally, one
regularization term is used as follows:
\begin{align}
   \globalReg
\end{align}

Thirdly, too many topics involved by each document should be penalized.
However, the situation of no topics to explain one document is
non-existent since we enforce one constraint that all words must be related to
certain topics with total explanation.  Hence, this objective intuitively
aimed at reducing topic distribution within one document. To capture this
semanticcs, we use another regularizer:
\begin{align}
\localReg
\end{align}

Fourthly, too many vocabularies involved by each topic ought to be penalized.
This would increase the number of hidden topics within this model. It can be
intuitively seen as an support of topics number or cause of matrix extension
of $w$. One extra regularizer is employed:
\begin{align}
\coverReg
\end{align}
To acquire small penalty from the last regularization, all documents that are
related to certain topic should be attributed to a narrow set of vocabularies.

\subsection{Objectives}
\begin{align}
   \minimize{w}
   \hs & \globalReg + \localReg + \coverReg \\
   \subjectto
   \hs & \forall n,\ k,\ 0 \leq  \wnk \leq 1 \\
    \hs & \forall n,\ \sumk \wnk = 1
\end{align}

 \subsection{Optimization Decomposition} \label{nseparate}

The first optimization task:
  \begin{align}
   \minimize{\wone}
   \hs & \dummyterm   % dummy loss
   + \mutw{1} + \quadraterm{1} \\
   \subjectto
   \hs & \forall n,\ \sumk \wnk \leq 1 \\
   \hs & \forall n,\ k,\ \wnk \geq 0
 \end{align}
 where $r$ is a arbitrary value to enforce the resource to be fully
 distributed. Note that this subproblem can be solved by Frank-Wolf Algorithm.

 The second optimization task:
 \begin{align}
   \minimize{\wtwo}
   \hs & \globalReg  % group-lasso
   + \mutw{2} + \quadraterm{2}
 \end{align}

  The third optimization task:
 \begin{align}
   \minimize{\wthree}
   \hs & \localReg  % group-lasso
   + \mutw{3} + \quadraterm{3}
 \end{align}

   The fourth optimization task:
 \begin{align}
   \minimize{\wfour}
   \hs &  \coverReg  % group-lasso
   + \mutw{4} + \quadraterm{4}
 \end{align}

 The closed-form solution of 2-4 subproblems can be found through Blockwise
 Coordinate Descent Procedures (Han Liu.).

\subsection{Overall Solution Procedure}
Repeat

1. Resolve $\wone$, $\wtwo$, $\wthree$, $\wfour$ in separate subproblem
formulated in section \ref{nseparate}.

2. Update the combination indicator $\z$ by $\z = \frac{1}{4} (\wone + \wtwo +
\wthree + \wfour)$

3. Refine the multiplier $\yone$ and $\ytwo$ with gradient update rule:
\begin{align}
\yone = \yone - \alpha \cdot (\wone - \z) \\
\ytwo = \ytwo - \alpha \cdot (\wtwo - \z) \\
\ythree = \ythree - \alpha \cdot (\wthree - \z) \\
\yfour = \yfour - \alpha \cdot (\wfour - \z)
\end{align}

Until Convergence.

\subsection{Implementation Details}
To handle large scale data and manage extensibility, we implement a new data
structure - extensible sparse matrice.

\subsection{mono-topic variant}
1. the constraint enforces single topic for each document is
\begin{align}
    \forall d, \sum_k \max_{n \in d} |w_{nk}| = 1
   \end{align}

2. first subproblem becomes
\begin{align}
    \min r_1 |1-\sum_k w_nk| + r_2 \big| 1-\sum_k \max_{n \in d} |w_{nk}| \big| + linear + quadratic
   \end{align}

3. no need to solve third subproblem

4. fourth subproblem is modified
\begin{align}
    \min \lambda_b \sum_k \max \big((\sum_{w \in voc(k)} \max_{word(n)=w}
    |w_{nk}|) -1, 0\big) + linear + quadratic
   \end{align}
\newpage
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection*{Acknowledgments}

\subsubsection*{References}
\small{

}

\end{document}