\section{Recursion Equations}

\begin{frame}{Recursion Equations}{Recursion Equation}
  \textbf{Recursion equation:}
  \begin{itemize}
    \item
      Runtime description for recursive functions:
  \end{itemize}
  \vspace{2em}
  \begin{displaymath}
    {\color{MainA}T(n)} =
    \begin{cases}
      \hfill
      \smash{
        \overbrace{f_0(n)}^\text{%
          \footnotesize%
          trivial case for $n_0$
        }
      }
      \vphantom{f_0(n)} & n = n_0\\[0.5em]
      \smash{
        \underbrace{
          a \cdot {\color{MainA}T\left(\frac{n}{b}\right)}
        }_\text{
          \footnotesize%
          \begin{tabular}{c}
            \text{solving of {\color{MainA}$a$}}\\
            \text{subproblems}\\
            \text{with reduced}\\
            \text{input size {\color{MainA}$\frac{n}{b}$}}
          \end{tabular}
        }
      } \hspace{0.5em} + \hspace{1.5em}
      \smash{
        \underbrace{
          f(n)
          \vphantom{\left(\frac{n}{b}\right)}
        }_\text{
          \makebox[0pt][c]{
            \footnotesize%
            \begin{tabular}{c}
              slicing and\\
              splicing of\\
              subsolutions
            \end{tabular}
          }
        }
      }
      \vphantom{\left(\frac{n}{b}\right)}
      &n > n_0
    \end{cases}
  \end{displaymath}
  \vspace{4em}
\end{frame}

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

\begin{frame}{Recursion Equations}{Recursion Equation}
  \textbf{Recursion equation:}
  \begin{itemize}
    \item<2->
    Runtime descripion for recursive functions:
    \begin{displaymath}
    {\color{MainA}T(n)} = \begin{cases}
      f_0(n) & n = n_0\\
      a \cdot {\color{MainA}T\left(\frac{n}{b}\right)} + f(n) & n > n_0
    \end{cases}
    \end{displaymath}
    \item<3->
      {\color{MainA}$n_0$} is usually small, {\color{MainA}$f_0(n_0) \; \in \; \Theta(1)$}
    \item<4->
      Usually, {\color{MainA}$a > 1$} and {\color{MainA}$b > 1$}
    \item<5->
      Dependent on the strategy of solving {\color{MainA}$T(n)$}
      {\color{MainA}$f_0$} is ignored
    \item<6->
      {\color{MainA}$T(n)$} is only defined for integers of
      {\color{MainA}$\frac{n}{b}$}, which is often ignored in benefit of
      a simpler solution
  \end{itemize}
\end{frame}

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

\subsection{Substitution Method}

\begin{frame}{Recursion Equations}{Substitution Method}
  \textbf{Substitution Method:}
  \begin{itemize}
    \item<2->
      Guess the solution and prove it with induction
    \item<3->
      Example:        \color{MainA}
      \begin{displaymath}
        T(n) = \begin{cases}
          \hfill 1 & n = 1\\
          2 \cdot T\left(\frac{n}{2}\right) + n & n > 1
        \end{cases}
      \end{displaymath}        \color{black}
    \item<4->
      Assumption:  {\color{MainA}$T(n) = n + n \cdot \log_2 n$}
  \end{itemize}
\end{frame}

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

\begin{frame}{Recursion Equations}{Substitution Method}
  \textbf{Induction:}
  \begin{itemize}
    \item<2->
      Induction basis (for {\color{MainA}$n = 1$}):
      ${\color{MainA}T(1)} = 1 + 1 \cdot \log_2 1 = 1$
    \item<3->
      Induction step (from {\color{MainA}$\frac{n}{2}$}
      to {\color{MainA}$n$}):
      \begin{align*}
        \onslide<4->{{\color{MainA}T(n)} & =
          2 \cdot {\color{MainA}T\left(\dfrac{n}{2}\right)} + n\\}
        \onslide<5->{{} & \stackrel{IA}{=} 2 \cdot \left(
            \dfrac{n}{2} + \dfrac{n}{2} \cdot \log_2 \dfrac{n}{2}
          \right) + n \\}
        \onslide<6->{{} & = 2 \cdot \left(
            \dfrac{n}{2} + \dfrac{n}{2} \cdot \left(\log_2 n - 1\right)
          \right) + n}\\
        \onslide<7->{{} & = n + n \, \log_2 n - n + n\\}
        \onslide<8->{{} & = n + n \, \log_2 n}
      \end{align*}
  \end{itemize}
\end{frame}

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

\begin{frame}{Recursion Equations}{Substitution Method}
  \textbf{Substitution Method:}
  \begin{itemize}
    \item<2->
      Alternative assumption
    \item<3->
      Example:
      \begin{displaymath}
        \color{MainA}
        T(n) = \begin{cases}
          \hfill 1 & n = 1\\
          2 \cdot T\left(\frac{n}{2}\right) + n & n > 0
        \end{cases}
      \end{displaymath}
    \item<4->
      Assumption:
      {\color{MainA}$T(n) \, \in \, O(n \, \log n)$}
    \item<5->
      Solution: Find {\color{MainA}$c > 0$} with
      {\color{MainA}$T(n) \leq c \cdot n \, \log_2 n$}
  \end{itemize}
\end{frame}

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

\begin{frame}{Recursion Equations}{Substitution Method}
  \textbf{Induction:}
  \begin{itemize}
    \item<2->
      Solution: Find {\color{MainA}$c > 0$} with
      {\color{MainA}$T(n) \leq c \cdot n \, \log_2 n$}
    \item<3->
      Induction step (from {\color{MainA}$\frac{n}{2}$} to
      {\color{MainA}$n$}):
      \begin{align*}
        \onslide<4->{{\color{MainA}T(n)} & =
          2 \cdot {\color{MainA}T\left(\dfrac{n}{2}\right)} + n\\}
        \onslide<5->{{} & \leq 2 \cdot \left(
          c \cdot \dfrac{n}{2} \, \log_2 \dfrac{n}{2}\right) + n\\}
        \onslide<6->{{} & = c \cdot n \, \log_2 n - c \cdot n \, \log_2 2 + n\\}
        \onslide<7->{{} & = c \cdot n \, \log_2 n - c \cdot n + n\\}
        \onslide<8->{{} & \leq c \cdot n \, \log_2 n,
          \hspace{0.5em} {\color{MainA}c \geq 1}}
      \end{align*}
  \end{itemize}
