\section{Dijkstra Algorithm}

\begin{frame}{Dijkstra Algorithm}{Shortest Path without Computer}
  \begin{itemize}
    \item
      Wanted: Shortest path from M to all other points
    \item
      Place pearls on crossings and clamp strings between them
  \end{itemize}
  \begin{figure}
    \includegraphics[width=0.45\linewidth]{Images/Dijkstra/DijkstraMapTF.png}
  \end{figure}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Shortest Path without Computer}
  \vspace{-1.5em}
  \begin{columns}
    \begin{column}{0.5\linewidth}
      \begin{figure}[!t]
        \includegraphics[width=0.6\linewidth]
          {Images/Dijkstra/DijkstraMapTF.png}
          \caption{Based on OpenStreetMaps; CC BY-SA 2.0}
      \end{figure}
      \vspace{-1.5em}
      \begin{itemize}
        \item
          Take the net and pull it slowly upwards until fully lifted
      \end{itemize}
    \end{column}
    \begin{column}{0.5\linewidth}
\rlap{\onslide<1>{\includegraphics[width=0.7\linewidth]{Images/Dijkstra/DijkstraTreeTF-split-o1.pdf}}}%
\rlap{\onslide<2>{\includegraphics[width=0.7\linewidth]{Images/Dijkstra/DijkstraTreeTF-split-o2.pdf}}}%
\rlap{\onslide<3>{\includegraphics[width=0.7\linewidth]{Images/Dijkstra/DijkstraTreeTF-split-o3.pdf}}}%
\rlap{\onslide<4>{\includegraphics[width=0.7\linewidth]{Images/Dijkstra/DijkstraTreeTF-split-o4.pdf}}}%
\rlap{\onslide<5>{\includegraphics[width=0.7\linewidth]{Images/Dijkstra/DijkstraTreeTF-split-o5.pdf}}}%
\rlap{\onslide<6>{\includegraphics[width=0.7\linewidth]{Images/Dijkstra/DijkstraTreeTF-split-o6.pdf}}}%
\rlap{\onslide<7>{\includegraphics[width=0.7\linewidth]{Images/Dijkstra/DijkstraTreeTF-split-o7.pdf}}}%
\rlap{\onslide<8->{\includegraphics[width=0.7\linewidth]{Images/Dijkstra/DijkstraTreeTF-split-o8.pdf}}}%
        \vspace{-0.5em}

      \begin{itemize}
        \item
          Each node (pearl) now has a specific height
        \item
          The distance to M is exactly the {\color{MainA}shortest path}
        \end{itemize}
    \end{column}
  \end{columns}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Shortest Path}
  \vspace{-1.5em}
  \begin{figure}
    \begin{adjustbox}{width=0.75\linewidth}
      \input{Images/Dijkstra/ShortestPath.tikz}
    \end{adjustbox}
    \label{fig:dijkstra:shortest_path_introduction}
    \caption{Shortest path from {\color{MainA}$s$} to
    {\color{MainA}$t$}}
  \end{figure}
  \vspace{-1.5em}
  \begin{itemize}
    \item
      Let {\color{MainB}$r$} be the shortest path from
      {\color{MainA}$s$} to {\color{MainA}$t$}
    \item
      For each node {\color{MainA}$u$} on path {\color{MainB}$r$}
      the path from {\color{MainA}$u$} to {\color{MainA}$t$} is
      the shortest path
  \end{itemize}
  \textbf{Proof:}
  \begin{itemize}
    \item
      If there was a shorter path from {\color{MainA}$s$} to
      {\color{MainA}$u$} then we could choose this path to get faster to
      {\color{MainA}$t$}
    \item
      Then {\color{MainB}$r$} would not be the shortest path
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Shortest Path}
  \vspace{-1.5em}
  \begin{figure}
    \begin{adjustbox}{width=0.75\linewidth}
      \input{Images/Dijkstra/ShortestPath.tikz}
    \end{adjustbox}
    \label{fig:dijkstra:shortest_path_introduction_re}
    \caption{Shortest path from {\color{MainA}$s$} to
      {\color{MainA}$t$}}
  \end{figure}
  \vspace{-1.5em}
  \begin{itemize}
    \item
      This is also correct for all sub paths on {\color{MainB}$r$}
    \item
      If the shortest path from {\color{MainA}$s$} to
      {\color{MainA}$t$} passes {\color{MainA}$u_1$} and
      {\color{MainA}$u_2$} then the sub path
      $({\color{MainA}u_1}, {\color{MainA}u_2})$
      is the shortest path from {\color{MainA}$u_1$} to
      {\color{MainA}$u_2$}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Shortest Path}
  \begin{figure}%
    \begin{adjustbox}{width=0.75\linewidth}%
      \input{Images/Dijkstra/ShortestPaths.tikz}%
    \end{adjustbox}%
    \vspace{-1.0em}
    \label{fig:dijkstra:shortest_paths_introduction}%
    \caption{Shortest paths from {\color{MainA}$s$} to
      {\color{MainA}$t$}}
  \end{figure}
  \vspace{-1.0em}
  \begin{itemize}
    \item
      If we know the shortest path form {\color{MainA}$s$}
      to the preceding nodes of {\color{MainA}$t$}
      \begin{math}
        (
          {\color{MainA}v_1},
          {\color{MainA}v_2},
          {\color{MainA}v_3}
        )
       \end{math}
       we can determine the shortest path to {\color{MainA}$t$}
   \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Shortest Path}
  \textbf{Idea:}
  \begin{itemize}
    \item
      Attach the cost of the shortest path to each node
    \item
      Let the information travel over the edges (message passing)
    \item
      In which order should we process the nodes?
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}
  \begin{columns}
    \begin{column}{0.6\linewidth}
      \textbf{Inventor:}
      \begin{itemize}
        \item
          Edsger Dijkstra (1930 - 2002)
        \item
          Computer scientist from Netherlands
        \item
          Won Turing-Award as one of few Europeans for his studies of
          structured programming
        \item
          Invented the Dijkstra-Algorithm in 1959
      \end{itemize}
    \end{column}
    \begin{column}{0.4\linewidth}
      \begin{figure}
        \includegraphics[width=0.75\linewidth]
          {Images/Dijkstra/Edsger_Wybe_Dijkstra.jpg}
        \caption{Portrait \copyright\; Hamilton Richards - manuscripts of
          Edsger W. Dijkstra, University Texas at Austin}
        \label{fig:dijkstra:portrait}
      \end{figure}
    \end{column}
  \end{columns}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}
  \vspace{-1.5em}
  \begin{columns}
    \begin{column}{0.55\linewidth}
      \textbf{Example:}
      \begin{itemize}
        \item
          Lift pearl {\color{MainA}$A$} a little bit
        \item
          Connection to pearl {\color{MainA}$B$} is hanging in the air
        \item
          Lift further until pearl {\color{MainA}$B$} starts to lift at
          $\SI{5}{m}$
        \item
          The shortest path to {\color{MainA}$B$} is now known
        \item
          Lift further: The wires from {\color{MainA}$C$},
          {\color{MainA}$D$}, {\color{MainA}$E$} and {\color{MainA}$F$}
          are now in the air
        \item
          One of the pearls {\color{MainA}$C$}, {\color{MainA}$D$},
          {\color{MainA}$E$} or {\color{MainA}$F$} is the next one\\
          {\color{gray}Which one?}
      \end{itemize}
    \end{column}
    \begin{column}{0.45\linewidth}
      \begin{figure}[!t]
        \includegraphics[width=\linewidth]
          {Images/Dijkstra/DijkstraTreeTF.pdf}
      \end{figure}
    \end{column}
  \end{columns}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}
  \vspace{-1.5em}
  \begin{columns}
    \begin{column}{0.55\linewidth}
      \textbf{Example:}
      \begin{itemize}
        \item
          At $\SI{11}{m}$ pearl {\color{MainA}$C$} gets lifted
        \item
          The wire to {\color{MainA}$D$} is now in the air
        \item
          One of the pearls {\color{MainA}$D$}, {\color{MainA}$E$}
          and {\color{MainA}$F$} is the next one\\
          {\color{gray}Which one?}
        \item
          At $\SI{12}{m}$ pearl {\color{MainA}$D$} gets lifted\\
          $\cdots$
        \item
          {\color{gray}How to translate this into an computer algorithm?}
      \end{itemize}
    \end{column}
    \begin{column}{0.45\linewidth}
      \begin{figure}[!t]
        \includegraphics[width=\linewidth]
          {Images/Dijkstra/DijkstraTreeTF.pdf}
      \end{figure}
    \end{column}
  \end{columns}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}
  \textbf{High level description:}
  Three types of nodes
  \begin{itemize}
    \item
      {\color{MainA}Settled:}
      For node {\color{MainA}$u$} we know
      {\color{MainA}$\mathrm{dist}(s, u)$}
      \hfill
      \raisebox{-0.5em}{\begin{adjustbox}{height=1.5em}%
        \input{Images/Dijkstra/Vertex/Vertex_Settled.tikz}%
      \end{adjustbox}}\\
      {\color{gray}(Pearl example: This pearl is hanging in the air)}
      \vspace{0.5em}
    \item
      {\color{MainA}Active:}
      For node {\color{MainA}$u$} we know a tentative distance
      {\color{MainA}$\mathrm{td}(u) \geq \mathrm{dist}(s, u)$}
      (Can be optimal but doesn't have to)
      \hfill
      \raisebox{-0.5em}{\begin{adjustbox}{height=1.5em}%
        \input{Images/Dijkstra/Vertex/Vertex_Active.tikz}%
      \end{adjustbox}}\\
      {\color{gray}(Pearl example: This pearl is laying on the table but\\
        one connected wire is already in the air)}
        \vspace{0.5em}
    \item
      {\color{MainA}Unreached:}
      We have not reached the node yet
      \hfill
      \raisebox{-0.5em}{\begin{adjustbox}{height=1.5em}%
          \input{Images/Dijkstra/Vertex/Vertex_Unreached.tikz}%
        \end{adjustbox}}\\
        {\color{gray}(Pearl example: This preal is hanging in the air)}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}
  \textbf{High level description:}
  \begin{itemize}
    \item
      Each iteration take the {\color{MainA}active} node
      {\color{MainA}$u$} with the
      {\color{MainA}smallest $\mathrm{td}(u)$}\\
      {\color{gray}(The pearl getting lifted next)}
    \item
      We update the state of the node {\color{MainA}$u$} to
      {\color{MainA}settled}\\
      {\color{gray}(The pearl gets lifted)}
    \item
      We check for each {\color{MainA}neighbor $v$} of node
      {\color{MainA}$u$} if we can reach {\color{MainA}$v$} faster
      than currently possible\\
      {\color{gray}(Check all outgoing wires from this pearl:
        Activate all connected pearls, update
        tentative distance if smaller)}
    \item
      Iterate until no active nodes exist anymore
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}
  \vspace{-1em}
  \begin{figure}[!h]
    \begin{adjustbox}{width=\linewidth}
      \def\DijkstraReverse{0}%
      \input{Images/Dijkstra/Dijkstra_Algorithm.tikz}
    \end{adjustbox}
    \vspace{-2em}
    \caption{%
      \only<1>{Start at {\color{MainA}$u_1$}}%
      \only<2>{Iteration 1}%
      \only<3>{Iteration 2}%
      \only<4>{Iteration 3}%
      \only<5>{Iteration 4}%
      \only<6>{Iteration 5}%
      \only<7>{Iteration 6}%
    }
  \end{figure}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Proof}
  \textbf{Proof:}
  \begin{itemize}
    \item
      \textbf{Assumption 1:}
      All edges have a positive length
    \item
      \textbf{Assumption 2:}
      Each node has a unique distance
      {\color{MainA}$\mathrm{dist}(s, u)$} to the start {\color{MainA}$s$}\\
      {\color{gray}(This was not the case on the previous slides)}\\[0.5em]
      This results in an easy and intuitive proof.\\
      {\color{gray}
        It is possible to show this without assumption 2.
        See references if interested}
      \vspace{1.0em}
    \item
      With assumption 2 there exists a sorting {\color{MainA}$u_1$},
      {\color{MainA}$u_2$}, $\ldots$ with that:
      \begin{displaymath}
        {\color{MainA}\mathrm{dist}(s, u_1)} <
        {\color{MainA}\mathrm{dist}(s, u_2)} <
        {\color{MainA}\mathrm{dist}(s, u_3)} <
        \ldots
      \end{displaymath}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Proof}
  \textbf{Proof:}
  \begin{itemize}
    \item
      With \textbf{assumption 2} there exists a sorting {\color{MainA}$u_1$},
      {\color{MainA}$u_2$}, $\ldots$ with that:
      \begin{displaymath}
        {\color{MainA}\mathrm{dist}(s, u_1)} <
        {\color{MainA}\mathrm{dist}(s, u_2)} <
        {\color{MainA}\mathrm{dist}(s, u_3)} <
        \ldots
      \end{displaymath}
    \item
      We want to show that the \textit{Dijkstra} algorithm finds the shortest
      path for each node {\color{MainA}$u_i$} so that
      {\color{MainA}$\mathrm{td}(u_i) = \mathrm{dist}(s, u_i)$} holds
    \item
      Additionally we show that each node gets solved in order of the distance:
      Node {\color{MainA}$u_i$} gets solved in iteration
      {\color{MainA}$i$}
      \begin{displaymath}
        {\color{MainA}u_1},
        {\color{MainA}u_2},
        {\color{MainA}u_3},
        \ldots
      \end{displaymath}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Proof}
  \textbf{To show:} Node {\color{MainA}$u_i$} gets solved in round
  {\color{MainA}$i$}
  \begin{enumerate}
    \item
      Node {\color{MainA}$u_i$} contains the correct distance
      ({\color{MainA}$\mathrm{td}(u_i) = \mathrm{dist}(s, u_i)$})
      and is active
    \item
      Node {\color{MainA}$u_i$} has the smallest value for
      {\color{MainA}$\mathrm{td}(u_i)$} and gets selected by the algorithm
  \end{enumerate}
  \vspace{1.0em}
  \textbf{Induction start:}
  \begin{enumerate}
    \item
      \begin{itemize}
        \item
          Only the start node {\color{MainA}$s = u_1$} is active and
          {\color{MainA}$\mathrm{td}(s) = 0$}
        \item
          Node {\color{MainA}$u_1$} gets solved and
          {\color{MainA}$\mathrm{td}(u_1) = \mathrm{dist}(s, u_1) = 0$}
      \end{itemize}
    \item
      Only the start node {\color{MainA}$u_1$} is active
  \end{enumerate}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Proof}
  \textbf{Induction step:} {\color{MainA}$i = i+1$}
  \begin{enumerate}
    \item
      \textbf{To show:} Node {\color{MainA}$u_{i+1}$} contains the correct
      distance ({\color{MainA}$\mathrm{td}(u_{i+1}) = \mathrm{dist}(s, u_{i+1})$})
      and is active
      \begin{itemize}
        \item
          On the shortest path from {\color{MainA}$s$} to
          {\color{MainA}$u_{i+1}$} is a preceding node that:
          \begin{displaymath}
            \color{MainA}\mathrm{dist}(s, u_{i+1})
            = \mathrm{dist}(s, v) + \mathrm{c}(v, u_{i+1})
          \end{displaymath}
          ({\color{MainA}$\mathrm{c}(v, u_{i+1})$} are the costs of the edge)
          \begin{figure}[!h]
            \begin{adjustbox}{width=0.9\linewidth}
              \def\DijkstraTD{0}%
              \input{Images/Dijkstra/Dijkstra_InductionOrder.tikz}
            \end{adjustbox}
          \end{figure}
        \item
          Hence
          {\color{MainA}$\mathrm{dist}(s, v) < \mathrm{dist}(s, u_{i+1})$}
          because {\color{MainA}$c > 0$} ($c$=cost of edge)
        \item
          Because {\color{MainA}$u_{i+1}$} is currently settled, the node
          {\color{MainA}$v$} is one of the preceding nodes
          {\color{MainA}$u_1, \dots, u_i$}, hence
          {\color{MainA}$v = u_j$} with
          {\color{MainA}$0 \leq j \leq i$}
      \end{itemize}
  \end{enumerate}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Proof - Example of Iteration 6}
  \begin{figure}[!h]
    \begin{adjustbox}{width=0.9\linewidth}
      \input{Images/Dijkstra/Dijkstra_Induction.tikz}
    \end{adjustbox}
  \end{figure}
  \begin{itemize}
    \item
      Preceding node of {\color{MainA}$u_6$} is
      {\color{MainA}$v = u_3$}
    \item
      In round 3
      \begin{math}
        {\color{MainA}\mathrm{td}(u_6) = }
        {\color{Hell-Gruen}\;2 + 4\;}
        {\color{MainA} = 6}
      \end{math}
      was already solved
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Proof}
  \begin{figure}[!h]
    \begin{adjustbox}{width=0.9\linewidth}
      \def\DijkstraTD{1}%
      \input{Images/Dijkstra/Dijkstra_InductionOrder.tikz}
    \end{adjustbox}
  \end{figure}
  \begin{enumerate}
    \item
      \textbf{To show:} Node {\color{MainA}$u_{i+1}$} contains the correct
      distance {\color{MainA}$\mathrm{td}(u_{i+1}) = \mathrm{dist}(s, u_{i+1})$}
      and is active
      \begin{itemize}
        \item
          With \textbf{induction assumption}:
          {\color{MainA}$v$} already contains the correct distance which
          was evaluated in round {\color{MainA}$j$}\\
          (edge from {\color{MainA}$v$} to {\color{MainA}$u_{i+1}$})
          and is stored in {\color{MainA}$\mathrm{td}(u_{i+1})$}
        \item
          {\color{MainA}$u_{i+1}$} is active because the preceding node
          was solved
      \end{itemize}
  \end{enumerate}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Proof}
  \begin{figure}[!h]
    \begin{adjustbox}{width=0.9\linewidth}
      \def\DijkstraTD{1}%
      \input{Images/Dijkstra/Dijkstra_InductionOrder.tikz}
    \end{adjustbox}
  \end{figure}
  \begin{enumerate}
    \setcounter{enumi}{1}
    \item
      \textbf{To show:} Node {\color{MainA}$u_{i+1}$} has the smallest
      value for {\color{MainA}$\mathrm{td}(u_{i+1})$} and gets selected by
      the algorithm
      \begin{itemize}
        \item
          All nodes with smaller {\color{MainA}$\mathrm{dist}$} are
          already solved
        \item
          All other nodes {\color{MainA}$u_k$} with
          {\color{MainA}$k > i + 1$} have a greater
          {\color{MainA}$\mathrm{dist}(s, u_k)$}
          and with that the {\color{MainA}$\mathrm{td}(u_k)$} is greater
          or equal
        \item[$\Rightarrow$]
          {\color{MainA}$u_{i+1}$} is the node with the
          smallest {\color{MainA}$\mathrm{td}$} and gets selected by the
          algorithm
      \end{itemize}
  \end{enumerate}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Implementation}
  \textbf{Implementation:}
  \begin{itemize}
    \item
      We have to manage a set of {\color{MainA}active nodes}
    \item
      We start with only the {\color{MainA}start node} in our set
    \item
      At the start of each iteration we need the node
      {\color{MainA}$u$} with the smallest
      {\color{MainA}$\mathrm{td}(u)$}\\[0.5em]
      {\color{gray}How to implement this?}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Implementation}
  \textbf{Implementation:}
  \begin{itemize}
    \item
      Using a {\color{MainA}priority queue} with
      {\color{MainA}$\mathrm{td}(u)$} as keys
    \item
      The following problem occurs:
      \begin{itemize}
        \item
          The {\color{MainA}tentative distance} of an active node might
          change multiple times before it is settled
        \item
          We have to change the key in our {\color{MainA}priority queue}
          without removing the entry
      \end{itemize}
  \end{itemize}
  \vspace{1em}
  \textbf{Limitations:}
  \begin{itemize}
    \item
    Often only \texttt{insert}, \texttt{getMin} and \texttt{deleteMin}
    are implemented
    \item[$\Rightarrow$]
    We only have access to the first element and not any desired one
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Implementation}
  \textbf{Alternative:}
  \begin{itemize}
    \item
      If a node reoccurs with a smaller {\color{MainA}$\mathrm{dist}$}
      we insert the element one more time into the
      {\color{MainA}priority queue}\\
      {\color{gray}(We do nothing if the distance is greater or equal)}
    \item
      We do not remove the old entry
    \item
      The node always gets solved with the smallest distance because of the
      {\color{MainA}smaller key}
    \item
      If a settled node reoccurs with a higher {\color{MainA}$\mathrm{dist}$}
      we remove it and do simply {\color{MainA}nothing}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

