\documentclass[12pt]{article}

\include{preamble}

\title{Math 368 / 621 Fall 2019 \\ Final Examination}
\author{Professor Adam Kapelner}

\date{December 16, 2019}

\begin{document}
\maketitle

\noindent Full Name \line(1,0){220} Circle Section and Class: A~B~C ~~ 368~621

\thispagestyle{empty}

\section*{Code of Academic Integrity}

\footnotesize
Since the college is an academic community, its fundamental purpose is the pursuit of knowledge. Essential to the success of this educational mission is a commitment to the principles of academic integrity. Every member of the college community is responsible for upholding the highest standards of honesty at all times. Students, as members of the community, are also responsible for adhering to the principles and spirit of the following Code of Academic Integrity.

Activities that have the effect or intention of interfering with education, pursuit of knowledge, or fair evaluation of a student's performance are prohibited. Examples of such activities include but are not limited to the following definitions:

\paragraph{Cheating} Using or attempting to use unauthorized assistance, material, or study aids in examinations or other academic work or preventing, or attempting to prevent, another from using authorized assistance, material, or study aids. Example: using an unauthorized cheat sheet in a quiz or exam, altering a graded exam and resubmitting it for a better grade, etc.
\\

\noindent I acknowledge and agree to uphold this Code of Academic Integrity. \\

\begin{center}
\line(1,0){250} ~~~ \line(1,0){100}\\
~~~~~~~~~~~~~~~~~~~~~signature~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ date
\end{center}

\normalsize

\vspace{-1cm}
\section*{Instructions}

This exam is 120 minutes and closed-book. You are allowed three pages (front and back) of a \qu{cheat sheet.} You may use a graphing calculator of your choice. Please read the questions carefully. If the question reads \qu{compute,} this means the solution will be a number otherwise you can leave the answer in choose, permutation, exponent, factorial or any other notation which could be resolved to a number with a computer. Questions marked \qu{[Extra Credit]} are extra credit for both 368 and 621 students. I also advise you to use pencil. The exam is 100 points total plus extra credit. Partial credit will be granted for incomplete answers on most of the questions. \fbox{Box} in your final answers. Good luck!

\pagebreak

\problem For all problems below, let $Z_1, Z_2, \ldots \iid \stdnormnot$ and let the column random variable vector $\Z = \bracks{Z_1~ \ldots~ Z_n}^\top$ where $n$ is finite. Consider $a, b,c  \in \naturals$ and three matrices $B_1, B_2$ and $B_3$ where

\beqn
B_1 + B_2 + B_3 &=& I_n, \\
\rank{B_1} &=& a, \\
\rank{B_2} &=& b, \\
\rank{B_3} &=& c, \\
a + b + c &=& n.
\eeqn

\benum

\subquestionwithpoints{20} Each one of the following expressions is distributed as a r.v. we learned about. Write explicitly the PDF or, more recommended is to use brand name notation e.g \qu{$\sim$ Beta($a$, $b$)}. Make sure you make all parameters as clear as possible. Some are challenging --- leave those blank until the end of the exam. The first one is done for you as an example.\\

\begin{multicols}{2}
\begin{enumerate}[i)]
\item $Z_{17}^2 + Z_{37}^2  + Z_{1984}^2 \sim \chisq{3}$ \\
\item $|Z_{17}| \sim $ \\
\item $\Z \sim$ \\
\item $\displaystyle\frac{Z_1}{Z_2} \sim$ \\
\item $\onevec_n^\top \Z \sim$ \\
%\item $\displaystyle\frac{Z_1 + \ldots + Z_n}{Z_{n + 1} + \ldots + Z_{2n}} \sim$ \\
%\item $\Z^\top \Z \sim$ \\
\item $\Z^\top (B_1 + B_2 + B_3) \Z \sim$ \\
\item $\Z^\top B_2 \Z \sim$ \\
\item $\Z^\top (I_n - B_2) \Z \sim$ \\
\item $\Z^\top B_1 \Z / a \sim$ \\
\item $\displaystyle\frac{Z_{n+1}}{\sqrt{\Z^\top B_1 \Z / a}} \sim$ \\
\item $\displaystyle\frac{Z_{n+1}^2}{\Z^\top B_1 \Z / a} \sim$ \\
\item $\displaystyle\frac{\Z^\top B_3 \Z / c}{\Z^\top B_1 \Z / a} \sim$ \\
\item $\displaystyle\frac{\Z^\top B_1 \Z}{\Z^\top B_1 \Z} \sim$ \\
\item $\displaystyle\frac{\Z^\top B_3 \Z}{\Z^\top B_1 \Z} \sim$ \\
\end{enumerate}
\end{multicols}
\pagebreak

\subquestionwithpoints{5} Let $\X = \twovec{X_1}{X_2} = \twobytwomat{1}{1}{1}{0} \twovec{Z_1}{Z_2}$. Find the PDF of $\X$ without using any vector or matrix notation (i.e. the PDF must be a function of $x_1, x_2$, numbers and fundamental constants) and simplify.\spc{8}

%\subquestionwithpoints{3} Let $\X = \twovec{X_1}{X_2} = \twobytwomat{1}{1}{1}{1} \twovec{Z_1}{Z_2}$. Is $\X$ multivariate normal-distributed? If so, find its PDF without using any vector or matrix notation (i.e. the PDF must be a function of $x_1, x_2$, numbers and fundamental constants). If not, explain.\spc{10}
\eenum

\problem Consider

\beqn
X_1, X_2 \iid \bernoulli{p}
\eeqn

\benum
\subquestionwithpoints{6} Prove that $T = X_1 + X_2 \sim \binomial{2}{p}$ using the discrete convolution formula.\spc{10}
\eenum

\problem Consider

\beqn
\X \sim \multinomial{n}{\bracks{p_1~p_2~\ldots~p_K}^\top}
\eeqn

\noindent where both $n$ and $\bracks{p_1~p_2~\ldots~p_K}^\top$ are in the parameter space for the multinomial and $K > n$. Its ch.f is

\beqn
\phi_X(\bracks{t_1~t_2~\ldots~t_K}^\top) = \tothepow{p_1 e^{it_1} + p_2 e^{it_2} +\ldots + p_K e^{it_K}}{n}.
\eeqn

\benum
\subquestionwithpoints{6} Write the PMF of $\X$ valid for all $\x \in \reals^K$ using the gamma function. \spc{7}
\subquestionwithpoints{3} Find $\cov{X_2}{X_3}$. \spc{2}
\subquestionwithpoints{4} Find $\prob{\X = \bracks{0~0~n~0~0~\ldots~0}^\top}$ \spc{2}

\subquestionwithpoints{4} Find $\prob{\X = \bracks{0~K~0~0~0~\ldots~0}^\top}$ \spc{2}

\subquestionwithpoints{6} Prove that $X_2 \sim \binomial{n}{p_2}$. \spc{7}

\eenum

\problem This question is about indicator functions

\benum
\subquestionwithpoints{3} Expand and simplify as much as you can: $\displaystyle\sum_{x \in \reals} x\indic{x \in \braces{-1, 0, 1}}$. \spc{2}
%\subquestionwithpoints{6} Expand and simplify as much as you can: $\sum_{x \in \naturals_0} x^{\indic{x \in \braces{1, 2, 3}}}$. \spc{0.5}

\subquestionwithpoints{3} Expand and simplify as much as you can: $\prod_{x \in \reals} \indic{x \in \braces{-1, 0, 1}}$. \spc{2}

%\subquestionwithpoints{6} Expand and simplify as much as you can: $\sum_{x \in \reals} \indic{x \in \braces{1, 2, 3}}\indic{x \in \braces{4, 5, 6}}$. \spc{0.5}
%
%
%\subquestionwithpoints{6} Expand and simplify as much as you can: $\sum_{x \in \reals} c \indic{x \in \braces{1, 2, \ldots, t}}$ where $c \in \reals$ is a constant and $t \in \naturals$ is a constant. \spc{0.5}
%
%\subquestionwithpoints{6} Expand and simplify as much as you can: $\sum_{x \in \reals} t \indic{x \in \braces{1, 2, \ldots, t}}$ where $c \in \reals$ is a constant and $t \in \naturals$ is a constant. \spc{0.5}
%
%\subquestionwithpoints{6} Expand and simplify as much as you can: $\sum_{x \in \reals} x \indic{x \in \braces{1, 2, \ldots, t}}$ where $c \in \reals$ is a constant and $t \in \naturals$ is a constant. \spc{0.5}
%
%\subquestionwithpoints{6} Expand and simplify as much as you can: $\sum_{x \in \reals} \oneover{x!} \indic{x \in \naturals}$. \spc{0.5}
\eenum

\problem Consider

\beqn
Y \sim \text{Laplace}(0, b)  \quad \text{and} \quad \phi_Y(t) = \oneover{1 + b^2 t^2} %= \oneover{2b}e^{-\frac{|x|}{b}}
\eeqn

\benum
\subquestionwithpoints{3} Write a complex integral expression that will recover the PDF of $Y$ using its ch.f. Do not evaluate.\spc{5}

\subquestionwithpoints{4} Show that $\var{Y} = 2b^2$.\spc{6}

\subquestionwithpoints{5} Let $X_n \sim \text{Laplace}\parens{0, \oneover{n}}$. Prove $X_n \convd X$ where $X \sim \text{Deg}(0)$ \textit{without} using the answer from the next question. \spc{4}

\subquestionwithpoints{4} Let $X_n \sim \text{Laplace}\parens{0, \oneover{n}}$. Prove $X_n \convLp{2} 0 $. \spc{3}
\eenum

\problem Consider

\beqn
X_n \sim \text{ParetoI}(k, n)
\eeqn

\benum
\subquestionwithpoints{4} What is $f_{X_n}(x)$? Make sure the function is valid for all $x \in \reals$.\spc{3}

\subquestionwithpoints{5} Prove $X_n \convp k$. \spc{6}

\eenum

\problem Consider $X \sim \betanot{\alpha}{\beta}$.

\benum
\subquestionwithpoints{5} Let $Y = \sqrt{X}$. Find the PDF of $Y$. Do not simplify. \spc{6}
\eenum

\problem Below are some questions about inequalities.

\benum
\subquestionwithpoints{4} Prove Markov's inequality from first principles. Make sure you state all assumptions clearly.\spc{6}

\subquestionwithpoints{3} Let $X_1$ be a r.v. with mean zero and variance $\sigsq_1$ and $X_2$ be a r.v. with mean zero and variance $\sigsq_2$. Show that $\expe{|X_1 X_2|}$ cannot be more than $\sigma_1 \sigma_2$. If you use any of the results from class, make sure you cite those results at the point in your proof where they are used.\spc{2}
\eenum

\problem Consider

\beqn
Y~|~X = x \sim \exponential{\oneover{x}} \quad \text{and} \quad X \sim \exponential{1}.
\eeqn

\benum

%\subquestionwithpoints{4} [Extra Credit] Let r.v. $X$ have a PMF or PDF which is even. Prove that its ch.f. is real. For example, the PDF of the Laplace with mean zero is even and its ch.f. is real. You must show all work clearly to get credit.\spc{4}

\subquestionwithpoints{3} Find $\expe{Y}$. \spc{1}

\eenum

\problem Below are extra credit exercises.

\benum

\subquestionwithpoints{4} [Extra Credit] Let r.v. $X$ have a PMF or PDF which is even. Prove that its ch.f. is real. For example, the PDF of the Laplace with mean zero is even and its ch.f. is real. You must show all work clearly to get credit.\spc{4}

\subquestionwithpoints{4} [Extra Credit] Place three points inside a circle at random. Connect the three points by lines to form a triangle. What is the probability the triangle contains the center of the circle? You must show all work clearly to get credit. \spc{5}

\eenum

\end{document}

\problem Below are some theoretical exercises.

\benum
\subquestionwithpoints{6} Let $S^2_n$ be the estimator for sample variance we discussed in class. What assumption(s) are required for $(n-1)S^2_n / \sigsq \sim \chisq{n-1}$?\\

\eenum

\problem Below are some theoretical exercises.

\benum

\subquestionwithpoints{6} Find the density $f_Y(y)$ where $Y = X^2$ where $X \sim \logistic{0}{1}$. \spc{6} %mid2

\subquestionwithpoints{4} [Extra Credit] Let r.v. $X$ have a PMF or PDF which is even. Prove that its ch.f. is real. For example, the PDF from problem 2 is even and its ch.f. is real.

\subquestionwithpoints{6} Let $X_n \sim \binomial{1234}{\oneover{n}}$. Prove that $X_n \convp 0$. Prove $\forall \epsilon \in (0,1)$. \spc{3}

\eenum

\end{document}

\subquestionwithpoints{6} Let $X \sim \text{Weibull}(k, \lambda)$ and $c$ is a positive constant. Circle the statement(s) that are true.