\end{frame}

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

\subsection{Recursion Tree Method}

\begin{frame}{Recursion Equations}{Recursion Tree Method}
  \textbf{Recursion tree method:}
  \begin{itemize}
    \item<2->
      Can be used to make assumptions about the runtime
    \item<3->
      Example:\\
      \begin{displaymath}
        {\color{MainA}T(n)}
        = 3 \cdot {\color{MainA}T\left(\dfrac{n}{4}\right)} + \Theta(n^2)
        \leq 3 \cdot {\color{MainA}T\left(\dfrac{n}{4}\right)} + c \cdot
        n^2
      \end{displaymath}
  \end{itemize}
\end{frame}

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

\begin{frame}{Recursion Equations}{Recursion Tree Method}
  \begin{figure}
    \begin{adjustbox}{width=\linewidth}
      \input{Images/RecursionEquations/Example_RecursionTree.tikz}
    \end{adjustbox}
    \caption{recursion tree of example}
    \label{fig:recursion_equations:example_recursion_tree}
  \end{figure}
\end{frame}

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

\begin{frame}{Recursion Equations}{Recursion Tree Method}
  \begin{figure}
    \begin{adjustbox}{width=\linewidth}
      \input{Images/RecursionEquations/Example_RecursionTree_Levels.tikz}
    \end{adjustbox}
    \caption{levels of the recursion tree}
    \label{fig:recursion_equations:example_recursion_tree_layer}
  \end{figure}
\end{frame}
%-------------------------------------------------------------------------------

\begin{frame}{Recursion Equations}{Recursion Tree Method Costs}
  \textbf{Costs of connecting the partial solutions:}\\
  \hspace{1.5em}(excludes the last layer)
  \begin{itemize}
    \item<2->
      Size of partial problems on {\color{MainA}level $i$}:
      \begin{math}
        \color{MainA}{
          \mathrm{s}_i(n) = \left(\frac{1}{4}\right)^i \cdot n
        }
      \end{math}
    \item<3->
      Costs of partial problems on {\color{MainA}level $i$}:
      \begin{displaymath}
        \color{MainA}{
          T_{i_p}(n) =
          c \cdot \left(\left(\tfrac{1}{4}\right)^i \cdot n\right)^2
        }
      \end{displaymath}
    \item<4->
      Number of partial problems on {\color{MainA}level $i$}:
      {\color{MainA}$n_{i} = 3^{i}$}
    \item<5->
      Costs on {\color{MainA}level $i$}:
        \color{MainA}
      \begin{displaymath}
        T_i(n) = 3^{i} \cdot c \cdot \left(
          \left(\tfrac{1}{4}\right)^{i} \cdot n
        \right)^2
        = \left(\tfrac{3}{16}\right)^{i} \cdot c \cdot n^2
      \end{displaymath}
        \color{black}
  \end{itemize}
\end{frame}

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

\begin{frame}{Recursion Equations}{Recursion Tree Method Costs}
  \textbf{Costs of solving partial solutions:} (only the last layer)
  \begin{itemize}
    \item<2->
      Size of partial problems on the {\color{MainA}last level}:
      {\color{MainA}$\mathrm{s}_{i+1}(n) = 1$}
    \item<3->
      Costs of partial problem on the {\color{MainA}last level}:
      {\color{MainA}$T_{i+1_p}(n) = d$}
    \item<4->
      With this the depth of the tree is:
      \begin{displaymath}
        \left(\tfrac{1}{4}\right)^i \cdot n = 1
        \hspace{1.5em}\Rightarrow n = 4^i
        \hspace{1.5em}\Rightarrow\color{MainA} i = \log_4 n
      \end{displaymath}
      \vspace{-1em}
    \item<5->
      Number of partial problems on the {\color{MainA}last level}:
      \begin{displaymath}
        \color{MainA}{n_{i+1} = 3^{\log_4 n}    \onslide<6-> \quad=
          n^{\log_4 3}}  \quad \leftarrow \text{next slide}
      \end{displaymath}
    \item<7->
      Costs on the {\color{MainA}last level}:
      {\color{MainA}$T_{i+1}(n) = d \cdot n^{\log_4 3}$}
  \end{itemize}
\end{frame}

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

\begin{frame}{Fun with logarithm}{Logarithm}
  \begin{itemize}
  \item
    Transforming $\color{MainA}3^{\log_4 n}$ using general log rules
    {\color{MainA}
    \begin{align*}
      \log_4 n & = \log_4 \left(3^{\log_3 n} \right) &&
      \qquad \text{\color{black}using } n= 3^{\log_3 n}\\
      \onslide<2->{& = {\log_3 n \cdot \log_4 3} &&
        \qquad \text{\color{black}using } \log a^b = b \cdot \log a}
    \end{align*}}
  \item<3->
    This proves the general log rule
    {\color{MainA}$\log_b c = \log_a c \cdot \log_b a$}
  \item<4->
    Now the whole expression:
\color{MainA}
    \begin{align*}
      3^{\log_4 n} & = 3^{\log_3 n \cdot \log_4 3} &&
      \qquad \text{\color{black}using reformulation above}\\
      \onslide<5->{& = {\left(3^{\log_3 n}\right)}^{\log_4 3} &&
        \qquad \text{\color{black}using }x^{a \cdot b} = (x^a)^b}\\
      \onslide<6->{& = n^{\log_4 3}}
    \end{align*}
  \item<7->
    {\color{gray}This term will recur in the master theorem}
  \end{itemize}
\end{frame}

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