% \begin{frame}{Dijkstra Algorithm}{Implementation - Example}
%   \begin{columns}[T]
%     \begin{column}{0.6\linewidth}
%       \begin{center}
%         \begin{adjustbox}{width=0.9\linewidth}
%           \input{Images/Dijkstra/PriorityQueue.tikz}
%         \end{adjustbox}
%       \end{center}
%     \end{column}
%     \begin{column}{0.4\linewidth}
%       Priority queue:
%     \end{column}
%   \end{columns}
% \end{frame}

\begin{frame}{Dijkstra Algorithm}{Implementation - Example}
  \begin{tabular}{@{}l@{}}
\rlap{\onslide<1>{\includegraphics[width=1.1\textwidth]{Images/Dijkstra/disjkstra-priority-queue-o1.pdf}}}%
\rlap{\onslide<2>{\includegraphics[width=1.1\textwidth]{Images/Dijkstra/disjkstra-priority-queue-o2.pdf}}}%
\rlap{\onslide<3>{\includegraphics[width=1.1\textwidth]{Images/Dijkstra/disjkstra-priority-queue-o3.pdf}}}%
\rlap{\onslide<4>{\includegraphics[width=1.1\textwidth]{Images/Dijkstra/disjkstra-priority-queue-o4.pdf}}}%
\rlap{\onslide<5>{\includegraphics[width=1.1\textwidth]{Images/Dijkstra/disjkstra-priority-queue-o5.pdf}}}%
\rlap{\onslide<6>{\includegraphics[width=1.1\textwidth]{Images/Dijkstra/disjkstra-priority-queue-o6.pdf}}}%
\rlap{\onslide<7>{\includegraphics[width=1.1\textwidth]{Images/Dijkstra/disjkstra-priority-queue-o7.pdf}}}%
\rlap{\onslide<8>{\includegraphics[width=1.1\textwidth]{Images/Dijkstra/disjkstra-priority-queue-o8.pdf}}}%
\rlap{\onslide<9>{\includegraphics[width=1.1\textwidth]{Images/Dijkstra/disjkstra-priority-queue-o9.pdf}}}%
\rlap{\onslide<10>{\includegraphics[width=1.1\textwidth]{Images/Dijkstra/disjkstra-priority-queue-o10.pdf}}}%
\rlap{\onslide<11->{\includegraphics[width=1.1\textwidth]{Images/Dijkstra/disjkstra-priority-queue-o11.pdf}}}
  \end{tabular}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Runtime analysis}
  Graph with {\color{MainA}$n$} nodes and {\color{MainA}$m$}
  edges: ({\color{MainA}$m \geq n$})
  \begin{itemize}
    \item
      Each node gets solved exactly {\color{MainA}one time}
    \item
      When solving a node it's outgoing edges are taken into account
    \item
      Each edge triggers at maximum one \texttt{insert} operation
    \item
      The number of operations on the {\color{MainA}priority queue}
      is at maximum {\color{MainA}$O(m)$}
    \item
      This results in a runtime of
      {\color{MainA}$O(m \cdot \log m)$}\\
      {\color{gray}($\log m$ because of at max. $m$ elements in the priority
        queue)}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Runtime analysis}
  Runtime of {\color{MainA}$O(m \cdot \log m)$}:
  \begin{itemize}
    \item
      Because of {\color{MainA}$m \leq n^2$} we have a maximum runtime of
      {\color{MainA}$O(m \cdot \log n)$}, because
      {\color{MainA}$\log n^2 = 2 \, \log n$}
    \item
      With a complex {\color{MainA}priority queue} the runtime can be
      reduced to {\color{MainA}$O(m + n \, \log n)$}
      \begin{itemize}
        \item
          For example with a \textbf{Fibonacci heap}
        \item
          This results in a better runtime for complex graphs
          {\color{MainA}$m \sim n^2$}
        \item
          Complex heaps create a management overhead
        \item[$\Rightarrow$]
          In practice {\color{MainA}$m \in O(n)$} with a
          \textbf{binary heap} being faster\\
          {\color{gray}(See lecture 6)}
      \end{itemize}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Additional comments}
  \textbf{Termination criteria:}
  \begin{itemize}
    \item
      Terminate as soon as the target node {\color{MainA}$t$} is settled\\
      $\ldots$ never before because tentative distance might change:
      \begin{displaymath}
        {\color{MainA}\mathrm{td}(t) \geq \mathrm{dist}(s, t)}
      \end{displaymath}
    \item
      Before the node {\color{MainA}$t$} is solved
      {\color{MainA}all nodes $u$} with
      {\color{MainA}$\mathrm{dist}(s, u) \leq \mathrm{dist}(s, t)$}
      are settled
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Additional comments}
  \textbf{Termination criteria:}
  \begin{itemize}
    \item
      Not only the {\color{MainA}single source \textbf{single} target}
      shortest path problem is solved by the Dijkstra algorithm but also the
      {\color{MainA}single source \textbf{all} targets} problem
    \item
      This sounds wasteful but there is not a (much) better method for general
      graphs\\
      \textbf{Intuitive:} We only know that there is no shorter path if
      all nodes in the distance of {\color{MainA}$\mathrm{dist}(s, t)$} are
      evaluated
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Additional comments}
  \textbf{Calculate the shortest path:}
  \begin{itemize}
    \item
      With the current implementation of the Dijkstra algorithm we only get
      the {\color{MainA}length} of the path\\
      {\color{gray}How to get the path itself too?}
    \item
      If we save the preceding node of the current shortest path on
      {\color{MainA}settling} of each node we can reconstruct the
      {\color{MainA}path}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}
  \vspace{-1em}
  \begin{figure}[!h]
    \begin{adjustbox}{width=\linewidth}
      \def\DijkstraReverse{1}%
      \input{Images/Dijkstra/Dijkstra_Algorithm.tikz}
    \end{adjustbox}
    \vspace{-2em}
    \caption{%
      \only<1>{Start at {\color{MainA}$u_1$}}%
      \only<2>{Iteration 1}%
      \only<3>{Iteration 2}%
      \only<4>{Iteration 3}%
      \only<5>{Iteration 4}%
      \only<6>{Iteration 5}%
      \only<7>{Iteration 6}%
    }
  \end{figure}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Additional comments}
  \textbf{Enhancement:}
  \begin{itemize}
    \item
      In our proof we used the assumption that all costs are
      {\color{MainA}not~negative} (even $> 0$)
    \item
      With {\color{MainA}negative costs} there might be
      {\color{MainA}negative cycles}:
  \end{itemize}
  \begin{columns}
    \begin{column}{0.4\linewidth}
      \begin{figure}[!h]
        \begin{adjustbox}{width=0.7\linewidth}
          \input{Images/Dijkstra/NegativePath.tikz}
        \end{adjustbox}
        \caption{Here no problem $\ldots$}
      \end{figure}
    \end{column}
    \begin{column}{0.4\linewidth}
      \begin{figure}[!h]
        \begin{adjustbox}{width=0.7\linewidth}
          \def\DijkstraNegativeCycle{0}%
          \input{Images/Dijkstra/NegativeCycle.tikz}
        \end{adjustbox}
        \caption{$\ldots$ but here}
      \end{figure}
    \end{column}
  \end{columns}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Additional comments}
  \textbf{Negative cycles:}
  \begin{columns}[c]
    \begin{column}{0.7\linewidth}
      \begin{adjustbox}{width=\linewidth}
        \def\DijkstraNegativeCycle{1}%
        \input{Images/Dijkstra/NegativeCycle.tikz}
      \end{adjustbox}
    \end{column}
    \begin{column}{0.3\linewidth}
      \begin{itemize}
        \item
          No cycle:\\
          {\color{MainA}cost of 1}
        \item
          1 cycle:\\
          {\color{MainA}cost of 0}
        \item
          2 cycles:\\
          {\color{MainA}cost of -1}
        \item
          3 cycles:\\
          {\color{MainA}cost of -2}
        \item
          $\cdots$
      \end{itemize}
    \end{column}
  \end{columns}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Additional comments}
  \textbf{Enhancement:}
  \begin{itemize}
    \item
      We need a different algorithm to deal with negative edges
      \begin{itemize}
        \item
          For example the \textbf{Bellman-Ford} algorithm
        \item
          If the graph is {\color{MainA}acyclic} we can simply use a
          topological sorting (with DFS) and settling the nodes in order
          of this sorting
      \end{itemize}
    \item
      Another (not only) in artificial intelligence used variant of the Dijkstra
      algorithm is the {\color{MainA}A* algorithm}\\
      Additional information given:\\
      \hspace{1.5em}{\color{MainA}$h(u)$}
      $=$ estimated value for {\color{MainA}$\mathrm{dist}(u, t)$}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Example - Negative costs (e-car consumption)}
  \textbf{Dijkstra algorithm:}\\
  Message passing only from solved nodes
  \begin{figure}
    \begin{adjustbox}{width=0.8\linewidth}
      \def\DijkstraAlgorithm{1}%
      \input{Images/Dijkstra/MessagePassing.tikz}
    \end{adjustbox}
  \end{figure}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Example - Negative costs (e-car consumption)}
  \textbf{Bellman-Ford algorithm:}\\
  Message passing from all nodes until the path lengths are stable
  \begin{figure}
    \begin{adjustbox}{width=0.8\linewidth}
      \def\DijkstraAlgorithm{0}%
      \input{Images/Dijkstra/MessagePassing.tikz}
    \end{adjustbox}
  \end{figure}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Application}
  \textbf{Application example:}
  \begin{itemize}
    \item
      Route planner for car trips (exercise sheet)
    \item
      Route planner for bus / train connections\\
      {\color{gray}What could the graph look like?}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Application}
  \textbf{Space-time graph:}
  \begin{adjustbox}{width=1.1\linewidth}
    \input{Images/Dijkstra/ApplicationExample.tikz}
  \end{adjustbox}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Application in image processing}
  \begin{columns}[T]%
    \begin{column}{\linewidth-6em}%
      \vspace{-1em}
      \begin{figure}[!h]%
        \includegraphics[width=0.7\linewidth]%
          {Images/Dijkstra/Axon.jpg}%
        \caption{Neurons under fluorescence microscope}%
      \end{figure}%
    \end{column}%
    \begin{column}{6em}%
      \qrcode[height=6em]%
        {http://www.imagescience.org/meijering/software/neuronj/}%
    \end{column}%
  \end{columns}%
  \begin{itemize}
    \item
      \textbf{Task:}
      Measure length of axons (connections of neurons)
    \item
      Demo with ImageJ plugin NeuronJ\\
      \url{http://www.imagescience.org/meijering/software/neuronj/}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Dijkstra Algorithm}{Application: Trace axons}
  \begin{adjustbox}{width=0.9\linewidth}
    \input{Images/Dijkstra/Axon_CloseUp.tikz}
  \end{adjustbox}
  \begin{itemize}
    \item
      Image as graph: Each pixel is a node
    \item
      Implicit edges: Each pixel has an edge to it's 8 neighbours
      {\color{gray}(no need to save the edges)}
    \item
      Costs for nodes (not edges): bright pixels are cheap,\\
      dark pixels are costly
  \end{itemize}
\end{frame}

%%% ===================================================================
%%% This should be at the END of the file !!!!!!
%%%
%%% Local Variables:
%%% mode: latex
%%% TeX-master: "~/TeX/TeXinput/Scripts/Algo-Data-EMS/AlgoDat/Lecture-13/Lecture.tex"
%%% End:
%%% ===================================================================


\documentclass{article}
\usepackage{amsmath}
\usepackage[margin=1.0in]{geometry}
\usepackage{xcolor}

\begin{document}

\noindent
Does $\displaystyle \sum_{n=1}^\infty \frac{(-1)^nn}{n+1}$
diverge, converge absolutely, or converge conditionally?

\subsection*{Note}

While the series $\displaystyle \sum_{n=1}^\infty \frac{(-1)^nn}{n+1}$ is alternating, the sequence $b_n = |a_n| = \frac{n}{n+1}$ satisfies NEITHER property: $b_n$ is not decreasing and the sequence $b_n$ does not have limit zero. So the Alternating Series Test doesn't apply. {\color{red} It is not at this point that we declare the series to diverge. Rather, trying the Alternating Series Test made sense because we had an alternating series, but the problem is that we got NO INFORMATION.} We must try a different test.

\subsection*{Solution}

\begin{align*}
\lim_{n \to \infty} a_n
&= \lim_{n \to \infty} \frac{(-1)^nn}{n+1}
\end{align*}
does not exist (as we get every other term of this sequence to be a number close to $1$ and every other term of this sequence to be a number close to $-1$). Since $\displaystyle \lim_{n \to \infty} a_n$ does not exist, the series $\displaystyle \sum_{n=1}^\infty \frac{(-1)^nn}{n+1}$ diverges by the Test for Divergence.

\end{document}%%%%%%%%%%%%%%%%%

\begin{align*}
L&=\lim_{n \to \infty} \sqrt[n]{|a_n|}\\
&= \lim_{n \to \infty} \sqrt[n]{\left| \right|}\\
\end{align*}

\begin{align*}
\lim_{n \to \infty} a_n
&= \lim_{n \to \infty} \\
\end{align*}

Since $\sum |a_n| = \sum a_n$, the series $\displaystyle \sum_{n=1}^\infty AAAAAAAAAAAAAA$ converges absolutely.

Since $|r| < 1$, the series ...  converges by the Geometric Series Test.

Since $|r| \geq 1$, the series ...  diverges by the Geometric Series Test.

The function $f(x)=\frac{}{}$ is continuous, positive, and decreasing on $[1,\infty)$.

\subsection*{Solution}


\documentclass[letterpaper, 10pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amsthm,amssymb,scrextend}
\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{silence}
\WarningFilter{latex}{You have requested package}
\input{ltx/pkg/preamble}

\begin{document}

\lhead{MAT224 Linear Algebra II}
\chead{Linear Combinations \& Independence}
\rhead{Week 02}

\title{Linear Algebra II \\ \Large{MAT224}}
\author{Lennart Döppenschmitt}
% \maketitle
% \tableofcontents

\section*{Linear combinations}%
\label{sec:Linear combinations}

\textbf{Textbook:} Section 1.3

\lb
\textbf{Definition 1.3.1}
\lb
\begin{enumerate}
\item
Let $V$ be a vector space and $ \cb{\vec v_1, \ldots, \vec v_k}$ be a family of vectors
in $V$. A \emph{linear combination} of vectors $\vec v_1, \ldots, \vec v_k$ in $S$
with \emph{coefficients} $α_1, \ldots, α_k ∈ \R$ is a sum
\[ α_1 \vec v_1 + α_2 \vec v_2 +  \cdots + α_k \vec v_k \]
\item
A linear combination where all coefficients are zero is called \emph{trivial}.
\item
To abbreviate a linear combination as above, we may write
\[ \Sum[i=1][k] α_i \vec v_i\]
\end{enumerate}

\lb
Notice that this is the most arbitrary vector we can build with the vectors
$\vec v_1, \ldots \vec v_k$ available in $S$.

\lb
\textbf{Example}
\lb
\begin{enumerate}
    \item
        Polynomials in $\pol{3}{\R}$ are linear combinations of the \emph{monomials}
        $\cb{1, x, x^2, x^3}$.
    \vspace{200pt}
    \item
        Let $\Bigg \{\vec e_1 = \tttpl{1}{0}{0} \vec e_2 = \tttpl{0}{1}{0}, \vec e_3 = \tttpl{0}{0}{1} \Bigg \}$
        be the standard vectors in $\R^3$. Observe that every posible vector in
        $\R^3$ of the form $\tttpl{x}{y}{z}$ is a linear combination of these
        standard vectors.
\end{enumerate}

\newpage
% \lb
% \textbf{Lemma}
% \lb
% Let $V$ be a vector space with a subspace $U \subseteq V$ and
% $S = \cb{ \vec v_1, \ldots, \vec v_k}$ a family of vectors.
% \lb
% If $\vec v_i ∈ U$ for all $i = 1, \ldots, k$, then all linear combinations are in $U$ as well.

\lb
\textbf{Definition 1.3.1} (continued)
\lb
Let $V$ be a vector space and $S = \cb{\vec v_1, \ldots, \vec v_k}$ be a family of vectors
in $V$. The set of all linear combinations of vectors in $S$ is called the \emph{span of S}
and denoted
\[ \spn{S} = \spn{\vec v_1, \ldots, \vec v_k} \]

\pr
By convention, $\spn{\emptyset} = \cb{\vec 0}$.

\lb
\textbf{Example}
\lb
\begin{enumerate}
    \item \[ \pol{3}{\R} = \spn{1, x, x^2, x^3} \]
    \item \[ \mat{2}{\R} = \spn{\qquad ? \qquad } \]
\end{enumerate}

\newpage
\lb
\textbf{Discussion}
\lb
Let $R$ and $S$ be two families of vectors in a vector space $V$.
\begin{enumerate}
    \item
        If $R$ is contained in $S$, show that $\spn{R}$ is contained in $\spn{S}$.
    \item
        Show that we have the following equality of sets.
        \[ \spn{ R \cup S} = \spn R + \spn S \]
    \item
        Is it true that $R \subseteq \spn R$?
\end{enumerate}

\vspace{300pt}

\lb
\textbf{Theorem 1.3.4}
\lb
For any vector space $V$ and family of vectors
$S = \cb{\vec v_1, \ldots, \vec v_k}$ in $V$
\[ \spn{S} \subseteq V\] is a subspace of $V$.
\begin{proof}

\end{proof}

\newpage
\lb
\textbf{Corollary}
\lb
The spans of two families of vectors
$R = \cb{\vec v_1, \ldots, \vec v_k}$ and
$S = \cb{\vec w_1, \ldots, \vec w_l}$ are equal
\[ \spn R = \spn S \]
if $R$ is contained in $\spn S$ and $S$ is contained in $\spn R$.

\vspace{200pt}
\lb
\textbf{Discussion}
\lb
Consider in $\pol{3}{\R}$ the family of polynomials
\[ S = \cb{1 ~,~ x-2x^2 ~,~ 2x^2+3x^3 ~,~ 1+4x^2 ~,~ 5x^3} \]
\begin{enumerate}
    \item Show that $ \cb{1, x, x^2, x^3} \subseteq \spn S \subseteq \pol{3}{\R}$
    \item Use the Corollary above to conclude that $\spn S = \pol{3}{\R}$
\end{enumerate}

\newpage
\section*{Linear Independence}%
\label{sec:Linear Independence}

\textbf{Textbook:} Section 1.4

\lb
\textbf{Prelude}
\lb
We have seen that a vector space can have different spanning sets, for example:
\begin{align*}
    \R^2 &= \tx{span} \Bigg \{ \ttpl{1}{0}, \ttpl{0}{1} \Bigg \} \\
    \R^2 &= \tx{span} \Bigg \{ \ttpl{1}{1}, \ttpl{-1}{1}, \ttpl{2}{0} \Bigg \}
\end{align*}
\q{How do these two spans differ?}
\lb
\lb
\lb
\lb
\lb
\lb
\lb
\textbf{Definition 1.4.2}
\lb
Let $S = \cb{\vec v_1, \ldots, \vec v_k} $ be a family of vectors in a vector space $V$.
\begin{enumerate}
    \item
        The family of vectors is called \emph{linearly independent} if only the
        trivial linear combination of the vectors in $S$ is equal to zero.
        \pr
        That is,
        \[ α_1 \vec v_1 + \cdots + α_k \vec v_k = \vec 0 \]
        only for the coefficients $α_1 = α_2 = \cdots = α_k = 0$.
    \item
        In he opposite case, when there does exist a nontrivial combination of the vectors in $S$
        which is zero, we call the family \emph{linearly dependent}.
\end{enumerate}

\lb
\textbf{Intuition}
\lb
When does a linear combinatin of vectors equal to zero? It means that the concatenation of
`arrows` representing the vectors results in a loop.
\lb
Moreover, such a loop is trivial if it is just a point, i.e. no interior area.

\newpage

\lb
\textbf{Example}
\lb
\begin{enumerate}
    \item The family of polynomials $ \cb{1+x ~,~ 1-x ~,~ 1+2x}$ in $\pol{1}{\R}$ is linearly
        dependent
    \item The family of monomials $ \cb{1 ~,~ x ~,~ x^2}$ in $\pol{2}{\R}$ is linearly
        independet
\end{enumerate}

\lb
\textbf{Discussion}
\lb
Can yo ufind a linearly dependent family of four vectors in $\R^3$
such that any three of them are linealry independent?

\vspace{200pt}
\lb
\textbf{Theorem} (Reduce)
\lb
If $ \cb{\vec v_1, \ldots, \vec v_k}$ is a linearly dependent family of vectors
in a vector space $V$, there exists an index $j ∈ \cb{1, \ldots, k}$ such that
\begin{enumerate}
    \item
        $\vec v_j ∈ \spn{\vec v_1, \ldots, \vec v_{j-1}}$
    \item
        $\spn{\vec v_1, \ldots, \widehat{\vec v_j}, \ldots, \vec v_k}
        = \spn{\vec v_1, \ldots, \vec v_k}$
\end{enumerate}
\begin{proof}
\end{proof}

\newpage
\lb
\textbf{Example}
\lb
Verify that the above lemma applies to the example in the prelude.
\vspace{100pt}

\vspace{200pt}
\lb
\textbf{Theorem} (Extend)
\lb
If $ S = \cb{\vec v_1, \ldots, \vec v_k}$ is a linearly independent family of vectors
in a vector space $V$ that does not span $V$,
then
\begin{enumerate}
    \item
        There exists $\vec v \in V$ such that $\vec v \ni \spn S$.
    \item
        The family $ \cb{\vec v_1, \ldots, \vec v_k, \vec v}$ is linearly independent
\end{enumerate}
\begin{proof}
\end{proof}

\newpage
\lb
\textbf{Theorem}
\lb
Let $ S = \cb{\vec v_1, \ldots, \vec v_k}$ be a family of vectors in a vector space $V$.
Then $S$ is linearly independent if and only if every vector in $\spn{S}$ has a
unique representation as a linear combination of vectors in $S$.

\lb
That is, if and only if $S$ is linearly independent, we have that
\[ α_1 \vec v_1 + \cdots + α_k \vec v_k = β_1 \vec v_1 + \cdots + β_k \vec v_k \]
is equivalent with $α_i = β_i$ for all $i ∈ \cb{1, \ldots, k}$.
\begin{proof}
\end{proof}

\vspace{230pt}
\lb
\textbf{Intuition}
Can you explain the above theorem graphically?

\newpage
\lb
\textbf{True or False?} (Theorems from above might be helpful)
\lb
\begin{enumerate}
    \item[$\square$]
        If $S$ is an independent family of vectors and $R \subseteq S$, then
        $R$ is also independent.
    \item[$\square$]
        A family $ \cb{\vec v}$ of a single vector is always independent.
    \item[$\square$]
        A family $ \cb{\vec v, \vec w}$ of two vectors is dependent if and only if
        $\vec v$ and $\vec w$ are a multiple of each other.
    \item[$\square$]
        A linearly dependent family $ \cb{\vec v_1, \ldots, \vec v_k}$ only contains
        (sub-) families which are dependent as well.
    \item[$\square$]
        A family $ \cb{\vec v_1, \ldots, \vec v_k}$ containing a linearly dependent
        family $ \cb{\vec v_1, \ldots, \vec v_k} $ is also dependent.
    \item[$\square$]
        Is it possible that two families of vectors $R$ and $S$ are not equal, but
        $\spn R = \spn S$?
    \item[$\square$]
        There is a linearly independent family of fours polynomials in $\pol{2}{\R}$.
\end{enumerate}

\end{document}


\section{Operation of the Currency Input Circuit}

\subsection{Initialization}
\begin{enumerate}
\item After powering on the circuit, reset all of the flip flops to ensure that their value is zero. If this is not done, a user can continuously reboot the machine and collect an amount of change equal to the value of the flip flops as they are powered on. Since they are not guaranteed to initialize to a 1 or 0, skipping this step could lead to losing a lot of money.
\end{enumerate}

\subsection{How to Record an Accepted Bill}
\begin{enumerate}
\item Since we do not have an actual bill reader, we need to set the number of dollars being inserted using switches 1 through 6. These correspond to inputs $B_0$ through $B_5$ to the ALU.
\item While the selector bit is set to addition, generate a single clock pulse by pressing the upper button followed by the lower button.
\item On the falling edge, the flip flops will be set to the sum of the input and the value that is already stored in the flip flops.
\end{enumerate}

\begin{figure}[H]
\centering
\includegraphics[width=16.5cm]{input} \\
%\vspace{1em} \hspace{1em}
\caption{This is the completed input circuit. 4-bit adders (74LS83) are used in the circuit diagram instead of 4-bit ALUs (74\_181) to simplify the diagram. In the actual implementation, the adders are replaced with ALUs.}
\label{input-circuit}
\end{figure}


\chapter{Extending Galois connections between funcoids and reloids}

\begin{defn}
~
\begin{enumerate}
\item $\Phi_{\ast} f = \lambda b \in \mathfrak{B}: \bigsqcup \setcond{ x \in
\mathfrak{A} }{ f x \sqsubseteq b }$;
\item $\Phi^{\ast} f = \lambda b \in \mathfrak{A}: \bigsqcap \setcond{ x \in
\mathfrak{B} }{ f x \sqsupseteq b }$.
\end{enumerate}
\end{defn}

\begin{prop}
  ~
  \begin{enumerate}
    \item If $f$ has upper adjoint then $\Phi_{\ast} f$ is the upper adjoint
    of $f$.

    \item If $f$ has lower adjoint then $\Phi^{\ast} f$ is the lower adjoint
    of $f$.
  \end{enumerate}
\end{prop}

\begin{proof}
  By theorem \bookref{adj-max}.
\end{proof}

\begin{lem}
  $\Phi^{\ast} \torldout = \tofcd$.
\end{lem}

\begin{proof}
  $(\Phi^{\ast} \torldout) f = \bigsqcap \setcond{ g
  \in \mathsf{FCD} }{
 \torldout g \sqsupseteq f } =
  \bigsqcap^{\mathsf{FCD}} \setcond{ g \in \mathbf{Rel}
  }{ \torldout g \sqsupseteq
  f } = \bigsqcap^{\mathsf{FCD}} \setcond{ g \in
  \mathbf{Rel} }{ g \sqsupseteq f } =
  \tofcd f$.
\end{proof}

\begin{lem}
  $\Phi_{\ast} \torldout \neq
  \tofcd$.
\end{lem}

\begin{proof}
  $(\Phi_{\ast} \torldout) f = \bigsqcup \setcond{ g
  \in \mathsf{FCD} }{
 \torldout g \sqsubseteq f }$

  $(\Phi_{\ast} \torldout) \bot \neq \bot$.
\end{proof}

\begin{lem}
  $\Phi^{\ast}  \tofcd = \torldout$.
\end{lem}

\begin{proof}
  $(\Phi^{\ast}  \tofcd) f = \bigsqcap \setcond{ g \in
  \mathsf{RLD} }{ \tofcd g
  \sqsupseteq f } = \bigsqcap^{\mathsf{RLD}} \setcond{ g \in \mathbf{Rel}
  }{ \tofcd g \sqsupseteq f } =
  \bigsqcap^{\mathsf{RLD}} \setcond{ g \in \mathbf{Rel} }{ g
  \sqsupseteq f } = \torldout f$.
\end{proof}

\begin{lem}
  $\Phi_{\ast} \torldin = \tofcd$.
\end{lem}

\begin{proof}
  $(\Phi_{\ast} \torldin) f = \bigsqcup \setcond{ g
  \in \mathsf{FCD} }{
 \torldin g \sqsubseteq f } = \bigsqcup
  \setcond{ g \in \mathsf{FCD} }{ g \sqsubseteq
  \tofcd f } = \tofcd f$.
\end{proof}

\begin{thm}
The picture at figure~\ref{dia:fcd-rld-gal} describes values of functions~$\Phi_{\ast}$ and~$\Phi^{\ast}$.
All nodes of this diagram are distinct.
\begin{figure}
  \begin{tikzcd}
    %\arrow[loop left]{l}{\Phi_{\ast}} \id \arrow[loop right]{r}{\Phi^{\ast}} \\
    \tofcd %\arrow{u}{\Phi^{\ast}}
      \arrow[leftrightarrow, shift left=1.0ex]{r}{\Phi_{\ast}} &
      \torldin \arrow[shift left=1.0ex]{l}{\Phi^{\ast}} \\
    \torldout \arrow[leftrightarrow]{u}{\Phi^{\ast}} \arrow{d}{\Phi_{\ast}} \\
    \text{other}
  \end{tikzcd}
  \caption{\label{dia:fcd-rld-gal}}
\end{figure}
\end{thm}

\begin{proof}
Follows from the above lemmas.
\end{proof}

\begin{question}
What is at the node ``other''?
\end{question}

Trying to answer this question:

\begin{lem}
$(\Phi_{\ast}\torldout)\bot = \Omega^{\mathsf{FCD}}$.
\end{lem}

\begin{proof}
We have $\torldout\Omega^{\mathsf{FCD}} = \bot$.
$x\nsqsubseteq\Omega^{\mathsf{FCD}} \Rightarrow
\torldout x\sqsupseteq\Cor x\sqsupset\bot$.
Thus $\max\setcond{x\in\mathsf{FCD}}{\torldout x=\bot}=
\Omega^{\mathsf{FCD}}$.

So $(\Phi_{\ast}\torldout)\bot = \Omega^{\mathsf{FCD}}$.
\end{proof}

\begin{conjecture}
$(\Phi_{\ast}\torldout)f =
\Omega^{\mathsf{FCD}}\sqcup\tofcd f$.
\end{conjecture}

The above conjecture looks not natural, but I do not see
a better alternative formula.

\begin{question}
What happens if we keep applying $\Phi^{\ast}$ and $\Phi_{\ast}$ to the node ``other''?
Will we this way get a finite or infinite set?
\end{question}


\chapter{Compilers}
\graphicspath{ {./images/} }

\textbf{Compilers}\cite{dragonbook} are that piece of software that \textit{translates} the programs in one language to other languages. They are different from \textbf{Assembler}. An assembler converts the assebly codes to final machines' instruction codes according to the ISA of the target computer hardware. Compilers are generally thought to convert higher level programs to low level programs. They are of different types like Cross compilers, Bootstrap Compilers (it comes under cross compilers)
, Transilers and De-compilers. The basic operations carried by most of the compilers are :\\

\section{Lexical Analysis}
This is the first part of any compiler that takes in the program spurce code and does word-substitutions, Macro expansions, generating \textit{Tokens} and cleaning up extra white spaces. All this work is done by using regular expression to increase compactness of input code. The tokens when combined in a special way form what is known as \textbf{Abstract Syantax Trees (AST)}. The regular expressions are interpreted as \textit{Non-Deterministic Finite Automata (NDFA)} since the states or the inner expression in between any two parts of a Regex can't be fixed since it will vary with the input used. Thus the NDFA is converted to \textit{Deterministic Finite Automata (DFA)} which will be parsed to AST using the technique of \textit{$\epsilon$-Transformation}.\\

\section{Syntax Analysis or Parsing}
This is the process of recombining the context-free grammatical tokens into a data structure, syntax tree. It is important that any sort of syntax error should be reported at this point of compilation. Context-free grammar is a recursive notation for describing sets of strings and imposing a structure on each such string \underline{without consiering the context in which the syntax is generated}.\\

\subsection{Abstract Syntax Trees and Abstract Binding Trees}
In computer science, an abstract syntax tree (AST), or just syntax tree, is a tree representation of the abstract syntactic structure of source code written in a programming language. It is "Abstract" in the sense that it does not represent every detail appearing in the real syntax of the programming language used, but rather just the structural and content-related details.\\

An abstract syntax tree is an ordered tree whose leaves are variables and whose interior nodes are operators whose arguments are its children. Ast’s are classified into a variety of sorts corresponding to different forms of syntax that divide ASTs into syntactic categories. For example, common programming languages often have a syntactic distinction between commands and an expression. These are two type of sorts for ASTs.\\

\textbf{Abstract Binding Trees (ABT)} enrich the meaning of AST with the methods to introduce new variables and symbols called \textit{bindings} to an existing AST. It should be noted that it is a theoretical concept and \underline{only AST is used in compilers while parsing}. The additions are performed within a given range of significance of the added terms called its \textit{scope}. In desinging type systems, we therefore deal with these ABT to construct new language.\\

Writing a new Programming language grammar basically involves writing syntactic sugar for \textit{Expressions, Statements }(often called commands) and \textit{Declaration} like header declarations and variable assisgnments. There are some \textbf{rules} defined and \textit{Judgements and derivations} are performed on them. The derivations are mostly inductive and also covers the finite aspect of machine. This can be understood in a way that DFA is mantained and machine never goes to a NDFA state.\\

For example here is a derivation of successor of a natural number, [ succ(succ(succ(zero)))nat ] :

\[\dfrac{\dfrac{\dfrac{\dfrac{}{zero\quad nat}}{succ(zero) \quad nat}}{succ(succ(zero)) \quad nat}}{succ(succ(succ(zero))) \quad nat} \] .

The denominator in the expression is an inductive derivation from the numberator conditions.

\section{Type Checking}
This is one of the most important part where data consistency is checked of input source code. The Compiler may exploit or depend on type information, which makes it natural to combine calculation of types with the actual translation of source code. A language is considered to be type-safe if does not allow violation of basic data types. The varying level of strictness leads to formation of weak and strong type safe languages.\\

\begin{figure}[!htb]
\centering
  \includegraphics[scale=0.5]{type_check}
  \caption{Design Space of Types (source : Dragon book of compiler Technology)}
\end{figure}

It shows a diagram of the design space of static vs. dynamic and weak vs. strong typing, placing some well-known programming languages in this design space.

\section{Optimization}

This part deals with converting the present code as AST into more efficient using a series of transformations that makes the code use lesser sources but produces same output and variable scope is maintained throughout. Some of the common optimization techniques are :\\

\textbf{Peephole optimizations} replaces a complex multi-step instruction by an instruction of less-number of steps preferably a single step, for example multiplication by 2 is equivalent ot rotating bits to left by 1. \textbf{Data Flow Optimizations} convert expressions into simple to reduce number of duplicate expressions. \textbf{Static Single Assignment (SSA) optimization} involves assigning variables only a single value. If variables have same values then they are considered as one variable only. Scope Tables are used in these cases.

\section{Code Generation}
This part converts the optimized AST into machine codes of a hardware compatible ISA representation. It involves expoliting complex instructions and carefully placing jump and branch instructions for the machine code program. This part is also to be optimized and texhiques of \textit{Register allocation, Instruction selection, Instruction scheduling} and \textit{Rematerialization}.

\section{Interpreter}
Interpreters are different from the compilers only in the aspect it performs operations as the code is produced without having a previously compiled code and then executing it. It works similar to a compiler in terms of basic steps but uses an \textbf{Interediate Representation (IR)} of AST before producing machine code and executing it too.\\

\textbf{Low Level Virtual Machine (LLVM)}\cite{llvm} is a popular compiler technology that takes into account the benefits of IR in compilation. It is used all over now and has increased portability of softwares a lot. Recently the \textit{Polyhedral Loop Optimizations (Polly)} in the LLVM IR has gained popularity and they have been used in all possible places.\\


% !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}

\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 {Weekly Quiz 9} }

