\chapter{Reinforcement Learning}\label{chapter:architecture}
In this chapter we explain the some concepts of Reinforcement Learning, a starting technique of RL and the three techniques that we used in this work. It is dived in 5 sections: explanation of the concepts and definitions of Reinforcement Learning, Q-Learning, Deep Q Network, Dueling Double Deep Q Networks and Deep Deterministic Policy Gradient.

\section{Concepts and Definitions}
Reinforcement Learning is a field of machine learning that studies a computational way to learn how to reach an objective taking actions over the time to maximize a reward function depending on environment's reaction of each action taken. To understand the RL techniques that we explain, we must learn some concepts and definitions.

An \textbf{agent} is the actor that will interact with the environment and understand if the action that it took was good or bad for the task. The \textbf{environment} is everything that the agent will interact, eg.: if we are drinking water the environment is the kitchen, the  bottle of water, how much water last in the bottle. The environment is formally announced as a a Markov Decision Process (MDP), \cite{mdp}. A MDP is a 4-tuple ($S, A, P_a, R_a$) where:
\begin{itemize}
    \item S is a finite set of state that describe the features of the environment. A state $s$ describes the environment in a certain time $t$.
    \item A is a finite set of actions that the agent can take at any time. An action $a$ describes an action taken in a state $s$.
    \item $P_a(S,S') = Pr(s_{t+1}=s' | s_t=s, a_t=a)$ is the probability of agent taking the action $a$ on state $s$ at time $t$ will lead to state $s'$.
    \item $R_a(S,S')$ is the reward for the agent taking an action $a$ on a state $s$ at time $t$ leading on a state $s'$.
\end{itemize}

\begin{figure}[H]
    \centering
    \includegraphics[scale=0.5]{images/rl.png}
    \caption{Example of model of Reinforcement Learning. Image from \cite{rlsutton}.}
    \label{fig:my_label}
\end{figure}

Some definitions of RL:

\begin{defn}A \textbf{Policy} $\pi$ is a rule to select actions. It can be deterministic, if the state maps the actions, or stochastic,  when the action choice depends on a probability distribution. The optimal policy is called $\pi^*$. \end{defn}

\begin{defn}An \textbf{Episode} is analogous to an epoch in RL. An Episode $\tau$ is a sequence of 3-tuple ($s, a, r$). \end{defn}

\begin{defn}A \textbf{Step} is the acting of the agent. Each episode have certain number of steps. In our domain, the agent can take approximately 300 steps until the current episode finishes.\end{defn}

\begin{defn}The \textbf{Experience Replay} buffer (or \textbf{Memory}) of an agent is analogous to a dataset in RL. It is the previous $N$ episodes passed, with $N$ being defined by the programmer.\end{defn}

\begin{defn}The \textbf{Value} of a state $V(s)$ describes the expected return after state $s$. It can be interpreted as how good $s$ can be, based on previous memories.\end{defn}

\begin{defn}The \textbf{Q-Value} $Q(s, a)$ describes how good is to take an action $a$ in a given state $s$. The optimal \textit{Q-Value} is called $Q^*$. The \textit{Q-Value} is formally described as:
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.35]{images/qvalue.png}
        \caption{Formal description of \textit{Q-Value}. Image from \cite{tgze}.}
        \label{fig:qvalue}
    \end{figure}
\end{defn}