\begin{frame}{Recursion Equations}{Total costs}
  \textbf{Total costs:}
  \begin{itemize}
    \item<2->
      Costs of {\color{MainA}level i}:
      $T_i(n) = \left(\frac{3}{16}\right)^i \cdot c \cdot n^2$
    \item<3->
      Costs of {\color{MainA}last level}:
      $T_{i+1}(n) = d \cdot n^{\log_4 3}$
  \end{itemize}
  \vspace{0.5em}
  \onslide<4->
  \begin{displaymath}
    \mathclap{T(n) =}\underbrace{
      \sum\limits_{i = 0}^{(\log_4 n) - 1} \left(\tfrac{3}{16}\right)^i
      \mathrlap{\cdot c \cdot n^2}
    }_{
      \text{
        \footnotesize
        \begin{tabular}{c}
          geometric series,\\
          constant\\
          \color{gray}
          $\left(\begin{array}{c}
            \text{even with}\\
            \text{infinite elements}
          \end{array}\right)$
        \end{tabular}
      }
    } \hspace{1.25em} + \hspace{0.5em} \underbrace{
      d \cdot n^{\log_4 3}
      \vphantom{\sum\limits_{i = 0}^{(\log_4 n) - 1}}
    }_{
      \text{
        \makebox[0pt][c]{
          \footnotesize
          \begin{tabular}{c}
            {\color{MainA}$\log_4 3 < 1$},\\
            grows a lot\\
            slower than {\color{MainA}$n^2$}
          \end{tabular}
        }
      }
    } ~ \in \mathcal{O}(n^2)
  \end{displaymath}
  \begin{itemize}
    \item<5->
      Here: The costs of connecting the partial problems dominate
  \end{itemize}
\end{frame}

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

\begin{frame}{Recursion Equations}{Geometric Series}
  \begin{itemize}
    \item
      \textbf{Geometric progression:}\\
      Quotient of two neighboring sequence parts is constant
      \begin{displaymath}
        2^0, 2^1, 2^2, \ldots, 2^k
      \end{displaymath}
    \item<2->
      \textbf{Geometric series:}\\
      The series (cumulative sum) of a geometric sequence\\
    \item<3->
      For $\mid q \mid < 1$:
      \begin{displaymath}
        \sum\limits^{\infty}_{k=0} a_0 \cdot q^k = \dfrac{a_0}{1 - q}
        \hspace{1.5em}\Rightarrow\text{constant}
      \end{displaymath}
  \end{itemize}
\end{frame}

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

\begin{frame}{Recursion Equations}{Proof of $O(n^2)$}
  \textbf{Proof of $\mathcal{O}(n^2)$:}
  \begin{itemize}
    \item<2->
      We know:
      \begin{align*}
        T(n) &= 3T\left(\frac{n}{4}\right) + \Theta(n^2)\\
        {} &\leq 3T \left(\frac{n}{4}\right) + c \cdot n^2
      \end{align*}
    \item<3->
      Assumption:
      {\color{MainA}$T(n) \, \in \, \mathcal{O}(n^2)$},
      so there exists a {\color{MainA}$k > 0$} with
      \begin{displaymath}
        \color{MainA}
        T(n) \leq k \cdot n^2
      \end{displaymath}
  \end{itemize}
\end{frame}

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

\begin{frame}{Recursion Equations}{Proof of $O(n^2)$}
  \textbf{Proof of $\mathcal{O}(n^2)$:}
  \begin{itemize}
    \item<2->
      Presumption:
      {\color{MainA}$T(n) \, \in \, \mathcal{O}(n^2)$},
      so there exists a {\color{MainA}$k > 0$} with
      \begin{displaymath}
        \color{MainA}{T(n) < k \cdot n^2}
      \end{displaymath}
    \item<3->
      Substitution method:
      \begin{align*}
        \onslide<4->{T(n) & \leq 3 \cdot T \left( \frac{n}{4}\right)  + c \cdot n^2\\}
        \onslide<5->{{} & \leq 3 \, k \cdot \left( \frac{n}{4}\right)^2  + c \cdot n^2\\}
        \onslide<6->{{} & = \frac{3}{16} \, k \cdot n^2  + c \cdot n^2\\}
        \onslide<7->{{} & \leq k \cdot n^2
        \hspace{6em}\text{for } k \geq \frac{16}{13} \, c}
      \end{align*}
  \end{itemize}
\end{frame}

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

\subsection{Master theorem}

\begin{frame}{Recursion Equations}{Master theorem}
  \textbf{Master theorem:}
  \begin{itemize}
    \item<2->
      Solution approach for a recursion equation of the form:
      \begin{displaymath}
        \color{MainA}{
          T(n) = a \cdot T\left(\frac{n}{b}\right) + f(n),%
          \hspace{1.5em}a \geq 1, b > 1
        }
      \end{displaymath}
    \item<3->
      {\color{MainA}$T(n)$} is the runtime of an algorithm $\ldots$
      \begin{itemize}
        \item<4->
          $\ldots$ which divides a {\color{MainA}problem of size $n$}
          in {\color{MainA}$a$ partial problems}
        \item<5->
          $\ldots$ which solves each partial problem recursively\newline
          \hphantom{$\ldots$} with a
          {\color{MainA}runtime of $T\left(\tfrac{n}{b}\right)$}
        \item<6->
          $\ldots$ which takes {\color{MainA}$f(n)$} steps to
          merge all partial solutions
      \end{itemize}
  \end{itemize}
\end{frame}

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

\subsubsection{Master theorem (Simple Form)}

\begin{frame}{Recursion Equations}{Master theorem (Simple Form)}
  \textbf{Master theorem:}
  \begin{itemize}
    \item<2->
      In the examples we have seen that $\ldots$
      \begin{itemize}
        \item<3->
          Either the runtime of {\color{MainA}connecting the solutions}
          dominates
        \item<4->
          Or the runtime of {\color{MainA}solving the problems} dominates
        \item<5->
          Or both have {\color{MainA}equal influence on runtime}
      \end{itemize}
    \item<6->
      \textbf{Simple form:}
      Special case with runtime of connecting the solutions
      {\color{MainA}$f(n) \in O(n)$}
  \end{itemize}
\end{frame}

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

