\documentclass[a4paper]{article}
\usepackage{listings}
\usepackage{fontspec}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{multicol}
\usepackage{changepage}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}

\usetikzlibrary{arrows.meta,automata}

\renewcommand{\thesection}{Task \arabic{section}}
\renewcommand{\thesubsection}{\arabic{subsection})}
\renewcommand{\thesubsubsection}{\alph{subsubsection})}

\setmonofont[Scale=0.9]{Antikor Mono Medium}

\input{fasto.sty}

\setlength{\parskip}{5pt}
\setlength{\parindent}{0pt}

\title{W4 - IPS}
\author{Nikolaj Gade (qhp695)}
\date{May 2022}

\makeatletter
\renewenvironment{table}%
  {\renewcommand{\familydefault}{\ttdefault}\selectfont
  \@float{table}}
  {\end@float}
\makeatother


\begin{document}
\maketitle
\section{}
\begin{table}[h]
\begin{center}
\begin{tabular}{|r||r|r|r|l|} \hline
  SubExp & I/O? & Elim? & UsedVars & OptSubExp                                   \\ \hline \hline
       1 &   no &     - &    u & u                                           \\ \hline
       2 &   no &     - &    x & x+x                                         \\ \hline
       3 &  yes &     - &    x & foo(x)                                      \\ \hline
       4 &   no &     - &      & 7                                           \\ \hline
       5 &  yes &    no &    x & let x=foo(x) in 7                           \\ \hline
       6 &   no &     - &  x,u & x+u                                         \\ \hline
       7 &   no &     - &  y,x & y*x                                         \\ \hline
       8 &   no &   yes &  y,x & y*x                                         \\ \hline
       9 &  yes &    no &    x & let y=(let x=foo(x) in 7) in y*x            \\ \hline
      10 &  yes &   yes &    x & let y=(let x=foo(x) in 7) in y*x            \\ \hline
      11 &  yes &    no &    u & let x=u in let y=(let x=foo(x) in 7) in y*x \\ \hline
\end{tabular}
\end{center}
\end{table}

\section{}
\subsubsection{}
\begin{table}[h]
\begin{center}
\begin{tabular}{|r||r|r|r|} \hline
   i & succ[i] & gen[i] & kill[i] \\ \hline \hline
   1 &       2 &        &         \\ \hline
   2 &     3,7 &    a,b &         \\ \hline
   3 &       4 &        &         \\ \hline
   4 &       5 &      a &       t \\ \hline
   5 &       6 &      b &       a \\ \hline
   6 &       7 &      t &       b \\ \hline
   7 &       8 &        &         \\ \hline
   8 &       9 &        &       z \\ \hline
   9 &      10 &    a,b &       b \\ \hline
  10 &    1,11 &    b,z &         \\ \hline
  11 &      12 &        &         \\ \hline
  12 &         &      a &         \\ \hline
\end{tabular}
\end{center}
\end{table}

\newpage
\subsubsection{}
\begin{table}[h]
\begin{center}
\begin{tabular}{|r||c|c||c|c||c|c||c|c|} \hline
    & \multicolumn{2}{c||}{Initial} & \multicolumn{2}{c||}{Iteration 1} & \multicolumn{2}{c||}{Iteration 2} & \multicolumn{2}{c|}{Iteration 3} \\
  i & out[i] & in[i] & out[i] & in[i] & out[i] & in[i] & out[i] & in[i] \\ \hline \hline
  1 & & & a,b   & a,b   & a,b   & a,b   & a,b   & a,b   \\ \hline
  2 & & & a,b   & a,b   & a,b   & a,b   & a,b   & a,b   \\ \hline
  3 & & & a,b   & a,b   & a,b   & a,b   & a,b   & a,b   \\ \hline
  4 & & & b,t   & a,b   & b,t   & a,b   & b,t   & a,b   \\ \hline
  5 & & & a,t   & b,t   & a,t   & b,t   & a,t   & b,t   \\ \hline
  6 & & & a,b   & a,t   & a,b   & a,t   & a,b   & a,t   \\ \hline
  7 & & & a,b   & a,b   & a,b   & a,b   & a,b   & a,b   \\ \hline
  8 & & & a,b,z & a,b   & a,b,z & a,b   & a,b,z & a,b   \\ \hline
  9 & & & a,b,z & a,b,z & a,b,z & a,b,z & a,b,z & a,b,z \\ \hline
 10 & & & a     & a,b,z & a,b   & a,b,z & a,b   & a,b,z \\ \hline
 11 & & & a     & a     & a     & a     & a     & a     \\ \hline