\begin{enumerate}
\item Let $k = 0.53$. $\prob{X > c} < \cprob{X > 2c}{X > c}$
\item Let $k = 0.53$. $\prob{X > c} > \cprob{X > 2c}{X > c}$
\item Let $k = 1$. $\prob{X > c} < \cprob{X > 2c}{X > c}$
\item Let $k = 1$. $\prob{X > c} > \cprob{X > 2c}{X > c}$
\end{enumerate}

\subquestionwithpoints{6} Let $X \sim \text{ParetoI}(1, \lambda)$ Let $f_X(x)$ denote its density and $F_X(x)$ denote its CDF, both valid for all $x \in \reals$. Set $\lambda$ so that the 80-20 Pareto Principle holds. Circle the statement(s) that are true.

\begin{enumerate}
\item $Q[X, 0.8] = Q[X, 0.2]$
\item $Q[X, 0.8] = f_X(0.8)$
\item $Q[X, 0.8] = F_X(0.8)$
\item $ \frac{\displaystyle\int_{-\infty}^{F^{-1}_X(0.8)} x f_X(x)}{\displaystyle\expe{X}} = 0.2$
\item $\lambda = 0.8$
\end{enumerate}

\subquestionwithpoints{10} Let $X_1, X_2, \ldots \iid \exponential{\lambda}$ and $N \sim \poisson{\lambda}$. Each statement below is either true or false. Circle the statement(s) that are true for all $k \in \naturals$.

\begin{enumerate}
\item $N = X_1 + X_2 + \ldots + X_k$
\item $X_1 + X_2 + \ldots + X_k \sim \text{Gamma}(k, \lambda)$
\item $\prob{X_1 + X_2 + \ldots + X_k < 1} = Q(k, \lambda)$
\item $\prob{X_1 + X_2 + \ldots + X_k < 1} = P(k, \lambda)$
\item $\prob{N < 1} = Q(k, \lambda)$
\item $\prob{N < 1} = P(k, \lambda)$
\item $\prob{N < k} = Q(k, \lambda)$
\item $\prob{N < k} = P(k, \lambda)$
\item $\prob{X_1 + X_2 + \ldots + X_k > 1} = \prob{N < k}$
\end{enumerate}

\subquestionwithpoints{6} Consider a continuous r.v. $X$ with known CDF $F_X$ Let $Y = -X$ and find the CDF of $Y$ in terms of $F_X$.  \spc{1}

\subquestionwithpoints{8} Let $X_1, X_2 \iid \text{Logistic}\parens{0, 1}$. Provide an expression for the $\prob{X_1 + X_2 > a}$ where $a \in \reals$ is a constant. Your answer must consist of just numbers, symbols and no brand name notation e.g. $\int_\reals \oneover{1 + x^2} dx$ but not  $\int_{\support{X_1}} \text{Logistic}\parens{0, 1} dx$. Do NOT simplify! \spc{5}

%\subquestionwithpoints{6} Assume $\Gamma(\half) = \sqrt{\pi}$ and show that $\Gamma(\frac{5}{2}) = \frac{3}{4}\sqrt{\pi}$. \spc{1}

\subquestionwithpoints{6} Let $X \sim \poisson{\lambda}$ and let $Y = g(X) = \sqrt{X}$. Find its PMF $p_Y(y)$ and make sure $p_Y(y)$ is valid $\forall y \in \reals$. Do NOT simplify. \spc{4}

\subquestionwithpoints{4} Let $X \sim \text{BetaPrime}(\alpha, \beta) := \oneover{B(\alpha, \beta)} \frac{x^{\alpha - 1}}{(x + 1)^{\alpha + \beta}} \indic{x \geq 0}$. Decompose the PDF of $X$ into $c \cdot k(x)$ where $k(x)$ is the kernel and $c$ is the normalization constant. \spc{1}
%
%\beqn
%c &=& \\
%&& \\
%&& \\
%k(x) &=& \\
%&&\\
%\eeqn

\subquestionwithpoints{6} Let $X \sim \text{BetaPrime}(\alpha, \beta)$. Prove that $X^{-1} \sim \text{BetaPrime}(\beta, \alpha)$. \spc{10}

%\subquestionwithpoints{6} Let $X \sim \text{Gamma}\parens{\alpha, \beta}$ and let $Y = g(X) = X^{-1}$. Find its PDF $f_Y(y)$ and make sure $f_Y(y)$ is valid $\forall y \in \reals$. \spc{10}

\subquestionwithpoints{8} Consider dependent continuous r.v.'s $X_1, X_2$ with known JDF $f_{X_1, X_2}(x_1, x_2)$ valid for all $x_1, x_2 \in \reals$. Consider the transformation of these r.v.'s of $H = \frac{X_1}{\natlog{X_2}}$. Find a formula that will allow you to compute its PDF $f_H(h)$ in terms of the JDF $f_{X_1, X_2}(x_1, x_2)$. \spc{7}

\subquestionwithpoints{8} Let $X_1, X_2 \iid \text{Laplace}(0,1)$ and let $R = \frac{X_1}{X_2}$. Write a mathematical expression for $f_R(r)$. Do NOT simplify! \spc{4}

\subquestionwithpoints{8} Let $\Xoneton \iid \exponential{\lambda}$. Find the distribution of their minimum. If it is a brand name r.v. we studied, indicate it with the value(s) of its parameters. \spc{5}

\subquestionwithpoints{8} Let $X_1, \ldots, X_{37} \iid \stduniform$. Find $\prob{X_{(17)} \leq \half}$. Express your answer in terms of the regularized incomplete beta function. \spc{8}

\subquestionwithpoints{8} If $X_1, X_2 \iid \text{ExtNegBin}(k, p) := \frac{\Gamma(k + x)}{\Gamma(k)x!} (1 - p)^x p^k \indic{x \in \naturals_0}$ who share the ch.f. $\phi_X(t) = \tothepow{\frac{1-p}{1-pe^{it}}}{k}$, show that $T = X_1 + X_2$ is also distributed as a ExtNegBin and find its parameters. If you use the following properties of ch.f.'s make sure to reference them in your proof by saying \qu{(P1)}, \qu{(P2)}, etc.

\footnotesize
\begin{itemize}
\item[(P0)] $\phi_X(0) = 1$
\item[(P1)] $\phi_X(t) = \phi_Y(t)$ is equivalent to $X \equalsindist Y$
\item[(P2)] If $Y = aX + b$ then $\phi_Y(t) = e^{ibt}\phi_X(at)$
\item[(P3)] If $X_1, X_2 \inddist$ and $T = X_1 + X_2$ then $\phi_T(t) = \phi_{X_1}(t) \phi_{X_2}(t)$ and if $X_1, X_2 \iid$ then $\phi_T(t) = \phi_{X}(t)^2$
\item[(P4)] For any moment that exists, $\expe{X^n} = \phi^{(n)}_X(0) / i^n$
\end{itemize}
\normalsize~\spc{6}

\subquestionwithpoints{8} [MA] If $Y~|~X = x \sim \text{ExtNegBin}(k, x)$ and $X \sim \betanot{\alpha}{\beta}$. Find the PMF of $Y$, a r.v. model you have not seen yet. Simplify \textit{as much as possible}. For those curious, $Y \sim \text{BetaNegBin}(k, \alpha, \beta)$, appropriately named the \qu{beta negative binomial}. \spc{8}