\begin{frame}{Recursion Equations}{Master theorem (Simple Form)}
  \textbf{Simple form:}
  \onslide<2->
  \begin{displaymath}
    \color{MainA}{
      T(n) = a \cdot T\left(\frac{n}{b}\right) +
      \underbrace{c \cdot n}_{
        \text{
          \clap{
            \footnotesize\color{black}
            \begin{tabular}{c}
              Is any $f(n)$\\
              in general form
            \end{tabular}
          }
        }
      },
      \hspace{1.5em}a \geq 1, b > 1, c > 0
    }
  \end{displaymath}
  \begin{itemize}
     \item<3->
       This yields a runtime of:
  \end{itemize}
  \onslide<4->
  \vspace{2em}
  \begin{displaymath}
    T(n) = \begin{cases}
      \Theta({\color{MainA}
        \smash{\overbrace{n^{\log_b a}}^{
          \text{\clap{Number of leaves}}
        }}
      }) & \text{if } a > b\\
      \Theta(n \, \log n) & \text{if } a = b\\
      \Theta(n) & \text{if } a < b
    \end{cases}
  \end{displaymath}
\end{frame}

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

\begin{frame}{Recursion Equations}{Master theorem (Simple Form)}
  \begin{figure}[!h]
    \begin{adjustbox}{width=\linewidth}
      \def\AlgoREDivide{3}% 3
      \def\AlgoRESize{0.5}% 1/2
      \def\AlgoREScale{4.444}% 15 / (3/2)^3
      \input{Images/RecursionEquations/ProblemSizeTree.tikz}
    \end{adjustbox}
    \caption{simple recursion equation with {\color{MainA}$a = 3, b = 2$}}
    \label{fig:recursion_equations:master_theorem_tree_3_2}
  \end{figure}
  \onslide<2->
  \textbf{Case 1:} {\color{MainA}$a > b$}
  \begin{itemize}
    \item<3->
      Three partial problems with $\frac{1}{2}$ the size
    \item<4->
      Solving the partial problems dominates (last layer, leaves)
    \item<5->
      Runtime of {\color{MainA}$\Theta(n^{\log_b a})$}
  \end{itemize}
\end{frame}

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

\begin{frame}{Recursion Equations}{Master theorem (Simple Form)}
  \begin{figure}[!h]
    \begin{adjustbox}{width=\linewidth}
      \def\AlgoREDivide{2}% 2
      \def\AlgoRESize{0.5}% 1/2
      \def\AlgoREScale{15}% 15
      \input{Images/RecursionEquations/ProblemSizeTree.tikz}
    \end{adjustbox}
    \caption{simple recursion equation with {\color{MainA}$a = 2, b = 2$}}
    \label{fig:recursion_equations:master_theorem_tree_2_2}
  \end{figure}
  \onslide<2->
  \textbf{Case 2:} {\color{MainA}$a = b$}
  \begin{itemize}
    \item<3->
      Two partial problems with $\frac{1}{2}$ the size
    \item<4->
      Each layer has equal costs, $\log n$ layers
    \item<5->
      Runtime of {\color{MainA}$\Theta(n \, \log n)$}
  \end{itemize}
\end{frame}

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

%\begin{frame}{Recursion Equations}{Master theorem (Simple Form)}
%  \begin{figure}[!h]
%    \begin{adjustbox}{width=\linewidth}
%      \def\AlgoREDivide{2}% 2
%      \def\AlgoRESize{0.25}% 1/4
%      \def\AlgoREScale{15}% 15
%      \input{Images/RecursionEquations/ProblemSizeTree.tikz}
%    \end{adjustbox}
%    \caption{simple recursion equation with
%      {\color{MainA}$a = 2, b = 4$}}
%    \label{fig:recursion_equations:master_theorem_tree_2_4}
%  \end{figure}
%  \textbf{Case 3:} {\color{MainA}$a < b$}
%  \begin{itemize}
%    \item
%      Two partial problems with $\frac{1}{4}$ the size
%    \item
%      Connecting all partial solutions dominates (first layer, root)
%    \item
%      Runtime of {\color{MainA}$\Theta(n)$}
%  \end{itemize}
%\end{frame}

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

\begin{frame}{Recursion Equations}{Master theorem (Simple Form)}
  \begin{figure}[!h]
    \begin{adjustbox}{width=\linewidth}
      \def\AlgoREDivide{2}% 2
      \def\AlgoRESize{0.33}% 1/3
      \def\AlgoREScale{15}% 15
      \input{Images/RecursionEquations/ProblemSizeTree.tikz}
    \end{adjustbox}
    \caption{simple recursion equation with {\color{MainA}$a = 2, b = 3$}}
    \label{fig:recursion_equations:master_theorem_tree_2_3}
  \end{figure}
  \onslide<2->
  \textbf{Case 3:} {\color{MainA}$a < b$}
  \begin{itemize}
    \item<3->
      Two partial problems with $\frac{1}{3}$ the size
    \item<4->
      Connecting all partial solutions dominates (first layer, root)
    \item<5->
      Runtime of {\color{MainA}$\Theta(n)$}
  \end{itemize}
\end{frame}

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

\begin{frame}{Recursion Equations}{Master theorem (Simple Form)}
  \textbf{For a recursion equation like}
  \begin{displaymath}
    \color{MainA}{%
      T(n) = a \cdot T\left(\frac{n}{b}\right) + c \cdot n,%
      \hspace{1.5em}a \geq 1, b > 1, c > 0%
    }%
  \end{displaymath}
  \vspace{-1.0em}
  \begin{itemize}
    \item<2->
      $\ldots$ yields a runtime of:
  \end{itemize}
  \onslide<3->
  \begin{displaymath}
    T(n) = \begin{cases}
      \Theta(n^{\log_b a}) & \text{if } a > b\\
      \Theta(n \, \log_b n) & \text{if } a = b\\
      \Theta(n) & \text{if } a < b
    \end{cases}
  \end{displaymath}
  \begin{itemize}
    \item<4->
      Proof with \textit{geometric series}:
      Number of operations per layer grows / shrinks by constant factor
      $\tfrac{a}{b}$
  \end{itemize}
