\documentclass{article}
\usepackage{fullpage}
\usepackage{nopageno}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{enumerate}
\allowdisplaybreaks

\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}

\begin{document}
\title{Notes}
\date{April 2, 2014}
\maketitle
\section*{lesson 19 (bc)}
\section*{lesson 20 solution by separation of variables}
\begin{align*}
  \text{PDE}&&u_{tt}&=c^2u_{xx}&0<&x<L&0<&t<\infty\\
  \text{BC}&&&&x=0,&x=L\\
  \text{IC}&&u(x,0)&=f(x)\\
  &&u_t(x,0)&=g(x)
\end{align*}
\section*{19}
names book gives for boundaries
\begin{enumerate}
\item
controlled endpoints
\begin{align*}
  u(0,t)&=g_1(t)\\
  u(L,t)&=g_2(t)
\end{align*}
1st kind, dirichlet
\item
force at boundaries
\begin{align*}
  u_x(0,t)&=g_1(t)\\
  u_x(L,t)&=g_2(t)
\end{align*}
snd kind newmann
\item
elastic attachement
\begin{align*}
  u_x(0,t)+cu(0,t)&=g_1(t)\\
  u_x(L,t)+cu(L,t)&=g_2(t)
\end{align*}
3rd kind, mixed or robin
\end{enumerate}
using terminology like transverse, longitudinal and torsional waves.
Transverse waves vibrate perpindicular to reference axis or direction of motion. Like a jump rope.
Longitudinal waves vibrate parallel to reference axis/direction of motion. Like a slinky.
Torsional waves represent rotational vibrations about reference axis or direction of motion.

\subsubsection*{2nd kind}
recall that vertical force$=T\cdot\sin(\theta)=T\cdot\frac{\tan(\theta)}{\sec(\theta)}=T\cdot\frac{x_x}{\sqrt{1+{u_x}^2}}$. Zero vertical force at enpoint means $T\cdot\frac{x_x}{\sqrt{1+{u_x}^2}}=0$ or $u_x=0$. So frictionless endpoints. When there is an applied force on the endpoints, then the derivative picks up something.

Note that in longitudinal displacement (slinky example) maximum displacement is at the top, because that is where the most force is. as you go down, less force, less displacement.

\subsubsection*{3rd kind}
page 150. Still frictionless, but elastic attachment.

vertical force of spring is $-hl\sin(\theta)=-hl_0\frac{\sin(\theta)}{\cos(\theta)}=-hu=T\frac{u_x}{\sqrt{1+{u_x}^2}}$. Assum $\sqrt{1+{u_x}^2}\approx 1$ then $Tu_x=-hu$. Note that $l$ is the length of the little spring contraption and $l_0$ is the initial (short) length.
\section*{20}
separation of variables
\begin{align*}
  u&=X(x)T(t)\\
  X(x)T''(t)&=c^2X''(x)T(t)\\
  \frac{T''(t)}{c^2T(t)}&=\frac{X''(x)}{X(x)}=\lambda\\
  \intertext{$\lambda$ is a constant, separation constant}
  -\infty&<\lambda<\infty\\
  T''-c^2\lambda T&=0,\quad X''-\lambda X=0\\
  0&<t<\infty\quad 0<x<L\\
  \frac{\mathrm{d}^2y}{\mathrm{d}x^2}-\mu^2&=0\quad e^{\mu x},e^{-\mu x}\\
  \frac{\mathrm{d}^2y}{\mathrm{d}x^2}+0&=0\quad 1,x\\
  \frac{\mathrm{d}^2y}{\mathrm{d}x^2}+\mu^2&=0\quad \cos(\mu x),\sin(\mu x)\\
\end{align*}
cases are
\begin{enumerate}
\item
$\lambda=\mu^2$ positive
\item
$\lambda=0$
\item
$\lambda=-\mu^2$ negative
\end{enumerate}
earlier, $\lambda=\mu^2$ was eliminated on the ground that it gave exponntially increasing solutions in time $t$. Time solutions now $e^{+\mu ct},e^{-\mu ct}$.

The real reason $\mu^2>0$ can be eliminated is that the BC for $X(x)$ cannot be satisfied. $\lambda=\mu^2>0$ $X''-\mu^2X=0$
\begin{align*}
  X&=c_1\cosh(\mu x)+c_2\sinh(\mu x)\\
  X(0)&=0=c_1\cdot1+c_2\cdot0\quad c_1=0\\
  X&=c_2\sinh(\mu x)\\
  X(L)&=0=c_2\sinh(\mu L)
\end{align*}
want nontrivial solution $c_2\ne0$. $\sinh(\mu L)=0$, only solution is $\mu=0$ and since $\mu^2>0$ we have no solutions.
\end{document}


\section{\texorpdfstring{Primitives}{Primitives}}
\vspace{5mm}
\large

\subsection{Basics, symmetric and asymmetric ciphers}

\begin{agreement}
	\textbf{Alice} and \textbf{Bob} are communicating parties, used only as abstraction in some protocol.

	Should not be different person. E.g. disk encryption for 10 years means that the same person will decrypt in the future. Therefore Alice and Bob are the same person but time is different.
\end{agreement}

\begin{definition}
	Passive attacker has an access to the communication line and listens trying to figure out text that was sent.
\end{definition}
\begin{definition}
	Active attacker can not only listen to the communication but also alter the bits.
\end{definition}