\begin{Instruction}

\paragraph{Reporting Issues} If you find any issues with the solutions, reach out to Chi Wang (author) or Luis Perez (reviewer).

\end{Instruction}

\section{} We are computing the longest common subsequence between two strings of length four $S = X_1X_2X_3X_4$ and $T = Y_1Y_2Y_3Y_4$. We fill the array $C$ where $C_{i ,j}$ is the length of the longest common subsequence between the prefix of length $i$ from $S$ and the prefix of length $j$ from $T$. The array $C$ can be found below with some entries masked. What can be said about $X_1$ and $Y_4$ (the first character of S and the last character of Y)?

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

\begin{Solution}
The characters are equal. We can see this by looking at row $1$. It is all $0$'s until we get to column 4, which means that $X_1$ matched none of the characters in $Y$ except for $Y_4$.
\end{Solution}

\section{} What can be said about $X_2 $ and $Y_3$?

\begin{Solution}
By a similar argument, if we look at row labeled 2, it is all 0s until we reach the column labeled 3. As such, this is where it matched with $Y_3$. So the two characters are equal.
\end{Solution}

\section{} What can be said about $X_2$ and $Y_4$?

\begin{Solution}
Suppose $X_2$ matches $Y_4$. By row $2$, we know that $X_2$ matches $Y_3$. However, that would mean $Y_3$ and $Y_4$ are the same character, but if we look at row $1$, we see that $X_1$ only matches $Y_4$, which means $Y_4 \neq Y_3$, as asuch $X_2 \neq Y_4$.
\end{Solution}