\end{frame}

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

\subsubsection{Master theorem (General Form)}

\begin{frame}<beamer>{\LectureToC}
  \tableofcontents[currentsection,
    currentsubsection,
    subsubsectionstyle=show/shaded/shaded/shaded]
\end{frame}

\begin{frame}{Recursion Equations}{Master theorem (General Form)}
  \textbf{Master theorem (general form):}
  \onslide<2->
  \begin{displaymath}
  \color{MainA}{%
    T(n) = a \cdot T\left(\frac{n}{b}\right) + f(n),%
    \hspace{1.5em}a \geq 1, b > 1%
  }%
  \end{displaymath}
  \vspace{-1.0em}
  \begin{itemize}
    \item<3->
      \textbf{Case 1:}
      {\color{MainB}$T(n) \in \Theta(n^{\log_b a})$}
      \hfill if {\color{MainA}
        $f(n) \in \mathcal{O}(n^{\log_b a-\varepsilon}), ~ \varepsilon > 0$
      }\\
      {\color{gray}Solving the partial problems dominates\\
        (last layer, leaves)}
      \vspace{1.5em}
    \item<4->
      \textbf{Case 2:}
      {\color{MainB}$T(n) \in \Theta(n^{\log_b a} \log n)$}
      \hspace{1.5em} if {\color{MainA}
        $f(n) \in \Theta(n^{\log_b a})$
      }\\
      {\color{gray}Each layer has equal costs, $\log_b n$ layers}
  \end{itemize}
\end{frame}

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

\begin{frame}{Recursion Equations}{Master theorem (General Form)}
  \textbf{Master theorem (general form):}
  \begin{itemize}
    \item
      \textbf{Case 3:}
      {\color{MainB}$T(n) \in \Theta(f(n))$}
      \hfill if {\color{MainA}
        $f(n) \in \Omega(n^{\log_b a+\varepsilon}), ~ \varepsilon > 0$
      }\\
      {\color{gray}Connecting all partial solutions in first layer (root) dominates}\\[1.0em]
      {\color{MainA}Regularity condition:}
      \begin{align*}
        a \cdot f\left(\frac{n}{b}\right) \leq c \cdot f(n), \hspace{1.5em} &
        0 \leq c \leq 1,\\
        {} & n > n_0
      \end{align*}
  \end{itemize}
\end{frame}

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

\begin{frame}{Recursion Equations}{Master theorem (General Form) - Case 1}
  \textbf{Case 1 - Example:}
  \onslide<2->{{\color{MainB}$T(n) \in \Theta(n^{\log_b a})$}}
  \hfill if {\color{MainA}
    $f(n) \in O(n^{\log_b a-\varepsilon}), ~ \varepsilon > 0$
  }\\
  {\color{gray}Solving the partial problems dominates (last layer, leaves)}
  \vspace{1.0em}
  \begin{itemize}
    \item<3->
      $T(n) = 8 \cdot T(\tfrac{n}{2}) + 1000 \cdot n^2$
      \vspace{-0.5em}
      \begin{displaymath}
        a = 8, ~ b = 2, ~ f(n) = 1000 \cdot n^2, ~
        \smash{\underbrace{
          \log_b a = \log_2 8 = 3
        }_{\text{$n^3$ leaves}}}
      \end{displaymath}
      {\color{MainA}\begin{math}
        f(n) \in \mathcal{O}(n^{3-\varepsilon})$
        $\Rightarrow$
        $T(n) \in \Theta(n^3)
      \end{math}}
      \vspace{1.5em}
    \item<4->
      $T(n) = 9 \cdot T(\tfrac{n}{3}) + 17 \cdot n$
      \vspace{-0.5em}
      \begin{displaymath}
        a = 9, ~ b = 3, ~ f(n) = 17 \cdot n, ~
        \smash{\underbrace{
          \log_b a = \log_3 9 = 2
        }_{\text{$n^2$ leaves}}}
      \end{displaymath}
      {\color{MainA}\begin{math}
        f(n) \in \mathcal{O}(n^{2-\varepsilon})$
        $\Rightarrow$
        $T(n) \in \Theta(n^2)
      \end{math}}
   \end{itemize}
\end{frame}

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

\begin{frame}{Recursion Equations}{Master theorem (General Form) - Case 2}
  \textbf{Case 2:}
  \onslide<2->{{\color{MainB}$T(n) \in \Theta(n^{\log_b a} \log n)$}}
  \hspace{1.5em} if {\color{MainA}
    $f(n) \in \Theta(n^{\log_b a})$
  }\\
  {\color{gray}Each layer has equal costs, $\log n$ layers}
  \vspace{1.0em}
  \begin{itemize}
    \item<3->
      $T(n) = 2 \cdot T(\tfrac{n}{2}) + 10 \cdot n$
      \vspace{-0.5em}
      \begin{displaymath}
        a = 2, ~ b = 2, ~ f(n) = 10 \cdot n, ~
        \smash{\underbrace{
          \log_b a = \log_2 2 = 1
        }_{\text{$n^1$ leaves}}}
      \end{displaymath}
      {\color{MainA}\begin{math}
        f(n) \in \Theta(n^{\log_2 2})$
        $\Rightarrow$
        $T(n) \in \Theta(n \, \log n)
      \end{math}}
      \vspace{1.0em}
    \item<4->
      $T(n) = T(\tfrac{2\,n}{3}) + 1$
      \vspace{-0.5em}
      \begin{displaymath}
        a = 1, ~ b = \tfrac{3}{2}, ~ f(n) = 1, ~
        \smash{\underbrace{
          \log_b a = \log_{\nicefrac{3}{2}} 1 = 0
        }_{\text{$n^0$ leaves $=$ 1 leaf}}}
      \end{displaymath}
      \hfill\\[1.0em]
      {\color{MainA}\begin{math}
        f(n) \in \Theta(n^{\log_{\nicefrac{3}{2}} 1})$
        $\Rightarrow$
        $T(n) \in \Theta(n^0 \log n) = \Theta(\log n)
      \end{math}}
  \end{itemize}