\end{tabular}
\end{center}
\end{table}

\subsubsection{}
\begin{table}[h]
\begin{center}
\begin{tabular}{|r||c|c||c|} \hline
  i & kill[i] & out[i] & Interferes with \\ \hline \hline
  4 &       t &    b,t &               b \\ \hline
  5 &       a &    a,t &               t \\ \hline
  6 &       b &    a,b &               a \\ \hline
  8 &       z &  a,b,z &             a,b \\ \hline
  9 &       b &  a,b,z &             a,z \\ \hline
\end{tabular}
\end{center}
\end{table}

\subsubsection{}
\resizebox{100pt}{!}{%
\begin{tikzpicture}
  \node[font=\bfseries] (a) {a};
  \node[font=\bfseries] (b) [right of=a] {b};
  \node[font=\bfseries] (t) [below of=a] {t};
  \node[font=\bfseries] (z) [below of=b] {z};

  \draw (t) to (b);
  \draw (a) to (t);
  \draw (z) to (a);
  \draw (z) to (b);
  \draw (b) to (a);
\end{tikzpicture}
}

\newpage

\subsubsection{}

\begin{table}[h]
\begin{center}
\begin{tabular}{|c|c||c|} \hline
  Node & Neighbors &           Color \\ \hline \hline
     z &           & \color{blue}  1 \\ \hline
     b &         z & \color{red}   2 \\ \hline
     a &       b,z & \color{green} 3 \\ \hline
     t &       a,b & \color{blue}  1 \\ \hline
\end{tabular}
\end{center}
\end{table}

\resizebox{100pt}{!}{%
\begin{tikzpicture}
  \node[font=\bfseries] (a) [color=red] {a};
  \node[font=\bfseries] (b) [right of=a, color=green] {b};
  \node[font=\bfseries] (t) [below of=a, color=blue] {t};
  \node[font=\bfseries] (z) [below of=b, color=blue] {z};

  \draw (t) to (b);
  \draw (a) to (t);
  \draw (z) to (a);
  \draw (z) to (b);
  \draw (b) to (a);
\end{tikzpicture}
}

\subsubsection{}

\begin{table}[h]
\begin{center}
\begin{tabular}{|c|c||c|} \hline
  Node & Neighbors &           Color \\ \hline \hline
     z &           & \color{blue}  1 \\ \hline
     b &         z & \color{red}   2 \\ \hline
     t &         b & \color{blue}  1 \\ \hline
     a &     b,t,z &           spill \\ \hline
\end{tabular}
\end{center}
\end{table}

\resizebox{100pt}{!}{%
\begin{tikzpicture}
  \node[font=\bfseries] (a) [color=gray] {a};
  \node[font=\bfseries] (b) [right of=a, color=red] {b};
  \node[font=\bfseries] (t) [below of=a, color=blue] {t};
  \node[font=\bfseries] (z) [below of=b, color=blue] {z};

  \draw (t) to (b);
  \draw [color=gray] (a) to (t);
  \draw [color=gray] (z) to (a);
  \draw (z) to (b);
  \draw [color=gray] (b) to (a);
\end{tikzpicture}
}