\section{} What is the value of the heart?

\begin{Solution}
We're basically asking whether $X_3$ matches $Y_4$. We know that $Y_4$ does not match any other characters in $Y$. However, we see that $X_3$ matches $Y_2$. As such, $X_3$ cannot match $Y_4$.

This means the hearth has a value of $1$ (max of the up and left cells).
\end{Solution}

\section{} What is the value of the spade?
\begin{Solution}
We're asking if $X_4$ matches $Y_3$. We know already that $Y_3$ does not match any other characters in $Y$ (this is clear from row labeled 2). However, $X_4$ matches $Y_1$. As such, $X_4$ does not match $Y_3$.

Therefore, the spade has a value of $1$ (max of the up and left cells).
\end{Solution}

\section{} What is the value of the clubs?

\begin{Solution}
This is asking if $X_4$ matches $Y_2$. We know $Y_2$ matches $X_3$ and $X_4$ matches $Y_1$. However, we know $X_3$ does not match $Y_1$. Therfore $X_4$ doe snot match $Y_2$.

Therefore, the clubs has a value of $1$ (max of the up and left cells).
\end{Solution}

\section{} What is the value of the diamonds?

\begin{Solution}
This is asking if $Y_4$ matches $X_4$. We know $X_4$ matches $Y_1$. However, from before, we know that $Y_4$ in unique in $Y$, as such, $Y_4$ doe snot match $X_4$.