\end{frame}

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

\begin{frame}{Recursion Equations}{Master theorem (General Form) - Case 3}
  \textbf{Case 3:}
  \onslide<2->{{\color{MainB}$T(n) \in \Theta(f(n))$}}
  \hfill if {\color{MainA}
    $f(n) \in \Omega(n^{\log_b a+\varepsilon}), ~ \varepsilon > 0$
  }\\
  {\color{gray}Connecting all partial solutions in first layer (root) dominates}
  \vspace{1.0em}
  \begin{itemize}
    \item<3->
      $T(n) = 2 \cdot T(\tfrac{n}{2}) + n^2$
      \vspace{-0.5em}
      \begin{displaymath}
        a = 2, ~ b = 2, ~ f(n) = n^2, ~
        \smash{\underbrace{
          \log_b a = \log_2 2 = 1
        }_{\text{$n^1$ leaves}}}
      \end{displaymath}
      {\color{MainA}$f(n) \in \Omega(n^{1+\varepsilon})$}
  \end{itemize}
\end{frame}

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

\begin{frame}{Recursion Equations}{Master theorem (General Form) - Case 3}
  \textbf{Case 3:}
  {\color{MainB}$T(n) \in \Theta(f(n))$}
  \hfill if {\color{MainA}
    $f(n) \in \Omega(n^{\log_b a+\varepsilon}), ~ \varepsilon > 0$
  }\\
  {\color{gray}Connecting all partial solutions in first layer (root) dominates}
  \vspace{1.0em}
  \begin{itemize}
    \item
      $T(n) = 2 \cdot T(\tfrac{n}{2}) + n^2$
    \item<3->
      {\color{MainA}$f(n) \in \Omega(n^{1+\varepsilon})$}
    \item<4->
      Check if {\color{MainA}regularity condition} also holds:\\
      \begin{align*}
        a \cdot f\left(\frac{n}{b}\right) &\leq c \cdot f(n)\\
        2 \cdot \left(\frac{n}{2}\right)^2 &\leq c \cdot n^2
        \hspace{1.5em} \Rightarrow \frac{1}{2} \cdot n^2 \leq c \cdot n^2
        \hspace{1.5em} \Rightarrow c \geq \frac{1}{2}
      \end{align*}
      {\color{MainA}$\Rightarrow T(n) \in \Theta(n^2)$}
  \end{itemize}
\end{frame}

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

\begin{frame}{Recursion Equations}{Master theorem (General Form)}
  \textbf{Master theorem:}
  \begin{itemize}
    \item<2->
      Not always applicable:
      {\color{MainA}$T(n) = 2 \cdot T(\frac{n}{2}) + n \, \log n$}
  \end{itemize}
  \onslide<3->
  \vspace{1.0em}
  \begin{displaymath}
    a = 2, ~ b = 2, ~ f(n) = n \, \log n, ~
    \underbrace{\log_b a = \log_2 2 = 1}_{\text{$n^1$ leaves}}
  \end{displaymath}
  \begin{itemize}
    \item<4->
      \textbf{Case 1:}
      $f(n) \notin O(n^{1-\varepsilon})$
    \item<5->
      \textbf{Case 2:}
      $f(n) \notin \Theta(n^1)$
    \item<6->
      \textbf{Case 3:}
      $f(n) \notin \Omega(n^{1+\varepsilon})$
  \end{itemize}
  \onslide<7->
  \begin{center}
    {\color{MainA}$n \, \log n$} is \textit{asymptotically} larger than
    {\color{MainA}$n$},\\ but not \textit{polynominal} larger
  \end{center}
\end{frame}

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

\begin{frame}{Recursion Equations}{Master theorem - Summary}
  \textbf{Master theorem:}
  \onslide<2->
  \begin{displaymath}
    \color{MainA}{
      T(n) = a \cdot T\left(\frac{n}{b}\right) + f(n)
    }
  \end{displaymath}
  \vspace{-1.0em}
  \begin{itemize}
    \item<3->
      Three cases depending on the dominance of the terms
    \item<4->
      \textbf{Case 1:}
      Solving the partial problems is \textit{polynominal} bigger than merging
      all solutions\\
      {\color{MainA}$T(n) \in \Theta(n^{\log_b a})$}, \hspace{3.5em}
      {\color{gray}$T(n) \in \Theta(\text{number of leaves})$}
      \vspace{0.5em}
   \item<5->
     \textbf{Case 2:}
     Each layer has equal costs\\
     {\color{MainA}$T(n) \in \Theta(n^{\log_b a} \log n)$}, \hspace{1.5em}
     {\color{gray}$\log n$ layers}
     \vspace{0.5em}
   \item<6->
     \textbf{Case 3:}
     Connecting all partial solutions is \textit{polynominal} bigger than
     solving all partial problems\\
     {\color{MainA}$T(n) \in \Theta(f(n))$}
  \end{itemize}
\end{frame}