\begin{lstlisting}
   gcd(a,b) {
    M[addra] := a
1:  LABEL start
    a2 := M[addra]
2:  IF a2 < b THEN next ELSE swap
3:  LABEL swap
    a4 := M[addra]
4:  t := a4
5:  a5 := b
    M[addra] := a5
6:  b := t
7:  LABEL next
8:  z := 0
    a9 := M[addra]
9:  b := b mod a9
10: IF b = z THEN end ELSE start
11: LABEL end
    a := M[addra]
12: RETURN a
   }
\end{lstlisting}

\section{}
Edges without a label should be assumed to be labeled with \epsilon.

\begin{tikzpicture}
  \begin{scope}
    \node[] (0) at (0,0) {start};
    \node[draw, rectangle, rounded corners=2mm] (a) at (3,0) {\color{ForestGreen}\textbf{(a*b|a)*(b|}$\boldsymbol\epsilon$\textbf{)}};
    \node[draw, state, double] (b) at (6,0) {};
  \end{scope}

  \begin{scope}[rounded corners=2mm, -Triangle]
    \draw (0) -- (a);
    \draw (a) -- (b);
  \end{scope}
\end{tikzpicture}

\begin{tikzpicture}
  \begin{scope}
    \node[] (0) at (0,0) {start};
    \node[draw, rectangle, rounded corners=2mm] (a) at (2,0) {\color{ForestGreen}\textbf{(a*b|a)*}};
    \node[draw, rectangle, rounded corners=2mm] (c) at (4,0) {\color{red}\textbf{b|}$\boldsymbol\epsilon$};
    \node[draw, state, double] (b) at (6,0) {};
  \end{scope}

  \begin{scope}[rounded corners=2mm, -Triangle]
    \draw (0) -- (a);
    \draw (a) -- (c);
    \draw (c) -- (b);
  \end{scope}
\end{tikzpicture}

\begin{tikzpicture}
  \begin{scope}
    \node[] (0) at (0,0) {start};
    \node[state] (a) at (2,0) {};
    \node[draw, rectangle, rounded corners=2mm] (d) at (2,2) {\color{ForestGreen}\textbf{a*b|a}};
    \node[draw, rectangle, rounded corners=2mm] (c) at (4,0) {\color{red}\textbf{b|}$\boldsymbol\epsilon$};
    \node[draw, state, double] (b) at (6,0) {};
  \end{scope}

  \begin{scope}[rounded corners=2mm, -Triangle]
    \draw (0) -- (a);
    \draw (a) -- (c);
    \draw (c) -- (b);
    \draw (a) to [out = 135, in=225] (d);
    \draw (d) to [out = 315, in=45] (a);
  \end{scope}
\end{tikzpicture}

\begin{tikzpicture}
  \begin{scope}
    \node[] (0) at (0,0) {start};
    \node[state] (a) at (2,0) {};
    \node[state] (d) at (0.5,2.5) {};
    \node[draw, rectangle, rounded corners=2mm] (e) at (2,3.5) {\color{ForestGreen}\textbf{a*b}};
    \node[draw, state] (f) at (2,1.5) {};
    \node[draw, state] (g) at (3.5,2.5) {};
    \node[draw, rectangle, rounded corners=2mm] (c) at (4,0) {\color{red}\textbf{b|}$\boldsymbol\epsilon$};
    \node[draw, state, double] (b) at (6,0) {};
  \end{scope}

  \begin{scope}[rounded corners=2mm, -Triangle]
    \draw (0) -- (a);
    \draw (a) -- (c);
    \draw (c) -- (b);
    \draw (a) to [out = 135, in=180] (d);
    \draw (g) to [out = 0, in=45] (a);
    \draw (d) -- (e);
    \draw (d) -- (f);
    \draw (e) -- (g);
    \draw (f) -- (g) node[midway, above] {a};
  \end{scope}
\end{tikzpicture}