Therefore, the diamons has a value of $1$ (max of the up and left cells).
\end{Solution}

\section{} Consider the LCS problem from lecture 25 and our dynamic programming algorithm for it. Given input strings of lengths $m$ and $n$, what is the memory complexity of this algorithm?

\begin{Solution}
$O(mn)$ because we fill in an $m \times n$ array and each cell takes $O(1)$ time to fill.
\end{Solution}

\section{} When we are filling up the i-th row of our dynamic programming table C, what rows do we need tohave access to?

\begin{Solution}
We need to access the value in the $i$-th row and ($i-1$)-th row only. This is clear from the recursive definition of the algorithm given by:

\begin{align*}
C[i][j] = \begin{cases}
    1 + C[i-1][j-1] & X[i] == Y[j] \\
    \max\{C[i-1][j], C[i][j-1] \}&
\end{cases}
\end{align*}
\end{Solution}

\section{} Given the observation above can we optimize our space complexity further?

\begin{Solution}
Yes we can reduce the memory complexity to $O(\min \{ m,n \})$
\end{Solution}

\end{document}


%% Journal of Open Research Software Latex template -- Created By Stephen Bonner and John Brennan, Durham Universtiy, UK.

\documentclass{jors}
\usepackage{amsmath,amssymb}
\usepackage[backend=biber,url=true,doi=true,sorting=none]{biblatex}
\usepackage{hyperref}
\usepackage{tikz}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{color}

\bibliography{vampy}
\lstset{language=Python}
\renewcommand{\lstlistingname}{Algorithm}

%% Set the header information
\pagestyle{fancy}
\definecolor{mygray}{gray}{0.6}
\renewcommand\headrule{}
\rhead{\footnotesize 3}
\rhead{\textcolor{gray}{UP JORS software Latex paper template version 0.1}}

\begin{document}

{\bf Software paper for submission to the Journal of Open Research Software} \\

To complete this template, please replace the blue text with your own. The paper has three main sections: (1) Overview; (2) Availability; (3) Reuse potential. \\

Please submit the completed paper to: editor.jors@ubiquitypress.com

\rule{\textwidth}{1pt}

\section*{(1) Overview}

\vspace{0.5cm}

\section*{Title}

VaMpy: A Python Package to Solve 1D Blood Flow Problems

\section*{Paper Authors}

1. Diem, Alexandra K.; \\
2. Bressloff, Neil W.

\section*{Paper Author Roles and Affiliations}
1. Institute for Complex Systems Simulation \& Computational Engineering and Design, Faculty of Engineering and the Environment, University of Southampton, UK \\
2. Computational Engineering and Design, Faculty of Engineering and the Environment, University of Southampton, UK

\section*{Abstract}
Finite-differences methods such as the Lax-Wendroff method (LW) are commonly used to solve 1D models of blood flow. These models solve for blood flow and lumen area and are useful in disease research, such as hypertension and atherosclerosis, where flow and pressure are good indicators for the presence of disease. Despite the popularity of the LW method to solve the blood flow equations, no implementation a LW solver for these equations has been published and made publicly available. This leads to the re-implementation of the same methods within different research groups and makes verification of results more difficult. The Vascular Modelling in Python (VaMpy) toolkit is a Python package that aims to fill this gap. It implements Richtmyer's two-step Lax-Wendroff scheme to solve 1D model equations of blood flow in arterial trees and aims at facilitating the solution of blood flow problems for various medical applications.

\section*{Keywords}
Python; blood flow; arterial tree; hemodynamics; finite differences; partial differential equations

\section*{Introduction}