\section{State Error Objective}

	\begin{frame}
		\frametitle{Robot Models Used}
		\begin{columns}[T]
			\begin{column}{0.45\textwidth}
				\onslide<2->{
				\begin{block}{Cartesian Co-ordinate Model}
					\[
					\begin{bmatrix}
						\dot{x} \\
						\dot{y} \\
						\dot{\theta}
					\end{bmatrix}
					=
					\begin{bmatrix}
						\cos(\theta) & 0 \\
						\sin(\theta) & 0 \\
						0 & 1
					\end{bmatrix}
					\begin{bmatrix}
						\upsilon \\
						\omega
					\end{bmatrix}
					\]
				\end{block}
				}
				\onslide<3->{
				\begin{block}{Kino-dynamic Model}
					\[
					\begin{bmatrix}
						\dot{x} \\
						\dot{y} \\
						\dot{\theta} \\
						\dot{\upsilon} \\
						\dot{\omega}
					\end{bmatrix}
					=
					\begin{bmatrix}
						\cos(\theta) & 0 & 0 & 0 \\
						\sin(\theta) & 0 & 0 & 0 \\
						0 & 1 & 0 & 0 \\
						0 & 0 & 1 & 0 \\
						0 & 0 & 0 & 1
					\end{bmatrix}
					\begin{bmatrix}
						\upsilon \\
						\omega \\
						a \\
						\alpha
					\end{bmatrix}
					\]
				\end{block}
				}
			\end{column}
			\begin{column}{0.49\textwidth}
				\centering
				\onslide<1->{\includegraphics[scale=0.35]{pictures/robot_cart.pdf}}
			\end{column}
		\end{columns}
	\end{frame}

	\begin{frame}
		\frametitle{Robot Models Used}
		\begin{columns}[T]
			\begin{column}{0.45\textwidth}
				\onslide<2->{
				\begin{block}{Polar Co-ordinate Model}
					\begin{align*}
						\rho& = \sqrt{(x-x_g)^2+(y-y_g)^2} \\[0.4cm]
						\phi& = \arctan2\big((y-y_g),(x-x_g)\big) \\[0.4cm]
						\alpha&= \theta - \phi
					\end{align*}
				\end{block}
				}
			\end{column}
			\begin{column}{0.49\textwidth}
				\centering
				\onslide<1->{\includegraphics[scale=0.35]{pictures/robot_polar.pdf}}
			\end{column}
		\end{columns}
	\end{frame}

	\begin{frame}
		\frametitle{NMPC General Formulation}
		\begin{block}{NMPC Optimization Problem}
			\begin{align*}
				&\underset{\mathbf{X}^*,\mathbf{U}^*}{\text{min    }}
				\tikzmark{a9}J_{N_p}(\mathbf{x}_k) = \sum_{j=0}^{N_p}\tikzmark{a11}\ell (\mathbf{x}_{k + j},\mathbf{u}_{k + j}), \\
				&\quad \text{subject to:} \notag  \\
				&\quad\quad \tikzmark{a12}\mathbf{x}_{k+j+1} = \mathcal{F}(\mathbf{x}_{k+j},\mathbf{u}_{k+j}), & \forall\text{ } 0\leq j\leq N_p-1  \\
				&\quad\quad \tikzmark{a13}\mathbf{x}_{k+j} \in \mathbb{X}, & \forall \text{ }0\leq j\leq N_p \\
				&\quad\quad \tikzmark{a14}\mathbf{u}_{k+j} \in \mathbb{U}, & \forall \text{ }0\leq j\leq N_p-1 \\
				&\quad\quad \tikzmark{a15}\mathbf{d}_{\imath,k+j}  \geq \xi_o, & \forall\text{ } 0\leq j\leq N_p-1, \text{ }\forall\text{ } \imath \in I_s
			\end{align*}
		\end{block}

		\begin{tikzpicture}[overlay, remember picture]
			\coordinate (A9) at ($({pic cs:a9})+(1ex, 1ex)$);
			\coordinate (A11) at ($({pic cs:a11})+(1ex, 1ex)$);
			\coordinate (A12) at ($({pic cs:a12})+(0ex, 0.5ex)$);
			\coordinate (A13) at ($({pic cs:a13})+(0ex, 0.5ex)$);
			\coordinate (A14) at ($({pic cs:a14})+(0ex, 0.5ex)$);
			\coordinate (A15) at ($({pic cs:a15})+(1ex, -0.5ex)$);
			\pause
			\node [fit=(A9), baseline] (n9) {};
			\node[cyan, above of= n9, node distance = 3em] (t9_1) {\small cost};
			\node[cyan, above of= n9, node distance = 2em] (t9) {\small function};
			\draw [<-, cyan] (n9.north) to [below] (t9.south);
			\pause
			\node [fit=(A11), baseline] (n11) {};
			\node[cyan, above of= n11, node distance = 3em] (t11_1) {\small regulatory};
			\node[cyan, above of= n11, node distance = 2em] (t11) {\small term};
			\draw [<-, cyan] (n11.north) to [below] (t11.south);
			\pause
			\node [fit=(A12), baseline] (n12) {};
			\node[cyan, left of= n12, node distance = 4em] at (2.8,3) (t12) {\small model};
			\node[cyan, below of= t12, node distance = 1em] (t12_1) {\small constraints};
			\draw [<-, cyan] (n12.west) to [right] (t12.east);
			\pause
			\node [fit=(A13), baseline] (n13) {};
			\node[cyan, left of= n13, node distance = 4em] (t13) {\small state};
			\node[cyan, below of= t13, node distance = 1em] (t13_1) {\small boundries};
			\draw [<-, cyan] (n13.west) to [right] (t13.east);
			\pause
			\node [fit=(A14), baseline] (n14) {};
			\node[cyan, below of= t13, node distance = 3em] (t14) {\small input};
			\node[cyan, below of= t14, node distance = 1em] (t14_1) {\small boundries};
			\draw [<-, cyan] (n14.west) to [right] (t14.east);
			\pause
			\node [fit=(A15), baseline] (n15) {};
			\node[cyan, below of= n15, node distance = 3em] (t15_1) {\small static};
			\node[cyan, below of= n15, node distance = 2em] (t15) {\small obstacles};
			\draw [<-, cyan] (n15.south) to [above] (t15.north);
		\end{tikzpicture}
	\end{frame}

	\begin{frame}
		\frametitle{NMPC General Formulation}
		\begin{block}{Regulatory Terms (e.g., Cartesian coordinate)}
			\[
				\begin{aligned}
					\ell (\mathbf{x}_{k + j},\mathbf{u}_{k + j}) = \enspace
					\tikzmark{a1}
					(\mathbf{x}_{k + j} - \mathbf{x}_{ref})^T \enspace \mathbf{Q_1} \enspace (\mathbf{x}_{k + j} - \mathbf{x}_{ref})
					\tikzmark{a2}
					\enspace + \enspace
					\tikzmark{a3}
					\Delta\mathbf{u}_{k + j}^T \enspace \mathbf{R} \enspace \Delta\mathbf{u}_{k + j}
					\tikzmark{a4}
					\enspace \tikzmark{a7}-\tikzmark{a8} \enspace
					\tikzmark{a5}
					\sum_{i=0}^{I} \mathbf{d}_{i,k + j}^T \enspace \mathbf{Q_2} \enspace \mathbf{d}_{i,k + j}
					\tikzmark{a6}
				\end{aligned}
			\]
		\end{block}
		\vfill
		\vfill
		\vfill
		\begin{itemize}
			\item $\mathbf{d}_{i,k + j}$: distance between the robot and $i^{th}$ detected moving obstacle.
		\end{itemize}

		\begin{tikzpicture}[overlay, remember picture]
			\coordinate (A1) at ($({pic cs:a1})+(+0.1ex, 2ex)$);
			\coordinate (A2) at ($({pic cs:a2})+(-0.1ex,-0.5ex)$);
			\coordinate (A3) at ($({pic cs:a3})+(+0.1ex, 2ex)$);
			\coordinate (A4) at ($({pic cs:a4})+(-0.1ex,-0.5ex)$);
			\coordinate (A5) at ($({pic cs:a5})+(+0.1ex, 3.5ex)$);
			\coordinate (A6) at ($({pic cs:a6})+(-0.1ex,-2.5ex)$);
			\coordinate (A7) at ($({pic cs:a7})+(+1ex, 0.5ex)$);
			\coordinate (A8) at ($({pic cs:a8})+(-1ex, 0.5ex)$);
			\pause
			\node [fit=(A1)(A2), draw=tugreen, thick, rounded corners] (n1) {};
			\node[overlay, below of= n1, node distance = 6em] (t1) {State Error};
			\node[overlay, below of= n1, node distance = 7em] (t1_1) {Objective};
			\draw [thick, -latex, tugreen] (n1.south) to [above] (t1.north);
			\pause
			\node [fit=(A3)(A4), draw=tugreen, thick, rounded corners] (n2) {};
			\node[overlay, below of= n2, node distance = 6em] (t2) {Input Penealty};
			\node[overlay, below of= n2, node distance = 7em] (t2_1) {Objective};
			\draw [thick, -latex, tugreen] (n2.south) to [above] (t2.north);
			\pause
			\node [fit=(A5)(A6), draw=tugreen, thick, rounded corners] (n3) {};
			\node[overlay, below of= n3, node distance = 5.7em] (t3) {Dyn Obstacles};
			\node[overlay, below of= n3, node distance = 6.7em] (t3_1) {Objective};
			\draw [thick, -latex, tugreen] (n3.south) to [above] (t3.north);
			\pause
			\node [fit=(A7)(A8), draw=red, circle] (n4) {};
			\node[overlay, red, above of= n4, node distance = 8em] (t4) {Maximization};
			\draw [thick, -latex, red] (n4.north) to [below] (t4.south);
		\end{tikzpicture}