\begin{agreement}
	Passive attacker is represented by \textbf{Eve}. Active attacker is represented by \textbf{Mallory} or \textbf{Man in the middle}.
\end{agreement}

\begin{definition}
	Plain text - not encrypted information that is to be delivered to from Alice to Bob.
\end{definition}
\begin{definition}
	Cipher text - encrypted plain text. Ideally properties of the Cipher text should not reveal any info about Plain text.
\end{definition}

\begin{definition}
	Encryption box (function) - converts Plain text to Cipher text. We want the box to be parametrized by \textbf{Key}.

	Decryption box (function) - converts Cipher text back to plain text.
\end{definition}

Several reasons to keep Encryption algorithm public and the Key secret (Kerckhoffs Principle):
\begin{enumerate}
	\item Good cyphers are hard to find. As of know we know 10 strong cyphers.
	\item Well known cyphers are better analyzed. We cannot proof that any cypher is strong, only hope no weakness is found.
	\item Information tend to leak, there has already been cases of weak secret cyphers(cell phones protocol).
		Secondly, it is much easier to recover from leaked Key than cypher. For some protocols new Keys are generated periodically.
\end{enumerate}

\begin{definition}
	Symmetric encryption. Firstly we need Encryption function (E):
	\[ E:Plain = \{0,1\}^n \times Key = \{0,1\}^k \to Cipher=\{0,1\}^n \]
	Denote as $E_k(x) = y$.

	Decryption function (D):
	\[ D:\{0,1\}^n \times \{0,1\}^k \to \{0,1\}^n \]

	Properties:
	\[ \forall k \forall x: D(E(x, k), k) = x \iff E = D^{-1} \]

	Symmetric because Keys on the both sides are the same.
\end{definition}

We can say that E permutes $\{0,1\}^n$. We want E to behave as a random permutation, more formally there should be no \textbf{polynomial} algorithm to distinguish between Cipher text and a Random permutation.
Ciphers having that property are called Secure.

\begin{example}
	\textbf{Ceasars cipher} - shift alphabet by some constant. Not secure at all, alphabet is that small so we can try all possible constants.
\end{example}

Symmetric encryption has several disadvantages:
\begin{enumerate}
	\item For multiparties communication, new key is required for every pair. Distributing such keys secretly is not trivial.
	\item Plain text size can be derived from Cipher text size.
\end{enumerate}

\begin{definition}
	Asymmetric cipher: E, D are parametrized by different keys. Compatible E and D should still able to decode encoded text.
	\[ \forall (k_e, k_d) \forall x: D(E(x, k_e), k_d) = x \]

	However, deriving one key from another in pair $(k_e, k_d)$ should be impossible for the attacker.
	Ideally only one $k_d$ should correspond to a particular $k_e$.
\end{definition}

