\documentclass[11pt]{article}
\usepackage[cm]{fullpage}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{titlesec}
\usepackage[section]{placeins}
\usepackage{float}
\usepackage{amsmath}
\usepackage{multicol}

\setlength{\columnsep}{1cm}

\title{S1336 - Project 6}
\author{Erik Weilow}

\newcommand{\triplefigure}[3]{
\begin{figure}[H]
  \centering
  \begin{minipage}{0.3\textwidth}
    \centering
    \includegraphics[width=\textwidth]{#1}
  \end{minipage}
  \begin{minipage}{0.3\textwidth}
    \centering
    \includegraphics[width=\textwidth]{#2}
  \end{minipage}
  \begin{minipage}{0.3\textwidth}
    \centering
    \includegraphics[width=\textwidth]{#3}
  \end{minipage}
\end{figure}
}
\newcommand{\doublefigure}[2]{
\begin{figure}[H]
  \centering
  \begin{minipage}{0.4\textwidth}
    \centering
    \includegraphics[width=\textwidth]{#1}
  \end{minipage}
  \begin{minipage}{0.4\textwidth}
    \centering
    \includegraphics[width=\textwidth]{#2}
  \end{minipage}
\end{figure}
}
\newcommand{\singlefigure}[1]{
\begin{figure}[H]
  \centering
  \begin{minipage}{0.4\textwidth}
    \centering
    \includegraphics[width=\textwidth]{#1}
  \end{minipage}
\end{figure}
}
\newcommand{\singlewiderfigure}[1]{
\begin{figure}[H]
  \centering
  \begin{minipage}{0.6\textwidth}
    \centering
    \includegraphics[width=\textwidth]{#1}
  \end{minipage}
\end{figure}
}

\newcommand{\task}[2]{
  \subsection*{Subtask #1)}
  \begin{tabular}{|p{0.9\textwidth}}
    #2\\
  \end{tabular}
}

\newcommand{\subtask}[1]{
  \subsubsection*{#1}
}

\begin{document}
\maketitle
\newpage

\section*{10.10 Numerical solution of the potential within a rectangular region}
\task{a}{
  Determine the potential $V(x, y)$ in a square region with linear dimension $L = 10$.
  The boundary of the square is at a potential $V = 10$.
  Choose the grid size $\Delta x = \Delta y = 1$
}
\subtask{Guess the exact form of $V(x, y)$}
A guess is $V(x,y) = 10$. This is reasonable as all the boundaries are $V=10$ and the algorithm algorithm makes each point the average of the points around it.
For a small grid ($\Delta x = \Delta y = 5$), the center point would be instantly equal to 10.

\subtask{How many iterations are necessary to achieve 1\% accuracy?}
It takes about 56 relaxations to achieve 1\% accuracy, producing the shape seen in the below plots.
\doublefigure{./plots/1010/a_1.png}{./plots/1010/a_1_eq.png}

\subtask{Decrease the grid size by a factor of two, and determine the number of iterations that are now necessary to achieve 1\% accuracy.}
With a grid size $\Delta x = \Delta y = 0.5$, it takes instead closer to 225 relaxations to achieve the same accuracy.
\singlefigure{./plots/1010/a_2.png}
For these two simple cases, we can see that the amount of relaxations could potentially follow
$$
n_{relaxations} \propto \frac{1}{\Delta x \Delta y} \propto \left( n_{gridpoints} \right)^2
$$

This can be shown via the following loglog diagram showing the necessary relaxations for different grid sizes,
where the curve $0.56 n^2$ can be fit to the computed data.
\singlefigure{./plots/1010/a_1_dependency.png}

\task{b}{
  Consider the same geometry as in part (a), but set the initial potential at the interior sites
  equal to zero except for the center site whose potential is set equal to four.
}
\subtask{Does the potential distribution evolve to the same values as in part (a)?}
It does evolve to the same values as in part (a), but does so much slower with around 102 relaxations necessary.
\singlefigure{./plots/1010/b_1.png}

\subtask{What is the effect of a poor initial guess?}
The result is really only that the convergence is slower.

\subtask{Are the final results independent of your initial guess?}
The final results should be independent of the guess. Since each point in every relaxation step is set to the average of the 4 surrounding points, there will only be no change if all points in the grid fulfill $V(x,y) = 10$.

\task{c}{
  Now modify the boundary conditions so that the value of the potential at the four sides is 5, 10, 5, and 10, respectively.
  (Start with a reasonable guess for the initial values of the potential at the interior sites and iterate until 1\% accuracy is obtained.)
}
\subtask{Sketch the equipotential surfaces.}
By these boundary conditions, we get a saddle as seen in the below figure (and equipotential surfaces plot).
\doublefigure{./plots/1010/c_1.png}{./plots/1010/c_1_eq.png}

\subtask{What happens if the potential is 10 on three sides and 0 on the fourth?}
Now we get a sort of rounded slope, that tends toward $V=0$ for $y = 0$.
\doublefigure{./plots/1010/c_2.png}{./plots/1010/c_2_eq.png}

\section*{10.11 Gauss-Seidel relaxation}
\task{a}{
  Use a modification of the program used in 10.10 so that the potential at each site is updated sequentially.
  In this way the new potential of the next site is computed using the most recently computed values of its nearest neighbor potentials.
}
\subtask{Are your results better, worse, or about the same as for the simple relaxation method?}
The convergence is about twice as fast, but produces asymmetrical equipotential surfaces as seen in the left figure.
For the relaxations needed for different grid point counts, the constant is about half however.
\doublefigure{./plots/1011/a_1_eq.png}{./plots/1011/a_1_dependency.png}

The tradeoff is that the results might be slightly less accurate, with the bonus being increased speed.
For larger grids, this method also only have to store a single grid - a plus for the memory usage.

\task{b}{
  Imagine coloring the alternate sites of a grid red and black, so that the grid resembles a checkerboard.
  Modify the program so that all the red sites are updated first, and then all the black sites are updated.
}
\subtask{Do your results converge any more quickly than in part (a)?}
For smaller grids, this method requires perhaps a single iteration less.
For larger grids, the constant is still more or less the same.
\doublefigure{./plots/1011/b_1_eq.png}{./plots/1011/b_1_dependency.png}

It's worth noting that the loglog plot for the checkered method has data from computations matching the fit curve exactly,
while for the method in (a) the computed data trends above for small grids while still asymptotically matching the curve for larger grids.

\section*{10.26 The multigrid method}
\task{a}{
  Write a program that implements the multigrid method using Gauss-Seidel relaxation on a checkerboard lattice.
  Test your program on a 4x4 grid whose boundary sites are all equal to unity, and whose initial internal sites are set to zero.
}
\break
\break
While the results are fairly boring, here are some illustrative plots:
\doublefigure{./plots/1026/a_baseline_1.png}{./plots/1026/a_multigrid_1.png}
The result is that the rightmost plot, set up using the multigrid method, requires no further relaxation.
It was run with a single relaxation step on each progressive prolongation up to a grid size of $2^4$, with no relaxation on the restrictions down to a grid size of $2^2$.
In effect, we only need 4 relaxation steps to produce the rightmost grid,
which really isn't comparable to the leftmost plot that requires 12 relaxations and still isn't close to the exact result.

This effect is further amplified if we look at a larger grid ($2^6$):
\doublefigure{./plots/1026/a_baseline_1_2.png}{./plots/1026/a_multigrid_1_2.png}
In this case, the multigrid method was set up to run a single relaxation on each prolongation up to a grid size of $2^8$, with no relaxation on the restrictions down to a grid size of $2^6$.
The result is 8 relaxations necessary to produce an \textit{exact} result, rather than 3067 relaxations to produce an approximate result.

\task{b}{
  The exact solution for part (a) gives a potential of unity at each point.
}
\subtask{How many relaxation steps does it take to reach unity within 0.1\% at every site by simply using the 4x4 grid?}
As answered in (a), it takes about 12 relaxation steps.
\subtask{How many steps does it take if you use one coarse grid and continue until the coarse grid values are within 0.1\% of unity?}
This one is interesting, because the course grid is a single point surrounded by our boundary conditions.
On the first relaxation, it will be set to the average of the surrounding points ($V=1$), which results in the entire grid taking on unity.

\subtask{Is it necessary to carry out any fine grid relaxation steps to reach the desired accuracy on the fine grid?}
If we start with a grid size of 2x2, with a single non-boundary point in the middle, during all prolongations all the new points will take on the average of the neighbours (from the previous, smaller size).
This means that only the first course grid needs a relaxation step, as subsequent finer grids will become unity\footnote{resistance is futile}.

\subtask{Next start with the coarsest scale, which is just one site. How many relaxation steps does it take now?}
In this case, no relaxation steps should be necessary, according to the same reasoning as above.

\task{c}{
  Repeat part (b), but change the boundary so that one side of the boundary is held at a potential of 0.5.
}
\subtask{Experiment with different sequences of prolongation, restriction, and relaxation.}
I decided that the boundary from this task is boring and defined my own boundary
$$
V_{boundary}(x,y) = 5 + cos(x) sin(y)
$$
with a grid spanning $x\in[0, 2\pi], y\in[0, 2\pi]$.

The first sequence we can look at is one that does 5 relaxations on every prolongation, up to a grid size of $2^4$:
\doublefigure{./plots/1026/a_baseline_2.png}{./plots/1026/a_multigrid_2.png}
Here the multigrid method does a total of $54 + 5 \cdot 4 = 74$ relaxations and the simple iterative method does 192 relaxations, to produce the same accuracy.
\break
We can then look at a sequence that applies 5 relaxations on every prolongation, up to a grid size of $2^8$,
and then applies 5 relaxations on every restriction down to a grid size of $2^4$:
\doublefigure{./plots/1026/a_baseline_3.png}{./plots/1026/a_multigrid_3.png}
Here the multigrid method does a total of $46 + 8 \cdot 4 + 4 \cdot 4 = 94$ relaxations, still less than 192.
\break
The final sequence is similar to the previous one, but one that doesn't relax the grid on restrictions:
\doublefigure{./plots/1026/a_baseline_4.png}{./plots/1026/a_multigrid_4.png}
Here the multigrid method does a total of $53 + 8 \cdot 4 = 85$ relaxations, still less than 192.
It seems to be better not to apply relaxation when restricting the grid size, as the effect of that is to smooth the data rather than to just supersample it.

All in all, the multigrid method seems to be much more effective than just the standard Gauss-Seidel relaxation.

\end{document}


% !TEX root = ../main_lecture_notes.tex
\chapter{Consensus protocol}\label{chap:consensus}
Transactions flow through the network of full nodes. After reviewing them, the full nodes must agree on the transaction that will be recorded in the next block. to do this, an algorithm must be designed so that consensus is reached.
A consensus protocol must be based on one of the scarce resources available to the network peers which include
\begin{itemize}
	\item bandwidth
	\item computational power
	\item storage
\end{itemize}
The first solution that comes to mind for reaching consensus is a majority vote based on a message exchange system. This solution has been proposed by \citet{lamport1982the} within the famous "Byzantine general problem". A voting system inside a large network involves a colossal number of messages exchanged leading to the consumption of all the bandwidth, the failure of some nodes by denial of service and delays in the synchronization of the network. Practical solution like the celebrated Practical Byzantine Fault Tolerance (PBFT) presented in \citet{10.5555/296806.296824} have been implemented in some blockchain systems. Despite these advances, a change in methods was needed to accommodate a network that could grow indefinitely.\\

\noindent \citet{Na08} solved this scaling problem by proposing a system based on the election of a leader. The Proof-of-Work (\PoW) protocol appoints a leader based on its computing resources. Each node competes to solve a puzzle with a brute force search algorithm. The first node who is able to propose a solution append the next block. The search for a solution, referred to as mining, is associated with an operational cost borne by the nodes which is compensated by a reward expressed in the native blockchain cryptocurrency. The surge in cryptocurrency prices has led to a rush in block mining, leading to a major spike in the electricity consumption and electronic waste generation of blockchain networks. The blockchain network consumes as much electricity as countries the size of Thailand at the time of the writing. The need for a more environmentally friendly consensus protocol therefore becomes pivotal. Protocol such as \textit{Proof-of-Capacity} and \textit{Proof-of-Spacetime} use storage. Using storage is seen as a fairer and greener alternative by blockchain enthusiasts due to the general purpose nature of storage and the lower energy cost required by storage. The fact that most storage resources are owned by companies offering cloud storage solution poses a threat to the decentralized nature of the distributed ledger. The Proof-of-Interaction (\PoI) protocol, proposed by \citet{Abegg2021}, takes as leader the first node that is able to contact and obtain a response from a random sequence of nodes. This is a bandwidth-based alternative that is more scalable than majority voting. Along with bandwidth, computing power, and storage, a new resource has emerged with the advent of cryptocurrencies as a medium of exchange. The Proof-of-Stake protocol, described by \citet{Saleh2020}, selects a node with a probability proportional to the number of cryptocoins it holds. \\

\noindent The consensus protocol are applied so that a blocks are appended sequentially and not at the same time. Usually the consensus process is divided into time slots, also called rounds. The block generation time must be higher than the propagation delay in the network. If two blocks are created at the same time then a fork will occur. Two branches of the blockchain co-exists. A fork situation then resolves by applying the \textit{Longest Chain Rule} (LCR).
\begin{definition}
The \textit{Longest Chain Rule} states that if there exist several branches of the blockchain tehn the longest should be trusted.
\end{definition}
\noindent This definition implies that a threshold must be chosen in order to decide when shorter branches of the blockchain should be discarded. For instance, a branch can be considered legitimate if it is $k\in\mathbb{N}$ blocks ahead of its pursuers.
For the consensus protocol to be viable, nodes must be incentivized to follow the LCR.\\

\noindent This chapter is organized as follows. \cref{sec:voting} gives a brief description of the voting based ways to get consensus by reviewing the "generals" problem. \cref{sec:leader} goes through the leader based consensus protocols, including \PoW in \cref{ssec:pow}, \PoSp in \cref{ssec:posp}, \PoI in \cref{ssec:poi}, and \PoS in \cref{ssec:pos}. For an exhaustive list of the existing protocols the reader is referred to \href{https://tokens-economy.gitbook.io/consensus/}{https://tokens-economy.gitbook.io/consensus/}.

\section{Voting system}\label{sec:voting}
The problem of reaching consensus in a peer-to-peer network via a majority vote has been abstractedly compared to generals who must agree on a common battle plan. We start from the simple two general case before moving on the the situation of interest with several ones.
\subsection{Two generals problem}
Two generals wish to attack a city but they must agree on a timing to attack a city. They communicate via a messenger who must cross enemy territory at the risk of being intercepted. The first general $G_1$ sends a message to the second one $G_2$ saying
$$
\texttt{"I will attack tomorrow at dawn"}
$$
For the attack to succeed, both generals must attack at the same time. Because their communication medium is unreliable, then $G_1$ must await confirmation from $G_2$ in order to attack. If $G_1$ does not receive confirmation then she will not attack. $G_2$ is aware of that and respond
$$
\texttt{"I will follow your lead"}
$$
$G_2$ does not know whether the message went through and must wait for confirmation. This creates an infinite loop of messages and response, as on \cref{fig:message_loop}.
\begin{figure}[ht!]
 \begin{center}
\begin{tikzpicture}[->, >=stealth', auto, semithick, node distance=3cm]
\tikzstyle{every state}=[fill=white,draw=black,thick,text=black,scale=0.8]
\node[state]    (1)                     {$G_1$};
\node[state]    (2)[right of=1]   {$G_2$};
\path
(1) edge[bend left]     node{Attack}     (2)
(2) edge[bend left]     node{Confirmation}      (1);
\end{tikzpicture}
\end{center}
\caption{Message and confirmation loop}
\label{fig:message_loop}
\end{figure}
The two general problem is deemed unsolvable from a theoretical point of view and corresponds to a situation where two nodes communicate through an unreliable link. A practical solution for generals is to send many messengers hoping that at least one of them will succeed. This is only a thought experiment leading to the several general problem.
\subsection{Byzantine General problem}
The blockchain network contains more than two nodes, these nodes must agree on the transactions to confirm. In a permissionless blockchain the nodes do not trust each other. The problem of the previous section generalizes to more than two generals, assuming that some generals are traitors which corresponds to faulty nodes in the network. This problem is referred to as The "Byzantine general problem" and was coined by \citet{lamport1982the}. Assume that $n>2$ generals must agree on a common battle plan for instance "Attack" (A) or "Retreat" (R) and that they can only communicate by two party messages. Denote by $m(i,j)$ the message sent by general $i$ to general $j$. Each general $j$ receives $n-1$ messages and applies a function $f$ to determine the course of action, for instance
$$
f(\{m(i,j);\text{ }i = 1,\ldots,n\}) = \begin{cases}
A,& \text{if }\sum_{i = 1}^n\mathbb{I}_{m(i,j) =A} >n/2,\\
R, &\text{else}.
\end{cases}
$$
If there are no traitors, each general is communicating the same value to all the peers and consensus is reached as in \cref{fig:no_traitor}. If one general is traitor, then he might not communicate the same value to all the generals and no consensus can be reached. It is the case for $G_4$ in \cref{fig:_one_traitor}.
\begin{figure}[!ht]
 \begin{center}
 \subfloat[No traitor]{
\begin{tikzpicture}[->, >=stealth', auto, semithick, node distance=2cm]
\tikzstyle{every state}=[fill=white,draw=black,thick,text=black,scale=0.8]
\node[state]    (1)                     {$G_1$};
\node[state]    (2)[below of=1]   {$G_2$};
\node[state]    (3)[below of=2]   {$G_3$};
\node[state]    (4)[below of=3]   {$G_4$};
\node[state]    (5)[below of=4]   {$G_5$};
\node[draw] (6) [right of=1]   {$f({A,R,R,A,A}) = A$};
\node[draw] (7) [right of=2]   {$f({A,R,R,A,A}) = A$};
\node[draw] (8) [right of=3]   {$f({A,R,R,A,A}) = A$};
\node[draw] (9) [right of=
4]   {$f({A,R,R,A,A}) = A$};
\node[draw] (10) [right of=5]   {$f({A,R,R,A,A}) = A$};
\path
(1) edge[bend left]     node{A}     (2)
(2) edge[bend left]     node{R}      (1)
(2) edge[bend left]     node{R}      (3)
(3) edge[bend left]     node{R}      (2)
(3) edge[bend left]     node{R}      (4)
(4) edge[bend left]     node{A}      (3)
(4) edge[bend left]     node{A}      (5)
(5) edge[bend left]     node{A}      (4)
;
\path
(1) edge     (6)
(2) edge     (7)
(3) edge     (8)
(4) edge     (9)
(5) edge     (10)
;
\end{tikzpicture}
\label{fig:no_traitor}}
\hskip2em
 \subfloat[One traitor]{
\begin{tikzpicture}[->, >=stealth', auto, semithick, node distance=2cm]
\tikzstyle{every state}=[fill=white,draw=black,thick,text=black,scale=0.8]
\node[state]    (1)                     {$G_1$};
\node[state]    (2)[below of=1]   {$G_2$};
\node[state]    (3)[below of=2]   {$G_3$};
\node[state]    (4)[below of=3]   {\color{red}$G_4$};
\node[state]    (5)[below of=4]   {$G_5$};
\node[draw] (6) [right of=1]   {$f({A,R,R,R,A}) = R$};
\node[draw] (7) [right of=2]   {$f({A,R,R,R,A}) = R$};
\node[draw] (8) [right of=3]   {$f({A,R,R,R,A}) = R$};
\node[draw] (9) [right of=
4]   {$f({A,R,R,?,A}) = ?$};
\node[draw] (10) [right of=5]   {$f({A,R,R,A,A}) = A$};
\path
(1) edge[bend left]     node{A}     (2)
(2) edge[bend left]     node{R}      (1)
(2) edge[bend left]     node{R}      (3)
(3) edge[bend left]     node{R}      (2)
(3) edge[bend left]     node{R}      (4)
(4) edge[bend left]     node{\color{red}R}      (3)
(4) edge[bend left]     node{\color{red}A}      (5)
(5) edge[bend left]     node{A}      (4)
;
\path
(1) edge     (6)
(2) edge     (7)
(3) edge     (8)
(4) edge     (9)
(5) edge     (10)
;
\end{tikzpicture}
\label{fig:_one_traitor}}
\end{center}
\caption{Majority vote with or without a traitor}
\label{fig:majority_vote}
\end{figure}
To handle such a situation, roles are given to the general. One of them become the leader and the other are the lieutenants. We aim at finding an algorithm such that
\begin{itemize}
	\item[C1] All the loyal lieutenants obey the same order
	\item[C2] If the commanding general is loyal, then  every loyal lieutenants obey the order he sends
\end{itemize}

A first result from \citet{lamport1982the} is the following
\begin{theo}
There are no solution to the Byzantine General problem for $n<3m+1$ generals where $m$ is the number of traitors.
\end{theo}
\begin{proof}
Consider the situation where $n = 3$ and $m = 1$. The traitor is either the commander or one of the lieutenants as shown in
\begin{figure}[!ht]
 \begin{center}
 \subfloat[Commander is loyal]{
\begin{tikzpicture}[->, >=stealth', auto, semithick, node distance=4cm]
\tikzstyle{every state}=[fill=white,draw=black,thick,text=black,scale=0.8]
\node[state]    (1)               {C};
\node[state]    (2)[below left of=1]   {L1};
\node[state]    (3)[below right of=1]   {\begin{color}{red}L2 \end{color}};
\path
(1) edge[bend left]     node{A}     (3)
(1) edge[bend right]     node{A}      (2)
(2) edge[bend left]     node{A}      (3)
(3) edge[bend left]     node{R}      (2)
% (3) edge[bend left]     node{R}      (4)
% (4) edge[bend left]     node{A}      (3)
% (4) edge[bend left]     node{A}      (5)
% (5) edge[bend left]     node{A}      (4)
;
\end{tikzpicture}
\label{fig:commander_loyal}}
\hskip2em
\subfloat[Commander is a traitor]{
\begin{tikzpicture}[->, >=stealth', auto, semithick, node distance=4cm]
\tikzstyle{every state}=[fill=white,draw=black,thick,text=black,scale=0.8]
\node[state]    (1)               {\begin{color}{red}C \end{color}};
\node[state]    (2)[below left of=1]   {L1};
\node[state]    (3)[below right of=1]   {L2};
\path
(1) edge[bend left]     node{A}     (3)
(1) edge[bend right]     node{R}      (2)
(2) edge[bend left]     node{R}      (3)
(3) edge[bend left]     node{A}      (2)
% (3) edge[bend left]     node{R}      (4)
% (4) edge[bend left]     node{A}      (3)
% (4) edge[bend left]     node{A}      (5)
% (5) edge[bend left]     node{A}      (4)
;
\end{tikzpicture}
\label{fig:commander_traitor}}
\end{center}
\caption{Majority vote with or without a traitor}
\label{fig:majority_vote}
\end{figure}
Unfortunately for Lieutenant 2, there is no way for her to tell apart the situation pictured in \cref{fig:commander_loyal} and \cref{fig:commander_traitor} and therefore no way to ensure both C1 and C2. We prove the result for $n>3$ by contradiction. Assume that there is a way to verify both C1 and C2 with $3<n<3m+1$. We then construct a solution with generals by having one general simulate the commander plus at most $m-1$ generals, and the other two simulating at most $m$ generals. One of the generals gather all the traitors and is therefore a traitor. The other two are loyal generals as they only simulate loyals general. We have built a solution with three generals that we know is impossible.
\end{proof}
Now we need an algorithm that allows $n>3m+1$ generals to deal with $m$ traitors. The 'Oral Message' algorithm denoted by $\text{OM}(m)$ and summarized in \cref{alg:om} can handle $m$ traitors if the number of generals verifies $n>3m+1$.
\begin{algorithm}[!ht]
\caption{The Oral message algorithm $\text{OM}(m)$}\label{alg:om}
\begin{algorithmic}[1]
\If{$m=0$};
\For{$i =1 \to n-1$}
\State Commander sends $v_i = v$ to lieutenant $i$
\State Lieutenant $i$ set their value to $v$
\EndFor
\EndIf
\If{$m>0$};
\For{$i =1 \to n-1$}
\State Commander sends $v_i$ to lieutenant $i$
\State Lieutenant $i$ uses OM(m-1) to communicate $v_i$ to the $n-2$ lieutenants
\EndFor
\For{$i =1 \to n-1$}
\State Lieutenant $i$ set their value to $f(v_1, \ldots, v_{n-1})$
\EndFor
\EndIf
\end{algorithmic}
\end{algorithm}
Before looking into the theoretical justification of $\text{OM}(m)$, let us illustrate the algorithm with an example.
\begin{ex}
Consider the situation where $n = 4$ and $m = 1$ shown in \cref{fig:OM_algorithm_illustration}.
\begin{figure}[!ht]
 \begin{center}
 \subfloat[Commander is loyal]{
\begin{tikzpicture}[->, >=stealth', auto, semithick, node distance=4cm]
\tikzstyle{every state}=[fill=white,draw=black,thick,text=black,scale=0.8]
\node[state]    (1)               {C};
\node[state]    (2)[below left of=1]   {L1};
\node[state]    (3)[below of=1]   {L2};
\node[state]    (4)[below right of=1]   {\begin{color}{red}L3 \end{color}};
\path
(1) edge[bend right]     node{A}     (2)
(1) edge[bend left]     node{A}      (4)
(1) edge     node{A}      (3)
(2) edge[bend right, below]     node{A}      (3)
(3) edge[bend right, above]     node{A}      (2)
(4) edge[bend right, above]     node{R}      (3)
(3) edge[bend right, below]     node{A}      (4)
;
\end{tikzpicture}
\label{fig:commander_loyal_om}}
\hskip2em
\subfloat[Commander is a traitor]{
\begin{tikzpicture}[->, >=stealth', auto, semithick, node distance=4cm]
\tikzstyle{every state}=[fill=white,draw=black,thick,text=black,scale=0.8]
\node[state]    (1)               {\begin{color}{red}C \end{color}};
\node[state]    (2)[below left of=1]   {L1};
\node[state]    (3)[below of=1]   {L2};
\node[state]    (4)[below right of=1]   {L3};
\path
(1) edge[bend right]     node{A}     (2)
(1) edge[bend left]     node{R}      (4)
(1) edge     node{R}      (3)
(2) edge[bend right, below]     node{A}      (3)
(3) edge[bend right, above]     node{R}      (2)
(4) edge[bend right, above]     node{R}      (3)
(3) edge[bend right, below]     node{R}      (4)
;
\end{tikzpicture}
\label{fig:commander_traitor_om}}
\end{center}
\caption{Illustration of the \text{OM}(m) algorithm in the case where $n = 4$ and $m=1$.}
\label{fig:OM_algorithm_illustration}
\end{figure}
If the commander is loyal then one of the lieutenant is a traitor, see \cref{fig:commander_loyal_om}. The commander gives the order to attack to all the lieutenant $3$ tells the other that she heard retreat from the commander. The loyal lieutenants then apply the map $f$ to agree on their value
$$
f(A,A,R) = A,
$$
which coresponds to the order the commander sent, hence IC1 and IC2 are satisfied. If the commander is a traitor as in \cref{fig:commander_traitor_om}, then he sends conflicting order to the lieutenant but after communicating the value they received to each other finally agree on the following value
$$
f(A,R,R) = R,
$$
hence IC1 is satisfied and IC2 can be ignored since the commander is a traitor.
\end{ex}
\begin{theo}
Algorithm $\text{OM}(m)$ satisfies conditions IC1 and IC2 if $n>3m+1$.
\end{theo}
\begin{proof}
The proof follows from simple induction.\\

\noindent First assume that the commander is loyal. For $m = 0$, the commanders simply sends the value $v$ to all the lieutenants and IC2 holds. Assume that $\text{OM}(m-1)$ works when the commader is loyal. The commander sends $v$ to all the lieutenants. The lieutenants then applies $OM(m-1)$. Because $n-1>2k + m-1$, then it follows from the induction hypothesis that each loyal lieutenants get the value $v$ for each of the loyal lieutenants $j$. The loyal lieutenants $n-1-m > 2k-1>m $ outnumber the traitorous lieutenants and therefore set their value to
$$
f(v_1,\ldots, v_{n-1}) = v,
$$
and both IC1 and IC follow.\\

\noindent Let us assume that the commander is a traitor, we only have to worry about IC1 in that case. There are at most $m$ traitors and the commander is one of them. We therefore have $m-1$ traitors among the lieutenants. Since the total number of lieutenants exceeds three times the number of traitors $n-1>3m>3(m-1)$ then by applying $\text{OM}(m-1)$ all the loyal lieutenants receive the same vector of values $v_1,\ldots, v_{n-1}$, agree on the same value
$$
f(v_1,\ldots,v_{n-1}) =v,
$$
which leads to the verification of IC1.
\end{proof}
The main problem associated to this Oral message algorithm is the number of messages is $n^{m+1}$ which is prohibitive for large values of $n$ and $m$. A celebrated algorithm, called Practical Byzantine Fault Tolerance (PBFT) has been developped later on by \citet{10.5555/296806.296824} but still not fast enough to enable the infinite growth of the network associated to public and permissionless blockchains.
\section{Leader system}\label{sec:leader}
The scalability issue can be solved by opting for a leader based mechanism instead of a majority vote mechanism. The protocols presented in this section use computational power, storage and bandwidth to elect a leader each time a new block must be appended to the blockchain.
\subsection{Proof-of-Work}\label{ssec:pow}
The bitcoin blockchain relies on a consensus protocol based on computational power called Proof-of-Work (\PoW), presented in \citet{Na08}. A block consists of
\begin{itemize}
\item a header
\item a list of "transactions" that represents the information recorded through the blockchain.
\end{itemize}
The header usually includes
\begin{itemize}
\item the date and time of creation of the block,
\item the block height which is the index inside the blockchain,
\item the hash of the block
\item the hash of the previous block.
\end{itemize}
The hash of a block is obtained by concatenating the header and the transactions in a large character string thus forming a "message" denoted by $m$, to which a hash function $h$ is applied.
\begin{definition}
A hash function is a function that can map data of arbitrary size to fixed-sized values,
$$
h:\{0,1\}^\ast\mapsto \{0,1\}^d
$$
\end{definition}
The hash functions used in blockchain applications must be cryptographic, i.e.\
\begin{itemize}
\item quick to compute
\item one way
\item deterministic
\end{itemize}
\begin{remark}
It must be nearly infeasible to generate a message with a given hash value or to find two messages with the same hash value. A small change in the message should change dramatically the hash value so that the new hash value appears to be uncorrelated to the previous hash,
$$
\text{if }m_1\approx m_2\text{ then }h(m_1)\neq h(m_2).
$$
We will not expand on how to build such a cryptographic hash function, we refer the interested reader to the work of \citet{cryptoeprint:2011:565}.
\end{remark}
In the bitcoin blockchain as well as in many other applications, the standard is the SHA-256 function which converts any message into a hash value of $256$ bits. The latter is usually translated into a hexadecimal digest, for instance the hash value of the title of the present manuscript reads as
$$
\texttt{98b1146926548f6b57c4347457713ff2f035beda9c93f12fbc9b202e9c512e80}.
$$
Mining a block means finding a block hash value lower than some target which can only be achieved by brute force search thanks to the properties of cryptographic hash functions. In practice, the search for an appropriate hash value, referred to as a solution, is done by appending a nonce to the block message before applying the hash function. A nonce is a $32$ bits number, drawn at random by miners until a nonce resulting in a proper block hash value is found. For illustration, consider the block in Figure \ref{fig:block_not_mined}.

\begin{figure}[!ht]
		\includegraphics[width = \textwidth]{../Figures/block_not_mined.png}
		\captionsetup{width=0.8\textwidth}
		\centering
		\caption{A block that has not been mined yet.}
		\label{fig:block_not_mined}
\end{figure}
The hash value in decimal notation is $1.43e^{76}$ while the maximum value for a $256$ bits number is $2^{256}-1 \approx 1.16e^{77}$. We refer to the latter as the maximal target and denote it by $T_{\max}$. The Proof-of-Work protocol sets a target $T < T_{\max}$ and ask miners to find a nonce such that the hash value of the block is smaller than $T$. Practitioners would rather talk about the \textit{difficulty} which is defined as $D = T_{\max} / T$. If the difficulty is one, any hash value is acceptable. Increasing the difficulty reduces the set of allowable hash values, making the problem harder to solve. A hash value is then called \textit{acceptable} if its hexadecimal digest starts with a given number of zeros. If we set the difficulty to $2^4$, then the hexadecimal digest of the hash of the block must start with at least $1$ leading zero, making the hash value of the block in Figure \ref{fig:block_not_mined} not acceptable. After completing the nonce search we get the block in Figure \ref{fig:block_mined}.
\begin{figure}[!ht]
		\includegraphics[width = \textwidth]{../Figures/block_mined.png}
		\captionsetup{width=0.8\textwidth}
		\centering
		\caption{A mined block with a hash value having on leading zero.}
		\label{fig:block_mined}
\end{figure}
Note that it took $5$ attempts to find this nonce. The number of needed trials is geometrically distributed with parameter $1 / D$, which means that with a difficulty of $D = 2^4$ it takes on average $16$ trials. The protocol adjusts the difficulty automatically every $2,016$ block discoveries so as to (globally) maintain one block discovery every $10$ minutes on average. The time between two block discoveries depends on the number of hash values computed by the network at a given instant. At the time of writing, the network computes $182.58$ Exahashes per second and the difficulty is $27,967,152,532,434$.\footnote{Source: \href{https://www.bitcoinblockhalf.com/}{bitcoinblockhalf.com}} For an exhaustive overview of the mining process in the bitcoin blockchain, we refer the reader to the book of \citet[Chapter 10]{Antonopoulos2017}. As each trial (of the system) for mining a block is
independent of the others and leads to a success with very small probability, the overall
number of successes is binomially distributed and will be very well approximated by a
Poisson random variable. This justifies the Poisson process assumption made in the sequel to model the block arrival and the reward collecting processes. Empirical studies of the block inter-arrival times data tend to confirm this hypothesis, see the work of Bowden et al.\ \citet{Bowden2020}. The information recorded in a public blockchain may be retrieved by anyone and can be accessed through a blockchain explorer such as \href{https://www.blockchain.com/}{blockchain.com}, the content of the block of height $\#724724$ may be viewed through the following link \href{https://www.blockchain.com/btc/block/0000000000000000000954d42e8ced7017448cb9f39b364e371a1eec6e34463b}{block content}.\\

\noindent The \PoW protocol implies that the nodes are running computations 24/7 therefore consuming humungous quantity of electricity. Bitcoin mining originally started by running computations using the Central Processing Unit (CPU). It turns out that certain kinds of computation are more efficient on Graphics Processing Unit (GPU) than on CPUs. CPU is designed to complete a wide variety of task while computing hashes is very specific. GPU are tailored to run thousands of computation of the same type. Miners then turned to GPUs leading to a shortage of graphics card at the expense of PC gamers around the world! Eventually GPU got replaced by Application Specific Integrated Circuits (ASICs) that are designed to complete very specific task compared to graphic cards. ASICs consumes 10 times more power than graphic cards but compute 10,000 more hashes than a graphic card per time unit. Miners then decided to equip themselves with ASIC chips leading to harmful consequences
\begin{itemize}
    \item Increase of the network electricity consumption
    \item Increase in the e-waste generation. ASICs are single purpose and it cannot be repurpose for any other task. When ASICs become obsolete with the arrival of a new generation of chips, they are thrown in the trash.
    \item The main manufacturer of ASICs is a company called \href{https://www.bitmain.com/}{BITMAIN} which equips major mining pool such as \href{https://v3.antpool.com/home}{Antpool} and \href{https://btc.com}{BTC.com}. A threat on centralization exists since a company like BITMAIN could take control of the network by owning more than half of the overall hashpower.
\end{itemize}
A pro-ASIC argument is that it would be impossible for anyone (apart from BITMAIN) to suddenly acquire enough of these chips to have more than half of the world's hash power.
\subsection{Proof-of-SpaceTime and Proof-of-Capacity}\label{ssec:posp}
Consensus protocol based on storage capacities are seen by many as a fairer and greener alternative to \PoW. We describe below two such protocols \textit{Proof-of-Capacity} and \textit{Proof-of-Spacetime}.
\subsubsection{Proof-of-Capacity}
In the \textit{Proof-of-Capacity}, miners compute hashes and cache the result on their hard disk space. Mining then only requires to search through the cache for an admissible solution.
\subsubsection{Proof-of-Spacetime}
In the \textit{Proof-of-Spacetime}, the nodes store data and produce proofs to show that the data has been stored for a given time period. The probability of a node being chosen is proportional to the amount of data stored. This protocol has been designed for a specific application allowing nodes to provide storage to clients through the \href{https://filecoin.io/}{Filecoin project}.\\

\noindent To some extent the \textit{Proof-of-Capacity} protocol is similar to \PoW while the \textit{Proof-of-Spacetime} shares similarities with the \textit{Proof-of-Stake} protocol which is discussed below.\\

\noindent Such protocols do not generate ewaste because disk space can always be used for some other purpose. Storing data is less energy consuming than computing hashes. The problem of hiring external storage capacities from provider remains.
\subsection{Proof-of-Interaction}\label{ssec:poi}
The \textit{Proof-of-Interaction} protocol, introduced by \citet{Abegg2021}, asks each validating node to get in touch with a sequence of nodes. The number of nodes and the nodes to be contacted are drawn randomly so that the time to complete the task is also varying from one node to another. The block reward is shared by the contacting and responding nodes to create an incentive compatible environment. If we assume that the time required to complete the task is exponentially distributed then the time to generate a new block is the minimum of exponential random variables which is again exponentially distributed.
\PoI is still in the developping phase and many interesting work must be done to assess the security and viability of such protocol. Some nodes may indeed collude to send replies faster or not to send replies to some node. It is necessary to evaluate the probability and the opportunity for the nodes to collude.
\subsection{Proof-of-Stake}\label{ssec:pos}
Besides bandwidth, computing power and storage, one ressource that appears with the advent of cryptocurrencies as medium-of-exchange and store-of-value asset are the cryptocoins. Each time a block must be appended to the blockchain, a coin is drawn at random. The owner of that coin appends a new block and collect the reward. \\

\noindent Let the network be of size $N$. We denote by $\pi_i^t$ the proportion of coins owned by node $i\in\{1,\ldots,n\}$ at time $t\in\mathbb{N}$. Note that $\pi_i^t$ is exactly the probability of node $i$ being elected as leader at time $t$, we have
$$
\begin{cases}
\pi_i^t>0,\\
\sum_{i=1}^N\pi_i^t = 1,
\end{cases}
\text{ for } t>0.
$$
Denote by $S_t$ the total number of coins in circulation at time $t$ and by $R_t$ the size of the reward for appending a new block at time $t$. Let $A_i^t$ be the event of node $i$ appending a block at time $t$, the share of coins then evolves as
$$
\pi_i^t = \frac{S\cdot \pi_i^0 + \sum_{s=1}^{t-1}\mathbb{I}_{A_i^t}R_s}{S+\sum_{s=1}^{t-1}R_s},
$$
where
$$
\mathbb{I}_{A} = \begin{cases}1& \text{if A occurs},\\
0&\text{otherwise}.
\end{cases}
$$
Two potential issues needs to be studied
\begin{itemize}
    \item The Nothing-at-Stake (NaS) problem: If a a fork is ongoing then each branch will elect a leader who will append a block, collect the reward and perpetuate the disabreement.
    \item The rich get richer problem: When a node is chosen, it becomes richer which increase its likelihood to be chosen in future rounds.
\end{itemize}
The "rich get richer" problem will be extensively studied in \cref{chap:decentralization}. Regarding the NaS problem, the nodes when chosen by a branch decide whether they want to add a block, it is an option. The cryptocoin value comes from its use as a medium of exchange. A long lasting disagreement results in a useless cryptocoin with no value.\\

\noindent Let $\tau$ be the duration of the fork and let $\delta\in(0,1)$ be a discount rate, then the present value of a coin at $\tau$ is $1/(1+\delta)^\tau$. If $\mathbb{P}(\tau=\infty)>0$ then the coin value is zero when taking the expectation.\\

\noindent The nodes are therefore incentivized to follow the Longest chain rule in order to resolve the fork situation as soon as possible. This is essentially the rationale in \citet{Saleh2020} to show that
\begin{itemize}
    \item The coin value reaches a maximum if all the nodes follow the longest chain rule
    \item There exists an equilibrium in which the nodes follow the LCR if
    $$
    \min \pi_i^0 \cdot S\geq\frac{R_0}{\delta(1-\delta)^2},
    $$
    which corresponds to a minimum stake condition.
    \item If $\sum_t R_t < \infty$ then there exist no equilibrium for which
    $$
    \mathbb{P}(\tau=\infty)>0.
    $$
    A modest reward schedule precludes the posssibility of an ever lasting fork.
\end{itemize}
A practical implementation of the \PoS protocol to create a cryptocurrency is \href{https://www.peercoin.net/}{PeerCoin}, see the white paper by \citet{ppcoin}. The notion of coin age is introduced, the stake is actually defined by the number of coins times the number of time period during which the coins was hold. When a peer finds a block, a \textit{coinstake} transaction is made that transfers the node its own coin to reset the coin age to zero.


\documentclass{article}

\usepackage{color}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{geometry}
\usepackage{setspace}
\usepackage{indentfirst}
\usepackage{listings}
\usepackage{xcolor}
\lstset{
    columns=fixed,
    numbers=left,
    frame=none,
    backgroundcolor=\color[RGB]{245,245,244},
    keywordstyle=\color[RGB]{40,40,255},
    numberstyle=\footnotesize\color{darkgray},
    commentstyle=\it\color[RGB]{0,96,96},
    stringstyle=\rmfamily\slshape\color[RGB]{128,0,0},
    showstringspaces=false,
    language=c++,
}
\geometry{left=2cm,right=2cm,top=2cm,bottom=2cm}
\begin{document}
\begin{spacing}{2.0}
\vspace*{0.25cm}

\hrulefill

\thispagestyle{empty}

\begin{center}
\begin{large}
\sc{UM--SJTU Joint Institute \vspace{0.3em} \\ Data Structures and Algorithms \\(VE281)}
\end{large}

\hrulefill

\vspace*{5cm}
\begin{Large}
\sc{{Homework 2}}
\end{Large}

\vspace{2em}

\end{center}

\vfill

\begin{table}[h!]
\flushleft
\begin{tabular}{lll}
Name: Ji Xingyou \hspace*{2em}&
ID: 515370910197\hspace*{2em}
\\

Date: 10 October 2017

\end{tabular}
\end{table}

\hfill

\newpage
\tableofcontents
\newpage
\section{Theoretical Data}
\indent As is discussed in the class, we can get the following table summarizing the time complexity for each algorithms.

\begin{table}[!hbp]
\centering
\begin{tabular}{ccccc}
&Worst case complexity&Average case complexity&In place&Stable\\
\hline
\hline
Quick sort in place&$O(N^2)$&$O(NlogN)$&Yes&No\\
\hline
Rselect&$\Theta(n^2)$&$O(N)$&Yes&Yes\\
\hline
Dselect&&$O(N)$&No&Yes\\
\hline
\end{tabular}
\caption{Time complexity of comparison sorting}
\end{table}

In this report, we will first implement all these three algorithms, and then test the run time for each of them to see whether the above table makes sense.

The implementation of the algorithms is attached in the appendix.
\section{Result Analysis}
\indent After finishing implementing the above three algorithms, I wrote another program to test the run time of each algorithm. In this program, I set two clocks, noting the starting and finishing instance. To avoid uncertainty in the data, I wrote a while loop to create 10 different i each time so that I could get the average value. There is one thing which requires extra carefulness. That is, we must ensure that every time inside the while loop, all the six selection should meet the identical array.

The array size I chose is 10, 100, 1000, 5000, 10000, 50000, 100000 and 1000000.

The run time data for each algorithms is listed in table 2.
\begin{table}[!h]
\centering
\begin{tabular}{c|cccccc}
Array size&Quick sort in place&Rselect&Dselect\\
\hline
\hline
10&7&3&9\\
\hline
100&57&15&69\\
\hline
1000&983&191&1089\\
\hline
5000&4701&832&3921\\
\hline
10000&12018&1944&8860\\
\hline
50000&54800&6622&32207\\
\hline
100000&106287&12056&61333\\
\hline
1000000&1180554&113349&550122\\
\hline
\end{tabular}
\caption{Run time of linear time selection}
\end{table}

The run time comparison is shown in figure 1.
\begin{figure}[h!]
\begin{center}
\includegraphics[scale=1]{comparision.eps}
\caption{Run time comparison}
\end{center}
\end{figure}

Combining the table and figure above, we can conclude the following points.
\begin{enumerate}
\item For each linear time selection algorithm, the run time increases as the size of the array increases.\\
\item For any given array size, Rselect always has a leading performance, while Dselect ranks second and quick sort third.
\end{enumerate}

From the conclusion listed above, we can see that it fits the time complexity shown in table 1, which means the algorithms make sense.

This inspires me that in future learning, when dealing with selection, I should use Rselect as often as possible since it has the best performance.
\section{Appendix}
\subsection{Linear time selection algorithms}
\begin{lstlisting}[language=c++]
//
//  main.cpp
//  project2
//
//  Created by 季星佑 on 2017/9/27.
//  Copyright © 2017年 季星佑. All rights reserved.
//

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <cassert>

using namespace std;

int Rselect(int *arr,int n,int i);

int Dselect(int *arr,int n,int i);

int partition(int *arr,int left,int right,int size,int pivotat);

void selection_sort(int *arr,int n);
//////////////////////////////
//////////////////////////////
int main(int argc, const char * argv[])
{
    int choice;
    cin>>choice;
    int n;
    cin>>n;
    if(n==0)
    {
        return 0;
    }
    int i;
    cin>>i;
    if(i>=n||i<0)
    {
        return 0;
    }
    int a[n];
    for(int t=0;t<n;++t)
    {
        cin>>a[t];
    }
    int *arr=a;
    int result=0;
    switch(choice)
    {
        case 0:
            result=Rselect(arr,n,i);
            break;
        case 1:
            result=Dselect(arr,n,i);
        default:
            break;
    }
    cout<<"The order-"<<i<<" item is "<<result<<endl;
//    srand(time(0));
//    int times=100;
//    while(times--)
//    {
//        int size=100;
//        int index=rand()%size;
//        int *arr=new int [size];
//        int brr[size];
//        for(int i=0;i<size;++i)
//        {
//            arr[i]=rand()%size;
//        }
//        for(int i=0;i<size;++i)
//        {
//            brr[i]=arr[i];
//        }
//        sort(arr,arr+size);
//        int p=arr[index];
//        int q=Rselect(brr,size,index);
//        cout<<p-q<<endl;
//        delete [] arr;
//    }
    return 0;
}
//////////////////////////////
//////////////////////////////
int Rselect(int *arr,int n,int i)
{
    if(n==1)
    {
        return *arr;
    }
    int pivotat=rand()%n;
    pivotat=partition(arr,0,n-1,n,pivotat);
    if(pivotat==i)
    {
        return arr[pivotat];
    }
    else if(pivotat>i)
    {
        return Rselect(arr,pivotat,i);
    }
    else
    {
        return Rselect(arr+pivotat+1,n-pivotat-1,i-pivotat-1);
    }
}
//////////////////////////////
//////////////////////////////
int Dselect(int *arr,int n,int i)
{
    if(n<=1)
    {
        return *arr;
    }
    int group_num=n/5;
    int last=n%5;
    if(last==0)
    {
        last=5;
    }
    else
    {
        group_num++;
    }
    int *C=new int[group_num];
    int *temp=new int[5];
    int size=0;
    for(int p=0;p<group_num;++p)
    {
        if(p==group_num-1)
        {
            size=last;
        }
        else
        {
            size=5;
        }
        for(int q=0;q<size;++q)
        {
            temp[q]=arr[q+5*p];
        }
        selection_sort(temp,size);
        C[p]=temp[size/2];
    }
    int pivot=Dselect(C,n/5,n/10);
    delete [] C;
    delete [] temp;
    int pivotat=0;
    for(int p=0;p<n;++p)
    {
        if(arr[p]==pivot)
        {
            pivotat=p;
        }
    }
    pivotat=partition(arr,0,n-1,n,pivotat);
    if(pivotat==i)
    {
        return arr[pivotat];
    }
    else if(pivotat>i)
    {
        return Dselect(arr,pivotat,i);
    }
    else
    {
        return Dselect(arr+pivotat+1,n-pivotat-1,i-pivotat-1);
    }
}
//////////////////////////////
//////////////////////////////
int partition(int *arr,int left,int right,int size,int pivotat)
{
    swap(arr[0],arr[pivotat]);
    int i=1;
    int j=size-1;
    while(true)
    {
        while(i<size-1&&arr[i]<arr[0])
        {
            ++i;
        }
        while(j>0&&arr[j]>=arr[0])
        {
            --j;
        }
        if(i<j)
        {
            swap(arr[i],arr[j]);
        }
        else
        {
            break;
        }
    }
    swap(arr[0],arr[j]);
    return j;
}
//////////////////////////////
//////////////////////////////
void selection_sort(int *arr,int n)
{
    for(int i=0;i<n-1;++i)
    {
        int index=i;
        for(int j=i+1;j<n;++j)
        {
            if(arr[j]<arr[index])
            {
                index=j;
            }
        }
        if(index!=i)
        {
            int tmp=arr[index];
            arr[index]=arr[i];
            arr[i]=tmp;
        }
    }
}

\end{lstlisting}
\subsection{Run-time calculation}
\begin{lstlisting}[language=c++]
//
//  main.cpp
//  run_time_study
//
//  Created by 季星佑 on 2017/10/9.
//  Copyright © 2017年 季星佑. All rights reserved.
//

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <cassert>

using namespace std;

int Rselect(int *arr,int n,int i);

int Dselect(int *arr,int n,int i);

int partition(int *arr,int left,int right,int size,int pivotat);

void selection_sort(int *arr,int n);

void quick_sort_in_place(int *arr,int left,int right,int size);
//////////////////////////////
//////////////////////////////
int main(int argc, const char * argv[])
{
    int lines=1000000;
    srand(time(0));
    int t=10;
    long temp0=0;
    long temp1=0;
    long temp2=0;
    long time0=0;
    long time1=0;
    long time2=0;
    clock_t start,finish;
    int arr[lines];
    for(int i=0;i<lines;++i)
    {
        arr[i]=rand();
    }
    int brr[lines];
    for(int j=0;j<lines;++j)
    {
        brr[j]=arr[j];
    }
    //////////////////////////////
    while(t--){
    int index=rand()%lines;
    start=clock();
    for(int a=0;a<lines;++a)
    {
        arr[a]=brr[a];
    }
    quick_sort_in_place(arr,0,lines-1,lines);
    int p=arr[index];
    finish=clock();
    long t0=finish-start;
    temp0+=t0;
    //////////////////////////////
    start=clock();
    for(int a=0;a<lines;++a)
    {
        arr[a]=brr[a];
    }
    Rselect(arr,lines,index);
    finish=clock();
    long t1=finish-start;
    temp1+=t1;
    //////////////////////////////
    start=clock();
    for(int a=0;a<lines;++a)
    {
        arr[a]=brr[a];
    }
    Dselect(arr,lines,index);
    finish=clock();
    long t2=finish-start;
    temp2+=t2;
    //////////////////////////////
    time0+=temp0/1;
    time1+=temp1/1;
    time2+=temp2/1;
    }
    cout<<time0/10<<endl;
    cout<<time1/10<<endl;
    cout<<time2/10<<endl;
    return 0;
}
//////////////////////////////
//////////////////////////////
int Rselect(int *arr,int n,int i)
{
    if(n==1)
    {
        return *arr;
    }
    int pivotat=rand()%n;
    pivotat=partition(arr,0,n-1,n,pivotat);
    if(pivotat==i)
    {
        return arr[pivotat];
    }
    else if(pivotat>i)
    {
        return Rselect(arr,pivotat,i);
    }
    else
    {
        return Rselect(arr+pivotat+1,n-pivotat-1,i-pivotat-1);
    }
}
//////////////////////////////
//////////////////////////////
int Dselect(int *arr,int n,int i)
{
    if(n<=1)
    {
        return *arr;
    }
    int group_num=n/5;
    int last=n%5;
    if(last==0)
    {
        last=5;
    }
    else
    {
        group_num++;
    }
    int *C=new int[group_num];
    int *temp=new int[5];
    int size=0;
    for(int p=0;p<group_num;++p)
    {
        if(p==group_num-1)
        {
            size=last;
        }
        else
        {
            size=5;
        }
        for(int q=0;q<size;++q)
        {
            temp[q]=arr[q+5*p];
        }
        selection_sort(temp,size);
        C[p]=temp[size/2];
    }
    int pivot=Dselect(C,n/5,n/10);
    delete [] C;
    delete [] temp;
    int pivotat=0;
    for(int p=0;p<n;++p)
    {
        if(arr[p]==pivot)
        {
            pivotat=p;
        }
    }
    pivotat=partition(arr,0,n-1,n,pivotat);
    if(pivotat==i)
    {
        return arr[pivotat];
    }
    else if(pivotat>i)
    {
        return Dselect(arr,pivotat,i);
    }
    else
    {
        return Dselect(arr+pivotat+1,n-pivotat-1,i-pivotat-1);
    }
}
//////////////////////////////
//////////////////////////////
int partition(int *arr,int left,int right,int size,int pivotat)
{
    swap(arr[0],arr[pivotat]);
    int i=1;
    int j=size-1;
    while(true)
    {
        while(i<size-1&&arr[i]<arr[0])
        {
            ++i;
        }
        while(j>0&&arr[j]>=arr[0])
        {
            --j;
        }
        if(i<j)
        {
            swap(arr[i],arr[j]);
        }
        else
        {
            break;
        }
    }
    swap(arr[0],arr[j]);
    return j;
}
//////////////////////////////
//////////////////////////////
void selection_sort(int *arr,int n)
{
    for(int i=0;i<n-1;++i)
    {
        int index=i;
        for(int j=i+1;j<n;++j)
        {
            if(arr[j]<arr[index])
            {
                index=j;
            }
        }
        if(index!=i)
        {
            int tmp=arr[index];
            arr[index]=arr[i];
            arr[i]=tmp;
        }
    }
}

void quick_sort_in_place(int *arr,int left,int right,int size)
{
    if(size==0)
    {
        return;
    }
    int pivotat=rand()%size;
    if(left>=right)
    {
        return;
    }
    pivotat=partition(arr,left,right,size,pivotat);
    quick_sort_in_place(arr,left,pivotat-1,pivotat-left);
    quick_sort_in_place(arr+pivotat+1,0,right-pivotat-1,right-pivotat);
}

\end{lstlisting}
\subsection{Visualization}
\begin{lstlisting}[language=Matlab]
clear all;clc;
t0=[7 57 983 4701 12018 54800 106287 1180554];
t1=[3 15 191 832 1944 6622 12056 113349];
t2=[9 69 1089 3921 8860 32207 61333 550122];
size=[10 100 1000 5000 10000 50000 100000 1000000];
plot(size,t0,'o-',size,t1,'v-',size,t2,'*-');
xlabel('array size');
ylabel('speed^{-1}');
legend('quick\_sort\_in\_place','rselection\_sort','dselection\_sort');
\end{lstlisting}
\end{spacing}
\end{document}


\documentclass[12pt]{article}
% We can write notes using the percent symbol!
% The first line above is to announce we are beginning a document, an article in this case, and we want the default font size to be 12pt
\usepackage[utf8]{inputenc}
% This is a package to accept utf8 input.  I normally do not use it in my documents, but it was here by default in Overleaf.
\usepackage{pgfplots}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{color}
% These three packages are from the American Mathematical Society and includes all of the important symbols and operations
\usepackage{fullpage}
% By default, an article has some vary large margins to fit the smaller page format.  This allows us to use more standard margins.

\setlength{\parskip}{1em}
% This gives us a full line break when we write a new paragraph

\newcommand\w{\vec{w}}
\newcommand\wt{\vec{w}^T}
\newcommand\msb{S_B}
\newcommand\msw{S_W}

\begin{document}

	\title{%
		Homework for Math Structures \\
		\large Monday, Week 1
	}
	\author{Zhihan Yang}
	\maketitle

	\section{1.5 Question 4a}

	Propositions:
	\begin{itemize}
		\item P: Sales go up.
		\item Q: Expenses go up.
		\item R: Boss is happy.
	\end{itemize}

	Premises:
	\begin{itemize}
		\item Either sales or expenses will go up: $P \lor Q$.
		\item If sales go up, then the boss will be happy: $P \rightarrow R$.
		\item If expenses go up, then the boss will be unhappy: $Q \rightarrow \lnot R$.
	\end{itemize}

	Argument: Sales and expenses will not both go up: $\lnot(P \land Q)$.

	\begin{tabular}{ c  c  c | c  c  c  c  c  c  c  c  c  c  c  c  c  c  c  c  c  c  c  c  c  c | c  c  c  c  c  c }
		p & q & r & ( & ( & ( & p & $\lor$ & q & ) & $\&$ & ( & p & $\rightarrow$ & r & ) & ) & $\&$ & ( & q & $\rightarrow$ & $\sim$ & r & ) & ) & $\sim$ & ( & p & $\&$ & q & )\\
		\hline
		$\top$ & $\top$ & $\top$ &  &  &  & $\top$ & $\top$ & $\top$ &  & $\top$ &  & $\top$ & $\top$ & $\top$ &  &  & \textcolor{red}{$\perp$} &  & $\top$ & $\perp$ & $\perp$ & $\top$ &  &  & \textcolor{red}{$\perp$} &  & $\top$ & $\top$ & $\top$ & \\
		$\top$ & $\top$ & $\perp$ &  &  &  & $\top$ & $\top$ & $\top$ &  & $\perp$ &  & $\top$ & $\perp$ & $\perp$ &  &  & \textcolor{red}{$\perp$} &  & $\top$ & $\top$ & $\top$ & $\perp$ &  &  & \textcolor{red}{$\perp$} &  & $\top$ & $\top$ & $\top$ & \\
		$\top$ & $\perp$ & $\top$ &  &  &  & $\top$ & $\top$ & $\perp$ &  & $\top$ &  & $\top$ & $\top$ & $\top$ &  &  & \textcolor{red}{$\top$} &  & $\perp$ & $\top$ & $\perp$ & $\top$ &  &  & \textcolor{red}{$\top$} &  & $\top$ & $\perp$ & $\perp$ & \\
		$\top$ & $\perp$ & $\perp$ &  &  &  & $\top$ & $\top$ & $\perp$ &  & $\perp$ &  & $\top$ & $\perp$ & $\perp$ &  &  & \textcolor{red}{$\perp$} &  & $\perp$ & $\top$ & $\top$ & $\perp$ &  &  & \textcolor{red}{$\top$} &  & $\top$ & $\perp$ & $\perp$ & \\
		$\perp$ & $\top$ & $\top$ &  &  &  & $\perp$ & $\top$ & $\top$ &  & $\top$ &  & $\perp$ & $\top$ & $\top$ &  &  & \textcolor{red}{$\perp$} &  & $\top$ & $\perp$ & $\perp$ & $\top$ &  &  & \textcolor{red}{$\top$} &  & $\perp$ & $\perp$ & $\top$ & \\
		$\perp$ & $\top$ & $\perp$ &  &  &  & $\perp$ & $\top$ & $\top$ &  & $\top$ &  & $\perp$ & $\top$ & $\perp$ &  &  & \textcolor{red}{$\top$} &  & $\top$ & $\top$ & $\top$ & $\perp$ &  &  & \textcolor{red}{$\top$} &  & $\perp$ & $\perp$ & $\top$ & \\
		$\perp$ & $\perp$ & $\top$ &  &  &  & $\perp$ & $\perp$ & $\perp$ &  & $\perp$ &  & $\perp$ & $\top$ & $\top$ &  &  & \textcolor{red}{$\perp$} &  & $\perp$ & $\top$ & $\perp$ & $\top$ &  &  & \textcolor{red}{$\top$} &  & $\perp$ & $\perp$ & $\perp$ & \\
		$\perp$ & $\perp$ & $\perp$ &  &  &  & $\perp$ & $\perp$ & $\perp$ &  & $\perp$ &  & $\perp$ & $\top$ & $\perp$ &  &  & \textcolor{red}{$\perp$} &  & $\perp$ & $\top$ & $\top$ & $\perp$ &  &  & \textcolor{red}{$\top$} &  & $\perp$ & $\perp$ & $\perp$ & \\
	\end{tabular}

	The 4th column shows the truth values for the 3 premises to be right at the same time. As we can see, when all premises are true (row 3 and 6), the argument is always true. Therefore, the argument is valid.

	\section{1.5 Question 4b}

	Propositions:
	\begin{itemize}
		\item P: Tax rate goes up.
		\item Q: Unemployment rate goes up.
		\item R: There is a recession.
		\item S: GDP goes up.
	\end{itemize}

	Premises:
	\begin{itemize}
		\item If the tax rate and the employment rate both go up, then there will be a recession: $P \land Q \rightarrow R$.
		\item If the GDP goes up, then there will not be a recession: $S \rightarrow \lnot R$.
		\item The GDP and taxes are both going up: $S \land P$.
	\end{itemize}

	Argument: The employment rate is not going up: $\lnot Q$.

	\begin{tabular}{ c  c  c  c | c  c  c  c  c  c  c  c  c | c  c  c  c  c  c | c  c  c  c  c | c  c }

		P & Q & R & S & ( & ( & P & $\&$ & Q & ) & $\rightarrow$ & R & ) & ( & S & $\rightarrow$ & $\sim$ & R & ) & ( & S & $\&$ & P & ) & $\sim$ & Q\\
		\hline
		$\top$ & $\top$ & $\top$ & $\top$ &  &  & $\top$ & $\top$ & $\top$ &  & \textcolor{red}{$\top$} & $\top$ &  &  & $\top$ & \textcolor{red}{$\perp$} & $\perp$ & $\top$ &  &  & $\top$ & \textcolor{red}{$\top$} & $\top$ &  & \textcolor{red}{$\perp$} & $\top$\\
		$\top$ & $\top$ & $\top$ & $\perp$ &  &  & $\top$ & $\top$ & $\top$ &  & \textcolor{red}{$\top$} & $\top$ &  &  & $\perp$ & \textcolor{red}{$\top$} & $\perp$ & $\top$ &  &  & $\perp$ & \textcolor{red}{$\perp$} & $\top$ &  & \textcolor{red}{$\perp$} & $\top$\\
		$\top$ & $\top$ & $\perp$ & $\top$ &  &  & $\top$ & $\top$ & $\top$ &  & \textcolor{red}{$\perp$} & $\perp$ &  &  & $\top$ & \textcolor{red}{$\top$} & $\top$ & $\perp$ &  &  & $\top$ & \textcolor{red}{$\top$} & $\top$ &  & \textcolor{red}{$\perp$} & $\top$\\
		$\top$ & $\top$ & $\perp$ & $\perp$ &  &  & $\top$ & $\top$ & $\top$ &  & \textcolor{red}{$\perp$} & $\perp$ &  &  & $\perp$ & \textcolor{red}{$\top$} & $\top$ & $\perp$ &  &  & $\perp$ & \textcolor{red}{$\perp$} & $\top$ &  & \textcolor{red}{$\perp$} & $\top$\\
		$\top$ & $\perp$ & $\top$ & $\top$ &  &  & $\top$ & $\perp$ & $\perp$ &  & \textcolor{red}{$\top$} & $\top$ &  &  & $\top$ & \textcolor{red}{$\perp$} & $\perp$ & $\top$ &  &  & $\top$ & \textcolor{red}{$\top$} & $\top$ &  & \textcolor{red}{$\top$} & $\perp$\\
		$\top$ & $\perp$ & $\top$ & $\perp$ &  &  & $\top$ & $\perp$ & $\perp$ &  & \textcolor{red}{$\top$} & $\top$ &  &  & $\perp$ & \textcolor{red}{$\top$} & $\perp$ & $\top$ &  &  & $\perp$ & \textcolor{red}{$\perp$} & $\top$ &  & \textcolor{red}{$\top$} & $\perp$\\
		$\top$ & $\perp$ & $\perp$ & $\top$ &  &  & $\top$ & $\perp$ & $\perp$ &  & \textcolor{red}{$\top$} & $\perp$ &  &  & $\top$ & \textcolor{red}{$\top$} & $\top$ & $\perp$ &  &  & $\top$ & \textcolor{red}{$\top$} & $\top$ &  & \textcolor{red}{$\top$} & $\perp$\\
		$\top$ & $\perp$ & $\perp$ & $\perp$ &  &  & $\top$ & $\perp$ & $\perp$ &  & \textcolor{red}{$\top$} & $\perp$ &  &  & $\perp$ & \textcolor{red}{$\top$} & $\top$ & $\perp$ &  &  & $\perp$ & \textcolor{red}{$\perp$} & $\top$ &  & \textcolor{red}{$\top$} & $\perp$\\
		$\perp$ & $\top$ & $\top$ & $\top$ &  &  & $\perp$ & $\perp$ & $\top$ &  & \textcolor{red}{$\top$} & $\top$ &  &  & $\top$ & \textcolor{red}{$\perp$} & $\perp$ & $\top$ &  &  & $\top$ & \textcolor{red}{$\perp$} & $\perp$ &  & \textcolor{red}{$\perp$} & $\top$\\
		$\perp$ & $\top$ & $\top$ & $\perp$ &  &  & $\perp$ & $\perp$ & $\top$ &  & \textcolor{red}{$\top$} & $\top$ &  &  & $\perp$ & \textcolor{red}{$\top$} & $\perp$ & $\top$ &  &  & $\perp$ & \textcolor{red}{$\perp$} & $\perp$ &  & \textcolor{red}{$\perp$} & $\top$\\
		$\perp$ & $\top$ & $\perp$ & $\top$ &  &  & $\perp$ & $\perp$ & $\top$ &  & \textcolor{red}{$\top$} & $\perp$ &  &  & $\top$ & \textcolor{red}{$\top$} & $\top$ & $\perp$ &  &  & $\top$ & \textcolor{red}{$\perp$} & $\perp$ &  & \textcolor{red}{$\perp$} & $\top$\\
		$\perp$ & $\top$ & $\perp$ & $\perp$ &  &  & $\perp$ & $\perp$ & $\top$ &  & \textcolor{red}{$\top$} & $\perp$ &  &  & $\perp$ & \textcolor{red}{$\top$} & $\top$ & $\perp$ &  &  & $\perp$ & \textcolor{red}{$\perp$} & $\perp$ &  & \textcolor{red}{$\perp$} & $\top$\\
		$\perp$ & $\perp$ & $\top$ & $\top$ &  &  & $\perp$ & $\perp$ & $\perp$ &  & \textcolor{red}{$\top$} & $\top$ &  &  & $\top$ & \textcolor{red}{$\perp$} & $\perp$ & $\top$ &  &  & $\top$ & \textcolor{red}{$\perp$} & $\perp$ &  & \textcolor{red}{$\top$} & $\perp$\\
		$\perp$ & $\perp$ & $\top$ & $\perp$ &  &  & $\perp$ & $\perp$ & $\perp$ &  & \textcolor{red}{$\top$} & $\top$ &  &  & $\perp$ & \textcolor{red}{$\top$} & $\perp$ & $\top$ &  &  & $\perp$ & \textcolor{red}{$\perp$} & $\perp$ &  & \textcolor{red}{$\top$} & $\perp$\\
		$\perp$ & $\perp$ & $\perp$ & $\top$ &  &  & $\perp$ & $\perp$ & $\perp$ &  & \textcolor{red}{$\top$} & $\perp$ &  &  & $\top$ & \textcolor{red}{$\top$} & $\top$ & $\perp$ &  &  & $\top$ & \textcolor{red}{$\perp$} & $\perp$ &  & \textcolor{red}{$\top$} & $\perp$\\
		$\perp$ & $\perp$ & $\perp$ & $\perp$ &  &  & $\perp$ & $\perp$ & $\perp$ &  & \textcolor{red}{$\top$} & $\perp$ &  &  & $\perp$ & \textcolor{red}{$\top$} & $\top$ & $\perp$ &  &  & $\perp$ & \textcolor{red}{$\perp$} & $\perp$ &  & \textcolor{red}{$\top$} & $\perp$\\
	\end{tabular}

	The 7 seventh row shows that the argument $\lnot Q$ is always true when all premises are true. Therefore, $\lnot Q$ is a valid argument. (The truth values of the premises are shown in red.)

	\section{1.5 Question 4c}

	Propositions:
	\begin{itemize}
		\item P: Warning light comes.
		\item Q: The pressure is too high.
		\item R: The relief valve is clogged.
	\end{itemize}

	Premises:
	\begin{itemize}
		\item The warning light will come if and only if the pressure is too high and the relief valve is clogged: $P \iff Q \land R$.
		\item The relief valve is not clogged: $\lnot R$.
	\end{itemize}

	Argument: The warning light will come on if and only if the pressure is too high: $P \iff Q$.

    \begin{tabular}{ c  c  c | c  c  c  c  c  c  c  c  c | c  c | c  c  c  c  c }
    	P & Q & R & ( & P & $\leftrightarrow$ & ( & Q & $\&$ & R & ) & ) & $\sim$ & R & ( & P & $\leftrightarrow$ & Q & )\\
    	\hline
    	$\top$ & $\top$ & $\top$ &  & $\top$ & \textcolor{red}{$\top$} &  & $\top$ & $\top$ & $\top$ &  &  & \textcolor{red}{$\perp$} & $\top$ &  & $\top$ & \textcolor{red}{$\top$} & $\top$ & \\
    	$\top$ & $\top$ & $\perp$ &  & $\top$ & \textcolor{red}{$\perp$} &  & $\top$ & $\perp$ & $\perp$ &  &  & \textcolor{red}{$\top$} & $\perp$ &  & $\top$ & \textcolor{red}{$\top$} & $\top$ & \\
    	$\top$ & $\perp$ & $\top$ &  & $\top$ & \textcolor{red}{$\perp$} &  & $\perp$ & $\perp$ & $\top$ &  &  & \textcolor{red}{$\perp$} & $\top$ &  & $\top$ & \textcolor{red}{$\perp$} & $\perp$ & \\
    	$\top$ & $\perp$ & $\perp$ &  & $\top$ & \textcolor{red}{$\perp$} &  & $\perp$ & $\perp$ & $\perp$ &  &  & \textcolor{red}{$\top$} & $\perp$ &  & $\top$ & \textcolor{red}{$\perp$} & $\perp$ & \\
    	$\perp$ & $\top$ & $\top$ &  & $\perp$ & \textcolor{red}{$\perp$} &  & $\top$ & $\top$ & $\top$ &  &  & \textcolor{red}{$\perp$} & $\top$ &  & $\perp$ & \textcolor{red}{$\perp$} & $\top$ & \\
    	$\perp$ & $\top$ & $\perp$ &  & $\perp$ & \textcolor{red}{$\top$} &  & $\top$ & $\perp$ & $\perp$ &  &  & \textcolor{red}{$\top$} & $\perp$ &  & $\perp$ & \textcolor{red}{$\perp$} & $\top$ & \\
    	$\perp$ & $\perp$ & $\top$ &  & $\perp$ & \textcolor{red}{$\top$} &  & $\perp$ & $\perp$ & $\top$ &  &  & \textcolor{red}{$\perp$} & $\top$ &  & $\perp$ & \textcolor{red}{$\top$} & $\perp$ & \\
    	$\perp$ & $\perp$ & $\perp$ &  & $\perp$ & \textcolor{red}{$\top$} &  & $\perp$ & $\perp$ & $\perp$ &  &  & \textcolor{red}{$\top$} & $\perp$ &  & $\perp$ & \textcolor{red}{$\top$} & $\perp$ & \\
    \end{tabular}

	The 6th row shows that, even when both premises are truth, it is possible for the argument to be false. Therefore, the argument $P \iff Q$ is invalid.

	\section{1.5 Question 8a}

	The truth table for $(P \rightarrow Q) \land (Q \rightarrow R)$ looks like:

	\begin{tabular}{ c  c  c | c  c  c  c  c  c  c  c  c  c  c  c  c }
		P & Q & R & ( & ( & P & $\rightarrow$ & Q & ) & $\&$ & ( & Q & $\rightarrow$ & R & ) & )\\
		\hline
		$\top$ & $\top$ & $\top$ &  &  & $\top$ & $\top$ & $\top$ &  & \textcolor{red}{$\top$} &  & $\top$ & $\top$ & $\top$ &  & \\
		$\top$ & $\top$ & $\perp$ &  &  & $\top$ & $\top$ & $\top$ &  & \textcolor{red}{$\perp$} &  & $\top$ & $\perp$ & $\perp$ &  & \\
		$\top$ & $\perp$ & $\top$ &  &  & $\top$ & $\perp$ & $\perp$ &  & \textcolor{red}{$\perp$} &  & $\perp$ & $\top$ & $\top$ &  & \\
		$\top$ & $\perp$ & $\perp$ &  &  & $\top$ & $\perp$ & $\perp$ &  & \textcolor{red}{$\perp$} &  & $\perp$ & $\top$ & $\perp$ &  & \\
		$\perp$ & $\top$ & $\top$ &  &  & $\perp$ & $\top$ & $\top$ &  & \textcolor{red}{$\top$} &  & $\top$ & $\top$ & $\top$ &  & \\
		$\perp$ & $\top$ & $\perp$ &  &  & $\perp$ & $\top$ & $\top$ &  & \textcolor{red}{$\perp$} &  & $\top$ & $\perp$ & $\perp$ &  & \\
		$\perp$ & $\perp$ & $\top$ &  &  & $\perp$ & $\top$ & $\perp$ &  & \textcolor{red}{$\top$} &  & $\perp$ & $\top$ & $\top$ &  & \\
		$\perp$ & $\perp$ & $\perp$ &  &  & $\perp$ & $\top$ & $\perp$ &  & \textcolor{red}{$\top$} &  & $\perp$ & $\top$ & $\perp$ &  & \\
	\end{tabular}

	The truth table for $(P \rightarrow R) \land [ (P \iff Q) \lor (R \iff Q) ]$ looks like:

	\begin{tabular}{ c  c  c | c  c  c  c  c  c  c  c  c  c  c  c  c  c  c  c  c  c  c  c  c }
		P & Q & R & ( & ( & P & $\rightarrow$ & R & ) & $\&$ & ( & ( & P & $\rightarrow$ & Q & ) & $\lor$ & ( & R & $\rightarrow$ & Q & ) & ) & )\\
		\hline
		$\top$ & $\top$ & $\top$ &  &  & $\top$ & $\top$ & $\top$ &  & \textcolor{red}{$\top$} &  &  & $\top$ & $\top$ & $\top$ &  & $\top$ &  & $\top$ & $\top$ & $\top$ &  &  & \\
		$\top$ & $\top$ & $\perp$ &  &  & $\top$ & $\perp$ & $\perp$ &  & \textcolor{red}{$\perp$} &  &  & $\top$ & $\top$ & $\top$ &  & $\top$ &  & $\perp$ & $\top$ & $\top$ &  &  & \\
		$\top$ & $\perp$ & $\top$ &  &  & $\top$ & $\top$ & $\top$ &  & \textcolor{red}{$\perp$} &  &  & $\top$ & $\perp$ & $\perp$ &  & $\perp$ &  & $\top$ & $\perp$ & $\perp$ &  &  & \\
		$\top$ & $\perp$ & $\perp$ &  &  & $\top$ & $\perp$ & $\perp$ &  & \textcolor{red}{$\perp$} &  &  & $\top$ & $\perp$ & $\perp$ &  & $\top$ &  & $\perp$ & $\top$ & $\perp$ &  &  & \\
		$\perp$ & $\top$ & $\top$ &  &  & $\perp$ & $\top$ & $\top$ &  & \textcolor{red}{$\top$} &  &  & $\perp$ & $\top$ & $\top$ &  & $\top$ &  & $\top$ & $\top$ & $\top$ &  &  & \\
		$\perp$ & $\top$ & $\perp$ &  &  & $\perp$ & $\top$ & $\perp$ &  & \textcolor{red}{$\top$} &  &  & $\perp$ & $\top$ & $\top$ &  & $\top$ &  & $\perp$ & $\top$ & $\top$ &  &  & \\
		$\perp$ & $\perp$ & $\top$ &  &  & $\perp$ & $\top$ & $\top$ &  & \textcolor{red}{$\top$} &  &  & $\perp$ & $\top$ & $\perp$ &  & $\top$ &  & $\top$ & $\perp$ & $\perp$ &  &  & \\
		$\perp$ & $\perp$ & $\perp$ &  &  & $\perp$ & $\top$ & $\perp$ &  & \textcolor{red}{$\top$} &  &  & $\perp$ & $\top$ & $\perp$ &  & $\top$ &  & $\perp$ & $\top$ & $\perp$ &  &  & \\
	\end{tabular}

	Since their truth tables look identical, we have shown that the two logical expressions are equivalent.

	\section{1.5 Question 8b}

	The truth table for $(P \rightarrow Q) \lor (Q \rightarrow R)$ looks like:

	\begin{tabular}{ c  c  c | c  c  c  c  c  c  c  c  c  c  c  c  c }
		P & Q & R & ( & ( & P & $\rightarrow$ & Q & ) & $\lor$ & ( & Q & $\rightarrow$ & R & ) & )\\
		\hline
		$\top$ & $\top$ & $\top$ &  &  & $\top$ & $\top$ & $\top$ &  & \textcolor{red}{$\top$} &  & $\top$ & $\top$ & $\top$ &  & \\
		$\top$ & $\top$ & $\perp$ &  &  & $\top$ & $\top$ & $\top$ &  & \textcolor{red}{$\top$} &  & $\top$ & $\perp$ & $\perp$ &  & \\
		$\top$ & $\perp$ & $\top$ &  &  & $\top$ & $\perp$ & $\perp$ &  & \textcolor{red}{$\top$} &  & $\perp$ & $\top$ & $\top$ &  & \\
		$\top$ & $\perp$ & $\perp$ &  &  & $\top$ & $\perp$ & $\perp$ &  & \textcolor{red}{$\top$} &  & $\perp$ & $\top$ & $\perp$ &  & \\
		$\perp$ & $\top$ & $\top$ &  &  & $\perp$ & $\top$ & $\top$ &  & \textcolor{red}{$\top$} &  & $\top$ & $\top$ & $\top$ &  & \\
		$\perp$ & $\top$ & $\perp$ &  &  & $\perp$ & $\top$ & $\top$ &  & \textcolor{red}{$\top$} &  & $\top$ & $\perp$ & $\perp$ &  & \\
		$\perp$ & $\perp$ & $\top$ &  &  & $\perp$ & $\top$ & $\perp$ &  & \textcolor{red}{$\top$} &  & $\perp$ & $\top$ & $\top$ &  & \\
		$\perp$ & $\perp$ & $\perp$ &  &  & $\perp$ & $\top$ & $\perp$ &  & \textcolor{red}{$\top$} &  & $\perp$ & $\top$ & $\perp$ &  & \\
	\end{tabular}

	Since all truth values are true, we have shown that $(P \rightarrow Q) \lor (Q \rightarrow R)$ is a tautology.

\end{document}


\documentclass{article}
\usepackage{graphicx}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, amssymb, latexsym}

\usepackage{pgfplots}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{tikz}
\usepackage{nicefrac}
\pgfplotsset{every axis legend/.append style={
at={(0,0)},
anchor=north east}}
\usetikzlibrary{shapes,positioning,intersections,quotes}
\usetikzlibrary{arrows.meta,
                bending,
                intersections,
                quotes,
                shapes.geometric}

\definecolor{darkgreen}{rgb}{0.0, 0.6, 0.0}
\definecolor{darkred}{rgb}{0.7, 0.0, 0.0}
\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother
\title{Week 2}
\begin{document}
\pagenumbering{gobble}
\maketitle
\newpage
\pagenumbering{arabic}

\section*{Linear Regression}
The previously described home price data example is an example of a supervised learning regression problem.
\subsection*{Notation (used throughout the course)}
\begin{itemize}
  \item $m$ = number of training examples.
  \item $x's$ = input variables / features.
  \item $y's$ = output variable "target" variables.
  \item $(x,y)$ - single training example.
  \item $(x^i, y^j)$ - specific example (ith training example).
\end{itemize}

\includegraphics[width=0.8\textwidth]{resources/house_price_table}

\subsection*{With our training set defined - how do we used it?}
\begin{itemize}
  \item Take training set.
  \item Pass into a learning algorithm.
  \item Algorithm outputs a function (h = hypothesis).
  \item This function takes an input (e.g. size of new house).
  \item Tries to output the estimated value of Y.
\end{itemize}

$$h_{\theta}(x) = \theta_0 + \theta_1x$$

\begin{itemize}
  \item Y is a linear function of x!
  \item $\theta_0$ is zero condition.
  \item $\theta_1$ is gradient.
\end{itemize}

\subsection*{Cost function}
\begin{itemize}
  \item We may use a cost function to determine how to fit the best straight line to our data.
  \item We want to want to solve a minimization problem. Minimize $(h_{\theta}(x) - y)^2$.
  \item Sum this over the training set.
\end{itemize}

$$J(\theta_0, \theta_1) = \frac{1}{2m} \sum_{m}^{i=1}(h_{\theta}(x^{(i)}) - y^{(i)})^2$$

\subsection*{Example}

For $ \theta_0 = 0$ we have:

$$h_{\theta}(x) = \theta_1x\quad and \quad J(\theta_1) = \frac{1}{2m} \sum_{m}^{i=1}(h_{\theta}(x^{(i)}) - y^{(i)})^2$$

Data:
\begin{itemize}
  \item $\theta_1 = 1$ and $J(\theta_1)= 0$.
  \item $\theta_1 = 0.5$ and $J(\theta_1)= 0.58$.
  \item $\theta_1 = 0$ and $J(\theta_1)= 2.3$.
\end{itemize}

\includegraphics[width=0.7\textwidth]{resources/cost_function}

~\\
The optimization objective for the learning algorithm is find the value of $\theta_1$ which minimizes $J(\theta_1)$.
So, here $\theta_1 = 1$ is the best value for $\theta_1$.

\subsection*{A deeper insight into the cost function - simplified cost function}

The real cost function takes two variables as parameters! $J(\theta_0, \theta_1)$.

~\\We can now generates a 3D surface plot where axis are:

\begin{itemize}
  \item $X = \theta_1$.
  \item $Z = \theta_0$.
  \item $Y = J(\theta_0,\theta_1)$.
\end{itemize}

\includegraphics[width=0.8\textwidth]{resources/surface_cost_function}

The best hypothesis is at the bottom of the bowl.

~\\
Instead of a surface plot we can use a contour figures/plots.
\begin{itemize}
  \item Set of ellipses in different colors.
  \item Each colour is the same value of  $J(\theta_0,\theta_1)$, but obviously plot to different locations because $\theta_1$ and $\theta_0$ will vary.
  \item Imagine a bowl shape function coming out of the screen so the middle is the concentric circles.
\end{itemize}

\includegraphics[width=0.6\textwidth]{resources/contour_cost_function}

The best hypothesis is located in the center of the contour plot.

\section*{Gradient descent algorithm}
\begin{itemize}
  \item Begin with initial guesses, could be (0,0) or any other value.
  \item Continually change values of $\theta_0$ and $theta_1$ to try to reduce  $J(\theta_0, \theta_1)$.
  \item Continue until you reach a local minimum.
  \item Reached minimum is determined by the starting point.
\end{itemize}

\begin{center}
  \large
  \textbf{Surface plot of gradient descent.}
  \includegraphics[width=\textwidth]{resources/gradient_descent}
\end{center}

\begin{tikzpicture}[
  every edge/.style = {draw, -{Triangle[angle=60:1pt 3,flex]},
  bend right=11, blue,ultra thick},
  every edge quotes/.style = {font=\scriptsize, inner sep=1pt,
      auto, sloped}
  ]
  \fill (0,0) circle[radius=3pt];
  \path[name path=C] foreach \i in {4, 8, 16, 22, 28}
    {(0,0) circle[draw=red!\i, x radius=2*\i mm, y radius=\i mm, rotate=-5]};
  \foreach \i in  {4, 8, 16, 22, 28}
  \draw[line width=11.2/\i, draw=white!\i!gray]
  (0,0) circle[x radius=2*\i mm, y radius=\i mm, rotate=-5];
  \path[name path=V] (-4,2.4) .. controls + (0,-2) and + (-2,0) .. (0,0);
  %
  \draw [name intersections={of=C and V, sort by=C, name=A}]
  (A-5) edge ["${w[0]}$"] (A-4)
  (A-4) edge ["${w[1]}$"] (A-3)
  (A-3) edge ["${w[2]}$"] (A-2);
  \node[above,font=\large\bfseries] at (current bounding box.north) {Contour plot of gradient descent.};
\end{tikzpicture}

\begin{algorithm}
  \caption{Gradient Descent}\label{euclid}
  \begin{algorithmic}[1]
    \Large
    \While {not converged}
    \State $\theta_j := \theta_j - \alpha \frac{\partial}{\partial \theta_j} J(\theta_0, \theta_1)$.
    \State $(for\ j = 1\ and\ j=0)$
    \EndWhile
  \end{algorithmic}
\end{algorithm}

\subsection*{Two key terms in the algorithm:}
\begin{itemize}
  \item $\alpha$ term
  \item Derivative term
\end{itemize}

\subsection*{Partial derivative vs. derivative}
\begin{itemize}
  \item Use partial derivative when we have multiple variables but only derive with respect to one.
  \item Use derivative when we are deriving with respect to all the variables.
\end{itemize}

\subsection*{Derivative says:}
\begin{itemize}
  \item Let's look at the slope of the line by taking the tangent at the point.
  \item As a result, going towards the minimum (down) will result in a negative derivative; nevertheless, alpha is always positive, thus $J(\theta_1)$ will be updated to a lower value.
  \item Similarly, as we progress up a slope, we increase the value of $J(\theta_1)$.
\end{itemize}

\subsection*{$\alpha$ term}
\begin{itemize}
  \item If it's too small, it takes too long to converge.
  \item If it is too large, it may exceed the minimum and fail to converge.
\end{itemize}

\subsection*{When you get to a local minimum}
\begin{itemize}
  \item Gradient of tangent/derivative is 0
  \item So derivative term = 0
  \item $\alpha \cdot 0 = 0$
  \item So $\theta_1 = \theta_1 - 0$.
  \item So $\theta_1$ remains the same.
\end{itemize}

\section*{Linear regression with gradient descent}

Apply gradient descent to minimize the squared error cost function $J(\theta_0, \theta_1)$.

$$\frac{\partial}{\partial \theta_j} J(\theta_0, \theta_1) = \frac{\partial}{\partial \theta_j} \frac{1}{2m} \sum_{i=1}^{m} (h_{\theta}(x^{(i)}) - y^{(i)})^2$$

$$= \frac{\partial}{\partial \theta_j} \frac{1}{2m} \sum_{i=1}^{m} (\theta_0 + \theta_1x^{(i)} - y^{(i)})^2$$

~\\
For each case, we must determine the partial derivative:

$$j=0:\frac{\partial}{\partial \theta_0} J(\theta_0, \theta_1)=\frac{\partial}{\partial \theta_j} \frac{1}{m} \sum_{i=1}^{m} (h_{\theta}(x^{(i)}) - y^{(i)})$$

$$j=1:\frac{\partial}{\partial \theta_1} J(\theta_0, \theta_1)=\frac{\partial}{\partial \theta_j} \frac{1}{m} \sum_{i=1}^{m} (h_{\theta}(x^{(i)}) - y^{(i)})x^{(i)}$$

~\\
The linear regression cost function is always a convex function - always has a single minimum. So gradient descent will always converge to global optima.

\section*{Two extension to the algorithm}

\subsection*{Normal equation for numeric solution}
\begin{itemize}
  \item To solve the minimization problem we can solve it $[ min J(\theta_0, \theta_1) ]$ exactly using a numeric method which avoids the iterative approach used by gradient descent.
  \item Normal equations method.
  \item Can be much faster for some problems, but it is much more complicated (will be covered in detail later).
\end{itemize}

\subsection*{We can learn with a larger number of features}
\begin{itemize}
  \item e.g. with houses: Size, Age, Number bedrooms, Number floors...
  \item Can't really plot in more than 3 dimensions.
  \item Best way to get around with this is the notation of linear algebra (matrices and vectors).
\end{itemize}

\end{document}1


% Instructions: you don't need to change anything in the macros, but
% feel free to define new commands as you wish. Starting from the main
% body, change the specs (e.g., your
% name). use \begin{solution} \end{solution} environment to write your
% solutions. Don't forget to list your collaborators.

\documentclass[12pt,addpoints,answers]{exam}
%============Macros==================%
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage[margin=1in]{geometry}
%--------------Cosmetic----------------%
\usepackage{mathtools}
\usepackage{hyperref}
\usepackage{fullpage}
\usepackage{microtype}
\usepackage{xspace}
\usepackage[svgnames]{xcolor}
\usepackage[sc]{mathpazo}
\usepackage{enumitem}
\setlist[enumerate]{itemsep=1pt,topsep=2pt}
\setlist[itemize]{itemsep=1pt,topsep=2pt}
%----------Header--------------------%
\def\course{CSCE629 Analysis of Algorithms}
\def\term{Texas A\&M U, Fall 2019}
\def\prof{Lecturer: Fang Song}
\newcommand{\handout}[5]{
   \renewcommand{\thepage}{\arabic{page}}
   \begin{center}
   \framebox{
      \vbox{
    \hbox to 5.78in { \hfill \large{\course} \hfill }
       \vspace{2mm}
       \hbox to 5.78in { {\Large \hfill \textbf{#5}  \hfill} }
       \vspace{2mm}
       \hbox to 5.78in { \term \hfill \emph{#2}}
       \hbox to 5.78in { {#3 \hfill \emph{#4}}}
      }
   }
   \end{center}
   \vspace*{4mm}
}
\newcommand{\hw}[4]{\handout{#1}{#2}{#3}{#4}{Homework #1}}

%-----defs and commands-----%
%\def\mG{[\textbf{G}]\xspace}
\def\veps{\varepsilon}
\newcommand{\bit}{\{0,1\}}
\newcommand{\negl}{\text{negl}}
\newcommand{\corr}[1]{{\color{blue}{#1}}}
\newcommand{\alg}[1]{\textsf{#1}}
\newcommand{\class}[1]{\text{#1}}
\newcommand{\NP}{\class{NP}}
%=======Main document==============%
\begin{document}

%----Specs: change accordingly-----%
\newif\ifstudent % comment out false
 \studenttrue
% \studentfalse

\def\hwnum{11}
\def\issuedate{11/15/19}
\def\duedate{10am, 11/22/19} %
\def\yourname{your name} % put your name here
%------------------------------%

\ifstudent
\hw{\hwnum}{\issuedate}{Student: \yourname}{Due: \duedate}%
\else
\hw{\hwnum}{\issuedate}{\prof}{Due: \duedate}%
\fi

\noindent \textbf{Instructions.}

\begin{itemize}
\item Typeset your submission by \LaTeX, and submit in PDF
  format. Your solutions will be graded on \emph{correctness} and
  \emph{clarity}. You should only submit work that you believe to be
  correct, and you will get significantly more partial credit if you
  clearly identify the gap(s) in your solution. You may opt for the
  ``I’ll take 15\%'' option.
\item You may collaborate with others on this problem set.  However,
  you must \textbf{{write up your own solutions}} and \textbf{{list
      your collaborators and any external sources}} for each
  problem. Be ready to explain your solutions orally to a course staff
  if asked.
\item For problems that require you to provide an algorithm, you must
  give a precise description of the algorithm, together with a proof
  of correctness and an analysis of its running time. You may use
  algorithms from class as subroutines. You may also use any facts
  that we proved in class or from the book.
\item \textbf{If you describe a Greedy algorithm, you will get no
    credit without a formal proof of correctness, even if your
    algorithm is correct.}
\end{itemize}

\noindent This assignment contains \numquestions\ questions,
\numpages\ pages for the total of \numpoints \ points and
\numbonuspoints\ bonus points. A random subset of the problems will be
graded. \medskip

\paragraph{Exercises. Do not turn in.}
\begin{questions}
  \question A clique in an undirected graph $G =(V, E)$ is a subset
  $V' \subseteq V$ of vertices, each pair of which is connected by an
  edge in $E$. (In other words, a clique is a \emph{complete} subgraph
  of $G$. The size of a clique is the number of vertices it
  contains. The Clique problems asks to decide whether a clique of a
  given size $k$ exists in the graph. Show that Clique is
  \NP-complete.

  \question Given an integer $m\times n$ matrix $A$ and an integer
  $m$-vector $b$, the 0-1 integerprogramming problem asks whether
  there exists an integer $n$-vector $x$ with elements in the set \bit
  suchthat $Ax =b$. Prove that 0-1 integer programming is
  \NP-complete. (Hint: Reduce from 3-CNF-SAT.)

\end{questions}
\newpage
\paragraph{Problems to turn in.}

\begin{questions}

\question (Bonnie and Clyde) Bonnie and Clyde have just robbed a
  bank. They have a bag of money and want to divide it up. For each of
  the following scenarios, either give a polynomial-time algorithm, or
  prove that the problem is \NP-complete. The input in each case is a
  list of the $n$ items in the bag, along with the value of each.
  \begin{parts}
    \part[10] The bag contains $n$ coins, but only 2 different
    denominations: some coins are worth $x$ dollars, and some are
    worth $y$ dollars. Bonnie and Clyde wish to divide the money
    exactly evenly.
    \part[10] The bag contains $n$ coins, with an arbitrary number of
    different denominations, but each denomination is a nonnegative
    integer power of 2, i.e., the possible denominations are 1 dollar,
    2 dollars, 4 dollars, etc. Bonnie and Clyde wish to divide the
    money exactly evenly.

    \part[10] The bag contains $n$ checks, which are, in an amazing
    coincidence, made out to “Bonnie or Clyde.” They wish to divide
    the checks so that they each get the exact same amount of money.
    \part[10] The bag contains $n$ checks as in part (c), but this
    time Bonnie and Clyde are willing to accept a split in which the
    difference is no larger than 100 dollars.
\end{parts}
  \newpage

  \question[15] (Galactic Shortest Path) This problem originates from
  the \emph{Star Wars} movies. Luke, Leia, and friends are trying to
  make their way from the Death Star back to the hidden Rebel base. We
  can view the galaxy as an undirected graph $G = (V, E)$, where each
  node is a star system and an edge $(u, v)$ indicates that one can
  travel directly between $u$ and $v$. The Death Star is represented
  by a node $s$, the hidden Rebel base by a node $t$. Certain edges in
  this graph represent longer distances than others; thus each edge
  $e$ has an integer length $\ell_e\ge 0$. Also, certain edges
  represent routes that are more heavily patrolled by evil Imperial
  spacecraft; so each edge $e$ also has an integer risk $r_e \ge 0$,
  indicating the expected amount of damage incurred from
  special-effects-intensive space battles if one traverses this edge.

  It would be safest to travel through the outer rim of the galaxy,
  from one quiet upstate star system to another; but then one's ship
  would run out of fuel long before getting to its
  destination. Alternately, it would be quickest to plunge through the
  cosmopolitan core of the galaxy; but then there would be far too
  many Imperial spacecraft to deal with. In general, for any path $P$
  from $s$ to $t$, we can define its total length to be the sum of the
  lengths of all its edges; and we can define its total risk to be the
  sum of the risks of all its edges.

  So Luke, Leia, and company are looking at a complex type of shortest
  path problem in this graph: they need to get from $s$ to $t$ along a
  path whose total length and total risk are both reasonably small. In
  concrete terms, we can phrase the \emph{Galactic Shortest-Path
    Problem} as follows: Given a setup as above, and integer bounds
  $L$ and $R$, is there a path from $s$ to $t$ whose total length is
  at most $L$, and whose total risk is at most $R$?

  Describe a poly-time algorithm or prove that Galactic Shortest Path
  is \NP-complete.

\end{questions}
\end{document}