%		\tikz[baseline=(n1.base)]{
%			\node[draw=tugreen, ,thick, rounded corners, inner sep=5pt]
%			(n1){$xfoz$};
%			\node[overlay, below of= n1, node distance = 6em] (t1) {State Error};
%			\draw [thick, -latex, tugreen] (n1.south) to [above] (t1.north);
%		}
	\end{frame}

%	\begin{frame}
%		\frametitle{Cartesian Coordinate Model Block Diagram}
%		\begin{figure}[hbtp]
%			\centering
%			\includegraphics[scale=0.7]{pictures/block_diagram_cart.pdf}
%			\caption{NMPC Control Loop (Cartesian coordinate Model)}
%		\end{figure}
%	\end{frame}

	\begin{frame}
		\frametitle{NMPC General Formulation}
		\begin{block}{Regulatory Terms (e.g., Polar coordinate)}
			\parbox[c][4\baselineskip][t]{\textwidth}{
			\begin{align*}
				\ell_p (\vartheta_{k + j},\mathbf{u}_{k + j}) &=
				\pmb{[} \ell_{p,1}(\cdot,\cdot) \enspace \ell_{p,2}(\cdot,\cdot) \enspace \ell_{p,3}(\cdot,\cdot) \pmb{]}
				\enspace
				\mathbf{Q_1} \enspace
				\begin{bmatrix}
					\ell_{p,1}(\cdot,\cdot) \\
					\ell_{p,2}(\cdot,\cdot) \\
					\ell_{p,3}(\cdot,\cdot)
				\end{bmatrix}
				+ \cdots
			\end{align*}
			}
		\end{block}
		Error state objective is the minimization of: \\[0.5cm]
		\begin{itemize}
			\item Euclidean distance: $\ell_{p,1}(\cdot,\cdot) = \sqrt{(x_{k+j} - x_{ref})^2+(y_{k+j} - y_{ref})^2}$ \\ [0.2cm]
			\item Heading towards goal: $\ell_{p,2}(\cdot,\cdot) = \Big(\theta_{k+j} - \arctan2\big((y_{k+j} - y_{ref}),(x_{k+j} - x_{ref})\big)\Big)^2$ \\ [0.2cm]
			\item Reference orientation: $\ell_{p,3}(\cdot,\cdot) = (\theta_{k+j} - \theta_{ref})^2$
		\end{itemize}
	\end{frame}

%	\begin{frame}
%		\frametitle{Polar Coordinate Model Block Diagram}
%		\begin{figure}[hbtp]
%			\centering
%			\includegraphics[scale=0.64]{pictures/block_diagram_polar.pdf}
%			\caption{NMPC Control Loop (Polar coordinate Model)}
%		\end{figure}
%	\end{frame}

%	\begin{frame}
%		\frametitle{Kino-dynamic Model Block Diagram}
%		\begin{figure}[hbtp]
%			\centering
%			\includegraphics[scale=0.64]{pictures/block_diagram_kino.pdf}
%			\caption{NMPC Control Loop (Kino-dynamic Model)}
%		\end{figure}
%	\end{frame}

	\begin{frame}
		\frametitle{Potential Collision Zone}
		\centering
		\includegraphics[scale=0.6]{pictures/robots_pot_coll.pdf}
	\end{frame}