\begin{tikzpicture}
  \begin{scope}
    \node[] (0) at (0,0) {start};
    \node[state] (a) at (2,0) {};
    \node[state] (d) at (0,2.5) {};
    \node[draw, rectangle, rounded corners=2mm] (e) at (1.3,3.5) {\color{ForestGreen}\textbf{a*}};
    \node[state] (h) at (2.7,3.5) {};
    \node[draw, state] (f) at (2,1.5) {};
    \node[draw, state] (g) at (4,2.5) {};
    \node[draw, rectangle, rounded corners=2mm] (c) at (4,0) {\color{red}\textbf{b|}$\boldsymbol\epsilon$};
    \node[draw, state, double] (b) at (6,0) {};
  \end{scope}

  \begin{scope}[rounded corners=2mm, -Triangle]
    \draw (0) -- (a);
    \draw (a) -- (c);
    \draw (c) -- (b);
    \draw (a) to [out = 135, in=180] (d);
    \draw (g) to [out = 0, in=45] (a);
    \draw (d) -- (e);
    \draw (d) -- (f);
    \draw (e) -- (h);
    \draw (h) -- (g) node[midway, above] {b};
    \draw (f) -- (g) node[midway, above] {a};
  \end{scope}
\end{tikzpicture}

\begin{tikzpicture}
  \begin{scope}
    \node[] (0) at (0,0) {start};
    \node[state] (a) at (2,0) {};
    \node[state] (d) at (0,2.5) {};
    \node[state] (e) at (1.3,3.5) {};
    \node[state] (i) at (0.3,4.5) {};
    \node[state] (h) at (2.7,3.5) {};
    \node[draw, state] (f) at (2,1.5) {};
    \node[draw, state] (g) at (4,2.5) {};
    \node[draw, rectangle, rounded corners=2mm] (c) at (4,0) {\color{red}\textbf{b|}$\boldsymbol\epsilon$};
    \node[draw, state, double] (b) at (6,0) {};
  \end{scope}

  \begin{scope}[rounded corners=2mm, -Triangle]
    \draw (0) -- (a);
    \draw (a) -- (c);
    \draw (c) -- (b);
    \draw (a) to [out = 135, in=180] (d);
    \draw (g) to [out = 0, in=45] (a);
    \draw (d) -- (e);
    \draw (d) -- (f);
    \draw (e) to [out = 180, in=270] (i) node at (1.3,4.5) {a};
    \draw (i) to [out = 0, in=90] (e);
    \draw (e) -- (h);
    \draw (h) -- (g) node[midway, above right] {b};
    \draw (f) -- (g) node[midway, above left] {a};
  \end{scope}
\end{tikzpicture}

\begin{tikzpicture}
  \begin{scope}
    \node[] (0) at (0,0) {start};
    \node[state] (a) at (2,0) {};
    \node[state] (d) at (0,2.5) {};
    \node[state] (e) at (1.3,3.5) {};
    \node[state] (i) at (0.3,4.5) {};
    \node[state] (h) at (2.7,3.5) {};
    \node[state] (f) at (2,1.5) {};
    \node[state] (g) at (4,2.5) {};
    \node[state] (c) at (5,0) {};
    \node[state] (j) at (6.8,1) {};
    \node[state] (k) at (6.8,-1) {};
    \node[state, double] (b) at (8.6,0) {};
  \end{scope}

  \begin{scope}[rounded corners=2mm, -Triangle]
    \draw (0) -- (a);
    \draw (a) -- (c);
    \draw (a) to [out = 135, in=180] (d);
    \draw (g) to [out = 0, in=45] (a);
    \draw (d) -- (e);
    \draw (d) -- (f);
    \draw (e) to [out = 180, in=270] (i) node at (1.3,4.5) {a};
    \draw (i) to [out = 0, in=90] (e);
    \draw (e) -- (h);
    \draw (h) -- (g) node[midway, above right] {b};
    \draw (f) -- (g) node[midway, above left] {a};
    \draw (c) -- (j);
    \draw (c) -- (k);
    \draw (j) -- (b) node[midway, above right] {b};
    \draw (k) -- (b);
  \end{scope}