One-dimensional (1D) modelling of the cardiovascular system is useful in predicting and understanding the dynamics of blood pressure propagation \cite{Olufsen2000,Sherwin2003a,Alastruey2007,Kolachalama2007,Cousins2014,Diem2016a}. Here, arteries are regarded as 1D axisymmetric tubes that are described by flux $q$ inside the lumen and cross-sectional area $A$ of the vessel lumen along the vessel length. One popular finite-differences method to numerically solve the equations governing blood flow through arteries is Richtmyer's two-step Lax-Wendroff method \cite{LeVeque1992,Richtmyer1963}, which has been used by a number of groups \cite{Olufsen2000,Kolachalama2007,Diem2016a,Smith2002,Azer2007,Itu2011}. Alternative methods of solving the blood flow equations include for example variations of the Galerkin finite-element method, which instead solve the blood flow equations for flow velocity $u$ and cross-sectional area $A$ \cite{Sherwin2003a,Mynard2008}.

The computational implementation of the Lax-Wendroff method is straightforward and previously mentioned references have produced results that are validated against experimental results, justifying the popularity of the method. However, no openly available implementation of the Lax-Wendroff method could be found, which results in the same work being carried out numerous times. Whilst one open-source Python package implementing a haemodynamic model exists, pyNS focusses on the implementation of a 0D pulse wave propagation model, representing arteries as electrical circuits \cite{Manini2015}, and therefore its scope and application are different from VaMpy. Solutions computed using VaMpy are exported to the commonly used CSV file format, thereby allowing for the integration of data with most other software. For example, solutions calculated using VaMpy could be used as a boundary condition for higher order models of larger arteries further upstream.

\begin{figure}
  \center
  \includegraphics[width=\linewidth]{figures/geometry.png}
  \caption{Example geometry of a bifurcation implemented in VaMpy. The example represents the common carotid artery (parent vessel) and its two daughter vessels, which are used for validation purposes of the software. Artery segments have an upstream and downstream radius, where the downstream radius has to be equal to or smaller than the upstream radius. The radius of the vessel then is $r_0(z) = R_u \cdot \exp ( \log ( R_d/R_u ) \; z/L )$. \label{fig:geometry}}
\end{figure}

Arteries are considered to be elastic axisymmetrical tubes of initial radius $r_0(z)$ in a cylindrical coordinate system. The radius at rest is allowed to taper exponentially for an arterial segment if different values are given for the upstream radius $R_u$ and downstream radius $R_d$. An example geometry for the bifurcation of the common carotid artery, which is used to validate the solution calculated by VaMpy is shown in Figure~\ref{fig:geometry} Then the vessel radius for an arterial segment of length $L$ is
\begin{equation}
r_0(z) = R_u \cdot \exp \left( \log \left( \frac{R_d}{R_u} \right) \frac{z}{L} \right).
\end{equation}
Blood flow through arteries is governed by the Navier-Stokes equations for conservation of mass (continuity equation) and momentum in a 1D cylindrical coordinate system
\begin{gather}
\frac{\partial u_z(r,z,t)}{\partial z} + \frac{1}{r} \frac{\partial \left( ru_r(r,z,t) \right)}{\partial t} = 0\\
\begin{split}
\frac{\partial u_z(r,z,t)}{\partial t} + u_z(r,z,t) \frac{\partial u_z(r,z,t)}{\partial z} + u_r(r,z,t) \frac{\partial u_z(r,z,t)}{\partial r} + \frac{1}{\rho} \frac{\partial p(z,t)}{\partial z} =&\\
\frac{\nu}{r} \frac{\partial}{\partial r} \left( r \frac{\partial u_z(r,z,t)}{\partial r} \right) &,
\end{split}
\end{gather}
where $\boldsymbol{u} = (u_z(r,z,t), u_r(r,z,t))$ denotes blood flow veloctiy, $p(z,t)$ denotes blood pressure, which is assumed to be uniform across $r$ and the parameters $\rho$ and $\nu$ denote blood density and viscosity, respectively. By integration of the governing equations over cross-sectional area $A(z,t) = \pi R(z,t)^2$ the 1D conservation law
\begin{equation}\label{eq:conservation}
\frac{\partial \boldsymbol{U}}{\partial t} + \frac{\partial \boldsymbol{F}}{\partial z} = \boldsymbol{S}
\end{equation}
with
\begin{gather*}
  \boldsymbol{U} = \begin{pmatrix} A(z,t) \\ q(z,t) \end{pmatrix}, \quad \boldsymbol{F} = \begin{pmatrix} q(z,t)\\ \dfrac{q(z,t)^2}{A(z,t)} + f(r_0) \sqrt{A_0(z) A(z,t)} \end{pmatrix}, \quad \boldsymbol{S} = \begin{pmatrix} 0\\ S_1 \end{pmatrix}\\
  \begin{split}
    S_1 = -&\frac{2 \pi R(z,t)}{\delta_b \mathrm{Re}} \frac{q(z,t)}{A(z,t)} +\\
    &\left( 2 \sqrt{A(z,t)} \left( \sqrt{\pi} f(r_0) + \sqrt{A_0(z)} \frac{df(r_0)}{dr_0 } \right) - A(z,t) \frac{df(r_0)}{dr_0} \right) \frac{dr_0(z)}{dz}
  \end{split}
\end{gather*}
can be derived. Details on the derivation of \eqref{eq:conservation} can be found elsewhere \cite{Olufsen2000,Diem2016a}. Here, the unknowns are the vessel cross-sectional area $A(z,t)$ and flux $q(z,t)$. Elasticity of the vessel is described by the quantity $f(r_0)$ with relaxed vessel radius $r_0(z)$, $A_0(z)$ is the relaxed cross-sectional vessel area, $R(z,t)$ the vessel radius, $\delta_b$ is the boundary layer thickness and $\mathrm{Re}$ is the Reynold's number.

\begin{figure}
\center
  \begin{tikzpicture}[node distance=2.5cm,auto,>=latex]
    % coordinate axes
    \node[label=left:{\scriptsize$t$}] (a) {};
    \node[label=left:{\scriptsize$n$}] (b) [below of=a, node distance=3cm, coordinate] {};
    \node (c) [right of=b, node distance=10.0cm] {\scriptsize$z$};
    \path[->] (b) edge node {} (a);
    \path[->] (b) edge node {} (c);
    % axes labels
    \node[label=left:{\scriptsize$n+1/2$}] [below of=a, node distance=2.0cm, coordinate] {};
    \node[label=left:{\scriptsize$n+1$}] [below of=a, node distance=1.0cm, coordinate] {};
    \node (d) [below of=b, node distance=0.7cm, coordinate] {};
    \node[label={\scriptsize$m-2$}, right of=d, node distance=1.0cm, coordinate] {};
    \node[label={\scriptsize$m-1$}, right of=d, node distance=3.0cm, coordinate] {};
    \node[label={\scriptsize$m-\frac{1}{2}$}, right of=d, node distance=4.0cm, yshift=-0.08cm, coordinate] {};
    \node[label={\scriptsize$m$}, right of=d, node distance=5.0cm, coordinate] {};
    \node[label={\scriptsize$m+\frac{1}{2}$}, right of=d, node distance=6.0cm, yshift=-0.08cm, coordinate] {};
    \node[label={\scriptsize$m+1$}, right of=d, node distance=7.0cm, coordinate] {};
    \node[label={\scriptsize$m+2$}, right of=d, node distance=9.0cm, coordinate] {};
    % black at known positions
    \draw[fill=black] (1cm,-3cm) circle (0.1cm);
    \draw[fill=black] (3cm,-3cm) circle (0.1cm);
    \draw[fill=black] (5cm,-3cm) circle (0.1cm);
    \draw[fill=black] (7cm,-3cm) circle (0.1cm);
    \draw[fill=black] (9cm,-3cm) circle (0.1cm);
    % grey circles at half positions
    \draw[fill=gray] (4cm,-2cm) circle (0.1cm);
    \draw[fill=gray] (6cm,-2cm) circle (0.1cm);
    % white circle at unknown position
    \draw (5cm,-1cm) circle (0.1cm);
    % draw lines to indicate where circle is calculated from
    \draw (3cm,-3cm) -- (4cm,-2cm);
    \draw (5cm,-3cm) -- (4cm,-2cm);
    \draw (5cm,-3cm) -- (6cm,-2cm);
    \draw (7cm,-3cm) -- (6cm,-2cm);
    \draw (4cm,-2cm) -- (5cm,-1cm);
    \draw (5cm,-3cm) -- (5cm,-1cm);
    \draw (6cm,-2cm) -- (5cm,-1cm);
  \end{tikzpicture}
  \caption{Illustration of the LW method. The solution is fully known at time step $n$ (black circles) and we are looking for the solution at grid point $m$ at time step $n+1$ (white circle). To determine the unknown solution, two intermediate solutions at half grid points $m \pm 1/2$ and at half time step $n+1/2$ are determined from grid points $m-1$, $m$ and $m+1$ at current time step $n$. The intermediate solutions are then used in conjunction with the known solution at grid point $m$ and current time step $n$ to calculate the unknown solution at grid point $m$ and next time step $n+1$ \cite{Kolachalama2007}. \label{fig:laxwendroff}}
\end{figure}