\subquestionwithpoints{6} [Extra Credit] If $Y~|~X = x \sim \binomial{10}{x}$ and $X \sim \betanot{\alpha}{\beta}$ where $Y$ models the number of male births for women who have 10 children and $x$ is the probability of having a male child (assuming each child's gender is an independent and identically distributed process). If Jane had 2 girls, what is the distribution of her $X$? \spc{1}

\eenum

\end{document}


% !TEX options=--shell-escape
\documentclass [12pt]{article}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{float}
\usepackage{multirow}
\usepackage{xcolor}
\usepackage [ruled,vlined,commentsnumbered,titlenotnumbered]{algorithm2e} \usepackage{array}
\usepackage{booktabs}
\usepackage{url}
\usepackage{parskip}
\usepackage [margin=1in]{geometry}
\usepackage [T1]{fontenc}
\usepackage{cmbright}
\usepackage [many]{tcolorbox}
\usepackage [colorlinks = true,
            linkcolor = blue,
            urlcolor  = blue,
            citecolor = blue,
            anchorcolor = blue]{hyperref}
\usepackage{enumitem}
\usepackage{xparse}
\usepackage{verbatim}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{csquotes}
\usepackage[cache=false]{minted}
\usepackage{mdframed}
\usepackage{tikz}
\usepackage{algpseudocode}
\usepackage{wrapfig}

\usetikzlibrary{shapes.symbols}
\newtheorem{theorem}{Theorem}

\BeforeBeginEnvironment{minted}{\begin{mdframed}}
\AfterEndEnvironment{minted}{\end{mdframed}}

\DeclareTColorBox{Solution}{}{breakable, title={Solution}}
\DeclareTColorBox{Solution*}{}{breakable, title={Solution (provided)}}
\DeclareTColorBox{Instruction}{}{boxrule=0pt, boxsep=0pt, left=0.5em, right=0.5em, top=0.5em, bottom=0.5em, arc=0pt, toprule=1pt, bottomrule=1pt}
\DeclareDocumentCommand{\Expecting }{+m}{\textbf{[We are expecting:} #1\textbf{]}}
\DeclareDocumentCommand{\Points }{m}{\textbf{(#1 pt.)}}
\newcommand{\hint }[1]{\noindent{[\textbf{HINT:} \em #1 \em ]}} \newcommand{\pts }[1]{\textbf{(#1 pt.)}}

\begin{document}

{\LARGE \textbf{COMP 285 (NC A\&T, Spr `22)}\hfill \textbf{Homework 9} }
\vspace{1em}
\begin{Instruction}

\paragraph{Due.} Thursday, April 21st, 2022 @ 11:59 PM!
\end{Instruction}

\vspace{1em}
\begin{Instruction} \paragraph{Homework Expectations:} Please see \href{https://www.comp285.ml/homework/#general-homework-information}{Homework}.
\end{Instruction}

\vspace{1em}
\begin{Instruction}

\paragraph{Exercises} The following questions are exercises. We encourage you to work with a group and discuss solutions to make sure you understand the material.

\paragraph{Points} This assignment is graded out of 50 points. However, you can get up to 60 points if you complete everything. These are not bonus points, but rather points to help make-up any parts you miss.

\end{Instruction}

\begin{centering}
\section*{Fun with Greedy}
\end{centering}

\begin{Instruction}

\paragraph{Written Problems} The following questions are to be submitted in written/typed form to gradescope.

\end{Instruction}

\section{Greedy Strategies \Points{10}}
Let $G = (V, E)$ be an undirected unweighted graph. Say that a vertex $v$ can ``see'' an edge if $v$ is an endpoint of that edge. Say that a set $S \subseteq V$ is ``all-seeing'' if every edge $e \in E$ is seen by at least one vertex in $S$. In this problem, we will try to greedily construct the smallest all-seeing set possible.

Consider the following greedy algorithm to find an all-seeing subset:

\begin{algorithm}[H]
\caption{findAllSeeingSubset(G)}
\begin{algorithmic}
  \State $S = \{\}$
  \State \While{ $G$ contains edges }{
    \State choose an edge $e = (u,v)$ in $G$
    \State $S$.add($u$)
    \State $S$.add($v$)
    \State remove $u$ and all of its adjacent edges from $G$
    \State remove $v$ and all of its adjacent edges from $G$
  }
  \State \Return{ $S$ }
\end{algorithmic}
\end{algorithm}

\subsection{\Points{5}}
Prove that \emph{findAllSeeingSubset(G)} always returns an all-seeing subset.

\Expecting{A short but complete argument as to why the algorithm above always returns an all-seeing subset}

\subsection{\Points{5}} Give an example of a graph on which \emph{findAllSeeingSubset(G)} does not return a \textit{smallest} all-seeing subset, for at least one way of choosing edges.

\Expecting{An example graph and a brief justification why it does not return a smallest all-seeing subset.}

\section{Plucky's Slushies \Points{10}}
\begin{wrapfigure}{R}{0.3\textwidth }
\centering
\includegraphics[width=0.25\textwidth]{plucky.png}
\end{wrapfigure}

Plucky the penguin has a cooler with a capacity of $Q$ ounces. Plucky heads to the convenience store where there are $n$ flavors of slushies. Each slushy flavor $i$ has a cost per ounce $v_i > 0$ (measured in units of dollars per ounce) and a quantity $q_i > 0$ (measured in ounces). There are $q_i$ ounces of slushy $i$ available to Plucky, and for any real number $x \in [0, q_i]$, the total cost from $x$ ounces of slushy $i$ is $x \cdot v_i$.

Note that Plucky can take a fractional amount of each slushy. For example, perhaps there is 36 ounces of blue raspberry flavored slushy; Plucky can choose to put 12.345 ounces of blue raspberry slushy in the cooler.

Plucky wants to create the most expensive slushy concoction in the cooler. Thus, Plucky wants to choose an amount $x_i \geq 0$ to take for each flavor $i$ in order to maximize $\sum _{i} x_i v_i$ the total cost while satisfying:

\begin{enumerate}
  \item Plucky does not overfill the cooler (that is, $\sum _{i} x_i \leq Q$), and
  \item Plucky does not take more of a flavor than is available (that is, $0 \leq x_i \leq q_i$ for all $i$).
\end{enumerate}

Assume that $\sum _{i} q_i \geq Q$, so there always is some way to fill the cooler.

\subsection{Which flavor? \Points{2}} Suppose that Plucky has already have partially filled the cooler, and there is some amount of each slushy flavor left. Which flavor should Plucky choose next, and how much?

\Expecting{A short answer.}

\subsection{Greedy Algorithm \Points{4}}

Design a \textbf{greedy algorithm} which takes as input $Q$ along the tuples $(i, v_i, q_i)$ for $i = 0, \dots , n-1$, and outputs tuples $(i, x_i)$ so that conditions (1) and (2) hold and $\sum _i x_i v_i$ is as large as possible. Your algorithm should take $O(n\log (n))$.

Note that the tuples may be returned in any order.

\Expecting{Pseudocode AND an English explanation of what it is doing. A justification of the running time.}

\subsection{Proof of Correctness \Points{4}} Complete the inductive step below to prove that your algorithm is correct.

\begin{itemize}

  \item \textbf{Inductive hypothesis:} After making the $t$'th greedy choice, there is an optimal solution that extends the solution that the algorithm has constructed so far.
  \item \textbf{Base case:} Any optimal solution extends the empty solution, so the inductive hypothesis holds for $t=0$.
\end{itemize}

\Expecting{A proof of the inductive step: assuming the inductive hypothesis holds for $t-1$, prove that it holds for $t$. A proof by induction conclusion.}

\section{Monkey Island \Points{15}}
\label{sec:last}
One day, you get stranded on a beach on an island with $n$ monkeys. You will be here a while, so you want to make friends and introduce yourself to all the monkeys. However, you don’t want to leave the beach since the rest of the island looks dangerous. Each monkey $i$ will only be on the beach during one time interval $[a_i, b_i]$, inclusive. You can only introduce yourself to monkey $i$ during this time interval.

Your plan is to stand in the center of the beach and use the megaphone you brought with you to say “Hello” at certain times $t_1,\cdots,t_m$. Any monkey on the beach at one of the times $t_j \in \{t_1,\cdots,t_m\}$ will hear your introduction. It’s okay if a monkey hears your introduction more than once, but you want to make sure every monkey hears your introduction at least once.

\subsection{Greedy Algorithm \Points{6}}
The battery in your megaphone is low, so you want to minimize $m$, the number of times you use your megaphone.

\textbf{Devise a greedy algorithm} that takes as input the list of intervals $[a_i, b_i]$ and outputs a list of times $t_1,\cdots,t_m$ such that $m$ is as small as possible but every monkey hears your introduction. Your algorithm should run in time $O(n \log n)$ where $n$ is the number of monkeys on the island. You can assume every monkey visits the beach at some point.

\Expecting{Pseudocode and an English description of your algorithm, as well as a short justification of the runtime}

\subsection{Proof of Correctness \Points{9}} Give a formal proof that your algorithm is correct.
\Expecting{A proof by induction. So include a base case, inductive hypothesis, inductive step, and conclusion}

\pagebreak
\begin{Instruction}

\paragraph{Coding Problems} The following questions are to be submitted as a ".zip" file on Gradescope.

\end{Instruction}

\section{Coding \Points{25}}
After completing the written portion of the assignment, you should submit it to \href{https://www.gradescope.com/courses/350304}{Gradescope}.

For the coding portion, get your starter \href{https://replit.com/team/COMP285/HW9-Code}{C++ code} or \href{https://replit.com/team/COMP285/HW9-Code-Python}{Python code}.

Note that the starter code also include a few test cases you can run on repl.it. However, the full test suite is the one run on Gradescope.

Please reference the \texttt{README.md} included in your starter code for detailed instructions.

\section*{Submitting the Assignment}

This assignment is a combination of written and programming questions. Both portions of the assignment should be submitted through \href{https://www.gradescope.com/courses/350304}{Gradescope}.

The "Homework 9: Fun with Greedy" assignment is the written portion, for which you should submit a \textbf{typed} response to the non-coding questions (questions 1-\ref{sec:last}). Each response should clearly be marked with its corresponding number. You are free to use the provided templates, print the questions and write your answers, or to simply type your responses on a blank document (whatever works for you).

The "Homework 9: Coding" is the programming portion of the assignment. For this portion, download the "answers.cpp", "answers.h" (or just "answers.py") and "citations.txt" from your repl.it these files to \href{https://www.gradescope.com/courses/350304}{Gradescope}. You can upload the assignment as many times as you want.

\end{document}


\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage{amsmath}

\usepackage[hmargin=3cm,vmargin=6.0cm]{geometry}
%\topmargin=0cm
\topmargin=-2cm
\addtolength{\textheight}{6.5cm}
\addtolength{\textwidth}{2.0cm}
%\setlength{\leftmargin}{-5cm}
\setlength{\oddsidemargin}{0.0cm}
\setlength{\evensidemargin}{0.0cm}

%misc libraries goes here
%\usepackage{fitch}

\begin{document}

\section*{Student Information }
%Write your full name and id number between the colon and newline
%Put one empty space character after colon and before newline
Full Name : Yavuz Selim YEŞİLYURT \\
Id Number : 2259166 \\

% Write your answers below the section tags
\section*{Answer 1}
\hspace{5mm} 1)\\
\begin{table}[H]
\small
\centering
\begin{tabular}{|c|c|c|c|c|c|c|}
\hline
$p$ & $q$ & $\neg q$ & $p\rightarrow q$ & $\neg q\wedge(p \rightarrow q)$ & $\neg p$ & $ (\neg q\wedge(p \rightarrow q)) \rightarrow \neg p$\\
\hline
T & T & F & T & F & F & T\\
T & F & T & F & F & F & T\\
F & T & F & T & F & T & T\\
F & F & T & T & T & T & T\\
\hline
\end{tabular}
\end{table}
2)\\
\begin{table}[H]
\small
\centering
\begin{tabular}{|c|c|c|c|c|c|c|c|c|}
\hline
$p$ & $q$ & $r$ & $p \vee q$ & $\neg p$ & $\neg p \vee r$ & $(p \vee q) \wedge (\neg p \vee r) $ & $q \vee r$ & $((p \vee q) \wedge (\neg p \vee r)) \rightarrow q \vee r$\\
\hline
T & T & T & T & F & T & T & T & T\\
T & T & F & T & F & F & F & T & T\\
T & F & F & T & F & F & F & F & T\\
F & T & T & T & T & T & T & T & T\\
F & T & F & T & T & T & T & T & T\\
F & F & T & F & T & T & F & T & T\\
T & F & T & T & F & T & T & T & T\\
F & F & F & F & T & T & F & F &T\\
\hline
\end{tabular}
\end{table}

\section*{Answer 2}
\begin{equation*}
\begin{split}
 (p \rightarrow q) \vee (p \rightarrow r) &\equiv (\neg p \vee q) \vee (p \rightarrow r)\ \qquad \quad \ table\ 7, Equivalence \ 1\\
 &\equiv (\neg p \vee q) \vee (\neg p \vee r)\ \quad \quad \quad table\ 7, Equivalence\ 1\\
 &\equiv (q \vee r) \vee (\neg p \vee \neg p) \quad \quad \quad \ table\ 6, Associative\ Law \\ &\equiv (q \vee r) \vee \neg p \ \ \ \quad \qquad \qquad \ table\ 6, Idempotent\ Law\\ &\equiv \neg(q \vee r) \rightarrow \neg p \ \ \ \quad \qquad \quad \ table\ 7, Equivalence\ 3\\ &\equiv (\neg q \wedge \neg r) \rightarrow \neg p \ \qquad \qquad \ table\ 6, De\ Morgan's\ Second\ Law\\
\end{split}
\end{equation*}
\newpage
\section*{Answer 3}
\begin{enumerate}

	\item
	(a) All cats are friends with at least one dog.\\
	(b) Some cats are friends with all dogs.\\
	\item
	(a) $\forall x \forall y ((Eats(x,y) z\wedge Meal(y)) \rightarrow Customer(x)) $ \\
	(b) $\exists x \exists y (Chef(x) \wedge Meal(y) \wedge \neg Cooks(x,y))$\\
	(c) $\exists x \forall y \exists z(((Cooks(x,y) \wedge Chef(x)) \rightarrow Meal(y)) \rightarrow (Eats(z,y) \wedge Customer(z)))$\\
	(d) $\forall x \exists y \exists z ((Chef(z) \wedge Chef(x) \wedge (x \neq z) \wedge Meal(y) \wedge \neg Cooks(x,y) \wedge Cooks(z,y)) \rightarrow Knows(x,z))$\\

\end{enumerate}

\section*{Answer 4}
$ \neg p$ and $ p \rightarrow q$ are given as premises and $ \neg q $ is given as conclusion. Since $ \neg p$ is given as true, the lefthandside of $p \rightarrow q$ is ($p$) false. For situations which $p$ is false (third and fourth rows on Table 1) $ p \rightarrow q$ returns true. But its truth value does not depend on $ q $. $q$ can be true or false. Therefore we can not deduce that $ \neg q$ is true. So this argument cannot be a deduction rule in a sound deductive system.\\
\begin{table}[H]
\small
\caption{ Truth table for $p \rightarrow q$ }
\centering
\begin{tabular}{|c|c|c|}
\hline
$p$ & $q$ & $p\rightarrow q$\\
\hline
T & T & T \\
T & F & F \\
F & T & T \\
F & F & T \\
\hline
\end{tabular}
\end{table}

\section*{Answer 5}
\begin{table}[H]
\begin{enumerate}
\item $ p \implies q \hfill premise$
\item $ q \implies r          \hfill    premise$
\item $ r \implies p      \hfill    premise$\\
\begin{tabular}{|p{10cm}|}
\hline
\item $ q                    \hfill    assumed$
\item $ r              \hfill        \implies e,2,4$
\item $ p                \hfill       \implies e,3,5$\\
\hline
\end{tabular}
\item $ q \implies p              \hfill     \implies i,4-6$
\item $ p \iff q        \hfill   \iff i,1,7$\\
\begin{tabular}{|p{10cm}|}
\hline
\item $ p       \hfill          assumed$
\item $ q          \hfill      \implies e,1,9$
\item $ r       \hfill  \implies e,2,10$\\
\hline
\end{tabular}
\item $ p \implies r        \hfill   \implies i,9-11$
\item $ p \iff r                 \hfill     \iff i,3,12$
\item $ (p \iff q) \land (p \iff r)          \hfill     \land i,8,13$
\end{enumerate}
\end{table}
%%%%%%%%%%%%%%%%%%%%%%
\section*{Answer 6}

\begin{table}[H]
\begin{enumerate}
\item $ \forall x (Q(x) \implies R(x)) \hfill  premise$
\item $ \exists x (P(x) \implies Q(x)) \hfill  premise$
\item $ \forall x (P(x)) \hfill   premise$\\
\begin{tabular}{|p{10cm}|}
\hline
\item $ P(c) \implies Q(c) \hfill   assumed $
\item $ P(c) \hfill   \forall e,3$
\item $ Q(c) \hfill   \implies e,4,5$
\item $ Q(c) \implies R(c) \hfill   \forall e,1$
\item $ R(c) \hfill   \implies e,6,7$
\item $ P(c) \land R(c) \hfill   \land i,5,8$
\item $ \exists x (P(x) \land R(x))  \hfill   \exists i,9$\\
\hline
\end{tabular}
\item $ \exists x (P(x) \land R(x)) \hfill   \exists e,2,4-10$
\end{enumerate}
\end{table}

\end{document}


\documentclass[10pt]{article}

\usepackage{graphics,epsfig,calc,multicol}
\usepackage{amsmath,amscd,amssymb}
\usepackage{pstricks}
\usepackage{pst-plot}
\usepackage{pstricks-add}
\usepackage{epsfig}
\usepackage{graphicx,color}
\usepackage{amssymb,amsthm,amsfonts,amsmath,pifont}
\usepackage{array}

%\usepackage[utopia]{mathdesign}

\newtheorem{fact}{Fact}%[chapter]

\theoremstyle{definition}
\newtheorem{example}{Example}

\theoremstyle{remark}
\newtheorem*{sol}{Solution}

\psset{xunit=1.0cm,yunit=1.0cm,algebraic=true,dotstyle=o,dotsize=3pt 0,linewidth=0.8pt,arrowsize=3pt 2,arrowinset=0.25}

\def\RR{\mathbb{R}}
\def\pow{\mathop{\rm pow}\nolimits}

\global\oddsidemargin=0pt
\global\evensidemargin=0pt
%\global\topmargin=-20pt
%%\global\headheight=35pt
\global\headsep=35pt
\global\textheight=642pt
\global\textwidth=452pt

\begin{document}

\title{Triangles}
\author{Carlos Shine}
\date{}
\maketitle
\section{Introduction}
You probably know that, in a triangle $ABC$:
\begin{itemize}
\item The medians meet at a single point $G$, called \emph{centroid};
\item The internal angle bisectors meet at a single point $I$, called \emph{incenter};
\item Two external angle bisectors of $B$ and $C$ and the internal angle bisector of $A$ meet at a single point $I_a$, called \emph{$A$-excenter};
\item The perpendicular bisectors meet at a single point $O$, called \emph{circumcenter}, which is also the center of the circle that contains $A$, $B$ abd $C$ (called the \emph{circumcenter});
\item The altitudes meet at a single point $H$, called \emph{orthocenter}.
\end{itemize}

Now prove the following theorems:
\begin{fact}[Euler line]
The orthocenter $H$, the centroid $G$ and the circumcenter $O$ are collinear, and $HG = 2\cdot GO$.
\end{fact}

\begin{fact}[Nine-point circle]
The midpoints, the feet of the altitudes and the midpoints of the segments connecting the vertices to the orthocenter of a triangle lie on a circle with center in the midpoint of the segment connecting the orthocenter and the circumcenter.
\end{fact}

\begin{fact}[Nine-point circle facts]
In a triangle,
\begin{itemize}
\item the radius of the nine-point circle is half of the circumradius.
\item the center of the direct homothety taking the circumcircle to the nine-point circle is the orthocenter $H$.
\item the center of the inverse homothety taking the circumcircle to the nine-point circle is the centroid $G$.
\end{itemize}
\end{fact}

\begin{fact}[Orthocenter and the circumcircle]
The reflections of the orthocenter with respect to the sides of its triangle lie on its circumcircle.
\end{fact}

\begin{fact}[The ubiquitous in(ex)center property]
Let $I$ be the incenter and $I_a$ be the $A$-excenter of a triangle $ABC$ and $L$ be the midpoint of the arc $BC$ from the circumcircle of $ABC$ that doesn't contain $A$. Then there is a circle with center in $L$ that passes through $B$, $C$, $I$ and $I_a$. Notice that this means that $LB = LC = LI = LI_a$ and, moreover, $L$ is the midpoint of $II_a$.
\end{fact}

There is a number of ways of interpreting this fact:
\begin{enumerate}
\item $L$ is the circumcenter of the quadrilateral $BICI_a$;
\item You can find the incenter and $A$-excenter of a triangle by marking $L$ and drawing a circle with center in $L$ and radius $LB = LC$. The incenter is the intersection of said circle and angle bisector $AL$. This is particularly useful when you need to prove that $I$ is the incenter or $I_a$ is the $A$-excenter: you don't have to prove that it's the intersection of two bisectors.
\end{enumerate}

\begin{fact}[Internal, external, perpendicular bisectors]
Let $ABC$ be a triangle. Then the internal and external angle bisectors of $A$ and the perpendicular bisector of $BC$ determine a triangle whose vertices lie on the circumcircle.
\end{fact}

\begin{fact}[Buy two, get the third free]
Let $ABC$ be a triangle. If $P$ and $Q$ are points in the plane such that $\angle PAB = \angle CAQ$ and $\angle PBC = \angle ABQ$ (directed angles!) then $\angle PCA = \angle BCQ$. Points $P$ and $Q$ are said to be isogonal conjugates with respect to triangle $ABC$.
\end{fact}

\begin{fact}[Symmedians]
Let $ABC$ be a triangle. Then the isogonal line of the median through $A$ passes through the intersection of the tangent lines to the circumcircle through $B$ and $C$.
\end{fact}

\begin{fact}[More symmedian facts]
Let $AN$ be a symmedian of triangle $ABC$, $N$ on $BC$. Then
\begin{enumerate}
\item The ratio of the distances from any point in line $AN$ to $AB$ and $AC$ is equal to $\frac{AB}{AC}$.
\item $\frac{BN}{NC} = \left(\frac{AB}{AC}\right)^2$.
\end{enumerate}
\end{fact}

\section{Some trig}
Trigonometry can be a handful in many problems (trig bashers, rejoice!). So it's good to know some identities.
\begin{fact}[Triangle trig]
Let $ABC$ be a triangle. For simplicity, let $\alpha = \angle A$, $\beta = \angle B$, $\gamma = \angle C$, $a = BC$, $b = CA$, $c = AB$, $R$ be the circumradius, $p = \frac{a+b+c}2$ be the semiperimeter and $r$ be the inradius. Then
\begin{itemize}
\item $\sin\alpha = \frac a{2R}$
\item the distance from the circumcenter to side $BC$ is $R\cos\alpha = \frac a2\cot\alpha$
\item $r = 4R\sin\frac\alpha2\sin\frac\beta2\sin\frac\gamma2$
\item $p-a = 4R\cos\frac\alpha2\sin\frac\beta2\sin\frac\gamma2$
\item $p = 4R\cos\frac\alpha2\cos\frac\beta2\cos\frac\gamma2$
\end{itemize}
\end{fact}

This is not really a trigonometric identity, but it has the spirit of one.
\begin{fact}[Stewart's theorem]
Let $ABC$ be a triangle and $P$ be a point on side $BC$. Then
$$AB^2\cdot CP + AC^2\cdot BP = BC\cdot (AP^2 + BP\cdot CP)$$
\end{fact}

\section{Problems}
\begin{enumerate}
% Isogonal conjugates 101
\item Segments $AC$ and $BD$ meet at $P$, and $PA = PD$ and $PB = PC$. Let $O$ be the circumcenter of the triangle $PAB$. Prove that lines $OP$ and $CD$ are perpendicular.

% Orthocenter
\item Let $H$ be the orthocenter of triangle $ABC$. Prove that the circumcircles of triangles $AHB$, $BHC$ and $CHA$ have the same radius.

% Angle-chase FTW
\item Let $ABC$ be a right triangle with $\angle A = 90^\circ$, $AD$ be an altitude. Let $I,J,K$ be the incenters of triangles $ABC$, $ABD$ and $ACD$, respectively. Prove that $I$ is the orthocenter of the triangle $AJK$.

% More like midlines, but whatever
\item Let $C$ be a circle with center $O$, $AB$ be one of its diameters and $R$ be a point on $C$ different from $A$ and $B$. Let $P$ be the orthogonal projection of $O$ onto $AR$. Let $Q$ a point on line $OP$ such that $QP$ is half of $PO$ and $Q$ does not line on segment $OP$. The line that goes through $Q$ and is parallel to $AB$ meets the line $AR$ in $T$. Let $H$ be the intersection of lines $AQ$ and $OT$. Prove that $H$, $R$ and $B$ are collinear.

% Be patient and construct everything!
\item Let $ABC$ be an acute triangle. Let $H$ be the foot of the altitude from $A$, $\Gamma_A$ be the circle with diameter $AH$. $\Gamma_A$ meet lines $AB$ and $AC$ at $M\neq A$ and $N\neq A$, respectively. Line $\ell_A$ is the perpendicular line from $A$ to $MN$. Define $\ell_B$ and $\ell_C$. Prove that $\ell_A$, $\ell_B$ and $\ell_C$ meet at the same point.

% Nine-point
\item Suppose three circles with the same radius $r$ meet at point $O$. Let $P_1,P_2,P_3$ be the second intersections between two of the three circles. Prove that the circumcircle of the triangle $P_1P_2P_3$ has also radius $r$.

% Easy stuff
\item (Brazil) Let $ABCD$ be a convex quadrilateral, $M$ be the midpoint of $DC$, $N$ be the midpoint of $BC$ and $O$ be the intersection of diagonals $AC$ and $BD$. Prove that $O$ is the barycenter of triangle $AMN$ if and only if $ABCD$ is a parallelogram.

% Area
\item Let $ABC$ be an acute triangle. Suppose cevians $AD$, $BE$ and $CF$ meet at the circumcenter $O$ of $ABC$. Prove that
$$\frac1{AD} + \frac1{BE} + \frac1{CF} = \frac2R,$$
where $R$ is the circumradius of $ABC$.

% Euler line, but angle chase really
\item Let $A_1,B_1,C_1$ be the feet of the altitudes $AA_1,BB_1,CC_1$ respectively. Let $A_2,B_2,C_2$ be the points where the incircle touches the sides of triangle $A_1B_1C_1$. Prove that the Euler lines of triangles $ABC$ and $A_2B_2C_2$ coincide.

% Euler line
\item Given are six distinct points on a circle. The orthocenter of a triangle with vertices on three of these points is connected to the centroid of the triangle with vertices on the three remaining points. Prove that the twenty line segments that may be defined in this fashion meet at a single point.

% Orthocenter property
\item (Cono Sur 1998) Let $H$ be the orthocenter of acute triangle $ABC$ and $M$ be the midpoint of $BC$. Let $X$ be the intersection point of the line $HM$ and the arc $BC$ that doesn't contain $A$ of the circumcircle of $ABC$. Line $BH$ meets again the circumcircle of $ABC$ at $Y\neq B$. Prove that $XY = BC$.

% No bashing!
\item Let $G$, $H$ and $I$ be the centroid, the orthocenter and the incenter of an acute triangle. Prove that $\angle GIH > 90^\circ$.

% Cool inequality
\item (IMO 2001) Consider an acute-angled triangle $ABC$. Let $P$ be the foot of the altitude of triangle $ABC$ issuing from the vertex $A$, and let $O$ be the circumcenter of triangle $ABC$. Assume that $\angle C \geq \angle B+30^{\circ}$. Prove that $\angle A+\angle COP < 90^{\circ}$.

% Cool triangle stuff
\item (Cono Sur 2002) We know the following information about the triangle $ABC$: $\angle A$ is a right angle; we know the location of points $T$, where the incircle touches hypotenuse $BC$, $D$, intersection of the internal bisector of $\angle B$ with side $AC$ and $E$, intersection of the internal bisector of $\angle C$ with side $AB$. Describe how to construct, with compass and ruler, the vertices $A$, $B$ and $C$.

% Not much, but a cool problem nonetheless
\item (Cono Sur 2003) Let $ABC$ be an acute triangle such that $\angle B = 60^\circ$. The circle with diameter $AC$ cuts the internal bisectors of $\angle A$ and $\angle C$ at $M$ and $N$ respectively ($M\neq A$, $N \neq C$). The internal bisector of $\angle B$ cuts $MN$ and $AC$ at $R$ and $S$, respectively. Prove that $BR \leq RS$.

% Ubiquitous incenter
\item (IMO 2002) The circle $S$ has center $O$, and $BC$ is a diameter of $S$. Let $A$ be a point of $S$ such that $\angle AOB<120^\circ$. Let $D$ be the midpoint of the arc $AB$ which does not contain $C$. The line through $O$ parallel to $DA$ meets the line $AC$ at $I$. The perpendicular bisector of $OA$ meets $S$ at $E$ and at $F$. Prove that $I$ is the incenter of the triangle $CEF$.

% Ubiquitous incenter
\item (IMO 2006) Let $ABC$ be triangle with incenter $I$. A point $P$ in the interior of the triangle satisfies
$$\angle PBA+\angle PCA = \angle PBC+\angle PCB.$$

Show that $AP \geq AI$, and that equality holds if and only if $P=I$.

% Ubiquitous excenter
\item (Cono Sur 2008) Let $ABC$ be an isosceles triangle with $AC = BC$. Let $\Gamma$ be a semicircle with center on segment $AB$ that touches $AC$ and $BC$. A line is tangent to $\Gamma$ and cuts $AC$ and $BC$ at $D$ and $E$, respectively.

Suppose that the perpendicular lines through $D$ to $AC$ and through $E$ to $BC$ meet at a point $P$ inside triangle $ABC$. Let $Q$ be the orthogonal projection of $P$ in line $AB$. Prove that $\frac{PQ}{CP} = \frac12\cdot\frac{AB}{AC}$.

% Ubiquitous incenter
\item (Russia 2012) The points $A_1$, $B_1$, $C_1$ lie on the sides $BC$, $CA$ and $AB$ of the triangle $ABC$ respectively. Suppose that $AB_1 - AC_1 = CA_1 - CB_1 = BC_1 - BA_1$. Let $I_A$, $I_B$ and $I_C$ be the incenters of triangles $AB_1C_1$, $A_1BC_1$ and $A_1B_1C$ respectively. Prove that the circumcenter of triangle $I_AI_BI_C$ is the incenter of triangle $ABC$.

% Ubiquitous inexcenter
\item (IMO 2010) Let $I$ be the incentre of triangle $ABC$ and let $\Gamma$ be its circumcircle. Let the line $AI$ intersect $\Gamma$ again at $D$. Let $E$ be a point on the arc $BDC$ and $F$ a point on the side $BC$ such that
$$\angle BAF = \angle CAE < \frac12\angle BAC.$$

Finally, let $G$ be the midpoint of the segment $IF$. Prove that the lines $DG$ and $EI$ intersect on $\Gamma$.

% Reflection of orthocenter
\item (Olympic Revenge 2008) Let $ABC$ be a triangle and $\Gamma$ its circumcircle. Let $D$, $E$ and $F$ the midpoints of the arcs $BC$, $CA$ and $AB$ of $\Gamma$ that don't contain the other vertex of $ABC$. Let $P_A$, $P_B$ and $P_C$ the midpoints of segments $HD$, $HE$ and $HF$, respectively. Finally, let $Q_A$, $Q_B$ and $Q_C$ be the midpoints of $BC$, $CA$ and $AB$, respectively. Prove that if the lines $P_AQ_A$, $P_BQ_B$ and $P_CQ_C$ determine a triangle, then it is similar to the triangle $DEF$.

% Isogonal conjugates
\item (IMO 2004) In a convex quadrilateral $ABCD$, the diagonal $BD$ bisects neither the angle $ABC$ nor the angle $CDA$. The point $P$ lies inside $ABCD$ and satisfies
$$\angle PBC=\angle DBA\quad\text{and}\quad \angle PDC=\angle BDA.$$

Prove that $ABCD$ is a cyclic quadrilateral if and only if $AP=CP$.

% Isogonal conjugates
\item (USAMO 2008) Let $ABC$ be an acute, scalene triangle, and let $M$, $N$ and $P$ be the midpoints of $BC$, $CA$, and $AB$, respectively. Let the perpendicular bisectors of $AB$ and $AC$ intersect ray $AM$ in points $D$ and $E$ respectively, and let lines $BD$ and $CE$ intersect in point $F$, inside of triangle $ABC$. Prove that points $A$, $N$, $F$ and $P$ all lie on one circle.

% Symmedians
\item (Cono Sur 2009) Let $A$, $B$ and $C$ be three points such that $B$ is the midpoint of $AC$ and $P$ a point such that $\angle PBC = 60^\circ$. Let $Q$ be a point such that $PCQ$ is an equilateral triangle and $B$ and $Q$ are in different semiplanes with respect to $PC$, and $R$ be a point such that $APR$ is an equilateral triangle and $B$ and $R$ are in the same semiplane with respect to $AP$. Let $X$ be the intersection point of lines $BQ$ and $PC$ and $Y$ be the point of intersection of lines $BR$ and $AP$. Prove that the lines $XY$ and $AC$ are parallel.

% More isogonal conjugates
\item (USA TST, 2008) Let $ABC$ be a triangle with $G$ as its centroid. Let $P$ be a variable point on segment $BC$. Points $Q$ and $R$ lie on sides $AC$ and $AB$ respectively, such that $PQ \parallel AB$ and $PR \parallel AC$. Prove that, as $P$ varies along segment $BC$, the circumcircle of triangle $AQR$ passes through a fixed point $X$ such that $\angle BAG = \angle CAX$.

% Easy angle-chase
\item (Brazil 2010) Let $ABCD$ be a convex quadrilateral with $\angle B \neq 90^\circ$, and $M$ and $N$ be the midpoints of the sides $CD$ and $AD$, respectively. The lines perpendicular to $AB$ passing through $M$ and to $BC$ passing through $N$ intersect at point $P$. Prove that $P$ lies on the diagonal $BD$ if and only if the diagonals $AC$ and $BD$ are perpendicular.

\end{enumerate}

\end{document}


% !TEX options=--shell-escape
\documentclass [12pt]{article}

\usepackage {amsmath}
\usepackage {amsthm}
\usepackage {amssymb}
\usepackage {graphicx}
\usepackage {float}
\usepackage {multirow}
\usepackage {xcolor}
\usepackage {algorithmic}
\usepackage [ruled,vlined,commentsnumbered,titlenotnumbered]{algorithm2e} \usepackage {array}
\usepackage {booktabs}
\usepackage {url}
\usepackage {parskip}
\usepackage [margin=1in]{geometry}
\usepackage [T1]{fontenc}
\usepackage {cmbright}
\usepackage [many]{tcolorbox}
\usepackage [colorlinks = true,
            linkcolor = blue,
            urlcolor  = blue,
            citecolor = blue,
            anchorcolor = blue]{hyperref}
\usepackage {enumitem}
\usepackage {xparse}
\usepackage {verbatim}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{csquotes}
\usepackage[cache=false]{minted}
\usepackage{mdframed}
\usepackage{tikz}
\usetikzlibrary{shapes.symbols}
\newtheorem{theorem}{Theorem}

\BeforeBeginEnvironment{minted}{\begin{mdframed}}
\AfterEndEnvironment{minted}{\end{mdframed}}

\DeclareTColorBox {Solution}{}{breakable, title={Solution}}
\DeclareTColorBox {Solution*}{}{breakable, title={Solution (provided)}}
\DeclareTColorBox {Instruction}{}{boxrule=0pt, boxsep=0pt, left=0.5em, right=0.5em, top=0.5em, bottom=0.5em, arc=0pt, toprule=1pt, bottomrule=1pt}
\DeclareDocumentCommand {\Expecting }{+m}{\textbf {[We are expecting:} #1\textbf {]}}
\DeclareDocumentCommand {\Points }{m}{\textbf {(#1 pt.)}}
\newcommand {\hint }[1]{\noindent {[\textbf {HINT:} \em #1 \em ]}} \newcommand {\pts }[1]{\textbf {(#1 pt.)}}

\begin{document}

{\LARGE \textbf {COMP 285 (NC A\&T, Spr `22)}\hfill \textbf {Homework 6} }
\vspace {1em}
\begin{Instruction}

\paragraph {Due.} Wednesday, March 23rd, 2022 @ 11:59 PM!
\end{Instruction}

\vspace {1em}
\begin{Instruction} \paragraph {Homework Expectations:} Please see \href{https://www.comp285.ml/homework/#general-homework-information}{Homework}.
\end{Instruction}

\vspace {1em}
\begin{Instruction}

\paragraph {Exercises} The following questions are exercises. We encourage you to work with a group and discuss solutions to make sure you understand the material.

\paragraph {Points} This assignment is graded out of 100 points. However, you can get up to 120 points if you complete everything. These are not bonus points, but rather points to help make-up any parts you miss.

\end{Instruction}

\begin{centering}
\section*{Fun with Graphs}
\end{centering}

\begin{Instruction}

\paragraph{Written Problems} The following questions are to be submitted in written/typed form to gradescope.

\end{Instruction}

\section{Exercise: DFS Basics \Points{12}}
\label{sec:last}

Consider the following directed acyclic graph (DAG):

\begin{center}
\begin{tikzpicture}[scale=0.15]
    \tikzstyle{every node}+=[inner sep=0pt]
    \draw [black] (18.2,-24.6) circle (3);
    \draw (18.2,-24.6) node {$A$};
    \draw [black] (33.7,-17.6) circle (3);
    \draw (33.7,-17.6) node {$B$};
    \draw [black] (40.2,-32.8) circle (3);
    \draw (40.2,-32.8) node {$C$};
    \draw [black] (54.4,-23.6) circle (3);
    \draw (54.4,-23.6) node {$D$};
    \draw [black] (68.6,-23.6) circle (3);
    \draw (68.6,-23.6) node {$E$};
    \draw [black] (57.4,-23.6) -- (65.6,-23.6);
    \fill [black] (65.6,-23.6) -- (64.8,-23.1) -- (64.8,-24.1);
    \draw [black] (20.93,-23.37) -- (30.97,-18.83);
    \fill [black] (30.97,-18.83) -- (30.03,-18.71) -- (30.44,-19.62);
    \draw [black] (34.88,-20.36) -- (39.02,-30.04);
    \fill [black] (39.02,-30.04) -- (39.17,-29.11) -- (38.25,-29.5);
    \draw [black] (42.72,-31.17) -- (51.88,-25.23);
    \fill [black] (51.88,-25.23) -- (50.94,-25.25) -- (51.48,-26.09);
    \draw [black] (21.01,-25.65) -- (37.39,-31.75);
    \fill [black] (37.39,-31.75) -- (36.81,-31) -- (36.46,-31.94);
    \draw [black] (36.58,-18.44) -- (51.52,-22.76);
    \fill [black] (51.52,-22.76) -- (50.89,-22.06) -- (50.61,-23.02);
    \draw [black] (19.736,-22.025) arc (146.19641:36.07693:28.811);
    \fill [black] (66.96,-21.09) -- (66.9,-20.15) -- (66.09,-20.74);
    \draw [black] (66.48,-25.721) arc (-47.8655:-96.2357:29.916);
    \fill [black] (66.48,-25.72) -- (65.55,-25.89) -- (66.22,-26.63);
\end{tikzpicture}
\end{center}

In class, we saw how to use DFS to find a topological ordering of the the vertices; in the graph above, the unique topological ordering is $A,B,C,D,E$. We saw an example where we happened to start DFS from the first vertex in the topological order. In this exercise we'll see what happens when we start at a different vertex. Recall that when you run DFS, if it reached a node with no children (i.e. can't go any further), then it will resume the search at an unvisited vertex.

\subsection {\Points {6}}

Run DFS starting at vertex $C$, breaking any ties by alphabetical order.\footnote {For example, if DFS has a choice between $B$ or $C$, it will always choose $B$. This includes when DFS is starting a new tree in the DFS forest.}

\begin{enumerate}[label=(\alph *)]
    \item \Points{3} What do you get when you order the vertices by \textbf {ascending} start time?
    \item \Points{3} What do you get when you order the vertices by \textbf {descending} finish time?
\end{enumerate}

\subsection {\Points {6}}

Run DFS starting at vertex $C$, breaking any ties by \textbf {reverse} alphabetical order.\footnote {For example, when DFS has a choice between $B$ or $C$, it will always choose $C$. This includes when DFS is starting a new tree in the DFS forest.}

\begin{enumerate}[label=(\alph *)]
    \item \Points{3} What do you get when you order the vertices by \textbf {ascending} start time?
    \item \Points{3} What do you get when you order the vertices by \textbf {descending} finish time?
\end{enumerate}

\Expecting {For all four questions, an ordering of vertices. No justification is required.}

\pagebreak
\section{Exercise: The World as Graphs \Points{12}}
For each of the following real-world problems, try to formulate this as a problem about graphs.

Recall a graph is just a collection of vertices and edges.

What algorithmic problem about graphs do we need to solve in order to solve the following
problems? (Note, you don't actually have to solve these problems, just transform them into
graph problems that we might then be able to solve).

\begin{enumerate}
    \item \Points{4} Among actors, a ``Bacon number'' is the number of degrees of separation from an actor to Kevin Bacon. For example, Kevin Bacon's Bacon number is $0$. If an actor works in a movie with Kevin Bacon, the actor's Bacon number is $1$. If an actor $A$ works with an actor $B$ who worked with Kevin Bacon in a movie, then actor $A$'s Bacon number is $2$, and so forth. How would you represent this as a graph so we could answer questions like: (a) What is Samuel L. Jackson's Bacon number? and (b) list all the people with Bacon number equal to 6.

    \item \Points{4} You need to take a bunch of classes at North Carolina A\&T University, and some of them depend on each other. For example, you must take COMP 280 before taking COMP 285.Given a set of classes you need to take, and information about which class is a pre-requisite for which other class, generate an order in which to take all of the classes. Assume you can only take one class at a time. How would you repesent this as a graph so we can answer this question?

    \item \Points{4} You are about to purchase a bunch of fish. You have two very large fishtanks. Unfortunately, some of these species of fish will fight if they are put in the same tank. For each pair of species, you know whether they will fight or whether they will peacefully co-exist. How would you represent this as a graph such that you can find a way to separate the fish into two peaceful fishtanks if it exists.
\end{enumerate}

\Expecting{For each of the three questions above, explain how you'd represent them as graphs. What are the nodes? What are the edges? Are the edges directed? Are they weighted?}

\pagebreak
\section{Exercise: Kosaraju’s algorithm \Points{16}}
\begin{center}
    \input{image}
\end{center}

In this question, we ask you to run Kosarju's SCC algorithm (see Lecture 18, Slide 45). We summarize the gist here:

When running DFS, we will \textbf{break any ties by alphabetical order}. Remember that if DFS has reached a node with no un-visited children (i.e. can’t go any further), then it will resume the search at an unvisited vertex. We will decide which unexplored vertex to continue DFS by \textbf{alphabetical order}.

\subsection{First DFS \Points {4}}
What are the start and finish times of each node after running the first DFS from node $C$ on the \textbf {original} graph?

\begin{tabular}{c| c| c}
Node & Start Time & Finish Time \\
\hline A & & \\
\hline B & & \\
\hline C & 0 & \\
\hline D & & \\
\hline E & & \\
\end{tabular}

\Expecting {Fill in this table}

\subsection{Second DFS \Points {6}}

Next, we will run DFS from the node with the largest finish time on the \textbf{reversed} graph. What do you get when you order the vertices by \textbf{ascending} start time for the second DFS run?

\Expecting{An ordering of vertices. No justification is required}

\subsection{Connected components \Points{6}}

What are the strongly connected component(s) in this graph?

\Expecting {A list of sets of nodes. No justification is required}

\pagebreak
\section {Most Reliable Path \Points{20}}
You are planning for an exciting trip from the nice little town $s$ to the shining new city $t$ this summer! Sadly there is no direct flight from $s$ to $t$ so you need to take multiple flights to get to $t$. You created a directed graph $G(V,E)$ where each node represents an airport and each edge represents a flight connection from one airport to another.

Each flight connects two airports, but for each flight $e$ there is some probability $p_e$ that this flight can get cancelled due to COVID-19. Each $p_e$ is some value between 0 and 1, where $0$ means that this flight will always take place, and $1$ means that this flight will always get cancelled. You have to book your flights and hotels in advance. You can assume that all $p_e$ are independent of each other and \textbf{the graph has no cycles}.

\subsection{Measure of Reliability \Points{5}}
We can measure the reliability of a path $P$ as the probability having all flights in $P$ not being cancelled (eg, all the flights succeed). Suppose you decide to take the flights along the path $P$ with $k$ flights given by $e_1 \to e_2 \to \cdots \to e_k$. How reliable is this path, using $p_{e_1}, p_{e_2}, \cdots, p_{e_k}$.\footnote{For example, say I had a path that took flights $e_1$, $e_2$, and $e_3$. Then this whole trip succeeds only if $e_1$ \textbf{and} $e_2$ \textbf{and} $e_3$ are \textbf{not} cancelled. Since the events are independent, the reliability is given by $(1-p_{e_1})(1-p_{e_2})(1-p_{e_3})$}.

\Expecting{A mathematical expression summarizing the reliability of the path $P$.}

\subsection {Just to be safe? \Points {10}}
Create an algorithm to find the most reliable path between $s$ and $t$. By most reliable path, we mean that the path $P$ you find from $s$ to $t$ should have the highest probability of having all flights in $P$ not being cancelled. Having plenty of time for the trip, you \textbf {DO NOT} care about how long or how much it takes you to travel, but you hope to reach the destination with \textbf{exactly the route you planned}.

[Hint: Recall that $\log(A \times B) = \log(A) + \log(B)$, so you can convert multiplication into addition. It might help to recall Dijstra's Algorithm (Lecture 22). Lastly, note that $\log(x)$ where $0 \leq x \leq 1$ is negative, with smaller values of $x$ giving more negative results.]

\Expecting{Pseudocode or a very clear English description of your algorithm, an informal justification that your algorithm is correct, and brief analysis of running time}.

\subsection {Ethics \Points {5}} Imagine that, based on the excellent results of your flight planning algorithm, you have been hired by an online travel agency to give your expert advice on their route recommendation strategies. You discover that, by maximizing the reliability of suggested routes, the company would be overlooking other considerations such as carbon and time efficiency. Write a one-paragraph memo to the programming team explaining why these (or other) values should be taken into account.

\Expecting{two to four sentences explaining (1) what other measures are overlooked by an algorithm that maximizes reliability; and (2) the reasons why the airline should care about these measures, including some potential consequences of the omission}

\pagebreak
\section{Summer Camp\Points{10}}
Some North Carolina A\&T bulldogs decided to hold a summer camp event at the Hanging Rock State Park. The camping ground is divided into an $m \times n$ grid, where each cell can accommodate one standard-sized tent. The North Carolina A\&T bulldogs put up their tents connected on the grid (considering four cardinal directions). The following day, they discover another connected collection of tents put up by bulldogs from UNCG. Two groups of bulldogs are willing to make friends with each other and connect their living places into a single collection by putting up some spare tents. Please help design an $O(mn)$ algorithm that calculates the \textbf{minimum number of spare tents} that the bulldogs need to put up.

\paragraph {Example of camping ground layout}

If run your algorithm on this, the output should be 2 (We need to put up at least 2 tents to connect the two groups of tents).

\begin{figure}[H]
    \centering
    \includegraphics[scale=0.5]{tent.png}
    \label{fig:my_label}
\end{figure}

\textbf{Input}: A nested array $A$ of $m \times n$ where 1 represents a tent and 0 represents an empty camping ground.

[hint: You might want to locate all tents in one connected collection of tent first.]

\Expecting{Pseudocode of the algorithm or a clear English description, \textbf{the algorithm should run in O(mn) time.}}

\pagebreak
\begin{Instruction}

\paragraph{Coding Problems} The following questions are to be submitted as a ".zip" file on Gradescope.

\end{Instruction}

\section{Coding \Points{50}}
After completing the written portion of the assignment, you should submit it to \href{https://www.gradescope.com/courses/350304}{Gradescope}.

For the coding portion, get your starter \href{https://replit.com/team/COMP285/HW6-Code}{C++ code} or \href{https://replit.com/team/COMP285/HW6-Code-Python}{Python code}.

Note that the starter code also include a few test cases you can run on repl.it. However, the full test suite is the one run on Gradescope.

Please reference the \texttt{README.md} included in your starter code for detailed instructions.

\section*{Submitting the Assignment}

This assignment is a combination of written and programming questions. Both portions of the assignment should be submitted through \href{https://www.gradescope.com/courses/350304}{Gradescope}.

The "Homework 6: Fun with Graphs" assignment is the written portion, for which you should submit a \textbf{typed} response to the non-coding questions (questions 1-\ref{sec:last}). Each response should clearly be marked with its corresponding number. You are free to use the provided templates, print the questions and write your answers, or to simply type your responses on a blank document (whatever works for you).

The "Homework 6: Coding" is the programming portion of the assignment. For this portion, download the ".zip" file from replit and upload this ".zip" file as your answer to \href{https://www.gradescope.com/courses/350304}{Gradescope}. You can upload the assignment as many times as you want.

\end{document}


%%
%% Automatically generated file from DocOnce source
%% (https://github.com/hplgit/doconce/)
%%
%%

%-------------------- begin preamble ----------------------

\documentclass[%
oneside,                 % oneside: electronic viewing, twoside: printing
final,                   % draft: marks overfull hboxes, figures with paths
10pt]{article}

\listfiles               %  print all files needed to compile this document

\usepackage{relsize,makeidx,color,setspace,amsmath,amsfonts,amssymb}
\usepackage[table]{xcolor}
\usepackage{bm,ltablex,microtype}

\usepackage[pdftex]{graphicx}

\usepackage[T1]{fontenc}
%\usepackage[latin1]{inputenc}
\usepackage{ucs}
\usepackage[utf8x]{inputenc}

\usepackage{lmodern}         % Latin Modern fonts derived from Computer Modern

% Hyperlinks in PDF:
\definecolor{linkcolor}{rgb}{0,0,0.4}
\usepackage{hyperref}
\hypersetup{
    breaklinks=true,
    colorlinks=true,
    linkcolor=linkcolor,
    urlcolor=linkcolor,
    citecolor=black,
    filecolor=black,
    %filecolor=blue,
    pdfmenubar=true,
    pdftoolbar=true,
    bookmarksdepth=3   % Uncomment (and tweak) for PDF bookmarks with more levels than the TOC
    }
%\hyperbaseurl{}   % hyperlinks are relative to this root

\setcounter{tocdepth}{2}  % levels in table of contents

% --- fancyhdr package for fancy headers ---
\usepackage{fancyhdr}
\fancyhf{} % sets both header and footer to nothing
\renewcommand{\headrulewidth}{0pt}
\fancyfoot[LE,RO]{\thepage}
% Ensure copyright on titlepage (article style) and chapter pages (book style)
\fancypagestyle{plain}{
  \fancyhf{}
  \fancyfoot[C]{{\footnotesize \copyright\ 1999-2020, "Data Analysis and Machine Learning FYS-STK3155/FYS4155":"http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html". Released under CC Attribution-NonCommercial 4.0 license}}
%  \renewcommand{\footrulewidth}{0mm}
  \renewcommand{\headrulewidth}{0mm}
}
% Ensure copyright on titlepages with \thispagestyle{empty}
\fancypagestyle{empty}{
  \fancyhf{}
  \fancyfoot[C]{{\footnotesize \copyright\ 1999-2020, "Data Analysis and Machine Learning FYS-STK3155/FYS4155":"http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html". Released under CC Attribution-NonCommercial 4.0 license}}
  \renewcommand{\footrulewidth}{0mm}
  \renewcommand{\headrulewidth}{0mm}
}

\pagestyle{fancy}

% prevent orhpans and widows
\clubpenalty = 10000
\widowpenalty = 10000

% --- end of standard preamble for documents ---

% insert custom LaTeX commands...

\raggedbottom
\makeindex
\usepackage[totoc]{idxlayout}   % for index in the toc
\usepackage[nottoc]{tocbibind}  % for references/bibliography in the toc

%-------------------- end preamble ----------------------

\begin{document}

% matching end for #ifdef PREAMBLE

\newcommand{\exercisesection}[1]{\subsection*{#1}}

% ------------------- main content ----------------------

% ----------------- title -------------------------

\thispagestyle{empty}

\begin{center}
{\LARGE\bf
\begin{spacing}{1.25}
Project 2 on Machine Learning, deadline November 13 (Midnight)
\end{spacing}
}
\end{center}

% ----------------- author(s) -------------------------

\begin{center}
{\bf \href{{http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html}}{Data Analysis and Machine Learning FYS-STK3155/FYS4155}}
\end{center}

    \begin{center}
% List of all institutions:
\centerline{{\small Department of Physics, University of Oslo, Norway}}
\end{center}

% ----------------- end author(s) -------------------------

% --- begin date ---
\begin{center}
Nov 4, 2020
\end{center}
% --- end date ---

\vspace{1cm}

\subsection*{Classification and Regression, from linear and logistic regression to neural networks}

The main aim of this project is to study both classification and
regression problems by developing our own feed-forward neural network (FFNN) code. We can reuse the regression algorithms studied
in project 1. We will also include logistic regression for classification
problems and write our own FFNN code for studying
both regression and classification problems.  The codes developed in
project 1, including bootstrap and/or cross-validation as well as the
computation of the mean-squared error and/or the $R2$ or the accuracy score (classification problems) functions can
also be utilized in the present analysis.

The data sets that we propose here are (the default sets)

\begin{itemize}
\item Regression (fitting a continuous function). In this part you will need to bring back your results from project 1 and compare these with what you get from your Neural Network code to be developed here. The data sets could be
\begin{enumerate}

 \item Either the Franke function or the terrain data from project 1, or data sets your propose.

\end{enumerate}

\noindent
\item Classification. Here you will also need to develop a Logistic regression code that you will use to compare with the Neural Network code. The data set we propose are the so-called \href{{https://en.wikipedia.org/wiki/MNIST_database}}{MNIST} data set of images representing hand-written numbers from zero to nine. These are discussed intensively in the lecture notes on neural networks, see for example the slides from \href{{https://compphysics.github.io/MachineLearning/doc/pub/week41/html/week41.html}}{week 41}
\end{itemize}

\noindent
However, if you would like to study other data sets, feel free to
propose other sets. What we listed here are mere suggestions from our
side. If you opt for another data set, consider using a set which
has been studied in the scientific literature. This makes it easier
for you to compare and analyze your results. Comparing with existing results from the scientific literature  is also an essential
element of the scientific discussion.

In particular, when developing your own Neural Network and Logistic Regression codes for classification problems, the so-called Wisconsin Cancer data (which is a binary problem, benign or malignant tumors) may be studied. You can find more information about this at the \href{{https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_breast_cancer.html}}{Scikit-Learn site} or at the \href{{https://archive.ics.uci.edu/ml/datasets/breast+cancer+wisconsin+(original)}}{University of California at Irvine}.

We will start with a regression problem and we will reuse our codes from project 1 starting with writing our own Stochastic Gradient Descent (SGD) code.

\paragraph{Part a): Write your own Stochastic Gradient Descent  code, first step.}
In order to get started, we will now replace in our standard ordinary
least squares (OLS) and Ridge regression codes (from project 1) the matrix inversion
algorithm with our own SGD code. You can choose whether you want to
add the momentum SGD optionality or other SGD variants such as RMSprop
or ADAgrad. The lecture notes from \href{{https://compphysics.github.io/MachineLearning/doc/pub/week40/html/week40.html}}{week 40 contain more
details}

Perform an analysis of the results for OLS and Ridge regression as
function of the chosen learning rates, the number of mini-batches and
epochs as well as algorithm for scaling the learning rate. You can
also compare your own results with those that can be obtained using
for example \textbf{Scikit-Learn}'s various SGD options.  Discuss your
results. For Ridge regression you need now to study the results as functions of  the hyper-parameter $\lambda$ and
the learning rate $\gamma$.  Discuss your results.

You will need your SGD code for the setup of the Neural Network and Logistic Regression codes.

\paragraph{Part b): Writing your own Neural Network code.}
Your aim now, and this is the central part of this project, is to
write your own Feed Forward Neural Network  code implementing the back
propagation algorithm discussed in the lecture slides from \href{{https://compphysics.github.io/MachineLearning/doc/pub/week41/html/week41.html}}{week 41}.

We will focus on a regression problem first and study either the
Franke function or terrain data (or both or other data sets) from
project 1.  Discuss again your choice of cost function.

Write an FFNN code for regression with a flexible number of hidden
layers and nodes using the Sigmoid function as activation function for
the hidden layers. Initialize the weights using a normal
distribution. How would you initialize the biases? And which
activation function would you select for the final output layer?

Train your network and compare the results with those from your OLS and Ridge Regression codes from project 1.
You should test your results against a similar code using \textbf{Scikit-Learn} (see the examples in the above lecture notes from week 41) or \textbf{tensorflow/keras}.

Comment your results and give a critical discussion of the results
obtained with the Linear  Regression code and your own Neural Network
code.  Compare the results with those from project 1.
Make an analysis of the regularization parameters and the learning rates employed to find the optimal MSE and $R2$ scores.

A useful reference on the back progagation algorithm is \href{{http://neuralnetworksanddeeplearning.com/}}{Nielsen's
book}. It is an excellent
read.

\paragraph{Part c): Testing different activation functions.}
You should now also test different activation functions for the hidden layers. Try out the Sigmoid, the RELU and the Leaky RELU functions and discuss your results. You may also study the way you initialize your weights and biases.

\paragraph{Part d): Classification  analysis using neural networks.}
With a well-written code it should now be easy to change the
activation function for the output layer.

Here we will change the cost function for our neural network code
developed in parts b) and c) in order to perform a classification analysis.

We will here study the MNIST data set of hand-written numbers as
discussed in the lecture notes from \href{{https://compphysics.github.io/MachineLearning/doc/pub/week41/html/week41.html}}{week
41}. Use
the \textbf{Softmax} function as activation function. Your code should
however also be able to use a binary activation function as well.

To measure the performance of our classification problem we use the
so-called \emph{accuracy} score.  The accuracy is as you would expect just
the number of correctly guessed targets $t_i$ divided by the total
number of targets, that is

\[
\text{Accuracy} = \frac{\sum_{i=1}^n I(t_i = y_i)}{n} ,
\]

where $I$ is the indicator function, $1$ if $t_i = y_i$ and $0$
otherwise if we have a binary classification problem. Here $t_i$
represents the target and $y_i$ the outputs of your FFNN code and $n$ is simply the number of targets $t_i$.

Discuss your results and give a critical analysis of the various parameters, including hyper-parameters like the learning rates and the regularization parameter $\lambda$ (as you did in Ridge Regression), various activation functions, number of hidden layers and nodes and activation functions.

As stated in the introduction, it can also be useful to study other
datasets. In particular, the so-called Wisconsin Cancer
data (which is a binary problem, benign or malignant tumors) may be
studied. You find more information about this at the \href{{https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_breast_cancer.html}}{Scikit-Learn
site} or at the \href{{https://archive.ics.uci.edu/ml/datasets/breast+cancer+wisconsin+(original)}}{University of California
at Irvine}.

Again, we strongly recommend that you compare your own neural Network
code for classification and pertinent results against a similar code using \textbf{Scikit-Learn}  or \textbf{tensorflow/keras} or \textbf{pytorch}.

\paragraph{Part e): Write your Logistic Regression code, final step.}
Finally, we want to compare the FFNN code we have developed with
Logistic regression, that is we wish to compare our neural network
classification results with the results we can obtain with another
method.

Define your cost function and the design matrix before you start writing your code.
Write thereafter a Logistic regression code using your SGD algorithm. Study the results as functions of the chosen learning rates.
Add also an $l_2$ regularization parameter $\lambda$. Compare your results with those from your FFNN code as well as those obtained using \textbf{Scikit-Learn}'s logistic regression functionality.

The weblink  here \href{{https://medium.com/ai-in-plain-english/comparison-between-logistic-regression-and-neural-networks-in-classifying-digits-dc5e85cd93c3}}{\nolinkurl{https://medium.com/ai-in-plain-english/comparison-between-logistic-regression-and-neural-networks-in-classifying-digits-dc5e85cd93c3}}compares logistic regression and FFNN using the MNIST data set. You may find several useful hints and ideas from this article.

\paragraph{Part f) Critical evaluation of the various algorithms.}
After all these glorious calculations, you should now summarize the
various algorithms and come with a critical evaluation of their pros
and cons. Which algorithm works best for the regression case and which
is best for the classification case. These codes can also be part of
your final project 3, but now applied to other data sets.

\subsection*{Background literature}

\begin{enumerate}
\item The text of Michael Nielsen is highly recommended, see \href{{http://neuralnetworksanddeeplearning.com/}}{Nielsen's book}. It is an excellent read.

\item The textbook of \href{{https://www.springer.com/gp/book/9780387848570}}{Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer}, chapters 3 and 7 are the most relevant ones for the analysis here.

\item \href{{https://arxiv.org/abs/1803.08823}}{Mehta et al, arXiv 1803.08823}, \emph{A high-bias, low-variance introduction to Machine Learning for physicists}, ArXiv:1803.08823.
\end{enumerate}

\noindent
\subsection*{Introduction to numerical projects}

Here follows a brief recipe and recommendation on how to write a report for each
project.

\begin{itemize}
  \item Give a short description of the nature of the problem and the eventual  numerical methods you have used.

  \item Describe the algorithm you have used and/or developed. Here you may find it convenient to use pseudocoding. In many cases you can describe the algorithm in the program itself.

  \item Include the source code of your program. Comment your program properly.

  \item If possible, try to find analytic solutions, or known limits in order to test your program when developing the code.

  \item Include your results either in figure form or in a table. Remember to        label your results. All tables and figures should have relevant captions        and labels on the axes.

  \item Try to evaluate the reliabilty and numerical stability/precision of your results. If possible, include a qualitative and/or quantitative discussion of the numerical stability, eventual loss of precision etc.

  \item Try to give an interpretation of you results in your answers to  the problems.

  \item Critique: if possible include your comments and reflections about the  exercise, whether you felt you learnt something, ideas for improvements and  other thoughts you've made when solving the exercise. We wish to keep this course at the interactive level and your comments can help us improve it.

  \item Try to establish a practice where you log your work at the  computerlab. You may find such a logbook very handy at later stages in your work, especially when you don't properly remember  what a previous test version  of your program did. Here you could also record  the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning.
\end{itemize}

\noindent
\subsection*{Format for electronic delivery of report and programs}

The preferred format for the report is a PDF file. You can also use DOC or postscript formats or as an ipython notebook file.  As programming language we prefer that you choose between C/C++, Fortran2008 or Python. The following prescription should be followed when preparing the report:

\begin{itemize}
  \item Use Canvas to hand in your projects, log in  at  \href{{https://www.uio.no/english/services/it/education/canvas/}}{\nolinkurl{https://www.uio.no/english/services/it/education/canvas/}} with your normal UiO username and password.

  \item Upload \textbf{only} the report file or the link to your GitHub/GitLab or similar typo of  repos!  For the source code file(s) you have developed please provide us with your link to your GitHub/GitLab or similar  domain.  The report file should include all of your discussions and a list of the codes you have developed.  Do not include library files which are available at the course homepage, unless you have made specific changes to them.

  \item In your GitHub/GitLab or similar repository, please include a folder which contains selected results. These can be in the form of output from your code for a selected set of runs and input parameters.
\end{itemize}

\noindent
Finally,
we encourage you to collaborate. Optimal working groups consist of
2-3 students. You can then hand in a common report.

\subsection*{Software and needed installations}

If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages,
we recommend that you install the following Python packages via \textbf{pip} as
\begin{enumerate}
\item pip install numpy scipy matplotlib ipython scikit-learn tensorflow sympy pandas pillow
\end{enumerate}

\noindent
For Python3, replace \textbf{pip} with \textbf{pip3}.

See below for a discussion of \textbf{tensorflow} and \textbf{scikit-learn}.

For OSX users we recommend also, after having installed Xcode, to install \textbf{brew}. Brew allows
for a seamless installation of additional software via for example
\begin{enumerate}
\item brew install python3
\end{enumerate}

\noindent
For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution
you can use \textbf{pip} as well and simply install Python as
\begin{enumerate}
\item sudo apt-get install python3  (or python for python2.7)
\end{enumerate}

\noindent
etc etc.

If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up  all relevant dependencies for Python, namely
\begin{enumerate}
\item \href{{https://docs.anaconda.com/}}{Anaconda} Anaconda is an open source distribution of the Python and R programming languages for large-scale data processing, predictive analytics, and scientific computing, that aims to simplify package management and deployment. Package versions are managed by the package management system \textbf{conda}

\item \href{{https://www.enthought.com/product/canopy/}}{Enthought canopy}  is a Python distribution for scientific and analytic computing distribution and analysis environment, available for free and under a commercial license.
\end{enumerate}

\noindent
Popular software packages written in Python for ML are

\begin{itemize}
\item \href{{http://scikit-learn.org/stable/}}{Scikit-learn},

\item \href{{https://www.tensorflow.org/}}{Tensorflow},

\item \href{{http://pytorch.org/}}{PyTorch} and

\item \href{{https://keras.io/}}{Keras}.
\end{itemize}

\noindent
These are all freely available at their respective GitHub sites. They
encompass communities of developers in the thousands or more. And the number
of code developers and contributors keeps increasing.

% ------------------- end of main content ---------------

\end{document}


\documentclass[a4paper]{article}
\usepackage[a4paper,margin=25mm]{geometry}
\usepackage{graphicx,subcaption}
\usepackage{amsmath,amsfonts}
\usepackage[ruled,vlined]{algorithm2e}
\usepackage{tikz}
\usepackage{accents}
\usepackage{cite}
\newcommand{\rvec}[1]{\accentset{\rightarrow}{#1}}
\title{Notes on Hierarchical Softmax}
\author{G.A. Jarrad}
\begin{document}
\maketitle
\numberwithin{equation}{section}
\numberwithin{figure}{section}
\numberwithin{table}{section}

\section{Introduction}

In the context of natural language processing, it is common to maintain a list ${\cal V}$ of vocabulary words, which is usually
extracted from one or more training corpora of text documents that have been suitably tokenised.
Typically, the frequency of each token is observed, and the top $V=|{\cal V}|$ most common tokens form the vocabulary.
The vocabulary ${\cal V}$ is typically ordered, either lexicographically or by descending frequency.

When it comes to representing a given word token, $w_i\in{\cal V}$,
it can be uniquely indexed by its position $i$ in the ordered list.
Thus, for the example vocabulary ${\cal V} = \{{\tt cat},{\tt dog},{\tt frog},{\tt mouse}\}$,
we might choose $w_2=\texttt{dog}$ and $w_4=\texttt{mouse}$, etc.
Alternatively, one can form a {\em 1-of-$V$} or {\em one-hot} vector containing $V-1$ zeros and a single one in the $i$-th
element.
For example, $\texttt{repr}(\texttt{cat})=(1,0,0,0)$ and $\texttt{repr}(\texttt{frog})=(0,0,1,0)$, etc.
This latter representation is of fixed length $V$ ---
the question is, can we form a shorter, but still unique, vector representation?

The answer is, yes we can. To do so, we first need to hierarchically partition the vocabulary ${\cal V}$ into
a binary tree representation, which we shall call ${\cal T}_{\cal V}$.
One way to do this is to simply partition ${\cal V}$ into two roughly equally sized lists, and then
recursively partition each list until only single tokens remain in each innermost list.
Alternatively, we can utilise the word frequency $f_i$ of each token $w_i$ to produce the probability,
$p_i=f_i/\sum_{i'=1}^V f_{i'}$, that a word randomly sampled from the training corpora matches $w_i$.
Using these probabilities, one can then partition ${\cal V}$ into nested groups, e.g.\ according to
either Huffman or Fano coding (the latter method being simpler but producing less optimal code lengths).
Morin and Bengio\cite{Morin+Bengio-2005} indicate that the mean path length when taking word frequencies into account
is typically smaller than that for trees balanced equally across all words.
Alternatively, Mnih and Hinton~\cite{NIPS2008_3583} discuss some feature-driven algorithms for constructing
binary vocabulary trees.
Figure~\ref{fig:catdog} shows a simple (but unbalanced) binary tree representation of the example vocabulary.
\begin{figure}[hbt]
\centering
\begin{tikzpicture}[cnode/.style={draw,circle,minimum size=3em,inner sep=3pt}, rnode/.style={draw,rectangle,minimum size=3em,inner sep=3pt}]
    \node[cnode] (0) at (4,0) {$\eta_0$};
    \node[cnode] (1) at (2,-2) {$\eta_1$};
    \node[rnode] (2) at (6,-2) {\texttt{mouse}};
    \node[rnode] (3) at (0,-4) {\texttt{cat}};
    \node[cnode] (4) at (4,-4) {$\eta_2$};
    \node[rnode] (5) at (2,-6) {\texttt{dog}};
    \node[rnode] (6) at (6,-6) {\texttt{frog}};

    \draw[->] (0) edge node [pos=0.5, above] {\hspace*{-10mm}$b_1=0$} (1) ;
    \draw[->] (0) edge node [pos=0.5, above] {\hspace*{10mm}$b_1=1$} (2) ;
    \draw[->] (1) edge node [pos=0.5, above] {\hspace*{-10mm}$b_2=0$} (3) ;
    \draw[->] (1) edge node [pos=0.5, above] {\hspace*{10mm}$b_2=1$} (4) ;
    \draw[->] (4) edge node [pos=0.5, above] {\hspace*{-10mm}$b_3=0$} (5) ;
    \draw[->] (4) edge node [pos=0.5, above] {\hspace*{10mm}$b_3=1$} (6) ;
\end{tikzpicture}
\caption{\em An example of an unbalanced binary tree representation of a simple vocabulary.}
\label{fig:catdog}
\end{figure}

Note that the binary tree ${\cal T}_{\cal V}$ will contain $V$ leaf nodes, corresponding to the vocabulary words, and
$V-1$ internal nodes, corresponding to binary decision points.
%We denote the set of leaf nodes by ${\cal N}_{\cal V}$,
%and the residual tree of purely internal nodes by ${\cal T}'_{\cal V}={\cal T}_{\cal V}\backslash {\cal N}_{\cal V}$.
Additionally, the tree will have a maximum of $L_{\cal V}$ levels, where
$L_{\cal V}\ge 1+\lceil\log_2 V\rceil$ (with equality holding if the tree is balanced).
Thus, any path $(\eta_{i_1},\eta_{i_2},\ldots,\eta_{i_n})$
from the root node $\eta_{i_1}\equiv\eta_{0}$ to a leaf node $\eta_{i_n}$ will pass through $n \le L_{\cal V}$ nodes.
This fact gives use scope for reducing the size of the unique word representation vectors.

It can be seen from Figure~\ref{fig:catdog} that each path through ${\cal T}_{\cal V}$
from the root node towards any leaf node
can be uniquely determined by specifying the
sequence of binary branching decisions (0 for left, 1 for right) made along the path.
Thus, in this example we now have $\texttt{repr}(\texttt{cat})=(0,0)$,
$\texttt{repr}(\texttt{dog})=(0,1,0)$, $\texttt{repr}(\texttt{frog})=(0,1,1)$
and $\texttt{repr}(\texttt{mouse})=(1)$.
These binary sequences collectively form the hierarchical representation of this vocabulary.
Note that, for our simple example, the maximum size of the representation vector is three, whereas it would be two for a balanced tree,
in contrast to the fixed size of four for the 1-of-$V$ representation.
In general, the size is reduced from $V$ to about $\log_2 V$ by the binary partitioning.

\section{Probability Model}

A typical natural language problem is to determine which word $w\in{\cal V}$ is appropriate in some context
\textbf{x}. In probabilistic terms, this amounts to computing $p(w\;|\;\textbf{x})\doteq p(\texttt{repr}(w)\;|\;\textbf{x})$.
Here we assume that we have some binary tree representation ${\cal T}_{\cal V}$ of the vocabulary ${\cal V}$,
such that $\texttt{repr}(w)=(b_1,b_2,\ldots,b_n)$.
In order to decompose this probabilistic model, first reconsider Figure~\ref{fig:catdog}.
Recalling that $\texttt{repr}(\texttt{frog})=(0,1,1)$, we see that
\begin{eqnarray}
p(\texttt{frog}\;|\;\textbf{x})
& = & p(b_1=0,b_2=1,b_3=1\;|\;\textbf{x})
\nonumber\\
& = & p(b_1=0\;|\;\textbf{x})\,p(b_2=1\;|\;\textbf{x},b_1=0)\,
p(b_3=1\;|\;\textbf{x},b_1=0,b_2=1)
\,.\nonumber
\end{eqnarray}
To make this more general, let $\rvec{\rho}_{1:j}=(b_1, b_2, \cdots, b_j)$ for $j=1,2,\ldots,n$,
where each binary decision $b_j$ takes its corresponding value from the $j$-th element of $\texttt{repr}(w)$.
Then we see that
\begin{eqnarray}
p(w\;|\;\textbf{x})
& = & \prod_{j=1}^n p(b_j\;|\;\textbf{x},\rvec{\rho}_{1:j-1})\,,
\label{eq:p:word}
\end{eqnarray}
where $\rvec{\rho}_{1:n}=\texttt{repr}(w)$, and $\rvec{\rho}_{1:0}=()$ by definition.

Notionally, each subpath, specified by $\rvec{\rho}_{1:j}$ for $j<n$,
terminates at some unique, internal node $\eta_k$ in ${\cal T}_{\cal V}$, indexed by
$k=\iota(\rvec{\rho}_{1:j})$. By definition, $\iota(())=0$, so that we always start from the root node $\eta_0$.
Consequently, we allow each internal node $\eta_k$ to have parameters ${\bf\theta}_k$.
Furthermore, we suppose that composing the context $\textbf{x}$ with node $\eta_k$
results in local features ${\bf\phi}_k({\bf x})={\bf\phi}({\bf x},{\bf\theta}_k)$.
Hence, each factor of equation~\eqref{eq:p:word} becomes
\begin{eqnarray}
p(b_j\;|\;\textbf{x},\rvec{\rho}_{1:j-1}) & \doteq &
p(b_j\;|\;{\bf\phi}_{k}({\bf x}),k=\iota(\rvec{\rho}_{1:j-1}))
\,,
\label{eq:p:word:term}
\end{eqnarray}
for $j=1,2,\ldots,n$ with $n=|\texttt{repr}(w)|$.

\subsection{Logistic Model}

In a neural network framework, the probability terms comprising equation~\eqref{eq:p:word} would represent an output layer
consisting of, for each internal node of the tree ${\cal T}_{\cal V}$, an activation function of an affine transformation of the input $\textbf{x}$.
Here we choose the logistic sigma activation function $\sigma(x)=1/(1+\exp{(-x)})$, and thus suppose that the local features of node $\eta_k$
are given by ${\bf\phi}_k({\bf x})=\alpha_k + {\bf \beta}_k^{T}{\bf x}$.
Hence, we take the probability of the $j$-th binary decision $b_j$ along a specified path $\rvec{\rho}_{1:n}$ to be
\begin{eqnarray}
p(b_j\;|\;{\bf x},\rvec{\rho}_{1:j-1}) & \doteq & \sigma((2b_j-1)(\alpha_k+{\bf \beta}_k^{T}{\bf x}))\,,
\end{eqnarray}
from equation~\eqref{eq:p:word:term}. We have made use of the fact that $1-\sigma(x)=\sigma(-x)$.

Note that the scalar output of the neural network for node $\eta_k$ must represent one of either $p(b_j=0\;|\;\cdots)$ or
$p(b_j=1\;|\;\cdots)$. It is common to choose the former; however, we choose the latter here, since during training of the neural network
one would optimise the parameters to minimise the `distance' between $p(b_j\;|\;\cdots)$ and the known value of $b_j$.
In other words, we desire $p(b_j=1\;|\;\cdots)\approx 1$ when $b_j=1$, and $p(b_j=1\;|\;\cdots)\approx 0$ when $b_j=0$.
Hence, each hierarchical vector representation exactly specifies the desired output probabilities.

\subsection{Optimal Path}

In order to find the (or a) word $w^*$ in the vocabulary that best matches the context ${\bf x}$, it would appear on the surface
that we need to evaluate $p(w\;|\;\textbf{x})$ for every $w\in{\cal V}$. However, we can do better. Note that:
\begin{itemize}
\item The probability factors $p(b_j\;|\;\textbf{x},\rvec{\rho}_{1:j-1})$ are available at every internal node of the binary tree ${\cal T}_{\cal V}$,
e.g.\ via one computational pass of the context ${\bf x}$ through a neural network;

\item The tree is in fact a directed acyclic graph, which allows us to apply a graph search algorithm.
\end{itemize}
Consequently, we can apply the $A^*$ search algorithm (albeit without a heuristic function).

To do so, we start with an empty {\em chart} (or {\em closed set}) ${\cal C}$ that will contain evaluated subpaths,
and an empty {\em priority queue} (or {\em open set}) ${\cal Q}$ that will contain subpaths to be evaluated.
We then insert the empty subpath $()$ onto ${\cal Q}$ with its default score.
Then repeatedly, while ${\cal Q}$ is not empty, we remove the best scoring subpath and: (i) if it terminates at a leaf node, return the word $w$;
or else (ii) insert the subpath into ${\cal C}$, evaluate the two binary decisions ($b=0$ and $b=1$), and insert each new subpath with its corresponding score onto ${\cal Q}$.
Pseudo-code for this method is given by Algorithm~\ref{alg:astar}.
Note that, based on the implementation, the chart ${\cal C}$ might or might not be required (depending upon whether or not it is used to reconstruct the optimal path via backtracking);
here we show a chartless version.
\begin{algorithm}[hbt]
 \SetAlgoLined
 \SetKwInOut{Input}{Input}\SetKwInOut{Output}{Output}
 \Input{Binary vocabulary tree ${\cal T}_{\cal V}$; word context ${\bf x}$}
 \Output{Word $w$; score $s$; path $\rho$}
 \BlankLine
 ${\cal Q}\leftarrow [\;]$\tcp*[r]{Initialise queue}
 $\eta\leftarrow\textbf{get\_root}({\cal T}_{\cal V})$\tcp*[r]{Obtain root node}
 $\nu\leftarrow\textbf{make\_node}(\texttt{node}=\eta, \texttt{path}=(),\texttt{score}=1)$\tcp*[r]{Construct score node}
 $Q\leftarrow\textbf{insert\_node}({\cal Q}, \nu)$\tcp*[r]{Insert score node}
 \While{$|{\cal Q}|>0$}{
  $({\cal Q}, \nu)\leftarrow\textbf{pop\_node}({\cal Q})$\tcp*[r]{Obtain highest-scoring node}
  $s\leftarrow\textbf{get\_value}(\nu, \texttt{score})$\tcp*[r]{Obtain score}
  $\rho\leftarrow\textbf{get\_value}(\nu, \texttt{path})$\tcp*[r]{Obtain path}
  $\eta\leftarrow\textbf{get\_value}(\nu, \texttt{node})$\tcp*{Obtain tree node}
  \eIf{$\textbf{is\_leaf}(\eta)$}{
   $w\leftarrow\textbf{get\_word}(\eta)$\tcp*[r]{Obtain word}
   \KwRet{$(w,s,\rho)$}\;
  }{
   $p\leftarrow\textbf{calc\_prob}(\eta, {\bf x})$\tcp*[r]{Compute probability of $b=1$}
   $s'\leftarrow[s(1-p),sp]$\tcp*[r]{Construct scores}
   \For{$b\in\{0,1\}$}{
    $\eta'\leftarrow\textbf{get\_child}(\eta, b)$\tcp*[r]{Obtain child node}
    $\rho'\leftarrow\rho\oplus(b)$\tcp*[r]{Construct child path}
    $\nu'\leftarrow\textbf{make\_node}(\texttt{node}=\eta', \texttt{path}=\rho', \texttt{score}=s'[b])$\tcp*[r]{Construct score node}
    $Q\leftarrow\textbf{insert\_node}({\cal Q}, \nu')$\tcp*[r]{Insert score node}
   }
  }
 }
 \caption{Find word with highest probability}
 \label{alg:astar}
\end{algorithm}

Note that this algorithm can easily be adapted to produce the top $N$ most probable words.
Also note that, in practice, it is better to sum (negative) log-probability scores rather than directly multiply probabilities,
otherwise for large vocabulary trees we run into the problem of underflow of vanishingly small probabilities.
There are in fact many more subtleties to implementing the $A^*$ algorithm, including: how to build the priority queue;
how to insert a node ranked by score;
where to insert a node into a batch of equally-scored nodes; etc. These issues will not be addressed here.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\bibliography{hierarchical-softmax}{}
\bibliographystyle{plain}

\end{document}