\end{tikzpicture}

\section{}
\begin{table}[h]
\begin{center}
\begin{tabular}{|l||r|c|} \hline
  state & epsilon-closure & complete \\ \hline \hline
  0 & \{1,2,4\} & \\ \hline
\end{tabular}
\end{center}
\end{table}


\begin{equation}
\begin{split}
move(s'_0, \mathtt{a}) & = \epsilon\text{-closure}(\{t | s \in \{1,2,4\}\text{ and } s^{\mathtt{a}}t \in T\}) \\
& = \epsilon\text{-closure}(\{3,4\}) \\
& = \{3,4,6\} \\
& = s'_1
\end{split}
\end{equation}
\begin{equation}
\begin{split}
move(s'_0, \mathtt{b}) & = \epsilon\text{-closure}(\{t | s \in \{1,2,4\}\text{ and } s^{\mathtt{b}}t \in T\}) \\
& = \epsilon\text{-closure}(\{5\}) \\
& = \{4,5,6\} \\
& = s'_2
\end{split}
\end{equation}

\begin{table}[h]
\begin{center}
\begin{tabular}{|l||r|c|} \hline
  state & epsilon-closure & complete \\ \hline \hline
  0 & \{1,2,4\} & \checkmark \\ \hline
  1 & \{3,4,6\} & \\ \hline
  2 & \{4,5,6\} & \\ \hline
\end{tabular}
\end{center}
\end{table}

\begin{equation}
\begin{split}
move(s'_1, \mathtt{a}) & = \epsilon\text{-closure}(\{t | s \in \{3,4,6\}\text{ and } s^{\mathtt{a}}t \in T\}) \\
& = \epsilon\text{-closure}(\{4\}) \\
& = \{4\} \\
& = s'_3
\end{split}
\end{equation}
\begin{equation}
\begin{split}
move(s'_1, \mathtt{b}) & = \epsilon\text{-closure}(\{t | s \in \{3,4,6\}\text{ and } s^{\mathtt{b}}t \in T\}) \\
& = \epsilon\text{-closure}(\{3,5\}) \\
& = \{3,4,5,6\} \\
& = s'_4
\end{split}
\end{equation}

\newpage
\begin{table}[h]
\begin{center}
\begin{tabular}{|l||r|c|} \hline
  state & epsilon-closure & complete \\ \hline \hline
  0 & \{1,2,4\} & \checkmark \\ \hline
  1 & \{3,4,6\} & \checkmark \\ \hline
  2 & \{4,5,6\} & \\ \hline
  3 & \{4\} & \\ \hline
  4 & \{3,4,5,6\} & \\ \hline
\end{tabular}
\end{center}
\end{table}

\begin{equation}
\begin{split}
move(s'_2, \mathtt{a}) & = \epsilon\text{-closure}(\{t | s \in \{4,5,6\}\text{ and } s^{\mathtt{a}}t \in T\}) \\
& = \epsilon\text{-closure}(\{4\}) \\
& = \{4\} \\
& = s'_3
\end{split}
\end{equation}
\begin{equation}
\begin{split}
move(s'_2, \mathtt{b}) & = \epsilon\text{-closure}(\{t | s \in \{4,5,6\}\text{ and } s^{\mathtt{b}}t \in T\}) \\
& = \epsilon\text{-closure}(\{5\}) \\
& = \{4,5,6\} \\
& = s'_2
\end{split}
\end{equation}


\begin{table}[h]
\begin{center}
\begin{tabular}{|l||r|c|} \hline
  state & epsilon-closure & complete \\ \hline \hline
  0 & \{1,2,4\} & \checkmark \\ \hline
  1 & \{3,4,6\} & \checkmark \\ \hline
  2 & \{4,5,6\} & \checkmark \\ \hline
  3 & \{4\} & \\ \hline
  4 & \{3,4,5,6\} & \\ \hline
\end{tabular}
\end{center}
\end{table}

\begin{equation}
\begin{split}
move(s'_3, \mathtt{a}) & = \epsilon\text{-closure}(\{t | s \in \{4\}\text{ and } s^{\mathtt{a}}t \in T\}) \\
& = \epsilon\text{-closure}(\{4\}) \\
& = \{4\} \\
& = s'_3
\end{split}
\end{equation}
\begin{equation}
\begin{split}
move(s'_3, \mathtt{b}) & = \epsilon\text{-closure}(\{t | s \in \{4\}\text{ and } s^{\mathtt{b}}t \in T\}) \\
& = \epsilon\text{-closure}(\{5\}) \\
& = \{4,5,6\} \\
& = s'_2
\end{split}
\end{equation}


\begin{table}[h]
\begin{center}
\begin{tabular}{|l||r|c|} \hline
  state & epsilon-closure & complete \\ \hline \hline
  0 & \{1,2,4\} & \checkmark \\ \hline
  1 & \{3,4,6\} & \checkmark \\ \hline
  2 & \{4,5,6\} & \checkmark \\ \hline
  3 & \{4\} & \checkmark \\ \hline
  4 & \{3,4,5,6\} & \\ \hline
\end{tabular}
\end{center}
\end{table}

\begin{equation}
\begin{split}
move(s'_4, \mathtt{a}) & = \epsilon\text{-closure}(\{t | s \in \{3,4,5,6\}\text{ and } s^{\mathtt{a}}t \in T\}) \\
& = \epsilon\text{-closure}(\{4\}) \\
& = \{4\} \\
& = s'_3
\end{split}
\end{equation}
\begin{equation}
\begin{split}
move(s'_4, \mathtt{b}) & = \epsilon\text{-closure}(\{t | s \in \{3,4,5,6\}\text{ and } s^{\mathtt{b}}t \in T\}) \\
& = \epsilon\text{-closure}(\{3,5\}) \\
& = \{3,4,5,6\} \\
& = s'_4
\end{split}
\end{equation}

\newpage
\begin{table}[h]
\begin{center}
\begin{tabular}{|l||r|c|} \hline
  state & epsilon-closure & complete \\ \hline \hline
  0 & \{1,2,4\} & \checkmark \\ \hline
  1 & \{3,4,6\} & \checkmark \\ \hline
  2 & \{4,5,6\} & \checkmark \\ \hline
  3 & \{4\} & \checkmark \\ \hline
  4 & \{3,4,5,6\} & \checkmark \\ \hline
\end{tabular}
\end{center}
\end{table}

\begin{tikzpicture}
  \node[state] (0) at (0,0) {$S_0$};
  \node[state, double] (1) at (3,0) {$S_1$};
  \node[state, double] (2) at (0,-3) {$S_2$};
  \node[state] (3) at (3,-3) {$S_3$};
  \node[state, double] (4) at (6,0) {$S_4$};

  \begin{scope}[rounded corners=2mm, -Triangle]
    \draw (0) -- (1) node [midway, above] {\texttt{a}};
    \draw (0) -- (2) node [midway, left] {\texttt{b}};
    \draw (1) -- (3) node [midway, left] {\texttt{a}};
    \draw (1) -- (4) node [midway, above] {\texttt{b}};
    \draw (2) to [out=315, in=225] (3) node at (1.5,-2) {\texttt{a}};
    \draw (2) to [out=270, in=180, loop] (2) node at (-1.2,-4.1) {\texttt{b}};
    \draw (3) to [out=0, in=270, loop] (3) node at (4.2,-4.1) {\texttt{a}};
    \draw (3) to [out=135, in=45] (2) node at (1.5,-4) {\texttt{a}};
    \draw (4) -- (3) node [midway, above] {\texttt{a}};
    \draw (4) to [out=0, in=270, loop] (4) node at (7.2,-1.1) {\texttt{b}};
  \end{scope}
\end{tikzpicture}

\end{document}