\begin{defn}The \textbf{Bellman Equation} is the base of Reinforcement Learning. It can describe $V(s)$ and $Q(s,a)$ recursively based on the actual 4-tuple $(s, a, r, s')$ and t-tuple $(s, a, r, s', a')$ respectively. See Equations \ref{eq:value}, \ref{eq:qvalue} and \ref{eq:reward}.$\gamma$ is the discount rate which represents how much $Q(s', a')$ can impacts on $Q(s,a)$.

\begin{equation}
    V(s) = \max_a(R + \gamma V(s'))
    \label{eq:value}
\end{equation}

\begin{equation}
    Q(s,a) = \max_a(R + \gamma Q(s', a'))
    \label{eq:qvalue}
\end{equation}

\begin{equation}
    R = R(s, a, s')
    \label{eq:reward}
\end{equation}

 \end{defn}

\section{Q-Learning}\label{section:qlearning}
Q-Learning (\cite{qlearning}) is one of the basics algorithms of Reinforcement Learning. It works based on a matrix called \textit{Q-Table} where the columns represents the actions that the agent can take and the rows represents the set $S$ of states. The \textit{Q-Table} is updated for each step based on the Equation \ref{eq:qfunction} which is called \textit{Q-Function}, an adaption of the Bellman's Equation. See Figure \ref{fig:qtable}. The algorithm does the work when the environment has a small discretized state space, otherwise it would take a very long time to the matrix converge. Other problem of the technique is the keeping of a local maximum due to the lack of generalisation of states.
\begin{equation}
\underbrace{\text Q(s,a)}_{\scriptstyle\text{Q-Value}}=Q(s,a)+\mkern-34mu\underset{\text{Learning rate}}{\underset{\Bigl|}{\alpha}}\mkern-30mu[\underbrace{R(s,a)}_{\scriptstyle\text{Reward}}+\mkern-30mu\underset{\text{Discount rate}}{\underset{\Biggl|}{\gamma}}\mkern-75mu\overbrace{\max Q'(s',a')}^{\scriptstyle\substack{\text{Maximum predicted reward, given} \\ \text{new state and all possible actions}}}\mkern-45mu-Q(s,a)]
\label{eq:qfunction}
\end{equation}

\begin{figure}[H]
    \centering
    \includegraphics[scale=0.35]{images/Q-Learning_Matrix_Initialized_and_After_Training.png}
    \caption{Example of Q-Table. By LearnDataSci - Own work - Article, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=69947708, accessed on 15/09/2019.}
    \label{fig:qtable}
\end{figure}

\section{Deep Q Network}\label{section:dqn}
Deep Q Network (DQN) is the Deep Learning approach for Q-Learning created by \cite{dqn}. Although basic (comparing with today's techniques), it opened the eyes for Reinforcement Learning achieving impressive results with Atari's games. DQN solves the problems of Q-Learning using a non-linear function (Neural Networks) as a $Q*$ approximator. The loss function of the DQN changes every iteration $i$ and is derived from the Bellman's Equation. See Figure \ref{fig:dqnfuncloss}. $\theta$ represents the network's weights and $\rho(s,a)$ is the probability distribution over states and actions, also called behaviour distribution. DQN's algorithm is in the Figure \ref{fig:dqn_alg}.

\begin{figure}[H]
    \centering
    \includegraphics[scale=0.3]{images/dqn_lossfunc.png}
    \caption{$L_i(\theta_i)$ as loss function of DQN's Neural Networks.}
    \label{fig:dqnfuncloss}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[scale=0.5]{images/dqn_alg.png}
    \caption{Deep Q Networks algorithm with Experience Replay.}
    \label{fig:dqn_alg}
\end{figure}

\section{Double Deep Q Networks}\label{section:ddqn}
Deep Q Networks brings a maximization bias learning due to the $max_a' Q^*(s',a')$ part of the Q-Learning equation. In other words, it says that some Q-Values are more valuables than it should. And such overestimation can be problematic. \cite{doubledqn} brought the idea of Double Deep Q Networks to cure the overestimation problem based on the cure for the same problem in Q-Learning with Double Q-Learning, \cite{doubleqlearning}.

\cite{doubledqn} proposes a solution involving two $Q*$ estimators, one to update the other with certain rate of averaging $\tau$. Using it, the \textit{Q-Value} will be unbiased by the opposite estimator of the action selector. The algorithm proposed is in Figure \ref{fig:doubledqn_alg}. See Figure \ref{fig:doubledqnoverestim} for the results.

\begin{figure}[H]
    \centering
    \includegraphics[scale=0.8]{images/doubledqn_alg.png}
    \caption{Double DQN algorithm. Image from https://towardsdatascience.com/double-deep-q-networks-905dd8325412, accessed on 15/11/2019.}
    \label{fig:doubledqn_alg}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[scale=0.4]{images/double_dqn_estimation.png}
    \caption{Q-Value in fuction of time for some Atari games: estimation and truth values for DQN and Double DQN. Image from \cite{doubledqn}.}
    \label{fig:doubledqnoverestim}
\end{figure}

\section{Dueling Double Deep Q Networks}\label{section:ddqn}
Dueling Double Deep Q Networks (DDQN) brings the idea of Double DQN and a plus. \cite{DDQN} propose a dueling architecture for the final layers of the Neural Networks. It separates explicitly the state values and state-dependent action advantage via two different Neural Networks.

The motivation of the technique is that some environments do not depend fully of an action, sometimes if the agent does not do some action or does a single action, it takes more reward than taking multiple actions. It applies very well on our multi-agent context, for example if the right winger is intercepting the ball, it's more valuable to the left winger stays on it's position than intercepting.

The new \textit{Q-Function} will be naively described as Equation \ref{eq:ddqn}. Due to identifiability problem of sum, the back propagation can not tell what is $V(s) or A(s,a)$, so \cite{DDQN} propose the Equation \ref{eq:ddqn_best} to train the agent.

\begin{equation}
    Q(s,a;\theta, \alpha, \beta) = V(s; \theta, \beta) + A(s, a; \theta, \alpha)
\label{eq:ddqn}
\end{equation}

\begin{equation}
    Q(s,a;\theta, \alpha, \beta) = V(s; \theta, \beta) + (A(s, a; \theta, \alpha) - \frac{1}{A}\sum_{a'}A(s, a'; \theta, \alpha))
\label{eq:ddqn_best}
\end{equation}

\begin{figure}[H]
    \centering
    \includegraphics[scale=0.4]{images/dddqn.png}
    \caption{Deep Q Networks architecture against Dueling Deep Q Networks architecture. Image from \cite{DDQN}}
    \label{fig:ddqn}
\end{figure}

\section{Deep Deterministic Policy Gradient}\label{section:ddpg}
There is another branch of RL that studies the Policies and Values themselves. It is like the idea of a DDQN, where there is a Critic and an Actor algorithm, see Figure \ref{fig:acex}. Based on that architecture, \cite{DDPG} develop the Deep Deterministic Policy Gradient (DDPG). The DDPG tries to learn an optimal deterministic policy $\mu^*$.

\begin{figure}[H]
    \centering
    \includegraphics[scale=0.5]{images/AC_example.png}
    \caption{Illustration of an Actor Critic system. Image from \cite{rlsutton}.}
    \label{fig:acex}
\end{figure}

The input of the Actor is the current state and the output is a single real value representing an action chosen from a continuous action space. The output of the Critic is $V(s)$ where $s$ is the current state. See Figure \ref{fig:ddpgalg} to check DDPG's algorithm.

\begin{figure}[H]
    \centering
    \includegraphics[scale=0.3]{images/ddpgalg.png}
    \caption{Deep Deterministic Policy Gradient algorithm. Image from \cite{DDPG}.}
    \label{fig:ddpgalg}
\end{figure}

Evaluating Reinforcement Learning on Robocup Soccer Simulation 2D/chapters/architecture.tex


%              %
%%            %%
%%% PREAMBLE %%%
%%            %%
%              %

% Document class
\documentclass[11pt, a4paper]{report}

% Font
\usepackage{fontspec}
\setmainfont[%
	SmallCapsFont={* Caps},%	enable small capital font family
	SlantedFont={* Slanted},%	enable slanted font family
]{Latin Modern Roman}

% Language and typography
\usepackage{polyglossia}
\setdefaultlanguage[variant=british]{english}
\setotherlanguage{french}

\usepackage[autostyle=true]{csquotes}

\usepackage{microtype}

% Mathematics
\usepackage{mathtools}

% Hyperref
\usepackage[hidelinks]{hyperref}
\usepackage{cleveref}

%              %
%%            %%
%%% DOCUMENT %%%
%%            %%
%              %

% Information
\title{Mathematics}
\author{Alexandre Quenon}

% Text
\begin{document}
% *** Title page *** %
\maketitle

\tableofcontents

\chapter{The \enquote{mathtools} package}

	Mathematics writing is one of the most advantage of \LaTeX{} compared to common text editors.
	On a first approach, it looks like a programming language but it is in fact quite intuitive.

	The first package which was extremely useful in mathematics writing was \texttt{amsmath}.
	Since then, it has been upgraded by the \texttt{mathtools} package that I recommend to use.

	\section{Writing an equation}

		Writing an equation is simply done thanks to the \texttt{equation} environment.
		Maxwell's equations will be used as examples:
		\begin{equation}
			\vec{\nabla}\cdot\vec{B}=0.
			\label{eq::Maxwell:no_magnetic_monopole}
		\end{equation}

		Automatic numbering can be avoided by using the starred version: \texttt{equation*}.
		For instance:
		\begin{equation*}
			\vec{\nabla}\times\vec{E}=-\frac{\partial\vec{B}}{\partial t}.
		\end{equation*}

		Shorter forms of the unnumbered version are offered by the package: the \textbackslash [ \ldots \textbackslash ] wrapper:
		\[ \oint_{C} \vec{E} \cdot \mathrm{d}\vec{l} = - \iint_{S} \frac{\partial\vec{B}}{\partial t} \cdot \mathrm{d}\vec{S}. \]

		Inline equations are equations written in the text.
		For instance, I could specify that $\vec{B}$ in \cref{eq::Maxwell:no_magnetic_monopole} is the magnetic field.

	\section{Grouping equations}

		Two main environments can be used to group equations : \texttt{gather} and \texttt{align}.
		The former groups without aligning, the latter groups and aligns equations.

		Example based on \texttt{gather}:
		\begin{gather}
			\vec{\nabla} \times \vec{B}
				= \mu_0 \vec{\jmath}
				+ \varepsilon_0 \mu_0 \frac{\partial \vec{E}}{\partial t}, \\
			\intertext{which can be written in the integral form by applying the Green theorem}
			\oint_{C} \vec{B} \cdot \mathrm{d} \vec{l}
				= \mu_0 \iint_S \vec{\jmath} \cdot \mathrm{d} \vec{S}
				+ \varepsilon_0 \mu_0 \iint_S \frac{\partial \vec{E}}{\partial t} \cdot \mathrm{d} \vec{S}.
		\end{gather}

		Example based on \texttt{align}:
		\begin{align}
			\vec{B} &= \vec{\nabla} \times \vec{A}, \\
			\vec{E} &= -\vec{\nabla} V - \frac{\partial \vec{A}}{\partial t}.
		\end{align}

	\section{Matrices}

		Matrices can be generated thanks to the \texttt{matrix} environment which must be used inside a mathematical equation environment.
		There are several variants of \texttt{matrix} which produce different delimiters surrounding the matrix.

		The \enquote{mathtools} package offers starred version of the \texttt{matrix} environments which allow to pass an optional argument to specify the alignment inside the matrix's columns.

		Example for each type of \texttt{matrix} environments:
		\begin{align*}
			\text{\texttt{matrix}} && \text{\texttt{pmatrix}} && \text{\texttt{bmatrix}} && \text{\texttt{Bmatrix}} \\
			%
			\begin{matrix}
				x_{11} & x_{12} \\
				x_{21} & x_{22} \\
			\end{matrix}
			&&
			\begin{pmatrix}
				x_{11} & x_{12} \\
				x_{21} & x_{22} \\
			\end{pmatrix}
			&&
			\begin{bmatrix}
				x_{11} & x_{12} \\
				x_{21} & x_{22} \\
			\end{bmatrix}
			&&
			\begin{Bmatrix}
				x_{11} & x_{12} \\
				x_{21} & x_{22} \\
			\end{Bmatrix} \\
			%
			%
			&& \text{\texttt{vmatrix}} && \text{\texttt{Vmatrix}} && \\
			%
			&&
			\begin{vmatrix}
				x_{11} & x_{12} \\
				x_{21} & x_{22} \\
			\end{vmatrix}
			&&
			\begin{Vmatrix}
				x_{11} & x_{12} \\
				x_{21} & x_{22} \\
			\end{Vmatrix}
			&&
		\end{align*}

		Also compare
		\begin{align*}
			\begin{pmatrix}
				2  & -3 \\
				42 & 0
			\end{pmatrix}
			&&
			\text{VS}
			&&
			\begin{pmatrix*}[r]
				2  & -3 \\
				42 & 0
			\end{pmatrix*}
		\end{align*}
%
\end{document}


\documentclass[12pt]{article}
\usepackage[lmargin=1in,rmargin=1in,tmargin=1in,bmargin=1in]{geometry}

\usepackage{aryaman}

% \newtheorem*{alg}{Algorithm}
\newtheorem*{fac}{Fact}
% \newtheorem*{ex}{Example}
\DeclareMathOperator{\len}{len}

\title{The Miller Rabin Test\\\small CS 719 Course Report}
\author{Aryaman Maithani}
\date{\today}

\begin{document}
\maketitle
\tableofcontents

\section{Introduction}
    In this report, we are concerned with finding an algorithm for the following problem.

    \begin{tcolorbox}
        {\color{blue}Input:} An integer $n > 1$.

        {\color{blue}Output:} $\operatorname{isPrime}(n)$.
    \end{tcolorbox}

    The simplest way to do this is by trial division. Indeed, we simply divide $n$ by $2, 3, 4$, and so on, and see if the remainder is $0$ in any case. As we know, we only need to divide by numbers up to $\sqrt{n}$. The issue with this algorithm is that it is extremely inefficient, requiring $\Theta(\sqrt{n})$ operations, which is \emph{exponential} in the \emph{bit length} $\len(n)$. For example, if $n$ has $100$-decimal digits, it would take more than $10^{33}$ years to perform $\sqrt{n}$ divisions.

    However, note that the above algorithm does \emph{more} than what we expected from our algorithm. Namely, it not only tells us that the number is prime but also produces a nontrivial factor in the case that $n$ is composite. Na\"{i}vely, one might think that it is necessary for us to produce a prime factor to claim that a number is composite. However, that is not the case.

    In this report, we describe a much faster primality testing. This is a polynomial time algorithm. It allows for $100$-decimal digits numbers to be tested in less than a second. Unlike the earlier algorithm, it does \emph{not} give us a prime factor in the case that $n$ is composite.

    However, this algorithm is \emph{probabilistic}. This means that the algorithm can make a mistake. Fortunately, one has control over this probability, and can make it arbitrarily small (but not zero).

    For the rest of the talk, we shall assume that $n > 1$ is an \emph{odd} integer.
    Let $n = p_{1}^{e_{1}} \cdots p_{r}^{e_{r}}$ be its prime factorisation. \\
    By $\mathbb{Z}_{n}$, we shall denote the ring of integers modulo $n$. We have a ring homomorphism
    \begin{align*}
        \theta : \mathbb{Z}_{n} &\to \mathbb{Z}_{p_{1}^{e_1}} \times \cdots \times \mathbb{Z}_{p_{r}^{e_{r}}} \\
        [a]_{n} &\mapsto ([a]_{p_{1}^{e_1}}, \cdots, [a]_{p_{r}^{e_{r}}}).
    \end{align*}

    In fact, the Chinese Remainder Theorems tells us that the above is an isomorphism. This gives us a group isomorphism between the group of invertible elements of the two rings as
    \begin{equation} \label{eq:CRT-groups}
        (\mathbb{Z}_{n})^{\ast} \xrightarrow{\cong} (\mathbb{Z}_{p_{1}^{e_1}})^{\ast} \times \cdots \times (\mathbb{Z}_{p_{r}^{e_{r}}})^{\ast}.
    \end{equation}

    Several probabilistic primality tests, including the Miller–Rabin test, have the following general structure. \\

    \begin{blockquote}
        Define $\mathbb{Z}_{n}^{+}$ to be the set of nonzero elements of $\mathbb{Z}_{n}$. Note that $\md{\mathbb{Z}_{n}^{+}} = n - 1$. Moreover, $\mathbb{Z}_{n}^{+} = \mathbb{Z}_{n}^{\ast}$ iff $n$ is prime. Suppose also that we define a set $L_{n} \subset \mathbb{Z}_{n}^{+}$ such that:
        \begin{enumerate}
            \item there is an efficient algorithm that on input $n$ and $\alpha \in \mathbb{Z}_{n}^{+}$, determines if $\alpha \in L_{n}$;
            \item if $n$ is prime, then $L_{n} = \mathbb{Z}_{n}^{\ast}$; and
            \item if $n$ is composite, $\md{L_{n}} \le c(n - 1)$ for some universal constant $c < 1$.
        \end{enumerate}
    \end{blockquote}

    \begin{algo}
        To test for primality, we set a ``repetition parameter'' $k$, and choose random elements $\alpha_{1}, \ldots, \alpha_{k} \in \mathbb{Z}_{n}^{+}$. If $\alpha_{i} \in L_{n}$ for all $i \in \{1, \ldots, k\}$, then we output \texttt{true}; otherwise, we output \texttt{false}.
    \end{algo}

    \begin{obs}
        Let us note some properties of the above algorithm.
        \begin{enumerate}
            \item The algorithm is efficient since we can check $\alpha \in L_{n}$ efficiently.
            \item If $n$ is prime,  then the algorithm outputs \texttt{true},  and it does so \emph{correctly}.
            \item If $n$ is composite,  then the algorithm \emph{may} output \texttt{true},  with probability at most $c^{k}$.
        \end{enumerate}
    \end{obs}

     In particular, note that there is a \emph{one-sided error}.  In fancy language, this is a \emph{Monte Carlo algorithm}.

\section{First attempt}

    We now try to define a suitable candidate for $L_{n}$.
    \begin{defn}
        \begin{equation} \label{eq:green}
            L_{n} \vcentcolon= \{\alpha \in \mathbb{Z}_{n}^{+} : \alpha^{n - 1} = 1\}.
        \end{equation}
    \end{defn}

    Note that we can test $\alpha \in L_{n}$ efficiently, using a repeated-squaring algorithm.

    \begin{obs}
        It is easy to see that $L_{n} \subset \mathbb{Z}_{n}^{\ast}$. Indeed, $\alpha^{n - 2}$ acts as the inverse of $\alpha \in L_{n}$.

        However, one can even note that $L_{n}$ is a \emph{subgroup} of $\mathbb{Z}_{n}^{\ast}$. Indeed, defining $\varphi : \mathbb{Z}_{n}^{\ast} \to \mathbb{Z}_{n}^{\ast}$ to be the $(n - 1)$-power map $x \mapsto x^{n - 1}$, one sees that $L_{n} = \ker(\varphi)$.
    \end{obs}

    \begin{thm}
        If $n$ is prime, then $L_{n} = \mathbb{Z}_{n}^{\ast}$. \\
        If $n$ is composite \emph{and} $L_{n} \subsetneq \mathbb{Z}_{n}^{\ast}$, \emph{then} $\md{L_{n}} \le \frac{1}{2}(n - 1)$.
    \end{thm}

    \begin{proof}
        The first statement is clear. For the second, one recalls that $L_{n}$ is a subgroup of $Z_{n}^{\ast}$. Thus, $\frac{\md{\mathbb{Z}_{n}^{\ast}}}{\md{L_{n}}}$ is a positive integer. Therefore, if the integer is not $1$, it is at least $2$. Hence, we see
        \begin{equation*}
            \md{L_{n}} \le \frac{1}{2}\md{\mathbb{Z}_{n}^{\ast}} \le \frac{1}{2}(n - 1). \qedhere
        \end{equation*}
    \end{proof}
     However, there \emph{are} infinitely many odd composite $n$ for which $L_{n} = \mathbb{Z}_{n}^{\ast}$  and thus, they cannot be ignored.

    \begin{defn}
        An odd composite number $n$ such that $L_{n} = \mathbb{Z}_{n}^{\ast}$ is called a \emph{Carmichael number}.
    \end{defn}

    \begin{ex}
        The smallest Carmichael number is $561 = 3 \cdot 11 \cdot 17.$
    \end{ex}

    \begin{thm}
        $n$ is a Carmichael number iff $n$ is of the following form:
        \begin{enumerate}
            \item $n = p_{1} \cdots p_{r}$ for distinct primes $p_{i}$,
            \item $r \ge 3$,
            \item $(p_{i} - 1) \mid (n - 1)$ for all $i \in \{1, \ldots, r\}$.
        \end{enumerate}
    \end{thm}
    \begin{proof}
        Let $n = p_{1}^{e_{1}} \cdots p_{r}^{e_{r}}$ be a Carmichael number. Recalling \Cref{eq:CRT-groups}, we have
        \begin{equation*}
            \mathbb{Z}_{n}^{\ast} \cong \mathbb{Z}_{p_{1}^{e_1}}^{\ast} \times \cdots \times \mathbb{Z}_{p_{r}^{e_{r}}}^{\ast}.
        \end{equation*}
        Since $n - 1$ annihilates the left group, it annihilates the right group. But this happens iff
        \begin{equation*}
            p_{i}^{e_{i} - 1}(p_{i} - 1) \mid (n - 1)
        \end{equation*}
        for all $i \in \{1, \ldots, r\}$ (since each factor on the right is a cycle group). In particular, $(p_{1} - 1) \mid (n - 1)$. Moreover, if $e_{i} > 1$ for some $i$, then $p_{i} \mid n - 1$, a contradiction. Thus, $e_{i} = 1$ for all $i$.

        Now, we must show that $r \ge 3$. For the sake of contradiction, assume that $r = 2$. In this case, we have $n = p_{1} p_{2}$ for some $p_{1} > p_{2}$. We note that
        \begin{equation*}
            n - 1 = p_{1} p_{2} - 1 = (p_{1} - 1) p_{2} + (p_{2} - 1).
        \end{equation*}
        The above shows that $p_{1} - 1 \mid p_{2} - 1$, a contradiction since $p_{1} > p_{2}$.

        Conversely, suppose $n$ has the given form. Let $a$ be coprime to $n$ and hence, to each $p_{i}$. Then, by Fermat's Little Theorem, we have $a^{p_{i} - 1} \equiv 1 \mod p_{i}$. Since $n - 1$ is a multiple of $p_{i} - 1$, we get
        \begin{equation*}
            a^{n - 1} \equiv 1 \mod p_{i}
        \end{equation*}
        for all $i \in \{1, \ldots, r\}$. By the Chinese Remainder Theorem, we are now done.
    \end{proof}

\section{The Miller-Rabin test}

    We now define a new set $L_{n}'$ as follows.
    \begin{defn}
        Let $n - 1 = t 2^{h}$  where $t$ is odd, and $h \ge 1$.
        \begin{align}
            L_{n}' \vcentcolon= \{\alpha \in \mathbb{Z}_{n}^{+} :\ &\alpha^{t2^{h}} = 1 \text{ and} \nonumber \\
            & \alpha^{t2^{j + 1}} = 1 \Rightarrow \alpha^{t2^{j}} = \pm 1 \label{eq:sepia}\\
            & \qquad \text{ for } j = 0, \ldots, h - 1\}. \nonumber
        \end{align}
    \end{defn}

    The Miller-Rabin test uses this set $L_{n}'$.  By definition, it is clear that $L_{n}' \subset L_{n}$, since we have the condition \Cref{eq:green} from earlier.  \\
    In fact, $L_{n}'$ is precisely the set of those elements of $L_{n}$ which also satisfy \Cref{eq:sepia}.

    Testing whether a given $\alpha \in \mathbb{Z}_{n}^{+}$ belongs to $L_{n}'$ can be done using the following algorithm:
    \begin{algo}[Testing membership] \phantom{hi}
        \begin{enumerate}
            \itemsep1mm
            \item $\beta \leftarrow \alpha^{t}$
            \item if $\beta = 1$ then return \texttt{true}
            \item for $j \leftarrow 0$ to $h - 1$ do
            \begin{itemize}
                \item if $\beta = -1$ then return \texttt{false}
                \item if $\beta = 1$ then return \texttt{false}
                \item $\beta \leftarrow \beta^{2}$
            \end{itemize}
            \item return \texttt{false}
        \end{enumerate}
    \end{algo}
     This algorithm runs in time $O(\len(n)^{3})$ and thus, satisfies the first criteria.

    \begin{thm}
        If $n$ is prime, then $L_{n}' = \mathbb{Z}_{n}^{\ast}$.  If $n$ is composite, then $\md{L_{n}'} \le \frac{1}{4}(n - 1)$.
    \end{thm}

    \begin{proof}  %Write $n - 1 = t2^{h}$ with $t$ odd.
        \textbf{Case 1.} $n$ is prime.

        Note that we have $L_{n}' \subset L_{n} = \mathbb{Z}_{n}^{\ast}$.  Thus, it suffices to prove that $L_{n} \subset L_{n}'$.  But this follows because $x^{2} = 1 \Rightarrow x = \pm 1$ in a field.

        \textbf{Case 2.} $n = p^{e}$ for a prime $p \ge 3$ and $e \ge 2$.

        Recall that $L_{n}$ is the kernel of the $(n - 1)$-power map.  Since $\mathbb{Z}_{n}^{\ast}$ is cyclic, it follows that $\md{L_{n}} = \gcd(\varphi(n), n - 1)$.  We can explicitly calculate it to get

        \begin{equation*}
            \md{L_{n}'} \le \md{L_{n}} = \gcd(p^{e - 1}(p - 1), p ^{e} - 1) = p - 1 = \frac{p^{e} - 1}{p^{e - 1} + \cdots + 1} \le \frac{n - 1}{4}.
        \end{equation*}

        \textbf{Case 3.} $n = p_{1}^{e_{1}} \cdots p_{r}^{e_{r}}$ is the standard prime factorisation of $n$, with $r > 1$. \\~\\
        %
        Let $\theta : \mathbb{Z}_{n} \to \mathbb{Z}_{p_{1}^{e_1}} \times \cdots \times \mathbb{Z}_{p_{r}^{e_{r}}}$ be the ring isomorphism from earlier.  \\
        Write $n - 1 = t2^{h}$ and $\varphi(p_{i}^{e_{i}}) = t_{i}2^{h_{i}}$ in the usual way,  and let $g \vcentcolon= \min\{h, h_{1}, \ldots, h_{r}\}$.  Note that $g \ge 1$, and that each $\mathbb{Z}_{p_{i}^{e_{i}}}^{\ast}$ is a cyclic group of order $t_{i}2^{h_{i}}$.  \\~\\
        %
        We first show that $\alpha^{t2^{g}} = 1$.  By definition of $L_{n}'$, we may assume $g < h$.  Now, suppose $\alpha^{t2^{g}} \neq 1$,  and let $j$ be the smallest index in $g, \ldots, h - 1$ such that $\alpha^{t2^{j + 1}} = 1$.  By definition of $L_{n}'$, we have $\alpha^{t2^{j}} = -1$.  Let $i$ be such that $g = h_{i}$.  Writing $\theta(\alpha) = (\alpha_{1}, \ldots, \alpha_{r})$,  we have $\alpha_{i}^{t2^{j}} = -1$.  Thus, the order of $\alpha_{i}^{t}$ (in $\mathbb{Z}_{p_{i}^{e_{i}}}^{\ast}$) is equal to $2^{j + 1}$.  But this is a contradiction since it does not divide $\md{\mathbb{Z}_{p_{i}^{e_{i}}}^{\ast}} = t_{i}2^{h_{i}}$.  \hfill ($\because j \ge g = h_{i}$)

        For $j = 0, \ldots, h$, define $\rho_{j}$ to be the $(t2^{j})$-power map on $\mathbb{Z}_{n}^{\ast}$.  From the previous claim, and the definition of $L_{n}'$, it follows that $\alpha^{t2^{g - 1}} = \pm 1$ $\forall \alpha \in L_{n}'$.  Thus, $L_{n}' \subset \rho_{g - 1}^{-1}(\{\pm 1\})$ and hence,
        \begin{equation} \label{eq:01}
            \md{L_{n}}' \le 2\md{\ker(\rho_{g - 1})}.
        \end{equation}
        Also,
        \begin{flalign*}
            && \md{\ker(\rho_{j})} = \prod_{i = 1}^{r} \gcd(t_{i}2^{h_{i}}, t2^{j}) && \forall j \in \{0, \ldots, h\}.
        \end{flalign*}
         Since $g \le h$ and $g \le h_{i}$ for all $i$, we get
        \begin{equation} \label{eq:02}
            2^{r}\md{\ker(\rho_{g - 1})} = \md{\ker(\rho_{g})} \le \md{\ker(\rho_{h})}.
        \end{equation}
        Combining \Cref{eq:01}-\Cref{eq:02}, we get
        \begin{equation*}
            \md{L_{n}'} \le 2^{-r + 1}\md{\ker(\rho_{h})} = \frac{\md{L_{n}}}{2^{r - 1}}.
        \end{equation*}

        If $r \ge 3$, then we are done  since $\md{L_{n}} \le \md{Z_{n}^{\ast}} \le n - 1$, and $2^{r - 1} \ge 4$.

        If $r = 2$, then $n$ is not a Carmichael number and thus,
        \begin{equation*}
            \frac{\md{L_{n}}}{2^{r - 1}} = \frac{\md{L_{n}}}{2} \le \frac{1}{4}(n - 1),
        \end{equation*}
        and we are again done.
    \end{proof}

\end{document}


         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         % Information sheet for the Matlab lab - Maths 6111 %
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[10pt]{article}
\input ma_no_html_header

\usepackage{color}
\usepackage{hyperref}
\hypersetup{breaklinks=true,colorlinks=true}
%\input ma_header
\setlength{\parindent}{0pt}
\pagestyle{myheadings}
% \markright{
% \protect {\protect \epsfxsize=0.2 true cm \protect \epsffile {dolph.line.eps}}
% \it Maths 3018/6111 - Numerical methods \hfill}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\thispagestyle{empty}
\begin{center}
\textbf{\Large Maths 3018/6111 - Numerical Methods \\*[8mm]
Worksheet 1}\\*[.8cm]
\end{center}

\section*{Theory}

\begin{enumerate}
\item Write down the $1$, $2$ and $\infty$ vector norms of
  \begin{equation*}
    {\bf v}_1 =
    \begin{pmatrix}
      1 \\ 3 \\ -1
    \end{pmatrix}, \quad
    {\bf v}_2 =
    \begin{pmatrix}
      1 \\ -2
    \end{pmatrix}, \quad
    {\bf v}_3 =
    \begin{pmatrix}
      1 \\ 6 \\ -3 \\ 1
    \end{pmatrix}.
  \end{equation*}
\item Find the $1$ and $\infty$ matrix norms of
  \begin{equation*}
    A_1 =
    \begin{pmatrix}
      1 & 2 \\ 3 & 4
    \end{pmatrix}, \quad
    A_2 =
    \begin{pmatrix}
      -3 & 2 \\ 3 & 6
    \end{pmatrix}
  \end{equation*}
\item Find the condition numbers of the above matrices. What does this
  suggest about the numerical behaviour of an algorithm that used such
  a matrix?
\item Explain the difference between direct and indirect methods for
  solving linear systems. Give an example of when the latter may be
  more useful.
\end{enumerate}

\section*{Coding}

\begin{enumerate}
\item Enter each of the matrices above into Matlab. For each matrix,
  work out the transpose and inverse using in-built Matlab commands.
\item Check your calculation of the vector and matrix norms, and the
  condition numbers.
\item Write a function that takes a matrix, computes its condition
  number, and reports whether the matrix is suitably well-conditioned
  (the choice of criteria is up to you).
\item{} [3018 only] Write a bisection method to find the root of
  \begin{equation*}
    f(x) = \tan x - e^{-x}, \quad x \in [0,1].
  \end{equation*}
\end{enumerate}

\end{document}


\section{Learning with L* }
\label{section:L}

The \textit{L*} algorithm, described by Dana Angluin in \cite{LPaper}, is a learning algorithm whose goal is to build a \textit{mDFA} recognizing an unknown language $\U$ thanks to membership and equivalence queries. This operation can be done through some important bricks that are partially reused by the \textit{NL*} algorithm.

\subsection{The observation table}
In the previous section, we have talked about how the \textit{Learner} and the \textit{Teacher} interact, next we show how the \textit{Learner} stores the received answers in its memory, called \textit{observation table (O.T).}

$T: \omega \rightarrow \{0, 1\}$ is a mapping allowing to know if the \textit{Teacher} has accepted the word $\omega$.

\begin{definition}[Observation Table]
  The observation table is a matrix of booleans whose rows are indexed by the elements of a non-empty set of words called $S$ and whose columns are indexed by the elements of another non-empty set of words called $E$. And where the content of cell $c_{i,j}$ is $T(S[i] \cdot E[j])$\footnote{$S[i]$ (resp. $E[j]$) indicates the $i^{th}$ (resp $j^{th}$) element of $S$ (resp. $E$). Even if sets are meant not  to have the notion of order, we consider for simplicity that the $i^{th}$ element of the set is the $i^{th}$ element added in it}.
\end{definition}

$S$ and $E$ are respectively \textit{prefix-closed} and \textit{suffix-closed}.

\begin{definition}[Prefix (resp. Suffix)]
  A prefix (resp. suffix) of a word $\omega$, noted $Pref(\omega)$ (resp. $Suff(\omega)$), is a word $\omega_1 \in \Sigma^*$ such that $\exists \omega_2 \in \Sigma^*$ where $\omega_1 \cdot \omega_2 = \omega$ (resp. $\omega_2 \cdot \omega_1 = \omega$).
\end{definition}

\begin{definition}[Prefix-closedness and Suffix-closedness]
  A non-empty set $X$ of words is \textit{prefix-closed} if
  \[\forall \omega \in X, \forall Pref(\omega), Pref(\omega) \in X\]
  and suffix-closed if
  \[\forall \omega \in X, \forall Suff(\omega), Suff(\omega) \in X\]
  In both cases, $X$ should contain $\E$.
\end{definition}

The \OT must be completed by all words of the form $\omega \cdot \alpha$ for $\forall \omega \in S$ and $\forall \alpha \in \Sigma$ if $\omega \cdot \alpha \notin S$. This set of new words is added to the rows of the \OT and stored in a set called $SA$.
\[SA = \{\forall s \in S, \forall c \in \Sigma: s \cdot c \mid s \cdot c \notin S \} \]
Let $S_{ext}$ to be $S_{ext} = S \cup SA$.

\begin{definition}[Completeness]
  The \OT is complete if $\forall s \in S, \forall c \in \Sigma, \exists s' \in \Sigma \mid s \cdot c = s'$
\end{definition}

\begin{example}
  If $S = \{\E, a, aa\}$ and $\Sigma = \{a, b\}$ then
  \begin{align*}
    SA      & = \left\{ b, ab, aaa, aab \right\}\footnotemark{}
    \\
    S_{ext} & = \left\{\E, a, aa, b, ab, aaa, aab\right\}
  \end{align*}
\end{example}

\footnotetext{$b$ is in $SA$ since it is the concatenation of $\E \in S$ and $b \in \Sigma$}

\begin{notation}[The row function]
  For every $\omega$ in $S_{ext}$, $row(\omega)$ is the bit-vector made by the concatenation of all $T(\omega \cdot \omega')$ for $\omega' \in E$.
\end{notation}

\begin{remark}
  Adding a word $\omega$ in $S_{ext}$ means that a membership query is sent for every word in $\{\forall e \in E \mid \omega \cdot e\}$. Similarly, adding a word $\omega'$ means that a membership query is sent for all words in $\{\forall s \in S_{ext} \mid s \cdot \omega'\}$.
\end{remark}

\subsection{Closedness and Consistency in L*}

In both algorithms, the \OT should respect the closedness and the consistency properties. Notice that their definitions vary for the two algorithms. In this section, we define these two properties for the L* algorithm.

\begin{definition}[Closedness]
  The \OT is closed if
  \[\forall \omega \in SA, \exists \omega' \in S: row(\omega) = row(\omega')\]
\end{definition}

\begin{definition}[Row Similarity] Two rows $r_1, r_2$ are similar if they have the same corresponding bit-vector.
\end{definition}

\begin{definition}[Row Promotion]
  \label{def:rowProm}
  Given a word $\omega \in SA$, we say that $row(\omega)$ is promoted when $\omega$ is moved from $SA$ to $S$. After a row promotion, the \OT must be completed again.
\end{definition}

\begin{lemma}
  \label{lemmaPromoteLinePrefixClosedness}
  If we move an element from $SA$ to $S$ then $S$ remains \textit{prefix-closed}.
\end{lemma}

\begin{proof}
  Every element $\omega$ of $SA$ is the concatenation of an element $\omega' \in S$ and a letter $\alpha \in \Sigma$. We have that
  \[Pref(\omega) = Pref(\omega' \cdot \alpha) = \{\omega' \cdot \alpha\} \cup Pref(\omega')\]
  By definition, $S$ is prefix-closed, so if $\omega \in S$, then every prefix of $\omega'$ is already in $S$. Therefore adding $\omega$ in $S$ will not impact the prefix-closedness of $S$.
\end{proof}

If the table is not closed then $\exists \omega \in SA$ such that $\forall \omega' \in S \mid row(\omega) \neq row(\omega')$. To make the \OT closed we promote $row(\omega)$ and then, as said in \cref{def:rowProm}, we complete again the \OT

\begin{definition}[Consistency]
  The \OT is consistent if
  \[\forall \omega_1,\omega_2 \in S, \forall \alpha \in \Sigma:  row(\omega_1) = row(\omega_2) \rightarrow row(\omega_1 \cdot \alpha) = row(\omega_2 \cdot \alpha)\]
\end{definition}

If the table is not consistent, then there exists $row(\omega_1) = row(\omega_2)$ and $row(\omega_1 \cdot \alpha) \neq row(\omega_2 \cdot \alpha)$ for some $\alpha \in \Sigma \text{ and some }\omega_1, \omega_2 \in S$. We can restore the consistency property by making $\omega_1$ and $\omega_2$ no more similar. To do this, we have to add a new column in the \OT such that the new bit $b_1$ appended to $\omega_1$ is different to the bit $n_2$ appended to $\omega_2$. We look, therefore, for an $\alpha \in \Sigma \text{ and an } e \in E$ such that $T(\omega_1 \cdot \alpha \cdot e) \neq T(\omega_2 \cdot \alpha \cdot e)$ and we add $\alpha \cdot e$ in $E$. In this way we are sure that $b_1 = T(\omega_1 \cdot \alpha \cdot e)$ will be different from $b_2 = T(\omega_2 \cdot \alpha \cdot e)$.

We can now see the utility of the set $SA$. When we look for $T(\omega \cdot \alpha \cdot e)$ where $\omega \in S$, we are guaranteed that $\omega' = \omega \cdot \alpha$ exists in $S_{ext}$ since the table is complete. Therefore, $T(\omega \cdot \alpha \cdot e) = T(\omega' \cdot e)$ exists.

\begin{lemma}
  If we add a word $\omega = \alpha \cdot e$ where $\alpha \in \Sigma \text{ and } e \in E$ then $E$ remains \textit{suffix-closed}.
\end{lemma}

The proof is analogous to the one of \cref{lemmaPromoteLinePrefixClosedness}.

\subsection{L*: Automaton creation}

When the observation table is closed and consistent, the Learner can send to Teacher a conjecture of the language learnt so far.

The conjecture is an automaton where:
\begin{itemize}
  \item $Q = \{row(\omega), \forall \omega \in S\}$;
  \item $\delta(row(\omega), \alpha) = row(\omega \cdot \alpha, \forall \omega \in S, \alpha \in \Sigma\footnote{Again we know that $\omega' = \omega\cdot\alpha$ exists in $S_{ext}$ since the table is complete })$;
  \item $q_I = row(\E)$;
  \item $F = \{row(\omega) \mid \omega \in S \text{ and } T(\omega) = 1 \}$.
\end{itemize}

After sending the conjecture, if the Teacher answers \textit{Yes} the algorithm stops, otherwise the counter-example $\omega$ and all of its prefixes are added to $S$. The \OT is completed and the algorithm restart.

The following lemmas are a reformulation of those given in \cite{LPaper}.

\begin{lemma}
  \label{lemma:L_trans_from_QI}
  Let $q_I$ be the initial state and $\omega$ a word in $S_{ext}$ then $\delta(q_I, \omega) = row(\omega)$.
\end{lemma}

\begin{proof}
  We prove this by induction.
  Take a word $\omega \in S_{ext}$ and let $q_I$ be the initial state.
  If $length(\omega) = 0$, then $\delta(q_I, \omega) = \delta(q_I, \E) = \delta(row(\E), \E) =  row(\E \cdot \E) = row(\E) = q_I$ that is true by definition.
  Suppose this property is true for every word $\omega_n \in S_{ext}$ of length $n$, we want to prove that $\delta(q_I, \omega) = row(\omega)$ for a word $\omega \in S_{ext}$ of length $n+1$.
  Let $\omega$ be a word of length $n+1$ and let $\omega = \omega_n \cdot \alpha$ where $\alpha \in \Sigma$ and $\omega_n$ is the prefix of $\omega$ with length $n$. Since $S_{ext}$ is prefix closed, then $\omega_n$ should exists.
  We have $\delta(q_I, \omega) = \delta(q_I, \omega_n \cdot \alpha) = \delta(\delta(q_I, \omega_n), \alpha)$. By inductive hypothesis, $\delta(q_I, \omega_n)$ exists since $length(\omega_n) = n$ and is equal to $row(\omega_n)$, therefore $\delta(\delta(q_I, \omega_n), \alpha) = \delta(row(\omega_n), \alpha)$.
  The \OT is closed so $\exists s \in S$ such that $row(\omega_n) = row(s)$ and so $\delta(row(\omega_n), \alpha) = \delta(row(s), \alpha)$.
  By definition of the transition function, $\delta(row(s), \alpha) = row(s \cdot \alpha)$ and by construction of the $O.T$, for every $s \in S$ there exists an $s' \in S_{ext}$ such that $s' = s \cdot \alpha$.
  We can conclude that for every $\omega \in S_{ext}$ there exist a state represented by $row(\omega)$ such that $\delta(q_I, \omega) = row(\omega)$.
\end{proof}

\begin{lemma}
  \label{lemma:L_acceptance}
  Let $\omega \in S_{ext} \text{ and } e \in E$, then $\delta(q_I, \omega \cdot e)$ leads to an accepting state if $T(\omega \cdot e) = 1$.
\end{lemma}

\begin{proof}
  As previous lemma, we prove this by induction on the length of the word $e$.
  If $length(e) = 0$, then $e = \E$ and by definition, $row(\omega \cdot \E) = row(\omega)$ is acceptant if $T(\omega) = 1$.
  Suppose this property is true for every word $e_n$ of length $n$.
  Let $e_{n+1} \in E$ be a word of length $n + 1$, $e_n \in E$ the suffix of $e_{n+1}$ of length $n$ and $\alpha \in \Sigma$ such that $\alpha \cdot e_n = e_{n+1}$. Since $E$ is suffix close, we know that $e_n$ should exist in $E$.
  We have that $\delta(q_I, \omega \cdot e_{n+1}) = \delta(q_I, \omega \cdot \alpha \cdot e_n) = \delta(\delta(q_I, \omega), \alpha \cdot e_n) = \delta(row(\omega), \alpha \cdot e_n) = \delta(\delta(row(\omega), \alpha), e_n)$.
  Since the table is closed, $\exists \omega' \in S$ such that $row(\omega) = row(\omega')$, and so $\delta(row(\omega), \alpha) = \delta(row(\omega'), \alpha) = row(\omega' \cdot \alpha)$ thanks to the preceding lemma. Again, since the table is closed, $\exists \omega'' \in S$ such that $row(\omega' \cdot \alpha) = row(\omega'')$.
  We have now that $\delta(q_I, \omega \cdot e_{n+1}) = \delta(row(\omega''), e_n) = \delta(\delta(q_I, \omega''), e_n) = \delta(q_I, \omega'' \cdot e_n)$ and since the length of $e$ is $n$ then, by inductive hypothesis, $\delta(q_I, \omega'' \cdot e_n)$ leads to an accepting state if $T(\omega'' \cdot e_n) = 1$.
  We can conclude that $\delta(q_I, \omega \cdot e_{n+1})$ leads in an accepting state if $T(\omega \cdot e_{n+1}) = 1$.
\end{proof}

\begin{lemma}
  The automaton created is deterministic.
\end{lemma}

\begin{proof}
  We know that for every row $s_1 \in S$ and for every $\alpha \in \Sigma$, $s_1 \cdot \alpha \in S_{ext}$. Moreover, since the table is consistent, for every $s_2 \in S$, if $row(s_1)$ and $row(s_2)$ are similar, we have that $row(s_1 \cdot \alpha) = row(s_2 \cdot \alpha)$ for $\forall \alpha \in \Sigma$. Therefore there exists only one successor for every state $q \in Q$ and every letter $\alpha \in \Sigma$.
\end{proof}

\begin{lemma}
  \label{lemma:mDFA}
  The automaton created is minimal.
\end{lemma}

\begin{proof}
  We proceed by contradiction. Suppose that $\A$ is not minimal, then it is possible to merge\footnote{Two states can be merged if they recognize the same language or, because of the automaton is deterministic, they have same outgoing arrows} at least two states $q_i, q_j$ to reduce the number of states.
  By construction, $q_i$ and $q_j$ are linked to two different rows $r_i, r_j$ of the observation table. Let $\omega_1 \in S, \omega_2 \in S \mid row(\omega_1) = r_1 \text{ and } row(\omega_2) = r_2$. Since $r_1 \neq r_2 \text{ then } \exists e \in E \mid T(\omega_1 \cdot e) \neq T(\omega_2 \cdot e)$. Let $\A_1$ be the automaton $\A$ where $row(\omega_1)$ is the initial state and $\A_2$ be the automaton $\A$ where $row(\omega_2)$ is the initial state. The word $e$ will only be accepted by one of the two automata, so since they do not recognize the same language, we can't merge them in $\A$.
  This contradict the initial hypothesis and so $\A$ is minimum.
\end{proof}

\subsection{L* analysis}
\subsubsection{Correcteness}
The goal of the algorithm is to find an automaton recognizing the language of the Teacher. The final conjecture found by the Learner is correct since it has been accepted by the Teacher.

\subsubsection{Halting}
The algorithm terminates only if the Teacher answers $Yes$ to an \textit{equivalence query} of the Learner, but is the Learner able to find this final \textit{equivalence query} ?

We know, by definition, that the unknown language $\U$ is regular. It means that there exists an automaton $\A$ recognizing it with a \underline{finite} number $n$ of states.

If in a certain moment we have $n' < n$ different rows and the \OT is closed and consistent, the Learner make its conjecture. This conjecture cannot be the good one, since, from \cref{lemma:mDFA}, the automaton $\A'$ of the conjecture cannot have less states then $\A$. The Teacher must returns a counter-example allowing to separate two similar rows. The Learner repeats the algorithm, and after every \textit{equivalence query}, $n'$ will increase by one. When $n' = n$ the conjecture will have $n$ states, the same of $\A$, and therefore, the Teacher will answer $Yes$ to the last conjecture allowing the algorithm to stop.

\begin{remark}
  Note that \cref{lemma:L_trans_from_QI} and \cref{lemma:L_acceptance} say that the automaton $\A$ created from a closed and consistent table recognizes correctly the language understood so far and since the \textit{mDFA} is unique (see \cref{lemma:minimalAut}) then the conjecture is the correct one.
\end{remark}

\subsubsection{Time complexity}
Suppose that the L* algorithm (implemented in \cref{L_star_algo}) takes constant time to make the conjecture, to complete the \OT, to promote rows and to check if the table is consistent and closed.

Then time complexity should be measured on the size of the \OT or equivalently on the number of cell composing it.

In the worst case, the number of columns $n$ (that is the cardinal of $E$) equals the number of states $\A$.

The size of $S$ depends on the length $m$ of the counter-example provided by the Teacher. At least $S$ has $n$ rows, one per number of columns, but considering the length of $m$ then $|S|$\footnote{The cardinal of S} will have $O(n \times m)$ rows.

The size of $SA$ is at most $O(|\Sigma| \times |S|)$ since for every word in $|S|$ we can potentially have $|\Sigma|$ successors to add in $SA$.

In conclusion we have a matrix with a number of cells equal to
\begin{align*}
  |E|\times(|S| + |SA|) & = O(|E|\times(|S| + \Sigma \times |S|))     \\
                        & = O(2 \cdot |\Sigma| \times |E| \times |S|) \\
                        & = O(n^2 \times m)
\end{align*}

Hence, the time complexity is polynomial on the size of the automaton and the larger counter-example given by the Teacher.


\section{Measurement Editing and Feasibility Criteria}

\subsection{Line of Site Test}

This test checks to see if a celestial body obstructs the signal path between two objects.  There are three cases that can occur in this test: two spacecraft (case 1), a spacecraft and a participant on the surface of a celestial body (case 2), and two participants on the surfaces of different celestial bodies (case 3).  Below we address each of these cases starting with case 1.

For all three cases, assume the signal is generated by participant 1 at time $t_1$ and, if no obstruction occurs, the signal is received by participant 2 at time $t_2$. Define the location of the first participant at time $t_1$, expressed in $\mcF_{\mcI_1}$ as $\mathbf{r}_1^{\mcI_1}(t_1)$ which is calculated using Eq.~(\ref{Eq:LocalInertialAntennaPos1}). Define the location of participant 1 at time $t_1$, expressed in $\mcF_{\mcS}$, as $\mathbf{r}_1^{\mcS}(t_1)$ which is calculated using Eq.~(\ref{Eq:BaryInertialAntennaPos1}). Similarly, $\mathbf{r}_2^{\mcI_2}(t_2)$ and $\mathbf{r}_2^{\mcS}(t_2)$ are calculated using Eqs.~(\ref{Eq:LocalInertialAntennaPos2}) and (\ref{Eq:BaryInertialAntennaPos2}) respectively.

\subsubsection{Two Spacecraft}

\subsection{Height of Ray Path}

\subsection{Line of Sight}

The Line of Sight (LOS) test is a visibility test for inter-spacecraft measurements.
The algorithm presented here is based on Vallado\cite{vallado3} (pp. 307-311) with slight modifications to include light time correction when applicable (i.e. for all measurements except geometric measurements). This test first checks to see if the intersection of the perpendicular distance vector, $\mathbf{d}(\tau_{min})$,  with the ray path, $\boldsymbol{\rho}$, lies in between the two participants. It then checks to see if the ray path height, $h$, is above the minimum allowable ray path altitude $h_{min}$.

The geometry for the LOS test is shown in Fig.~({\ref{Fig:HORP}).
%
\begin{figure}[h!]
    \begin{center}
        \begin{picture}(270,145)
            \special{psfile= ./Images/HORP.eps hscale= 100 vscale= 100 hoffset = -45 voffset = -455}
             \makebox(115,15){${h}_{min}$}
             \makebox(35,20){$R_b$}
             \makebox(60,90){$\mathbf{r}_2$}
             \makebox(-150,150){$d$}
             \makebox(-150,210){$\boldsymbol{\rho}$}
             \makebox(-310,150){$\mathbf{r}_1$}
        \end{picture}
    \end{center}
    \vspace{.2 in}
    \caption{ Height of Ray Path Geometry }
    \label{Fig:HORP}
\end{figure}
%
The variables in Fig.~\ref{Fig:HORP} are defined as follows.
%
\begin{center}
    \begin{minipage}[t]{5.0 in}
        \begin{tabbing}[htbp!]
            123456 \= dummy line \kill
            $\mathbf{r}_1(t_1)$ \> Position vector of participant 1 at time of signal transmission, $t_1$ \\
            $\mathbf{r}_2(t_2)$ \> Position vector of participant 2 at time of signal reception, $t_2$ \\
            $\boldsymbol{\rho}$ \> Vector from location of particpant 1 at signal transmission to location\\
             \>  of participant 2 at signal reception\\
            $\rho$ \> Magnitude of $\boldsymbol{\rho}$ \\
            $R_b$ \> Central body radius \\
            $h_{min}$ \> Minimum acceptable ray path height \\
            $\tau$ \> Ray path parameter defined as the fraction of the unit distance along $\boldsymbol{\rho}$ \\
            $\mathbf{d}(\tau)$ \> Position vector of $\tau$ \\
            $\tau_{min}$ \> Value of $\tau$ minimizing the distance between $\boldsymbol{\rho}$ and the central body \\
            $d(\tau_{min})$ \> Distance from central body origin to ray path (measured perpendicular) \\
        \end{tabbing}
    \end{minipage}
\end{center}
%
The ray path vector, $\boldsymbol\rho$, is computed from
%
\begin{equation}
   \left[\boldsymbol{\rho}\right]_1 =  \left[ \mathbf{r}_2(t_2) \right]_1 -  \left[ \mathbf{r}_1 (t_1) \right]_1
\end{equation}
%
where the quantities $\mathbf{r}_2(t_2)$ and $\left[ \mathbf{r}_1 (t_1) \right]_1$ are determined during light time correction.
The ray path parameter, $\tau$, is a unitless value indicating a point along $\boldsymbol{\rho}$. The only value of $\tau$ that we care about here is $\tau_{min}$, which is computed from
%
\begin{equation}
     \tau_{min} = \frac{\left[ \mathbf{r}_2 (t_2) \right]_1 \cdot \left[\boldsymbol{\rho}\right]_1} {\rho^2}
\end{equation}
%
The position vector of $\tau$, $\mathbf{d}(\tau)$, is simply
%
\begin{equation}
    \mathbf{d}(\tau) = \left[ \mathbf{r}_2(t_2) \right]_1 -  \left[\boldsymbol{\rho}\right]_1 \tau
\end{equation}
%

Finally, the criteria for measurement feasibility is when the following statement is true
%
\begin{equation}
    T^2 - T > 0 \hspace{.05 in} \mbox{or} \hspace{.05 in} d(\tau_{min})^2 - R_b^2 >= 0
\end{equation}
%
For geometric measurements, $t_1 = t_2$.  For measurements involving participants about different central bodies, the LOS test is performed twice, once for each central body.

\subsection{Horizon Angle Test}

The horizon angle test checks to see if a space-based observer  is above the local horizon for a ground-based observer. The space-based observer can be a spacecraft or ground based observer on another celestial body.  The geometry for this test is shown in Fig. \ref{Fig:HorizonAngle} where the nomenclature is defined below.
%
\begin{figure}[h!]
    \begin{center}
        \begin{picture}(270,125)
            \special{psfile= ./Images/HorizonTest.eps hscale= 100 vscale= 100 hoffset = -45 voffset = -495}
             \makebox(290,100){$\mathbf{r}_s$}
             \makebox(-360,100){$\mathbf{r}_g$}
             \makebox(-300,200){$\boldsymbol{\rho}$}
             \makebox(-215,105){$\delta_m$}
             \makebox(-190,195){$\delta$}
        \end{picture}
    \end{center}
    \vspace{.02 in}
    \caption{ Height Angle Test Geometry }
    \label{Fig:HorizonAngle}
\end{figure}
%
\begin{center}
    \begin{minipage}[t]{5.0 in}
    \begin{tabbing}[htbp!]
            123456 \= dummy line \kill
            $\mathbf{r}_g$ \> Position vector of the ground-based observer at time $t_g$\\
            $t_g$ \> Time the signal is at the receiving electronics of the ground-based observer \\
            $\mathbf{r}_s$ \> Position vector of the space-based observer at time $t_s$ \\
            $t_s$ \> Time the signal is at the receiving electronics of the space-based observer \\
            $\boldsymbol{\rho}$ \> Vector from location of ground-based observer to  space-based observer\\
             \>  of participant 2 at signal reception\\
            $\delta_m$ \> Minimum elevation angle above local horizon of ground-based observer\\
        \end{tabbing}
    \end{minipage}
\end{center}
%
From inspection of Fig. \ref{Fig:HorizonAngle}, a space-based observer is above the local horizon
of a ground-based observer if
%
\begin{equation}
    \delta \geq \delta_m
\end{equation}
%
From manipulation of the inner product we know
%
\begin{equation}
    \sin{\delta} = \frac{ \boldsymbol{\rho} \cdot \mathbf{r}_g }{\rho r_g }
\end{equation}
%
where $\boldsymbol{\rho} = \mathbf{r}_s - \mathbf{r}_g$. Finally, the horizon check is true if the following statement is satisfied and false otherwise:
%
\begin{equation}
     \frac{ \boldsymbol{\rho} \cdot \mathbf{r}_g }{\rho r_g } > \sin{\delta_m}
\end{equation}

\subsection{Range Limit Test}
\subsection{Range Rate Limit Test}
\subsection{Solar Exclusion Angle Test}


%=======================   Default Templete   ==================
\documentclass[a4paper]{article}

% file with some default definations
\input{structure.tex}
\usepackage{listings}
\lstset{language=Python, basicstyle=\normalsize\sffamily\linespread{0.8}, numbers=left, numberstyle=\small, stepnumber=1, numbersep=5pt}
\usepackage{fancyhdr}
\setlength{\parindent}{0pt}
\usepackage{algorithm2e}
\usepackage{caption}
\pagestyle{fancy}
\fancyhf{}
\lhead{\textbf{\NAME\ (\ANDREWID)}}
\chead{\textbf{Assignment \HWNUM}}
\rhead{\COURSE}

%==================Header details======================
\newcommand\NAME{Raghukul, Vibhor}
\newcommand\ANDREWID{160538, 160778}
\newcommand\HWNUM{3}
\newcommand\COURSE{CS648}
\newcommand{\V}[1]{\boldsymbol{#1}}
%======================================================

% available formatted sections:
% - COMMAND LINE ENVIRONMENT: \begin{commandline} \end{commandline}
% - FILE CONTENTS ENVIRONMENT: \begin{file}[optional filename, defaults to "File"]
% - NUMBERED QUESTIONS ENVIRONMENT: \begin{question}[optional title]
% - WARNING TEXT ENVIRONMENT(can also be used for note): \begin{warn}[optional title, defaults to "Warning:"]
% - INFORMATION ENVIRONMENT(can be used to mention given details): \begin{info}[optional title, defaults to "Info:"]

%===============================================================
\begin{document}

% start of Q1
\begin{question}
\textbf{Estimating all-pairs distances exactly}
\end{question}
\begin{algorithm}[H]
\SetAlgoLined
\KwIn{$G$, partial distance matrix $M$}
\KwOut{complete distance matrix $M$ with error probability $< 1 - 1/n^2$}
 $D \leftarrow n$ x $n$ matrix to store distance \\
 \For{$i \leftarrow 1$ \KwTo $n$} {
 	\For{$j \leftarrow 1$ \KwTo $n$} {
 		\uIf{$M[i][j] =$ \#}{
 			$D[i][j]$ = infinity
 		}
 		\uElse {
 			$D[i][j] = M[i][j]$
 		}
 	}
 }
 \For{$k \leftarrow 1$ \KwTo $K$} {
 	$w \leftarrow$ Random vertex in $[1:n]$ \\
 	$dist = bfs($G, w$)$ \\
 	\For{$i \leftarrow 1$ \KwTo $n$} {
 		\For{$j \leftarrow 1$ \KwTo $n$} {
 		    $D[i][j] = min(D[i][j], dist[i] + dist[j])$
 		}
 	}
 }

 \Return{$M$}
 \caption{Monte Carlo Algorithm for exact distance}
\end{algorithm}
\subsection*{Complexity analysis}
In above algorithm $bfs(G, s)$ returns an array storing shortest distance of $s$ to each vertex in $G$.
Time complexity for $bfs$ is $O(n + m)$ ($m$ = number of vertices in graph).
In worst case there can be atmost $n \choose 2$ edges, i.e worst case complexity of $bfs$ is $O(n^2)$. Time taken to update $D$ in one iteration is also $O(n^2)$.
Since outer loop runs for $K$ times, total complexity is $O(n^2K)$.
In below analysis it is show that $K = O(\log n)$, hence complexity of above algorithm is $\V{O(n^2)\log n}$.

\subsection*{Error Analysis}

Error will occur if there are $\geq 1$ entries in $D$ which are not equal shortest path distance.
\begin{align*}
P(error) &= P(M[1][1] \text{ wrong } \cup M[1][2] \text{ wrong } \ldots \cup M[n][n]\text{ wrong })
\end{align*}
Applying Boole's inequality we get:
\begin{align*}
P(error) &\leq n^2*P(M[i][j] \text{ wrong }) \text{ ( distance between $i,j > n/100$ )}
\end{align*}
Note that there is $\leq$ sign in above line since the points for which distance $\leq n/100$ matrix $M$ stores correct value (since the value is taken directly from partial distance matrix).

\subsubsection*{$\V{M[i][j]}$ computed wrong}
We know that distance between $i, j$ is greater than $n/100$, i.e number of nodes between shortest path from $i$ to $j$ is $\geq n/100$.
So now consider first iteration of first loop. \\
Note that $w$ is choosen randomly, hence the probability that $w$ belongs to shortest path from $i$ to $j$ is $\geq \dfrac{n/100}{n} = \dfrac{1}{100}$. \\
If $w$ belong to shortest path then $D[i][j]$ will be updated correctly and will contain shortest path distance (since we are doing $bfs$ from $w$, we get shortest path value from $w\rightarrow i, w\rightarrow j$, $M[i][j]$ is simple the sum of these $2$ values).\\
Probability that $D[i][j]$ is wrong ($P(\delta)$) $\leq \dfrac{99}{100}$ \\
Probability that $D[i][j]$ is wrong even after $K$ iteration $\leq P(\delta)^{K} = \Bigg( \dfrac{99}{100} \Bigg)^K$ \\
Using this value in above equation we get

\begin{align*}
P(error) &\leq n^2 P(\delta) \\
		 &\leq n^2 \Bigg( \dfrac{99}{100} \Bigg)^K
\end{align*}
We want that error probability is $\leq 1/n^2$, or
\begin{align*}
P(error) &\leq \dfrac{1}{n^2} \\
n^2 \Bigg( \dfrac{99}{100} \Bigg)^K &\leq \dfrac{1}{n^2} \\
\Bigg( \dfrac{99}{100} \Bigg)^K &\leq \dfrac{1}{n^4}\\
K\log \Bigg( \dfrac{100}{99}\Bigg) &\geq 4\log n \\
K &\geq \V{\dfrac{4}{\log(100/99)} \log n}
\end{align*}
Taking $\V{K = 400\log n}$ we get that all entries of the distance matrix are correct with probability exceeding $1 - 1/n^2$.
\pagebreak

% start of Q2
\begin{question}[]
\textbf{Rumour Spreading}
\end{question}
We will partition the experiment into following three stages. Let $X$ is the number of persons knowing the rumour at any time.
The stages are -
\begin{enumerate}
    \item $X < c\log n$
    \item $c\log n < X < \frac{n}{2}$
    \item $\frac{n}{2} < X < n$
\end{enumerate}
Expected no. of days to spread the rumour is the sum of expected no. of days spent in each of these stages.

\textbf{(1)} Expected no. days spent in stage 1 -

Let $p$ is the probability that no new person comes to know about the rumour at the end of some day. Let $k$ be the no. of persons knowing the rumour at the start of the day.

Thus,
$$p = \left(\frac{k-1}{n-1}\right)^k \approx \left(\frac{k}{n}\right)^k$$
For $1 \leq k < \frac{n}{2}$, $p \leq \left(\frac{1}{2}\right)^k \leq \frac{1}{2}$

So, expected no. of days for at least one person to know the rumour is $\frac{1}{1 - p} \leq 2$.

Thus the expected no. of days for $c\log n$ persons to know the rumour is less than or equal to $2c\log n$.
Expected no. of days spent in first stage, $$N_1 = 2c\log n$$

\textbf{(2)} Expected no. days spent in stage 2 -

Let the label of the person called by $i^{th}$ person be $x_i$. Clearly, $x_1,x_2,...,x_k$ are independent.

Consider $f(x_1,x_2,...,x_k) : $ No. of persons not knowing the rumor at the end of day, if the no. of persons not knowing the rumor at the beginning of the day was $r$.

By linearity of expectation and since $n - \log n < r$,
$$E[f] = r\left(\frac{n-1}{n}\right)^{n-r} \leq \frac{r}{c}, c > 1$$

Clearly, $|f(A) - f(A_0)| \leq 1$ for all $A, A_0$ that differ only at the $i^{th}$ coordinate.

Thus, $f$ satisfies Lipshitz condition with parameters $c_1,c_2,...c_k$, $c_i = 1$.

Take $p$ such that $1 < p < c$, call a day good if $f < \frac{r}{p}$ at the end of the day.
Expected no. of good days required for this stage is $O(\log n)$

$$P[\text{day is bad}] = P\left[f \geq \frac{r}{p}\right]$$
$$P[\text{day is bad}] = P\left[f - \frac{r}{c} \geq \frac{r}{p}-\frac{r}{c}\right]$$
$$P[\text{day is bad}] \leq P\left[|f - E[f]| \geq \frac{ru}{c}\right], u > 0$$
Using the method of bounded difference,
$$P[\text{day is bad}] \leq \exp\left(\frac{-u^2r^2}{2c^2\sum {c_i}^2}\right)$$
Since $r < \frac{n}{2}$
$$P[\text{day is bad}] < \exp\left(\frac{-u^2n^2}{8c^2n}\right)$$
$$P[\text{day is bad}] \leq \exp(-kn)$$
Thus probability that a day is bad is inverse exponential in $n$ and hence the expected no. of days spent in second stage = $O(\log n)$.

\textbf{(3)} Expected no. days spent in stage 3 -

Let $k$ be the no. of persons knowing the rumour at the start of the day and let $r$ be the no. of persons not knowing the rumour at the start of the day ($r = n-k$).

Let $p_i$ be the probability that $i^{th}$ person does not know the rumour at the end of the day.
$$p_i = \left(\frac{n-1}{n}\right)^k$$
Since $k > \frac{n}{2}$,
$$p_i \leq \left(1 - \frac{1}{n}\right)^{\frac{n}{2}} \approx \frac{1}{\sqrt{e}}$$
Let $R_i$ be the random variable which takes value $1$ if the $i^{th}$ person does not know the rumour at the end of the day and $0$ otherwise.

Let $R$ be the no. of persons not knowing the rumour at the end of the day.

Now by linearity of expectation,
$$E[R] = \sum_{i=1}^r E[R_i]$$
$$E[R] = \sum_{i=1}^r p_i + 0(1- p_i)$$
$$E[R] \leq \frac{r}{\sqrt{e}}$$
Define a day to be good if the no. of persons not knowing the rumour reduces by more than $\frac{1}{\sqrt{2}}$ from the start of the day.

Thus the expected no. of good days required for spreading the rumour to $n$ people is $2\log n$.

Let $p$ be the probability that a day is bad,
$$p = P\left[R \geq \frac{r}{\sqrt{2}}\right]$$
Using Markov's inequality,
$$p \leq \frac{\sqrt{2}E[R]}{r}$$
$$p \leq \frac{\sqrt{2}}{\sqrt{e}} \leq \frac{7}{8}$$
and so probability of a day being good is at least $\frac{1}{8}$.

Thus the Expected no. of days spent in third stage, $$N_3 = 16\log n = q \log n$$

\pagebreak
% start of Q3
\begin{question}[]
\textbf{Approximate Ham-Sandwich Cut}
\end{question}
\begin{algorithm}[H]
\SetAlgoLined
\KwIn{set $P$ of $2n$ points, $n$ red and $n$ blue}
\KwOut{$(1 + 1/2)$-approximate ham-sandwich cut of $P$}
\Repeat{\textbf{True}} {
	$K \leftarrow$ random sample containing $k/2$ red, and $k/2$ blue points. \\
	$L$ = ExactHamCut($K, k$) \\
	\tcc*[f]{count red points that lie on either side, with reference to origin.}\\
	$c\_red\_l \leftarrow 0$  \\
	$c\_red\_r \leftarrow 0$  \\
	\For{$i \leftarrow 1$ \KwTo $n$} {
		\uIf{$P[i]$, $(0,0)$ \text{lie on same side of} $L$} {
			$c\_red\_l \leftarrow c\_red\_l + 1$\\
		}
		\uElse {
			$c\_red\_r \leftarrow c\_red\_r + 1$\\
		}
	}
	\uIf{$n/4\leq$c\_red\_l $\leq 3n/4$ \&\& $n/4\leq$c\_red\_r $\leq 3n/4$} {
		$red \leftarrow $ \textbf{True} \\
	}
	\uElse {
		$red \leftarrow$ \textbf{False}\\
	}

	\tcc*[f]{count blue points that lie on either side, with reference to origin.}\\
	$c\_blue\_l \leftarrow 0$  \\
	$c\_blue\_r \leftarrow 0$  \\
	\For{$i \leftarrow n+1$ \KwTo $2n$} {
		\uIf{$P[i]$, $(0,0)$ \text{lie on same side of} $L$} {
			$c\_blue\_l \leftarrow c\_blue\_l + 1$\\
		}
		\uElse {
			$c\_blue\_r \leftarrow c\_blue\_r + 1$\\
		}
	}
	\uIf{$n/4\leq$c\_blue\_l $\leq 3n/4$ \&\& $n/4\leq$c\_blue\_r $\leq 3n/4$} {
		$blue \leftarrow $ \textbf{True} \\
	}
	\uElse {
		$blue \leftarrow$ \textbf{False}\\
	}

	\uIf{blue \&\& red} {
		\Return{L}
	}
}
\caption*{Las-Vegas Algorithm for $(1 + 1/2)$-approximate ham-sandwich cut of $P$}
\end{algorithm}

\begin{algorithm}[H]
\SetAlgoLined
\KwIn{set $X$ of $m$ points, $m/2$ blue, $m/2$ red}
\KwOut{Exact Ham-Sandwich cut for $X, m$}
$R \leftarrow$ set of red points ($|R| = m/2$) \\
$B \leftarrow$ blue points \\
\For{$i\leftarrow 1$ \KwTo $m-1$} {
	\For{$j\leftarrow i+1$ \KwTo $m$} {
		$L \leftarrow$ line joining $X[i], X[j]$ \\
		\tcc*[f]{Check if L is an exact ham-sandwich cut.}\\
		\tcc*[f]{count red points that lie on either side, with reference to origin.}\\
		$c\_red\_l \leftarrow 0$  \\
		$c\_red\_r \leftarrow 0$  \\
		\For{$i \leftarrow 1$ \KwTo $m/2$} {
			\uIf{$R[i]$, $(0,0)$ \text{lie on same side of} $L$} {
				$c\_red\_l \leftarrow c\_red\_l + 1$\\
			}
			\uElse {
				$c\_red\_r \leftarrow c\_red\_r + 1$\\
			}
		}
		\uIf{c\_red\_l $\leq m/4$ \&\& c\_red\_r $\leq m/4$} {
			$red \leftarrow $ \textbf{True} \\
		}
		\uElse {
			$red \leftarrow$ \textbf{False}\\
		}

		\tcc*[f]{count blue points that lie on either side, with reference to origin.}\\
		$c\_blue\_l \leftarrow 0$  \\
		$c\_blue\_r \leftarrow 0$  \\
		\For{$i \leftarrow 1$ \KwTo $m/2$} {
			\uIf{$B[i]$, $(0,0)$ \text{lie on same side of} $L$} {
				$c\_blue\_l \leftarrow c\_blue\_l + 1$\\
			}
			\uElse {
				$c\_blue\_r \leftarrow c\_blue\_r + 1$\\
			}
		}
		\uIf{c\_blue\_l $\leq m/4$ \&\& c\_blue\_r $\leq m/4$} {
			$blue \leftarrow $ \textbf{True} \\
		}
		\uElse {
			$blue \leftarrow$ \textbf{False}\\
		}

		\uIf{blue \&\& red} {
			\Return{L}
		}
	}
}
\caption*{ExactHamCut}
\end{algorithm}
\subsection*{Overview}
We take random sample of size $k$, containing equal number of red and blue points,
compute their exact ham sandwich cut, and
check if this cut is an approximate ham sadwich cut for global set $P$ as well.
Is yes the we report it, else repeat. Since there are only $k\choose2$ possible cuts, for deterministic algorithm we just check all the possible pair in $O(k)$.

\subsection*{Complexity analysis}
Time complexity of exact ham-sandwich cut above is $O(k^3)$ (since we need to check $k\choose2$ lines). From below analysis we know that for $k = 40\log n$, the probability that algorithm will not terminate in $1$ iteration is $\leq \dfrac{1}{n^4}$. Complexity of Las Vegas algorithm for approximate ham cut above is in fact a geometric random variable with success probability $\geq 1- \dfrac{1}{n^4}$. Expected number of iteration ($E(t)$) is given by:
\begin{align*}
E(t) &= \dfrac{1}{\text{Success probability}} \\
	 &\leq \dfrac{1}{ 1- \dfrac{1}{n^4}} \\
	 &\leq \dfrac{n^4}{n^4 - 1} \\
\text{or } E(t) &= O(1)
\end{align*}
In one iteration we call ExactHamCut ($O(k^3)$) for a set of points of size $k$, and then we check for all points ($O(n)$). So overall complexity of one iteration is $O(k^3 + n) = O(\log^3 n + n)$. Since expected number of iterations is $O(1)$, expected total complexity of algorithm is given by: $O(\log^3 n + n) = \V{O(n)}$.

\subsection*{Error Analysis:}

Error will occur when more than $\frac{3n}{4}$ red or $\frac{3n}{4}$ blue points lie on either side of the line which is the exact ham-sandwich cut of $k$ points.

Without loss of generality assume that more than $\frac{3n}{4}$ red points lie on one side of the line.

This means that when selecting $\frac{k}{2}$ red points uniformly randomly, $\frac{k}{4}$ red points got sampled from a cluster of less than $\frac{n}{4}$ points and the other $\frac{k}{4}$ red points were sampled from the remaining points on the other side of the line.

So, we know that less than $\frac{n}{4}$ red points are on one side, let there be $\frac{n}{4} - x$ points, error event is the union of $\binom{\frac{n}{4} - x}{\frac{k}{4}}$ combinations for all $x \in \{1,...,\frac{n}{4} - \frac{k}{4}\}$ which is same as the probability of getting more than $\frac{k}{4}$ tails in $\frac{k}{2}$ tosses of a biased coin having probability of head as $\frac{3}{4}$.

Thus,
$$P[\text{error}] \leq 2*P[\text{more than } \frac{k}{4} \text{ tails occur in } \frac{k}{2} \text{ tosses}]$$
Taking into account the blue points, we have multiplied by $2$.
$$P[\text{error}] \leq 2*\sum_{i=\frac{k}{4}}^{\frac{k}{2}}\binom{\frac{k}{2}}{i}\frac{1}{4}^{i}\frac{3}{4}^{\frac{k}{2}-i}$$
$$P[\text{error}] \leq 2*\binom{\frac{k}{2}}{\frac{k}{4}}\frac{3}{4}^{\frac{k}{2}}\sum_{i=\frac{k}{4}}^{\frac{k}{2}}\frac{1}{3}^{i}$$
$$P[\text{error}] \leq 2*\binom{\frac{k}{2}}{\frac{k}{4}}\frac{3}{4}^{\frac{k}{2}}\frac{1}{3}^{\frac{k}{4}}\frac{3}{2}$$
$$P[\text{error}] \leq 3*4^{\frac{k}{4}}\frac{3}{4}^{\frac{k}{2}}\frac{1}{3}^{\frac{k}{4}}$$
$$P[\text{error}] \leq 3*\frac{3}{4}^{\frac{k}{4}}$$
$$P[\text{error}] \leq \frac{1}{2}^{\frac{k}{10}}, k>10$$

To get an error probability less than $n^{-4}$ take $k = 40 \log n$.
\end{document}


% Copyright 2018-2021 Melvin Eloy Irizarry-Gelpí
\setcounter{chapter}{4}
\chapter{Inductors}
%
...
%
\section{Preliminary}
%
Resistance is a property of the components in an electric circuit. You can use units of ohm to measure electric resistance. One ohm of electric resistance is equivalent to
\begin{equation}
	1 \text{ ohm} = 1 \text{ V/A}
\end{equation}
Another property is inductance, which is like a magnetic cousin of capacitance. You can measure inductance in units of henrys (H). One henry of inductance is equivalent to
\begin{equation}
	1 \text{ H} = 1 \text{ T m}^{2}\text{/A}
\end{equation}
In some ways, inductance measures the amount of magnetic flux per unit current.

A component in a circuit with an electric resistance is called a \textbf{resistor}. Similarly, a component in a circuit with a inductance is called an \textbf{inductor}. A circuit with both inductors and resistors is known as an RL circuit. The simplest RL circuit has one resistor and one inductor connected in series to a battery (a DC source).

A particular quantity $\tau_{L}$ is given by dividing the inductance by the resistance:
\begin{equation}
	\tau_{L} = \frac{L}{R}
	\label{eq.05L.tauL}
\end{equation}
This quantity is known as the \textbf{inductive time constant}. If resistance is in ohms and inductance is in henrys, then $\tau_{L}$ is in seconds. The inductive time constant is important because it describes how quickly voltage and current change in circuits with resistors and inductors. During the events that we will record, the voltage rises very suddenly and decreases in an exponential drop to a transient value:
\begin{equation}
	V(t) = V_{\infty} + (V_{0} - V_{\infty}) \exp{\left( - \frac{t}{\tau_{L}} \right)}
	\label{eq.05L.Vt}
\end{equation}
Here $V_{0}$ is the voltage when $t = 0$, and $V_{\infty}$ is the transient value of the voltage ($t \longrightarrow \infty$). The behavior of the voltage is slightly more complicated than what we observed for capacitors, and introduces difficulties when finding the exponential fit. In order to be able to find the exponential fit, it is convenient to work with
\begin{equation}
	\Delta(t) = V(t) - V_{\infty} = (V_{0} - V_{\infty}) \exp{\left( - \frac{t}{\tau_{L}} \right)}
	\label{eq.05L.Delta.t}
\end{equation}
The quantity $\Delta$ (``Delta'') has the canonical form of the exponential fit
\begin{equation}
	y = A \exp{(-Bx)}
\end{equation}
so fitting $\Delta$ gives an estimate of the time constant for voltage.
%
\section{Experiment}
%
Each experiment consist of using the triggering feature to collect voltage and current data as soon as the switch is turned on.

There are 8 runs of data:
\begin{enumerate}
	\item $R = 10$ ohm, without metallic core ($L = 0.005$ H).
	\item $R = 10$ ohm, with metallic core ($L$ is unknown).
	\item $R = 5$ ohm, without metallic core ($L = 0.005$ H).
	\item $R = 5$ ohm, with metallic core ($L$ is unknown).
	\item $R = 8.360655738$ ohm, without metallic core ($L = 0.005$ H).
	\item $R = 8.360655738$ ohm, with metallic core ($L$ is unknown).
	\item $R = 8.717948718$ ohm, without metallic core ($L = 0.005$ H).
	\item $R = 8.717948718$ ohm, with metallic core ($L$ is unknown).
\end{enumerate}
For runs 1 and 2 you use a single resistor with 10 ohm resistance. For runs 3 and 4 you use two 10 ohm resistors in parallel, giving
\begin{equation}
	R_{\text{eq}} = \frac{10 \times 10}{10 + 10} \text{ ohm} = 5 \text{ ohm}
\end{equation}
For runs 5 and 6 you use one 10 ohm resistor and one 51 ohm resistor in parallel, giving
\begin{equation}
	R_{\text{eq}} = \frac{10 \times 51}{10 + 51} \text{ ohm} = 8.360655738 \text{ ohm}
\end{equation}
For runs 7 and 8 you use one 10 ohm resistor and one 68 ohm resistor in parallel, giving
\begin{equation}
	R_{\text{eq}} = \frac{10 \times 68}{10 + 68} \text{ ohm} = 8.717948718 \text{ ohm}
\end{equation}
Note that the metallic core increases the value of the inductance by an unknown amount, and thus changes the value of the inductive time constant.
%
\section{Analysis}
%
Just like for capacitors and RC circuits, the analysis for RL circuits is pretty simple: you need to make a graph of voltage versus time and then include an \textbf{exponential} fit. The exponential fit is of the form:
\begin{equation}
	y = A e^{-Bx}
	\label{eq.05L.exp.fit}
\end{equation}
Comparing (\ref{eq.05L.exp.fit}) with (\ref{eq.05L.Delta.t}) leads to identifying the experimental estimate of $\tau_{L}$ with $1 / B$.
\newpage
However, some steps must be taken in order to prepare the data for the exponential fit:
\begin{enumerate}
	\item Truncate any triggering artifacts.
	\item Truncate the long time tail after the exponential drop.
	\item Shift the data vertically towards the horizontal axis such that the voltage settles near 0.
\end{enumerate}
The first truncation involves removing the first few values before the voltage begins to drop exponentially. The amount of data removed in this step is very small compared to the amount removed for capacitors.

For the second truncation, one good rule of thump is to use the expected value of $\tau_{L}$. You should keep a time region that is about $5\tau_{L}$ long in time, starting from the beginning of the exponential drop. For the runs with the metallic core inside the inductor, we do not know the exact value of the inductance, so we do not know the expected value for $\tau_{L}$. In this case use a time region that is about $23\tau_{L}$ with $\tau_{L}$ the time constant without the metallic core.

In order for the exponential fit to be accurate, the data needs to drop to zero. If you look at all the voltage versus time graphs, after the exponential drop the voltage settles on a non-zero value. You need to calculate $\Delta$ given by (\ref{eq.05L.Delta.t}). In a separate column, calculate the value of the voltage column minus the minimum value in the voltage column. If the voltage is in column \texttt{B} and the values begin in row \texttt{6}, then you should use a command like
\begin{center}
	\texttt{=B6 - MIN(B:B)}
\end{center}
to calculate the first value of $\Delta$. Apply this to the entire column. The statement \texttt{MIN(B:B)} finds the minimum value in column \texttt{B}. Make sure that your graph is $\Delta$ versus time and not voltage versus time. The exponential fit for the $\Delta$ should be more accurate and appropriate than the exponential fit for voltage.
%
\section{My Data}
%
In the spreadsheet that I shared with you, there are two results sheets: ``Results 1'' and ``Results 2''. In ``Results 1'' there is a table with ten columns and eight rows. Each of the rows corresponds to one of the eight runs mentioned above. Here are the columns:
\begin{itemize}
	\item Column A: run label.
	\item Column B: value of resistor resistance (in ohm).
	\item Column C: value of inductance (in H), if known.
	\item Column D: Expected value of $\tau_{L}$ (in s). Use (\ref{eq.05L.tauL}) with columns B and C, if the values are known, to calculate these expected values.
	\item Column E: Expected value of $1 / \tau_{L}$ (in 1/s). Just take the reciprocal of the values in column D.
	\item Column F: Fit value of $1 / \tau_{L}$ (in 1/s). This correspond to the $B$ value in the exponential fit. See (\ref{eq.05L.exp.fit}).
	\item Column G: Fit value of $\tau_{L}$ (in s). This is just the reciprocal of the values in column F.
	\item Column H: The percent difference between the theoretical values in column D and the experimental values in column G. This only works if the theoretical value is known.
	\item Column I: Fit value of inductance, found by multiplying the fit value of $\tau_{L}$ in column G by the resistor resistance in column B.
	\item Column J: The ratio of fit inductance, with and without the metallic core, for fixed resistance.
\end{itemize}
For the percent difference, use
\begin{equation}
	\text{Percent Difference } = 100 \times (\text{experiment } - \text{ theory}) / \text{theory}
\end{equation}
As you can see, the percent differences are very large. This indicates a major problem. In particular, the fit values for $\tau_{L}$ are always much smaller than the expected values. Looking at the values in column I, for the inductance from the fit $\tau_{L}$, you can see that when the metallic core is not used, the measured values of the inductance are smaller than the expected 0.005 H.

In order to fix our results, we need to account for the inherent resistance in the inductor. The inductor is just a wire in coil form, but it has a non-negligible amount of resistance. How can we estimate this value?

For lab 3 on circuits in parallel and series, we measured the resistance $R$ for each resistor by measuring the amount of current $I$ coming into the resistor and the voltage $V$ across the resistor. Then
\begin{equation}
	R = \frac{V}{I}
\end{equation}
In this current experiment we measured the current between the resistor and the inductor, and also the voltage across the inductor. Thus, we can estimate the \textbf{resistance of the inductor} $r$ by dividing the voltage by the current. In the sheet ``Ohm Resistance'' I did this for all eight runs using the full raw data, in an additional column (column D). Both voltage and current change with time, and you can see that the resistance also changes with time. But, both voltage and current settle to non-zero transient values, and thus the resistance also. As you can see, in general for all runs the transient value of the inductor resistance is close to 4.20 ohm. Thus, we are going to declare that the resistance of the inductor is
\begin{equation}
	r = 4.20 \text{ ohm}
\end{equation}
How do we incorporate the resistance of the inductor into our results? Well, the resistor and the inductor are connected in \textbf{series}. When two resistors are connected in series, the equivalent resistance is given by the \textbf{sum} of the individual resistances. So we can try to do the same here: the equivalent resistance of a circuit consisting of a resistor with resistance $R$ connected in series with an inductor with resistance $r$ is given by
\begin{equation}
	R_{\text{eq}} = R + r
\end{equation}
Then we used this value to calculate the expected time constant:
\begin{equation}
	\tau_{L} = \frac{L}{R_{\text{eq}}} = \frac{L}{R + r}
\end{equation}
This changes the expectations by a lot and actually makes the expected values much closer to the experimental values. In the sheet ``Results 2'' I calculated the equivalent resistance in column C and then used that to compute the expected time constant in column E and also to compute the experimental inductance in column J. Now the percent difference are much smaller!

Just to clarify: the disagreement between theory and experiment in ``Results 1'' is due to bad theory and not to a bad experiment. The only change in ``Results 2'' is in the expected values of the time constant due to the (correct) use of the equivalent resistance accounting for the resistance of the inductor.

Finally, a comment about the use of the metallic core. Keeping the resistance fixed, you can calculate the ratio of the observed inductance with the core and without the core:
\begin{equation}
	\text{Inductance Ratio} = \frac{L \text{ with core}}{L \text{ without core}}
\end{equation}
You can calculate this ratio for runs 1 and 2; runs 3 and 4; runs 5 and 6; and runs 7 and 8. As you can see from my results, this ratio is somewhat consistent across the four values and suggest that the inductance with the core is about 4.48 times the value of the inductance without the core.

Note that my run 7 and 8 are swapped: run 7 is with the core and run 8 is without the core.

In my spreadsheet I also analyzed the current data and showed that you can also get the time constant. This is in ``Run 1 Current''. You do not have to do this.
%
\section{Your Data}
%
...
%
\section{Your Lab Report}
%
In your lab report you should include:
\begin{enumerate}
	\item Two voltage versus time graphs. Choose any two of the eight runs. Include the exponential fit line, and also the equation.
	\item A table in your printed lab report similar to my table in the ``Results 2'' sheet from the shared spreadsheet.
\end{enumerate}
You should also
\begin{enumerate}
	\item Confirm that the inductor resistance is approximately 4.20 ohm by choosing one run, dividing the voltage by the current, and looking at the late time values of this ratio.
	\item Confirm that, for a given resistor, the measured time constant $\tau_{L}$ is larger when the core is used than when the core is not used.
	\item Confirm that, when the core is not used, the measured values for the inductance are close to the expected value of 0.005 H.
	\item Confirm that, when the core is used, the measured values of the inductance are close to almost 5 times the value of the inductance without the core.
\end{enumerate}


\chapter{Geometri}
\refresh
\subimport{./Triangles/}{Triangles.tex}
\subimport{./Quadrilaterals/}{Quadrilateral.tex}
\subimport{./Circle/}{Circle.tex}