Although these equations have been commonly used by various groups \cite{Olufsen2000,Kolachalama2007,Cousins2014,Devault2008}, no publicly accessible implementation of the solution to \eqref{eq:conservation} could be found, meaning that each publication from a separate group resulted in the reimplementation of the same or very similar methods and equations. Therefore, the Vascular Modelling in Python toolkit (VaMpy) was developed and published on GitHub\footnote{\url{https://github.com/akdiem/vampy}} with the documentation available on GitHub Pages\footnote{\url{http://akdiem.github.io/vampy/}}. Support for the use of VaMpy is mainly available via the Issue Tracker feature on GitHub, but also via contacting the authors.

\section*{Implementation and architecture}

The VaMpy implementation and architecture are described in this section. VaMpy is object-oriented to allow for an intuitive understanding of its design and to facilitate the addition of new features. The base of the package is the class ArteryNetwork, which defines the arterial tree. The class contains methods that are applied on the entire network of arteries as well as boundary conditions. Each artery within the tree is defined as an object of the class Artery, which contains its own solver instance. The solver itself is implemented in the independent class LaxWendroff that implements the Lax-Wendroff method as described below. This approach allows for the integration of other solvers within the software.

The code was developed in Python 2.7 and implements Richtmyer's two-step version of the Lax-Wendroff method \cite{LeVeque1992,Richtmyer1963}, which is second-order accurate in time and space. For a point in time, $n$, the solution at the next time step $n+1$ at grid location $m$ is given by
\begin{equation}\label{eq:lw_full}
  \boldsymbol{U}_m^{n+1} = \boldsymbol{U}_m^n - \frac{\Delta t}{\Delta z} \left( \boldsymbol{F}_{m+1/2}^{n+1/2} - \boldsymbol{F}_{m-1/2}^{n+1/2} \right) + \frac{\Delta t}{2} \left( \boldsymbol{S}_{m+1/2}^{n+1/2} + \boldsymbol{S}_{m-1/2}^{n+1/2} \right),
\end{equation}
where $\boldsymbol{U}_m^n = \boldsymbol{U}(m \Delta z, n \Delta t)$ is the solution at position $m \Delta z$ and time $n \Delta t$. The half time step values for $\boldsymbol{F}$ and $\boldsymbol{S}$ are determined by
\begin{equation}\label{eq:lw_half}
  \boldsymbol{U}_j^{n+1/2} = \frac{\boldsymbol{U}_{j+1/2}^n + \boldsymbol{U}_{j-1/2}^n}{2} + \frac{\Delta t}{2} \left( - \frac{\boldsymbol{F}_{j+1/2}^n - \boldsymbol{F}_{j-1/2}^n}{\Delta z} + \frac{\boldsymbol{S}_{j+1/2}^n + \boldsymbol{S}_{j-1/2}^n}{2} \right)
\end{equation}
for $j = m \pm 1/2$. An illustration of the computational procedure to determine $\boldsymbol{U}_m^{n+1}$ is shown in Figure~\ref{fig:laxwendroff}. It illustrates that both initial conditions at $n = 0$ for all $m$ and left and right boundary conditions are required to determine $\boldsymbol{U}$.

Boundary conditions are applied at both ends of the vessel and are either an inlet, outlet or bifurcation condition. The inlet boundary condition is used at the inlet of the parent vessel only \cite{Olufsen2000}. It requires flux values $q(0,t)$ to be prescribed. The inlet area is then calculated according to \eqref{eq:lw_full}
\begin{equation}
A_0^{n+1} = A_0^n - \frac{\Delta t}{\Delta z} \left( q_{1/2}^{n+1/2} - q_{-1/2}^{n+1/2} \right).
\end{equation}
This requires the evaluation of the term $q_{-1/2}^{n+1/2}$, which is estimated from
\begin{equation}
q_{0}^{n+1/2} = \frac{1}{2} \left( q_{-1/2}^{n+1/2} + q_{1/2}^{n+1/2} \right),
\end{equation}
where $q_{0}^{n+1/2}$ is evaluated from the function prescribing flux values at the inlet and $q_{1/2}^{n+1/2}$ is evaluated from \eqref{eq:lw_half}, see also \cite{Olufsen2000,Kolachalama2007}.

\begin{lstlisting}[frame=single,mathescape,caption={Iterative scheme to determine $\boldsymbol{U}_M^{n+1}$ using a 3WK boundary condition \cite{Kolachalama2007}. An initial guess is made for $p_M^{n+1}$, from which $q_M^{n+1} is calculated using \eqref{eq:3wk}$ and $A_M^{n+1}$ is calculated using the discretized mass conservation equation \eqref{eq:disc_mass}. Using $A_M^{n+1}$ the next iteration of $p_M^{n+1}$ is found via the state equation \eqref{eq:state}. The algorithm stops after $k_\text{max}$ iterations or when the difference between pressure estimates is less than the small threshold value $\epsilon$.}, label=alg:outlet]
$p_M^{n+1} = p_M^n$ # initial guess for $p_m^{n+1}$
$k = 0$
for $k \leq k_\text{max}$:
    $p_\text{old} = p_M^n$
    $q_M^{n+1} = q_M^n + \frac{p_M^{n+1} - p_M^n}{R_1} + \frac{\Delta t p_M^n}{R_1 R_2 C_T} - \frac{\Delta t q_M^n (R_1 + R_2)}{R_1 R_2 C_T}$
    $A_M^{n+1} = A_0^n - \frac{\Delta t}{\Delta z} ( q_M^{n+1} - q_{M-1}^{n+1} )$
    $p_M^{n+1} = f_M^{n+1} \left( 1 - \sqrt{\frac{(A_0)_M}{A_M^{n+1}}} \right)$
    if $| p_\text{old} - p_M^{n+1} | \leq \epsilon$:
        break
    $k = k + 1$
\end{lstlisting}

The outlet boundary condition is a 3WK and its implementation follows \cite{Kolachalama2007}. The 3WK equation is
\begin{equation}\label{eq:3wk}
  \frac{\partial p(z,t)}{\partial t} = R_1 \frac{\partial q(z,t)}{\partial t} - \frac{p(z,t)}{R_2 C_T} + \frac{q(z,t) (R_1 + R_2)}{R_2 C_T},
\end{equation}
where $R_1$, $R_2$ and $C_T$ are resistance and compliance parameters. Discretization of \eqref{eq:3wk} leads to
\begin{equation}
\frac{p_M^{n+1} - p_M^n}{\Delta t} = R_1 \frac{q_M^{n+1} - q_M^n}{\Delta t} - \frac{p_M^n}{R_2 C_T} + \frac{q_M^n (R_1 + R_2)}{R_2 C_T},
\end{equation}
where $M$ is the spatial position of the outlet. The 3WK boundary condition requires the evaluation of pressure in the vessel, which is related to area via the discretized State Equation \cite{Olufsen2000}
\begin{equation}\label{eq:state}
  p_M^{n+1} = f_M^{n+1} \left( 1 - \sqrt{\frac{(A_0)_M}{A_M^{n+1}}} \right).
\end{equation}
The outlet boundary condition is solved using an iterative scheme with an initial guess for $p_M^{n+1}$ (see Algorithm~\ref{alg:outlet}). This requires a discretized version of the mass conservation equation to obtain an estimate for $A_M^{n+1}$
\begin{equation}\label{eq:disc_mass}
A_M^{n+1} = A_0^n - \frac{\Delta t}{\Delta z} \left( q_M^{n+1} - q_{M-1}^{n+1} \right).
\end{equation}

Finally, a bifurcation boundary condition applies between any vessel that is not a terminal vessel and its two daughter vessels \cite{Olufsen2000,Kolachalama2007}. Relations between parent and daughter vessels lead to a system of eighteen equations for eighteen unknowns, which are solved using Newton's method according to
\begin{equation}\label{eq:newton}
\boldsymbol{x}_{k+1} = \boldsymbol{x}_k - (\boldsymbol{J}(\boldsymbol{x}_k))^{-1} \boldsymbol{f_J}(\boldsymbol{x}_k) \qquad \text{for } k = 0, 1, 2, \ldots
\end{equation}
where $k$ indicates the current iteration, $\boldsymbol = (x_1, x_2, \ldots, x_{18})$, $\boldsymbol{J}(\boldsymbol{x}_k))$ is the Jacobian of the system of equations and $\boldsymbol{f_J}(\boldsymbol{x}_k)$ is the vector of residuals. The full system of equations required to solve the boundary conditions at bifurcations can be found elsewhere \cite{Diem2016a,Olufsen1998}.

\begin{lstlisting}[frame=single,caption={Setup routine for a network of arteries. The artery network is created as a binary tree and contains $2^{\text{depth}}-1$ arteries. At each depth level the up- and downstream radii of daughter vessels are calculated using the scaling parameters $a$ and $b$. Artery objects are then created for each new daughter vessel and stored in a list.}, label=alg:arterynetwork]
pos = 0 # identifier for the next artery
arteries[pos] = Artery(Ru, Rd) # list containing artery
                               # objects
radii_u = [Ru] # list containing upstream radii of arteries
radii_d = [Rd] # list containing downstream radii of
               # arteries

for j in range(depth):
    # lists for storage of upstream/downstream radii
    # of arteries on the next depth level
    new_radii_u = []
    new_radii_d = []

    for i in range(length(radii_u)):
        # set daughter vessel radii using scaling
        # parameters a and b
        ra_u = radii_u[i] * a
        rb_u = radii_u[i] * b
        ra_d = radii_d[i] * a
        rb_d = radii_d[i] * b
        pos += 1
        # first daughter vessel using scaling
        # parameter a
        arteries[pos] = Artery(ra_u, ra_d)
        pos += 1
        # second daughter vessel using scaling
        # parameter b
        arteries[pos] = Artery(rb_u, rb_d)

        # store new radii for next iteration over j
        new_radii_u[i] = ra_u
        new_radii_u[i] = rb_u
        new_radii_d[i] = ra_d
        new_radii_d[i] = rb_d

    radii_u = new_radii_u
    radii_d = new_radii_d
\end{lstlisting}

A simulation model is set up by creating an ArteryNetwork object and solved by executing the following functions
\begin{verbatim}
from vampy.artery_network import ArteryNetwork
an = ArteryNetwork(Ru, Rd, a, b, lam, k, rho, nu, p0, depth, ntr, Re)
an.mesh(dx)
an.set_time(dt, T[, tc])
an.initial_conditions(q0)
an.solve(q_in, out_args)
\end{verbatim}
A network of arteries is created using the upstream and downstream radii Ru and Rd of the parent vessel, the radius-to-length ratio lam and scaling parameters a and b using Algorithm~\ref{alg:arterynetwork}. Two daughter vessels are created for a parent vessel by multiplying their upstream and downstream radii with scaling parameters a and b respectively. This process is repeated until the desired tree depth is reached and the number of arteries in the network is $2^{\text{depth}}-1$. A second setup routine exists to create an artery network, which is
\begin{verbatim}
an = ArteryNetwork(Ru, Rd, lam, k, rho, nu, p0, depth, ntr, Re)
\end{verbatim}
Here, Ru, Rd and lam are iterables (for example lists or Numpy arrays) of length depth containing these values for each artery. The remaining parameters required by the ArteryNetwork constructor are the elasticity parameter k, blood density rho, blood viscosity nu, diastolic pressure p0, number of output parameters ntr and Reynold's number Re. The latter method is used by the examples shown in this paper. The spatial discretisation is created by supplying the spatial step size dx, which is used internally to create Numpy arrays for all variables along the vessel. Timing parameters are the time step size dt, time of one period T and, optionally, the number of periods tc, which defaults to one if left unspecified. Initial conditions are supplied for $q(z,t)$ as a single value q0, while the initial condition for $A(z,t)$ is calculated from the radii at rest. The solve function is supplied with boundary condition parameters q\_in and out\_args, where q\_in contains the values $q(0,t)$ and out\_args contains the parameters for a three-element Windkessel (3WK) model.

The solver loops over the simulation time steps and creates a LaxWendroff object for each Artery object
\begin{verbatim}
lw = LaxWendroff(theta, gamma, artery.nx)
\end{verbatim}
with theta = dt/artery.dx, gamma = dt/2 and number of spatial steps artery.nx. The next time step is computed according to \eqref{eq:lw_full} and \eqref{eq:lw_half} at the inner grid points. Note that the time step size needs to fulfill the Courant-Friedrichs-Lewy (CFL) condition, which in this case is
\begin{equation}
\Delta t \leq \Delta x \cdot \left| \frac{q}{A} \pm c \right|^{-1},
\end{equation}
where $c = \sqrt{A/\rho \; \partial p/\partial A}$ is the wave speed. The CFL condition is automatically checked by the ArteryNetwork solver and the simulation stops with an error message if the condition is not met.

The following section demonstrates the use of VaMpy for the simulation of the common carotid artery as done by \cite{Kolachalama2007}. This publication was chosen as it provides detailed information on the geometry used and Windkessel parameter for the outlet boundary condition. A detailed walkthrough of how to write configuration and simulation files can be found on the documentation website\footnote{\url{http://akdiem.github.io/vampy/walkthrough.html}}.

\section*{Quality control}

\begin{figure}
  \center
  \includegraphics[width=\linewidth]{figures/cca_p_q.png}
  \caption{One pulse in the common carotid artery using VaMpy: a) flow rate, b) pressure. Comparison with the results for the same simulation in \cite{Kolachalama2007} validates the implementation of the blood flow equations in VaMpy.\label{fig:bifurcation}}
\end{figure}

The VaMpy Git repository contains unit tests to ensure functions perform as expected. Additionally, the file \texttt{bifurcation\_example.py} demonstrates VaMpy's performance by validating its results against results in \cite{Kolachalama2007} on the common carotid artery bifurcation. Whilst unit tests demonstrate that the functionality of the software meets expecations, validation against experimental results and other researchers' results ensures that the software additionally generates sensible output data and that parameters have been chosen sensibly. Users implementing arteries using other parameters than the ones tested in the example files in VaMpy should therefore always cross-check their results against experimental or other simulation results using the same parameters to ensure that the choice of parameters is realistic.

The solution computed using VaMpy is shown in Figure~\ref{fig:bifurcation} and matches the corresponding figures in \cite{Kolachalama2007}. Thus this example demonstrates that VaMpy performs as expected.

To execute the example run
\begin{verbatim}
python bifurcation_example.py bifurcation.cfg
\end{verbatim}
To plot the data created from the example run
\begin{verbatim}
python plot_example.py bifurcation.cfg
\end{verbatim}

The first version of VaMpy focusses on the simulation of a single bifurcation, i.~e. one parent vessel with two daughter vessels. The development of the first version of VaMpy was based on the simulation of flow through the middle cerebral artery in order to evaluate lymphatic drainage through the wall of the artery \cite{Diem2016a}, and for this purpose a single bifurcation was regarded sufficient. Validation on larger networks of arteries with multiple levels of bifurcations has therefore not been carried out yet, but is planned for the next release cycle. Additionally, it is planned to offer a choice of alternative outlet boundary conditions, such as the structured tree \cite{Olufsen2000,Cousins2014}. It has been demonstrated that by taking into account bifurcation pressure drops, the accuracy of reduced order models such as the system of equations \eqref{eq:conservation} can improve significantly compared to higher order models \cite{Chnafa2016}. This means that a similar accuracy of blood flow solutions could be achieved for 1D models compared to 2D or 3D models by increasing the depth of the arterial tree to be modelled.

\section*{(2) Availability}
\vspace{0.5cm}
\section*{Operating system}

VaMpy is compatible with any operating system that is compatible with Python 2.7 and the dependent packages.

\section*{Programming language}

VaMpy was written in and for Python 2.7 and above.

\section*{Additional system requirements}

There are no additional system requirements. However, the requirements for memory and processing power are dependent on the number of the grid points.

\section*{Dependencies}

NumPy, SciPy, Matplotlib, ConfigParser.

\section*{Software location:}

{\bf Archive}

\begin{description}[noitemsep,topsep=0pt]
	\item[Name:] GitHub
	\item[Persistent identifier:] https://github.com/akdiem/vampy/releases/tag/v1.0
	\item[Licence:] Three-Clause BSD
	\item[Publisher:]  Alexandra K. Diem
	\item[Version published:] v1.0
	\item[Date published:] 22/03/2017
\end{description}

{\bf Code repository}

\begin{description}[noitemsep,topsep=0pt]
	\item[Name:] GitHub
	\item[Persistent identifier:] https://github.com/akdiem/vampy
	\item[Licence:] Three-Clause BSD
	\item[Date published:] 26/04/2016
\end{description}

\section*{Language}

Python 2.7

\section*{(3) Reuse potential}

Modelling blood flow dynamics is a useful tool in vascular diseases research and 1D models provide good approximations. The method implemented in VaMpy is used by a variety of research groups \cite{Olufsen2000,Kolachalama2007,Itu2011} and therefore it is expected that the reuse potential for VaMpy is high, especially in multiscale simulations. Because the commonly accepted CSV file format is used for input and output data for VaMpy integration of results from VaMpy simulations with other third-party software packages is expected to be straightforward. For example, VaMpy could be used as a boundary condition for 3D simulations or constitute a part of multi-scale simulations.

The publication of this software additionally provides opportunities for other researchers to add functionality and because VaMpy has been validated on results published in the literature it simplifies and promotes reproducibility of results. The following features are planned for the next releases:

\begin{itemize}
\item asymmetric daughter vessel geometries with separate Windkessel parameters,
\item validation of the method on bifurcation networks larger than two levels and
\item integration of models of the dynamics of the artery wall.
\end{itemize}

The current release of VaMpy was developed to implement a bifurcation at the middle cerebral artery as part of a multi-scale model of lymphatic flow through the basement membrane embedded in the artery wall, which is relevant for resolving the mechanisms behind the onset and progression of Alzheimer's disease \cite{Diem2016a,Bakker2016}.

\section*{Acknowledgements}

The authors thank Maximilian Albert for advice and guidance on the use of GitHub repositories and Python code repository conventions.

\section*{Funding statement}

The development of this software was supported by an EPSRC Doctoral Training Centre grant (EP/G03690X/1).

\section*{Competing interests}

The authors declare that they have no competing interests.

\printbibliography

\vspace{2cm}

\rule{\textwidth}{1pt}

{ \bf Copyright Notice} \\
Authors who publish with this journal agree to the following terms: \\

Authors retain copyright and grant the journal right of first publication with the work simultaneously licensed under a  \href{http://creativecommons.org/licenses/by/3.0/}{Creative Commons Attribution License} that allows others to share the work with an acknowledgement of the work's authorship and initial publication in this journal. \\

Authors are able to enter into separate, additional contractual arrangements for the non-exclusive distribution of the journal's published version of the work (e.g., post it to an institutional repository or publish it in a book), with an acknowledgement of its initial publication in this journal. \\

By submitting this paper you agree to the terms of this Copyright Notice, which will apply to this submission if and when it is published by this journal.

\end{document}


\documentclass[11pt]{article}

%% WRY has commented out some unused packages %%
%% If needed, activate these by uncommenting
\usepackage{geometry}                % See geometry.pdf to learn the layout options. There are lots.
%\geometry{letterpaper}                   % ... or a4paper or a5paper or ...
\geometry{a4paper,left=2.5cm,right=2.5cm,top=2.5cm,bottom=2.5cm}
%\geometry{landscape}                % Activate for rotated page geometry
%\usepackage[parfill]{parskip}    % Activate to begin paragraphs with an empty line rather than an indent

%for figures
%\usepackage{graphicx}

\usepackage{color}
\definecolor{mygreen}{RGB}{28,172,0} % color values Red, Green, Blue
\definecolor{mylilas}{RGB}{170,55,241}
%% for graphics this one is also OK:
\usepackage{epsfig}

%% AMS mathsymbols are enabled with
\usepackage{amssymb,amsmath}

%% more options in enumerate
\usepackage{enumerate}
\usepackage{enumitem}

%% insert code
\usepackage{listings}

\usepackage[utf8]{inputenc}

\usepackage{hyperref}

%% colors
\usepackage{graphicx,xcolor,lipsum}

\usepackage{mathtools}

\usepackage{graphicx}
\newcommand*{\matminus}{%
  \leavevmode
  \hphantom{0}%
  \llap{%
    \settowidth{\dimen0 }{$0$}%
    \resizebox{1.1\dimen0 }{\height}{$-$}%
  }%
}

\title{On the Dirichlet modes of Scott and Furnival JPO 2012}
\author{Cesar B Rocha\thanks{Scripps Institution of Oceanography, University of California, San Diego; \texttt{crocha@ucsd.edu}}}
\date{\today}

\begin{document}

\include{mysymbols}

\maketitle

The ``Dirchlet modes'' Scott and Furnival JPO 2012 (hereafter SF12) should not
have zero slope at the surface unless the buoyancy frequency is zero or behaves
wildy at the surface. The authors do not show the buoyancy profile used in
their calculations. In an appendix the authors do mention that the buoyancy
frequency at the surface is very small. This can be account for the seemly
``flat'' slope of the ``Dirichlet modes'' near the surface (see their figure 1b).

\section{WKB approximate solution to ``Dirichlet modes''}

The ``'Dirichlet modes'', here denoted $\sD_n$, satisfy

\beq
\label{dirich_eigpb}
\frac{\dd}{\dd z}\left[\bur \frac{\dd\sD_n}{\dd z} \right] = -\kappa_n^2 \sD_n\com
\eeq
with boundary conditions
\beq
\label{dirich_bc}
@z = -H:\qquad \frac{\dd \sD_n}{\dd z}= 0\com
\eeq
and
\beq
\label{dirich_bc0}
@z = 0:\qquad \sD_n = 0 \per
\eeq
Equation \eqref{dirich_eigpb} can be rewritten as
\beq
\label{dirich_eigpb_wkb}
\bur \frac{\dd^2\sD_n}{\dd z^2} + \frac{\dd \sD_n}{\dd z}\,\frac{\dd}{\dd z}\bur + \kappa_n^2 \sD_n = 0\per
\eeq
We are going to obtain WKB approximate solution to \eqref{dirich_eigpb_wkb}. We use the following notation

\beq
\label{notation}
\ep \defn \frac{1}{\kappa_n} \qquad \text{and} \qquad S^2(z) \defn \ibur \per
\eeq
With the definitions in \eqref{notation} we have the renotated equation
\beq
\label{dirich_eigpb_wkb_ep}
\ep^2\,\frac{\dd^2\sD_n}{\dd z^2} - \frac{\dd}{\dd z}\left(\log S^2(z)\right)  \frac{\dd \sD_n}{\dd z} + S^2(z) \sD_n = 0\per
\eeq
In the WKB spirit we assume that $S^2(z)$ is slowly varying i.e., the buoyancy frequency $N^2(z)$ does not vary very fast. (This assumption may be problematic near the base of the mixed-layer.) We also assume that $\ep$ is small; the accuracy of the WKB solution improves with mode number. We now make the exponential approximation (e.g., Bender and Orszag)
\beq
\sD_n^e \defn \ee^{Q(z)/\ep}\per
\eeq
Thus we have
\beq
{\sD_n^e}' = \frac{Q'(z)}{\ep}\sD_n^e\com
\eeq
and
\beq
{\sD_n^e}'' = \left[\left(\frac{Q'(z)}{\ep}\right)^2 + \frac{Q''(z)}{\ep} \right]\sD_n^e\com
\eeq
where primes represent differentiation with respect to $z$.
We now expand $Q(z)$ in powers of $\ep$

\beq
\label{aseries}
Q(z) = Q_0(z)  + \ep\,Q_1(z) + \ep^2\,Q_2(z) + \mathcal{O}(\ep^3)\per
\eeq
Substituting \eqref{aseries} in \eqref{dirich_eigpb_wkb_ep} we obtain, to lowest order $\mathcal{O}(\ep^0)$,
\beq
\label{lowest_order_eqn}
Q_0'^2 + S^2(z) = 0\per
\eeq
Thus
\beq
\label{Q0}
Q_0 = \pm \ii \int^z \!\!\!S(\xi) \,\dd \xi \per
\eeq
At $\mathcal{O}(\ep)$ we have
\beq
\label{first_order_eqn}
2\,Q_0'Q_1' + Q_0'' - Q_0' \frac{\dd}{\dd z} \left(\log S^2(z)\right) = 0\per
\eeq
Hence
\beq
\label{Q_1}
Q_1  =   \frac{1}{2} \left(\log S^2(z)\right) - \frac{1}{2} \left(\log \pm \ii S(z) \right) + \text{const}\,\, \per
\eeq
Notice that the imaginary part in the $\log$ in \eqref{Q_1} just contributes to the irrelevant constant. Thus
\beq
Q_1 = \log \sqrt{S(z)} + \text{const} \,\, \per
\eeq
In the  ``physical optics'' approximation we keep the first two terms in the expansion \eqref{aseries}. The solution to \eqref{dirich_eigpb_wkb_ep}, consistent with the bottom boundary condition \eqref{dirich_bc}, is

\beq
D_n^{po} = A_n\, \sqrt{N(z)}\, \cos \left(\frac{\kappa_n}{f_0} \int_{-H}^{z} \!\!\!N(\xi) \dd \xi\right)\com
\eeq
where $A_n$ is a constant. By imposing the boundary condition at $z=0$ \eqref{dirich_bc0}, we obtain $\kappa_n$:

\beq
\label{kappan}
\kappa_n = \frac{(n + 1/2) \pi \, f_0}{\int_{-H}^0N(\xi)\dd \xi} \com\qquad n=0,1,2,\ldots
\eeq

The constant $A_n$ is determined by the normalization condition

\beq
\frac{1}{H}\int_{-H}^{0}\!\!\!\sD_n\,\sD_m \dd z = \delta_{mn}\com
\eeq
which gives
\beq
\label{an_eqn}
A_n^2 \, \int_{-H}^{0}\!\! N(z) \cos \left(\frac{\kappa_n}{f_0}\int_{-H}^{z}\!\!\!N(\xi) \dd \xi\right) \dd z = H\per
\eeq
The integral in \eqref{an_eqn} can be evaluated exactly by making the change of variables

\beq
\eta \defn \cos \left(\frac{\kappa_n}{f_0}\int_{-H}^{z}\!\!\! N(\xi) \dd \xi \right)\com\qquad \dd\eta = \frac{\kappa_n}{f_0}N(z) \dd z\com
\eeq
and using the expression for the eigenvalues \eqref{kappan}. We obtain
\beq
A_n = \left[\frac{2\,H}{\int_{-H}^{0}\! N(\xi) \dd \xi} \right]^{1/2}
\eeq
Thus the WKB approximate solution to the ``Dirichlet modes'' is
\beq
D_n^{po} = \left[\frac{2\,N(z)\,H}{\int_{-H}^{0}\! N(\xi) \dd \xi} \right]^{1/2}\!\!\cos \left( \frac{(n + 1/2)\pi}{\int_{-H}^{0} \!N(\xi) \dd \xi} \,\,\,\int_{-H}^{z} \!N(\xi) \dd \xi\right)\per
\eeq
The slope of $D_n^{po}$ is, to lowest order,
\beq
\frac{\dd D_n^{po}}{\dd z} =  \frac{\sqrt{2} \, N(z)\,(n+1/2)\pi}{\int_{-H}^0 N(\xi) \dd \xi}  \sin \left(\frac{(n + 1/2)\pi}{\int_{-H}^{0} \!N(\xi) \dd \xi} \,\,\,\int_{-H}^{z} \!N(\xi) \dd \xi\right)\per
\eeq

\subsection*{The constant buoyancy frequency limit}
With $N = \text{const.}$ we obtain

\beq
D_n^{po} = \sqrt{2} \cos\left[\pi\,(n+1/2)(1+z/H)\right]\com
\eeq
and

\beq
\frac{\dd D_n^{po}}{\dd z}\Bigg|_{z=0} = \sqrt{2}\,(n+1/2)\pi \sin \left[\pi\,(n+1/2)(1+z/H)\right]\per
\eeq
The slope at the surface is $\pm\sqrt{2} \,(n+1/2)\pi$ which is clearly non-zero.

\subsection*{Non-constant stratification}
For variable buoyancy profile the slope at the surface is
\beq
\frac{\dd D_n^{po}}{\dd z}\Bigg|_{z=0} =  \frac{\sqrt{2} \,N(0) \, H\,(n+1/2)\pi}{\int_{-H}^0 N(\xi) \dd \xi}\per
\eeq
The slope of the ``Dirichlet modes'' at the surface depends on the surface buoyancy frequency which can be problematic. The slope if $N(z)$ vanishes at the surface. Moreover, if $N(z)$ varies rapidly near the surface, the WKB approximation is not accurate.

\end{document}