Typical uses:
\begin{enumerate}
	\item Multiparties communication. Everybody generates a key pair. $k_e$ are public, $k_d$ are private.
		Alice encrypts message with Bobs $k_e$, Bob decrypts it with his own $k_d$.
		Key distribution is again a problem. Alice cannot check that publicly available key, that is marked as Bobs is really created by Bob and not by the attacker.
		No universal solution :(.
	\item Signature scheme.
		Alice signs a document with private $k_{sign}$. Everyone has access to $k_{verify}$ to convert to Plain text. Therefore, anybody can check that message was produced by Alice.
		Deals with active attackers. $k_e$ is private, $k_d$ is public.
	\item Both schemes could be combined, but we have to make sure that keys are different.
\end{enumerate}

Main disadvantage if the Asymmetric ciphers is the speed of computation, in general they are slow. At least $\bigO(n^2)$.
Furthermore, size of the message is bounded by the size of the Key. So, we cannot encrypt arbitrarily long messages.

\subsection{Hash functions}
\begin{definition}
	Hash function
	\[ h: \{0, 1\}^{\ast} \to \{0, 1\}^b, b \in \N \]
	Where b is a parameter of h, fixed \# of bits.
\end{definition}
\begin{propertiesBasic}
	Good hash function properties:
	\begin{enumerate}
		\item Impossible (computationally infeasible) to invert. Meaning, for given $y$ cannot find $x: h(x) = y$.
		\item Impossible to find collisions \textbf{called collision resistance}: $x \ne y: h(x) = h(y)$.
		\item Random hash function. Impossible to distinguish from random function.
	\end{enumerate}
\end{propertiesBasic}

\begin{example}
	With hash functions we can improve \textbf{signature scheme}. One can use hash function to allow multiple signatures on the same document.
	Alice send plain text X, then computes $E(h(X), k_{sign})$. Bob recomputes hash of the plain text and compares it with the has sent by Alice. (md5?)

	Such scheme requires hash function with low probability to find collisions. Otherwise the attacker will ask Alice to sign $x: h(x) = h(y)$, then send $y$ to Bob.
\end{example}

\begin{example}
	\textbf{Challenge-response authentication}

	Let's assume, Alice and Bob knows secret password X which will serve as a verification before communication starts.
	Alice cannot just send $h(X)$, because passive attacker could write down the password and trick Bob with it pretending to be Alice.

	Bob issues challenge C first (some random text). Alice produces a concatenation of 2 strings and uses hash: $h(X | C)$.
\end{example}

\begin{definition}
	\textbf{Nonce} - number used once. Long random number, e.g. Challenge in the Challenge-response authentication.
\end{definition}

\subsection{Random generator(RNG)}
Unfortunately, we can only create computationally indistinguishable RNG from random function. Should be:
\begin{enumerate}
	\item Unpredictable, even if attacker listened to the Gb of previous output.
	\item Cannot be influenced (e.g. by heating the Server room)
\end{enumerate}

Combining symmetric and asymmetric cipher. Firstly, Alice generate $k_{sym}$ random, one per message.
Then sends $E_{sym}(X, k_{sym}, E_{asym}(k_{sym}, k_{asym}) $.

Bob takes 2nd part of the message, decrypts $k_{sym}$ using asymmetric D. Then he decrypts first part of the message by $k_{sym}$.


\graphicspath{{Pics/}}

\newpage\section{Circles that are really touching}

\den{Mixtilinear Circle}{
    Let $\triangle ABC$ be an ordinary triangle, $I$ is its incenter, $D$ is
    the touch points of the incenter with $BC$. Let $\omega$ be the
    mixtilinear incircle. Let it touch $CA, AB$ at $E, F$. Furthermore, let
    $\omega\cap\odot ABC\equiv T$. Let $M_a, M_b, M_c$ be the midpoints of the
    smaller arcs $BC, CA, AB$, and $M_A, M_B, M_C$ be the midpoints of the
    major arcs $BC, CA, AB$.

    \figdf{.9}{Mixtilinear}{Mixtilinear Incircle: more than meets the eye}
}

\begin{prooof}
    $E, I, F$ are collinear. Consider the circle $ TI'EC $ and do so $T, I,
    M_A$ are collinear. Consider the circle $TIEC$ and apply reim's theorem
    and angle chasing. Also, proving that $AC$ touches $\odot TCD$ proves that
    $T, D, A'$ collinear.
\end{prooof}

\lem{}{
    The bundle $ (A, T; M_b, M_c) $ is harmonic. And $ TA $ is a symmedian of
    $\triangle TM_cM_b$.  \[\frac{TM_c}{M_cA}=\frac{TM_b}{M_bA}\]
}\label{lemma:mixtilinear_lemma_harmonic_with_arc_midpoints}
\newpage

\begin{minipage}{.4\linewidth}
    \lem{ISL 1999 G8}{
        Let $X$ be a variable point on the arc $AB$, and let $O_{1}$ and $O_{2}$
        be the incenters of the triangles $CAX$ and $CBX$. Then $X, O_{1}, O_{2}$
        and $T$ lie on a circle.
    }\label{lemma:two_incenters_of_a_cyclic_quad_cyclic_with_mixtilinear_touchpoint}

    \solu{
        Using similarity and
        \autoref{lemma:mixtilinear_lemma_harmonic_with_arc_midpoints}.
    }
\end{minipage}\hfill%
\begin{minipage}{.59\linewidth}
    \figdf{1}{incenters_cyclic_with_mixtilinear_touchpoint}{The two incenters are
    cyclic with $ T, X $}
\end{minipage}

\prob{https://artofproblemsolving.com/community/c6t48f6h1508805_nice_but_not_hard}
{AoPS1}{H}{
    Let $ABCD$ be a quadrilateral inscribed in a circle, such that the
    inradius of $\triangle ABC$ and $ACD$ are the same. Let $T$ be the
    touchpoint of $A$-mixtilinear incircle of the triangle $ABD$ with $\odot
    ABCD$. Let $I_1,I_2$ be the incenters of the triangles $ABC, ACD$
    respectively. Show that $I_1I_2$ and the tangents of $A,T$ wrt $\odot
    ABCD$ are concurrent.
}

\solu{
    We know from
    \autoref{lemma:two_incenters_of_a_cyclic_quad_cyclic_with_mixtilinear_touchpoint},
    that $TI_1I_2C$ is cylic. Moreover from the given condition, we know that
    $AC$ bijects $I_1, I_2$. Which gives us $I_1I_2\parallel TC$. Using that
    we can show that if $S= I_1I_2\cap M_cM_b$, then $TS$ is tangent to $\odot
    ABC$, which also gives us $SA$ is tangent to $\odot ABC$.

    \figdf{.6}{c6t48f6h1508805}{}
}

\begin{minipage}{.5\linewidth}
    % \prob{https://artofproblemsolving.com/community/c6h1570826p9686418}
    \den{Generalization of Mixtilinear Incirlce}{
        \href{https://artofproblemsolving.com/community/c6h1570826p9686418}{Consider}
        $\triangle ABC$ and let $M_c, M_b$ are midpoints of arcs $AB, AC$. Let $E,
        F$ on $AB, AC$ such that $EF\parallel M_bM_c$. Let $EM_b, FM_c$ meet $(ABC)$ second
        time at $P, Q$. Consider two intersection points $E', F'$ of $(EFPQ)$ with
        $AB, AC$ different from $E, F$. Then $EF'\cap E'F$ is the incenter of $ABC$.\\

        In other words, if $P$ lies on the arc $BC$, and $M_bP\cap AC = E$,
        $M_cP\cap AB = F'$, then $E, F', I$ are collinear.
    }
\end{minipage}\hfill%
\begin{minipage}{.49\linewidth}
    \figdf{.75}{Mixtilinear_Gene}{}
\end{minipage}

\begin{minipage}{.5\linewidth}
    \prob{}
    {Archer - EChen M1P3}{E}{
        Let the incenter  touch $ BC $ at $ D $. Let $ AI\cap BC = E,\ AI\cap
        \odot ABC = F$. \hl{Prove that $\odot DEF \cap \odot ABC = T$}, the
        mixtilinear touchpoint. Now let, $\odot DEF \cap \odot (I_a) = S_1,
        S_2 $. \hl{Prove that $ AT $ goes through either $S_1$ or $S_2$}.
    }

    \begin{solution}
        We already know that $T, D, E, F$ are cyclic. Then, let $S = AT\cap
        \odot DEF$, then we have,
        \[\angle FSE = \angle FDE = \angle FD'E\]
        Which means $S'$ is the reflection of $D'$ over $AI$, which definitely
        lies on $\left(I_a\right)$.
    \end{solution}
\end{minipage}\hfill%
\begin{minipage}{.49\linewidth}
    \figdf{.85}{Archer_EChen_M1P3}{\autoref{problem:Archer - EChen M1P3}}
\end{minipage}

\begin{minipage}{.5\linewidth}
    \prob{}{}{}{
        Let the $ B $-mixtilinear and $ C $-mixtilinear circles touch $ BC $ at $ X_B,
        X_C$ respectively. Then $X_B, X_C, T, M_a$ lie on a circle.
    }

    \begin{solution}
        We use mixtilinear related lemma to show that, $ST \cdot SM_a = SX_b \cdot
        SX_c$. We can do that by angle chase to show that $SI^2 = SX_b\cdot SX_c$.
    \end{solution}
\end{minipage}\hfill%
\begin{minipage}{.49\linewidth}
    \figdf{.9}{mixti_touch_with_BC}{}
\end{minipage}

\theo{}
{Root BC Inversion in Mixtilinear Circles}{
    If we invert around $A$ with the radius $\sqrt{bc}$, the mixtilinear
    incircle goes to the excicle.
}

\begin{prooof}
    We use \autoref{problem:Archer - EChen M1P3} to show the results.
\end{prooof}

\figdf{.6}{mixtilinear_root_bc}{}

\prob{}{Taiwan TST 2014 T3P3}{EH}{
    Let $M$ be any point on the circumcircle of $\triangle ABC$. Suppose the
    tangents from $M$ to the incircle meet BC at two points $X_1$ and $X_2$.
    Prove that $T, M, X_1, X_2$ lie on a circle.
}

\den{Curvilinear Incircle}{
    Let $ABCD$ be a cyclic quadrilateral. $AC$ meets $BD$ at $X$. We call the
    circle that touches $AX, BX$ and the circumcircle from the inside a
    curvilinear incircle. The curvilinear incircle touch $AX, BX$ at $P, Q$
    resp. And let the incircle of $\triangle ABD$ be $I$. Then $P, Q, I$ are
    collinear.

    \figdf{.7}{curvi_lem_1}{}
}

\theo{http://forumgeom.fau.edu/FG2003volume3/FG200325.pdf}
{Sawayama and Thebault's theorem}{
    Let $O_1, O_2$ be the centers of the two curvilinear incircles on $BD$ and
    $AC$. Then $O_1, I, O_2$ are collinear.
}

\solu{
    Notice that this is similar to the circles $ TIEC $ and $ TIFB $ in the
    mixtilinear circle figures.
}


 \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}
\lstset { %
    language=C++,
    backgroundcolor=\color{black!5}, % set backgroundcolor
    basicstyle=\footnotesize,% basic font setting
}
\newtheorem{theorem}{Theorem}
\newtheorem{remark}{Remark}
\newtheorem{lemma}[theorem]{Lemma}
\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]
\newtheorem{claim}{Claim}

\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.)}}

\begin {document}

\vspace {1em}
\begin {Instruction}
Adapted From Virginia Williams' lecture notes.
\end {Instruction}

{\LARGE \textbf {COMP 285 (NC A\&T, Spr `22)}\hfill \textbf {Lecture 25} }

\begin{centering}
\section*{Dynamic Programming III: Floyd-Warshall}
\end{centering}

\section{Overview}

Last lecture, we talked about dynamic programming (DP), a useful paradigm and one technique that you should immediately consider when you are designing an algorithm. We covered the Bellman-Ford algorithm for solving the single source shortest path problem, and we talked about the Fibonacci algorithm for solving the n-th Fibonacci number. This lecture, we will cover some more examples of dynamic programming, and start to see a recipe for how to come up with DP solutions. We will talk about three problems today: all-pairs shortest path, longest common subsequence, knapsack, and maximum weight independent set in trees. In general, here are the steps to coming up with a dynamic programming algorithm:

\begin{enumerate}
  \item \textbf{Identify optimal substructure}: how are we going to break up an optimal solution into optimal sub-solutions of sub-problems? We're looking for a way to do this so that there are overlapping sub-problems, so that a dynamic programming approach will be effective.
  \item \textbf{Recursively define the value of an optimal solution:} Write down a recursive formulation of the optimum, in terms of sub-solutions.
  \item \textbf{Find the optimal value:} Turn this recursive formulation into a dynamic programming algorithm to compute the value of the optimal solution.
  \item \textbf{Find the optimal solution:} Once we've figured out how to find the cost of the optimal solution, we can go back and figure out how to keep enough information in our algorithm so that we can find the solution itself.
  \item \textbf{Tweak the implementation}\footnote{We won't talk too much about this step in COMP285, even though it is often important in practice}: Often it's the case that the solutions that we come up with in the previous steps aren't implemented in the best way. Maybe they are storing more than they need to. In this final step (which we won't go into in too much detail in COMP 285), we go back through the DP solution we've designed, and optimize it for space, running time, and so on.
\end{enumerate}

In this class, we'll focus mostly on 1, 2, and 3. We'll see a few examples of 4, and occasionally wave our hands about 5

\section{Floyd-Warshall Algorithm}

The Floyd-Warshall Algorithm solves the All Pairs Shortest Path (APSP) problem: given  graph $G$, find the shortest path distances $d(s, t)$ for all $s$, $t \in V$ , and, for the purpose of storing the shortest paths, the predecessor $\pi(s, t)$ which is the node right before $t$ on th $s-t$ shortest path.

Let's speculate about APSP for a moment. Consider the case when the edge weights ar nonnegative. We know we can compute APSP by running Dijkstra's algorithm on each nod $v \in V$ and obtain a total runtime of $O(mn +  n^2 \log n)$. The runtime of the Floyd-Warshal algorithm, on the other hand, is $O(n^3)$. We know that in the worst case $m = O(n^2)$, an thus, the Floyd-Warshall algorithm can be at least as bad as running Dijkstra's algorithm  $n$ times! Then why do we care to explore this algorithm? The reason is that the Floyd-Warshal algorithm is very easy to implement compared to Dijkstra's algorithm. The benefit of using simple algorithms is that they can often be extended and in practice can run relatively fast compared to algorithms that may have a huge overhead.

An added benefit of the Floyd-Warshall algorithm is that it also supports negative edge weights, whereas Dijkstra's algorithm does not.

As mentioned, the optimum substructure with overlapping subproblems for shortest path is that for all node $k$ on an $s-t$ shortest path, $d(s, t) = d(s, k) + d(k, t)$. We refine this observation as follows. Suppose that the nodes of the graph are identified with the integer from $1 to n$. Then, if $k$ is the maximum node on an $s-t$ shortest path, then $d(s, t) =  d(s, k) + d(k, t)$ and moreover, the subpaths from $s$ to $k$ and from $k$ to $t$ only use nodes $u$ to $k - 1$ internally.

We hence get independent subproblems in which we compute $d^{k}(s, t)$ for all $s, t$ that are the smallest weight of an $s-t$ path that only uses nodes $1, \cdots , k$ internally. This motivates th Floyd-Warshall algorithm, Algorithm \ref{alg:floyd-warshall} below (please note that we will refer to the nodes of G by the names $1, \cdots , n$).

\begin{algorithm}
\caption{Floyd-Warshall Algorithm(G,s)}
\label{alg:floyd-warshall}
\begin{algorithmic}
\STATE $d_k(u,u) = 0, \forall u \in V, k \in \{0, \cdots, n\}$
\STATE $d_k(u,v) = \infty, \forall u \in V, u\neq v, k \in \{0, \cdots, n\}$
\STATE $d_0(u,v) = c(u,v), \forall (u,v) \in E$
\STATE $d_0(u,v) = \infty, \forall (u,v) \notin E$
\FOR{$k=1, \cdots, n$}
  \FOR{$(u,v) \in V$}
    \STATE $d_k(u,v) = \min\{ d_{k-1}(u,v), d_{k-1}(u,k) + d_{k-1}(k,v)$ \texttt{// update the estimate}
  \ENDFOR
\ENDFOR
\RETURN $d_n(u,v), \forall u,v \in V$
\end{algorithmic}
\end{algorithm}

We initialize each $d_0(u, v )$ as the edge weight $c(u, v )$ if $(u, v ) \in E$, else we set it to $\infty$ in the bottom-most row in our dynamic programming table. Now, as we increment $k$ to $1$, we effectively find the minimum distance path between $u, v \in V$ that go through node $1$, and populate the table with the results. We continue this process to find the shortest paths that go through nodes $1$ and $2$, then $1, 2,$ and $3$ and so on until we find the shortest path through all $n$ nodes.

\textbf{Negative cycles}. The Floyd-Warshall algorithm can be used to detect negative cycles: examine whether $d_n(u, u) < 0$ for any $u \in V$. If there exists $u$ such that $d_n(u, u) < 0$, there is a negative cycle, and if not, then there isn't. The reason for this is that if there is a simple path $P$ from $u$ to $u$ of negative weight (i.e., a negative cycle containing $u$), then $d_n(u, u)$ will be at most its weight, and hence, will be negative. Otherwise, no path can cause $d_n(u, u)$ to be negative.

\textbf{Runtime}. The runtime of the Floyd-Warshall algorithm is proportional to the size of the table $\{d_i (u, v )\}_{i,u,v}$ since filling each entry of the table only depends on at most two other entries filled in before it. Thus, the runtime is $O(n^3)$.

\textbf{Space usage}. Note that for both the algorithms we covered today, the Floyd-Warshall and Bellman-Ford algorithms, we can choose to store only two rows of the table instead of the complete table in order to save space. This is because the row being populated always depends only on the row right below it. This space saving optimization is not a general property of tables formed as a result of the dynamic programming method, and the slot dependencies in some dynamic programming problems may lie on arbitrary positions on the table thereby forcing us to store the complete table.

\textbf{A Note on the Longest Path Problem}

We discussed the shortest path problem in detail and provided algorithms for a number of variants of the problem. We might equally be interested in computing the longest simple path in a graph. A first approach is to formulate a dynamic programming algorithm. Indeed, consider any path, even the longest, between two nodes $s$ and $t$. Its length $\ell(s, t)$ equals the sum $\ell(s, k) + \ell(k, t)$ for any node $k$ on the path. However, this does not yield an optimal substructure: in general, neither subpath $s \to k$, $k \to t$ would be a longest path, and even if one is a longest path, the other one cannot use any nodes that appear on the first since the longest path is required to be simple. Hence the two subproblems $\ell(s, k)$ and $\ell(k, t)$ are not even independent! It turns out that finding the longest path does not seem to have any optimal substructure, which makes it difficult to avoid exhaustive search through dynamic programming. The longest path problem is actually a very difficult problem to solve and is NP-hard. The best known algorithm for it runs in exponential time.

\end{document}


%%% exercise 04 %%%
\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath, amssymb, amsfonts, enumitem, fancyhdr, tikz}
% get rid of paragraph indent
\setlength{\parindent}{0 pt}
% allow section.equation numbering
\numberwithin{equation}{section}
% allows you to copy-paste code segments. requires pygments to be installed
% natively, i.e. not just in a Python venv! for example, on WSL Ubuntu you
% need to sudo apt-get install python3-pygments (unfortunately, the version
% will be a bit dated) and cannot rely on your venv Pygments version.
\usepackage{minted}
% alternative to minted that does not require Python, LaTeX only. listings is
% however disgusting out of the box and some setup is required.
\usepackage{listings, xcolor}
% makes clickable links to sections
\usepackage{hyperref}
% make the link colors blue, as well as cite colors. urls are magenta
\hypersetup{
    colorlinks, linkcolor = blue, citecolor = blue, urlcolor = magenta
}
% fancy pagestyle so we can use fancyhdr for fancy headers/footers
\pagestyle{fancy}
% add logo in right of header. note that you will have to adjust logo path!
\fancyhead[R]{\includegraphics[scale = 0.15]{../bac_logo1.png}}
% don't show anything in the left and center header
\fancyhead[L, C]{}
% give enough space for logo by reducing top margin height, head separator,
% increasing headerheight. see Figure 1 in the fancyhdr documentation. if
% \topmargin + \headheight + \headsep = 0, original text margins unchanged.
\setlength{\topmargin}{-60 pt}
\setlength{\headheight}{50 pt}
\setlength{\headsep}{10 pt}
% remove decorative line in the fancy header
\renewcommand{\headrulewidth}{0 pt}

% title, author + thanks, date
\title{Exercise 4}
\author{Derek Huang\thanks{NYU Stern 2021, BAC Advanced Team.}}
\date{June 6, 2021\thanks{Original version released on March 11, 2021.}}

% shortcut links. the % characters strip extra spacing.
\newcommand{\pytest}{\href{https://docs.pytest.org/en/stable/}{pytest}}
\newcommand{\minimize}{%
    \href{%
        https://docs.scipy.org/doc/scipy/reference/generated/%
        scipy.optimize.minimize.html%
    }{\texttt{scipy.optimize.minimize}}%
}
% don't wanna keep typing this out
\newcommand{\loglossgrad}{\texttt{\_logistic\_loss\_grad}}

\begin{document}

\maketitle
% need to include this after making title to undo the automatic
% \thispagestyle{plain} command that is issued.
\thispagestyle{fancy}

\section{Introduction}

The goal of this exercise is to implement an $ \ell^2 $-regularized logistic
regression model, minimizing the convex objective using the L-BFGS
implementation provided by \minimize.

\section{Instructions}

\subsection{General}

The \texttt{exercise\_04.py} file contains a skeleton for the
\texttt{LogisticRegression} class, the \loglossgrad{} function, a unit test,
and a \pytest{} fixture. Your job is to implement the \texttt{predict} and
\texttt{score} methods of the \texttt{LogisticRegression} class and the
\loglossgrad{} function by replacing the
\texttt{\#\#\# your code goes here \#\#\#} comment blocks with the appropriate
Python code.

\medskip

Your code \textbf{must} be written in the areas marked by these blocks. Do
\textbf{not} change any of the pre-written code. The exercise is complete
$ \Leftrightarrow $ \texttt{pytest /path/to/exercise\_04.py} executes with
zero test failures. Note that attributes ending with \texttt{\_}, ex.
\texttt{coef\_}, are created \textbf{during} the fitting process.

\subsection{Objective and gradient}

Let $ \mathbf{X} \in \mathbb{R}^{N \times d} $ be the input matrix, each row
$ \mathbf{x}_k^\top $ a tranposed data example, and let $ \mathbf{y} \in
\{-1, 1\}^N $ be the response label vector, $ -1 $ for negative class, $ 1 $
for positive class. Let $ \mathbf{w} \in \mathbb{R}^d $ be the weight vector,
$ b \in \mathbb{R} $ the intercept term. Then, for $ \alpha \in [0, \infty) $,
the loss functional $ J_\mathcal{D} : \mathbb{R}^d \times \mathbb{R}
\rightarrow [0, \infty) $, $ \mathcal{D} \triangleq
(\mathbf{X}, \mathbf{y}) $, is such that
\begin{equation} \label{logreg_obj}
    J_\mathcal{D}(\mathbf{w}, b) = \sum_{k = 1}^N\log\left(
        1 + e^{-y_k(\mathbf{w}^\top\mathbf{x}_k + b)}
    \right) + \frac{1}{2}\alpha\Vert\mathbf{w}\Vert_2^2
\end{equation}

This form is exactly the unweighted objective minimized by scikit-learn for
two-class logistic regression. To minimize $ J_\mathcal{D} $, we will be using
L-BFGS, a \href{%
    https://en.wikipedia.org/wiki/Quasi-Newton_method%
}{quasi-Newton method} that is a limited-memory version of the \href{%
    https://en.wikipedia.org/wiki/Broyden\%E2\%80\%93Fletcher\%E2\%80\%93%
    Goldfarb\%E2\%80\%93Shanno_algorithm%
}{BFGS algorithm}. The $ \mathbf{w}, b $ gradients of $ J_\mathcal{D} $, which
are required\footnote{
    If the gradient is not provided, \minimize{} will approximate it with
    finite differences.
} by [L-]BFGS, are such that
\begin{equation} \label{logreg_grads}
    \begin{split}
        \nabla_\mathbf{w}J_\mathcal{D}(\mathbf{w}, b) & =
	    \sum_{k = 1}^N y_k\mathbf{x}_k\left[
	        \sigma\left(
	            y_k\left(\mathbf{w}^\top\mathbf{x}_k + b\right)
	        \right) - 1
	    \right] + \alpha\mathbf{w} \\
	    \nabla_bJ_\mathcal{D}(\mathbf{w}, b) & =
	    \sum_{k = 1}^N y_k\left[
	        \sigma\left(
	            y_k\left(\mathbf{w}^\top\mathbf{x}_k + b\right)
	        \right) - 1
	    \right]
    \end{split}
\end{equation}

Here $ \sigma : \mathbb{R} \rightarrow (0, 1) $ is the sigmoid function, i.e.
$ \sigma(x) \triangleq 1 / (1 + e^{-x}) $.

\subsection{Implementing \loglossgrad}

\loglossgrad{} computes the loss and gradient for a two-class logistic
regression model. It takes positional parameters \texttt{w}, the concatenated
coefficients $ [ \ \mathbf{w} \ \ b \ ]^\top \in \mathbb{R}^{d + 1} $,
\texttt{X}, the input matrix $ \mathbf{X} \in \mathbb{R}^{N \times d} $,
\texttt{y}, the label vector $ \mathbf{y} \in \{-1, 1\}^N $, and
\texttt{alpha}, the $ \alpha \in [0, \infty) $ parameter scaling the
$ \ell^2 $ regularization term. Note that \texttt{alpha} does \textbf{not}
show up in the signature of \texttt{LogisticRegression.\_\_init\_\_} and so
\texttt{1 / self.C} should be passed to the \texttt{alpha} parameter of
\loglossgrad, where \texttt{C} is the inverse regularization parameter
mentioned in the class docstring. The use of \texttt{C} here is to conform to
scikit-learn convention.

\medskip

The tuple \texttt{(loss, grad)} should be returned by \loglossgrad{}, where
\texttt{loss} gives the current loss functional value
$ J_\mathcal{D}(\mathbf{w}, b) $ and \texttt{grad} gives the loss functional
$ [ \ \mathbf{w} \ \ b \ ]^\top $ gradient
$ [ \ \nabla_\mathbf{w}J_\mathcal{D}(\mathbf{w}, b)^\top \ \
\nabla_bJ_\mathcal{D}(\mathbf{w}, b)^\top \ ]^\top $. \texttt{loss} must be of
type \texttt{float} or \texttt{numpy.float64} and \texttt{grad} must be of
type \texttt{numpy.ndarray} with a float \texttt{dtype}.

\subsection{Implementing \texttt{predict}}

The \texttt{predict} method of the \texttt{LogisticRegression} class, when
given \texttt{X}, a \texttt{numpy.ndarray} shape
\texttt{(n\_samples, n\_features)}, where \texttt{n\_features} $ = d $,
should return a \texttt{numpy.ndarray} shape \texttt{(n\_samples,)} giving the
predicted class labels, containing only the values in \texttt{self.classes\_}
recorded by the \texttt{fit} method. Given a new example $ \mathbf{x} \in
\mathbb{R}^d $ and parameters $\mathbf{w} \in \mathbb{R}^d $, $ b \in
\mathbb{R} $, the prediction rule $ F : \mathbb{R}^d \rightarrow
\{\gamma_{-1}, \gamma_1\} $ is such that
\begin{equation*}
    F(\mathbf{x}) \triangleq \gamma_1\mathbb{I}_{(0, \infty)}
        \left(\mathbf{w}^\top\mathbf{x} + b\right) +
        \gamma_{-1}\mathbb{I}_{(-\infty, 0]}
        \left(\mathbf{w}^\top\mathbf{x} + b\right)
\end{equation*}

Here $ \gamma_1 $, the positive class label, should be
\texttt{self.classes\_[1]}, while $ \gamma_{-1} $, the negative class label,
should be \texttt{self.classes\_[0]}. Be careful not to get them mixed up or
a correct implementation will predict the opposite of the scikit-learn
implementation for any new data example $ \mathbf{x} $.

\subsection{Implementing \texttt{score}}

The \texttt{score} method of the \texttt{LogisticRegression} class computes
the accuracy of the predictions. Given parameters $ \mathbf{w}, b $,
classification function $ F_{\mathbf{w}, b} $ (the model), and data
$ \mathcal{D} \triangleq \{(\mathbf{x}_1, y_1), \ldots (\mathbf{x}_N, y_N)\} $,
where the $ y_k $ values may either be numeric or class labels, the accuracy
$ \mathcal{A}_{\mathbf{w}, b} $ of the model on the data $ \mathcal{D} $ is
\begin{equation*}
    \mathcal{A}_{\mathbf{w}, b}(\mathcal{D}) = \frac{1}{|\mathcal{D}|}
        \sum_{(\mathbf{x}, y) \in \mathcal{D}}\mathbb{I}_{\{y\}}\left(
            F_{\mathbf{w}, b}(\mathbf{x})
        \right)
\end{equation*}

Note that $ \operatorname{im}\mathcal{A}_{\mathbf{w}, b} = [0, 1] $, so for
data $ \mathcal{D} $, $ \mathcal{A}_{\mathbf{w}, b}(\mathcal{D}) $ is the
fraction of examples the model correctly clasified.

\subsection{Tips}

\begin{enumerate}
    \item
    Carefully read the \texttt{LogisticRegression} method docstrings.

    \item
    Remember that \loglossgrad{} should return both the objective (loss
    functional) value \textbf{and} its $ [ \ \mathbf{w} \ \ b \ ]^\top $
    gradient. If \texttt{n\_features} $ = d $, allocate a
    \texttt{numpy.ndarray} shape \texttt{(n\_features + 1,)}, filling the
    first \texttt{n\_features} elements with the $ \mathbf{w} $ gradient
    and the last with the $ b $ gradient (partial derivative).

    \item
    \href{%
        https://docs.scipy.org/doc/scipy/reference/generated/%
        scipy.special.expit.html%
    }{\texttt{scipy.special.expit}} is a vectorized elementwise sigmoid
    function that is very useful for computing the $ \mathbf{w}, b $
    gradients defined in (\ref{logreg_grads}) for \loglossgrad.

    \item
    Remember that $ \alpha = 1 / C $, so $ 1 / C $ should be passed to the
    \texttt{alpha} parameter of \loglossgrad.

    \item
    Use NumPy functions whenever possible for efficiency, brevity, and ease
    of debugging.
\end{enumerate}

\end{document}


\vssub
\subsection{~Spatial propagation} \label{sub:xy_prop}
\vssub
\subsubsection{~General concepts}
\vsssub

Spatial propagation in \ws\ is described by the first terms of
Eq. (\ref{eq:bal_f_grid}). For spherical coordinates
[Eq. (\ref{eq:bal_sphere})], the corresponding spatial propagation step
becomes

%----------------------------%
% Step : Spatial propagation %
%----------------------------%
% eq:step_xy_prop

\begin{equation}
\frac{\p \cN}{\p t} + \frac{\p}{\p \phi} \, \dot{\phi} \cN +
\frac{\p}{\p \lambda} \, \dot{\lambda} \cN = 0
\: , \label{eq:step_xy_prop}
\end{equation}

\noindent
where the propagated quantity $\cN$ is defined as $\cN \equiv N \, c_g^{-1} \,
\cos\phi$. For the Cartesian grid, a similar equation is found for
$\cN \equiv N \, c_g^{-1}$. In this section equations for the more complicated
spherical grid are presented only. Conversion to a Cartesian grid is generally
a simplification and is trivial.

Equation~(\ref{eq:step_xy_prop}) in form is identical to the conventional
deep-water propagation equation, but includes effects of both limited depths
and currents. At the land-sea boundaries, wave action propagating toward the
land is assumed to be absorbed without reflection, and waves propagating away
from the coast are assumed to have no energy at the coastline. For so-called
`active boundary points' where boundary conditions are prescribed, a similar
approach is used. Action traveling toward such points is absorbed, whereas
action at the boundary points is used to estimate action fluxes for components
traveling into the model.

The spatial grids can use two different coordinate systems, either a `flat'
Cartesian coordinate system typically used for small scale and idealized test
applications, and a spherical (latitude-longitude) system used for most
real-world applications. In model version 3.14, the coordinate system was
selected at compile time with the {\code XYG} or {\code LLG} switches. In more
recent model versions, the grid type is now a variable defined in {\file
ww3\_grid} and stored in the {\file mod\_def.ww3} file.

There is an option for spherical grids to have simple closure, to be periodic
 in the longitude direction, e.g. so that energy can propagate east from the maximum longitude in the grid to the minimum longitude in the grid. This closure is ``simple'' insofar as the index for latitude does not change across this ``seam''. A ``not simple'' type of closure is also permitted: this is associated with tripole grids. The tripole grid is a type of irregular grid and so this closure is discussed further in (\ref{sub:num_space_curv}).

Up to model version 3.14, \ws\ considered only regular discrete grids,
where the two main grid axes ($x,y$) are discretized using constant
increments $\Delta x$ and $\Delta y$. In model version \WWver\
additional options have been included, including curvilinear grids and
unstructured grids. In the following sections these grid approaches
will be discussed, before additional propagation issues are addressed,
covering the Garden Sprinkler Effect (\ref{sub:num_GSE}), continuously
moving grids (\ref{sub:num_move}) unresolved islands
(\ref{sub:num_obst}), and rotated grids (\ref{sub:num_space_rotagrid}).