\section{Algorithmic Approaches}\label{chapter:algorithms}
In the previous section, we discussed several optimality criteria that apply to the problem of matching students to seminars. This chapter presents algorithms for computing matchings that fulfill some of those criteria, as well as evaluating them against each other. We will see that each of the algorithms has some disadvantages, which makes it necessary to consider the trade-offs when picking one for implementation.

\subsection{Greedy with Serial Dictatorship}\label{algo-rsd}
One of the simplest algorithms for the student-seminar matching problem is a greedy approach that iterates over the set of students and assigns each of the students to their most preferred seminar that still has some capacity left. In contrast to Gale \& Shapley's deferred acceptance algorithm for the stable marriage problem, this algorithm does not tentatively match students once they make their selection, but makes a final assignment for those students. Because of this, the algorithm finds a matching in $\mathcal{O}(n)$ time with $n$ being the number of students. This mechanism of letting students successively pick their highest available preference in order is known as serial dictatorship \cite{MANEA2007316}. In detail, the algorithm goes as follows:

\begin{algorithm} % enter the algorithm environment
    \caption{Random Serial Dictatorship Algorithm}
    \label{alg1} % and a label for \ref{} commands later in the document
    \begin{algorithmic} % enter the algorithmic environment
        \Require set of Students with preferences $S$, set of Seminars $T$
        \Ensure Pareto-Optimal Matching M
        \Function{RSD-Matching}{$S, T$}
        \State $M := \emptyset$
        \ForEach {$s \in \mathcal S $ in random order}
            \State $t :=$ highest ranked, available seminar on preference list of s
            \If{$t \neq null$}
                \State $M = M \cup \{(s, t)\}$
            \EndIf
        \EndFor
        \State\Return M
        \EndFunction
    \end{algorithmic}
\end{algorithm}

Even though this algorithm is very simple and fast, it has some desirable properties, including one of the optimality criteria defined before.

\subsubsection{Properties}
Since the order in which the students get to pick their match is pre-defined, we can easily show that the algorithm always produces a pareto-optimal matching.
\newtheorem{theorem}{Theorem}
\begin{theorem}
    A greedy algorithm that uses serial dictatorship always produces a pareto-optimal matching.
\end{theorem}
\begin{proof}
    Let $M$ be the matching produced by the algorithm. We assume that there exists a matching $N$ that pareto-dominates $M$. Now, let $s\in S$ be the first student who prefers his match in $N$ over $M$. Since $s$ prefers $N(s)$ over $M(s)$, the seminar $N(s)$ must have been unavailable when he made his pick. That means that another student $s' \in S$ exists, who picked $N(s)$ before $s$ could. However, we required that $s$ was matched to a better seminar in $N$, which means that $s'$ gets a worse match in $N$. This is a contradiction, so $N$ cannot pareto-dominate $M$ \cite{kun_2015}.
\end{proof}

We can also easily see that the algorithm is strategy-proof, because every applicant makes his final pick once it is his turn, so there is no benefit in misrepresenting preferences \cite{Klaus}.

\subsubsection{Drawbacks}
When looking at the algorithm, it is clear that it has a strict preference order over students, specified by the order in which students are matched in the for-loop. Additionally, the algorithm makes no effort to match all students: if it is a student's turn to pick his match, and none of the seminars on his preference lists are free, that student will not be matched at all. This problem gets worse when we consider that our problem statement allows for incomplete preference lists, which increases the chances of having a high number of unmatched students. To illustrate this, let us consider the following example in Table \ref{table:1}:

\begin{table}[h!]
    \centering
    \begin{tabular}{ll|ll}
    Agent   & Pref list     & Seminar   & Capacity \\
    $s_1$   & $t_1$, $t_2$  & $t_1$     & 1        \\
    $s_2$   & $t_1$         & $t_2$     & 1
    \end{tabular}
    \caption{Instance Where Serial Dictatorship Admits no Maximum Cardinality Matching}
    \label{table:1}
\end{table}

In this example, each seminar only has a capacity of one and both students have seminar $t_1$ as their first preference. If the algorithm first gives $s_1$ a chance to pick, $s_1$ will be matched to $t_1$, making $t_1$ full and not allowing $s_2$ to be matched. On the other hand, matching $s_2$ to $t_1$ first and then matching $s_1$ to $t_2$ also yields a pareto optimal matching, however, in this case, all the students are matched.

To address the other problem of preference over students, a simple approach is using the random serial dictatorship mechanism, which instead creates a random order of students as the pick order.

\subsection{Pareto Optimal Maximal Matchings for CHA}\label{algo-max-po}
We have seen that serial dictatorship is an easy and time-efficient mechanism for computing Pareto-optimal matchings. A big weakness of the approach, however, is that it finds just one of many possible Pareto-optimal matchings without making any guarantees about quality in regards to cardinality. Particularly, the example in \mbox{Table \ref{table:1}} shows how permutations of the same instance can produce matchings of different cardinality, which motivates the search for an algorithm that produces a Pareto-Optimal matching of maximum cardinality.

Abraham et. al \cite{Abraham:Pacha} have proposed a 3-phase algorithm for computing a maximum cardinality matching for the house allocation problem, which was extended by Sng \cite{SngThesis} for the many-to-one case (CHA). Before presenting the algorithm, an important lemma about Pareto optimal matchings has to be shown first, which is then used to prove the correctness of the algorithm. To characterize the lemma, we need to define the terms \emph{Maximality, Trade-in-Free and Cyclic Coalition} in regards to a matching $M$ first:
\begin{enumerate}
    \item \textbf{Maximal}: $M$ is maximal, if no student $s_i \in S$ and seminar $t_j \in T$ exists, so that $s_i$ is unassigned, $t_j$ is undersubscribed in $M$ and $t_j$ is on $s_i$'s preference list \cite{Abraham:Pacha}.
    \item \textbf{Trade-in-free}: $M$ is trade-in-free, if there are no student $s_i \in S$ and seminars $t_j, t_l \in T$, such that $s_i$ is assigned to $t_l$, but prefers $t_j$ over $t_l$ and $t_j$ is undersubscribed \cite{Abraham:Pacha}.
    \item \textbf{Cyclic coalition}: $M$ contains a cyclic coalition, if there exists a sequence of distinct assigned students $C = \langle s_0, s_1, \dots, s_{r-1} \rangle$ with $r \geq 2$, such that $s_i$ prefers $M(s_{i + 1 \bmod r})$ (i.e. the seminar assigned to the next student in $C$ after $s_i$) over $M(s_i)$ for every $i$ \cite{Abraham:Pacha}.
\end{enumerate}
Using these definitions, Sng now presents and proves the following lemma:
\newtheorem{lemma}[theorem]{Lemma}
\begin{lemma}\label{lemma-pacha}
    Let $M$ be a matching of a given instance of CHA. Then $M$ is Pareto optimal if and only if $M$ is maximal, trade-in-free and cyclic-coalition-free \cite{Abraham:Pacha}.
\end{lemma}

Using this lemma, the authors \cite{SngThesis, Abraham:Pacha} construct a 3-phased algorithm, where each phase fulfills one of the properties as described in Lemma \ref{lemma-pacha}, like so: Let $I$ be an instance of CHA and $G$ it is underlying graph, then perform the following steps:
\begin{enumerate}
    \item \textbf{Phase 1}: In order to guarantee maximality, compute a maximum-cardinality matching $M$ in $G$.
    \item \textbf{Phase 2}: Using the matching $M$ produced by step 1, the algorithm then fulfills the trade-in-free criteria as follows: Search for pairs $(s_i, t_j) \in M$ with $s_i \in S$ and $t_j \in T$ and where $t_j$ is undersubscribed in $M$ and $s_i$ prefers $t_j$ over his own match $t_l := M(s_i)$. Whenever such a pair is found, remove the existing assignment $(s_i, t_l)$ and add $(s_i, t_j)$ to $M$. Consequently, $t_l$ is now undersubscribed and may be assigned to another student. Therefore, we continue the search for such pairs until no such pair can be found for every student in $S$.
    \item \textbf{Phase 3}: The last phase of the algorithm eliminates any cyclic coalitions from $M$, if they exist, by using a modified version of \emph{Gale's Top Trading Cycles} (denoted by TTC) Method \cite{ShapleyTTC}. Essentially, the TTC method creates a graph from the matching $M$, where every student is represented by a node. Next, a directed edge is created from each student $s_i \in S$, to all students in $S$ who are assigned to the first seminar on $s_i$'s preference list. Now, there must be at least one cycle in this graph, as students may have an edge to themselves. The next step is identifying the cycles and implementing a trade among all agents of that cycle that reassigns the seminars among these students. After the trade, all students from that cycle are removed and these steps are repeated until the graph is empty. Once the graph is empty, $M$ is coalition-free by the correctness of the TTC method \cite{Abraham:Pacha}.
\end{enumerate}

\subsubsection{Properties}
Since all modifications to the matching in phase one and two are limited to swaps and no deletions, maximum cardinality is still guaranteed after the termination of phase three. Additionally, the resulting matching is also trade-in-free and cyclic-coalition-free, as those properties are guaranteed after performing phase two and three respectively. However, it is important to note that this algorithm, unlike the serial dictatorship mechanism, is not strategy-proof. Due to the fact that a maximum-cardinality matching is computed in step one, students are encouraged to provide short preference lists to have a higher chance of being matched to their first preference. Forcing students to provide complete preference lists could make this mechanism strategy-proof, if we can assume that students do not have any knowledge of the other student's preferences.

The runtime of the algorithm is dominated by finding a maximum cardinality matching (phase one), which yields a time complexity of $\mathcal{O}(E\sqrt{V})$ \cite{Abraham:Pacha} when using the Hopcroft-Karp algorithm. Phase one and two both take $\mathcal{O}(|E|)$ of time \cite{SngThesis}, which in total yields a worst-case complexity of $\mathcal{O}(E\sqrt{V})$ for finding a maximum cardinality pareto-optimal matching given any instance $I$ of the problem.

\subsection{Solving CHA as an Assignment Problem}\label{algo:assignment}
In order to find a rank-maximal matching, we will investigate a set of algorithmic methods that compute a maximum-cardinality, min-weight matching. We can easily see that such a matching must also be rank-maximal, since the min-weight property guarantees that the profile of the matching is lexicographically maximal among all matchings.
In Section \ref{intro_assignment}, we briefly presented the assignment problem: it is a combinatorial optimization problem, which assigns a set of agents to a set of tasks, wherein each agent-task tuple is assigned a cost, and to minimize the total cost of the assignment.

It is important to note that the assignment problem tries to find a perfect matching, meaning that all agents are assigned, and that a one-to-one matching (HA), contrary to one-to-many (CHA), is computed.

For this problem, there exist algorithms \cite{Munkres, Jonker1987} that find perfect, min-weight assignments in $\mathcal{O}(n^3)$ time. Using these algorithms for the one-to-many case with incomplete preference lists requires a transformation of the input, by introducing artificial edges with large weights, where no edges exist. Additionally, in the case of student-seminar matchings, seminars have to be duplicated according to their capacities to allow for performing one-to-many matchings.

However, we can see that the assignment problem is equivalent to finding a maximum-cardinality, min-weight matching in a bipartite, weighted graph. Therefore, we can construct such a graph, given the set of students, preference lists and seminars and apply graph algorithms that find matchings on such graphs for us. In fact, we can simply transform the student-seminar matching problem into an instance of the minimum-cost flow problem. The goal of this algorithm will be to send $|S|$ units of flow through the network, while minimizing the cost of the flow, which is indicated by a seminar's rank on the student's preference lists.

\subsubsection{Solving the Assignment Problem Using Flow-Networks}
The problem of matching students to seminars can be given as a bipartite graph \mbox{$G=(V=(S, T), E)$}, where $S$ is the set of students and $T$ the set of seminars. The set of edges $E$ is defined as follows: $E:= \{(s, t) \mid s \in S \land t \in T \land \mbox{t is on the preference list of s}\}$. Additionally, a weight function $W: E \rightarrow  \mathbb{N}$ is specified, which maps each edge to the position of the seminar on the student's preference list. In order to transform this bipartite graph into an input for the minimum-cost flow problem, a flow network has to be constructed using the bipartite graph.

To transform the bipartite graph into a flow network, we first add a source and sink vertex to the graph. Then, we add weights, capacities and edges from and to the source and sink. Specifically, we create an edge from the source to each of the student vertices with a capacity of 1 and a cost of 0. These edges indicate that a student can only be assigned once. Next, for every student we reuse the edges from the bipartite graph $G$, where each edge $e \in E$ is assigned a capacity of 1 and a cost of $W(e)$. The capacity, again indicates that a student can only be assigned once and the weight indicates the position of the seminar on the student's preference list. Finally, one edge is added from each seminar $t \in T$ to the sink with a capacity of $C(s)$ and a cost of 0.

\subsubsection{Properties}
The matching $M$ computed by the algorithms is Pareto optimal \cite{SngThesis} and has the minimum weight property, and therefore is rank-maximal;\cite{SngThesis} however, a large drawback is that this mechanism is not strategy-proof. Students are again encouraged to provide short preference lists in order to get matched to their most-preferred seminar. The algorithm tries to match every student, which means that students with a list of just one seminar will be prioritized over students, who prefer the same seminar but also supply other preferences. This problem could lead to having all students provide single-element preference lists, which increases the difficulty of finding maximum-cardinality matchings. Similarly to the previous algorithm, requiring complete preference lists could alleviate this problem and make the mechanism strategy-proof, if it can be assumed that students' preferences are not publicly known.

\subsection{Maximum Popular Matchings in CHA}\label{algo-max-pop}
Abraham et al. \cite{AbrahamPopular} presented an algorithm for finding a popular matching in the house allocation problem (without capacities), which either finds such a popular matching or reports that none exists in $\mathcal{O}(|V| + |E|)$ time. Manlove and Sng \cite{ManlovePopularMatchings} extended this algorithm for the many-to-one case (CHA) by developing a characterization of such popular matchings in CHA and then using it to construct an algorithm that finds a maximum-cardinality popular matching for any given instance, if it exists.
Intuitively, the algorithm will try to match as many applicants as possible to their most-preferred choice to fulfill the popularity criteria. Formally, Manlove and Sng \cite{ManlovePopularMatchings} define and prove an alternative characterization of popularity in order to develop their algorithm.

\subsubsection{An Alternative Characterization of Popular Matchings}
Given an instance $I$ of the CHA problem, for every applicant $a_i \in A$ let $h_j := f(a_i)$ be the first ranked house on $a_i$'s preference list. In this case, we call $h_j$ an f-house. For each house $h_j \in H$, define the set of applicants, who named $h_j$ as their first choice, as $f(h_j) = \{a_i \in A: f(a_i) = h_j\}$ and the size of that set as $f_j = |f(h_j)|$. For a matching $M$ in $I$, we now say that a house $h_j \in H$ is full if $|M^{-1}(h_j)| = c_j$, i.e. the maximum number of applicants is matched to that house, and undersubscribed if $|M^{-1}(h_j)| < c_j$. Additionally, for every applicant $a_i \in A$, we append a last-resort house $l(a_i)$ with capacity 1 to $a_i$'s preference list \cite{ManlovePopularMatchings}. The authors prove the following lemma \cite{ManlovePopularMatchings}:
\newtheorem{lemma-popular-1}[theorem]{Lemma}
\begin{lemma}\label{lemma-popular1}
    Let $M$ be a popular matching in $I$. Then for every f-house $h_j$, $|M^{-1}(h_j) \cap f(h_j)| = \min\{c_j, f_j\}$.
\end{lemma}
In other words, for a popular matching $M$ in $I$, every f-house $h_j$ is matched to at least the number of applicants who have $h_j$ as their first-preference but at most $h_j$'s capacity $c_j$. Next, for every applicant $a_i$, we define $s(a_i)$ to be the most-preferred house $h_j$ on his preference list, such that either (i) $h_j$  is not an f-house, meaning that it is not the first choice of any applicant, or (ii) $h_j$ is an f-house, but $h_j \neq f(a_i)$ and $f_j < c_j$. In simple terms, $s(a_i)$ is the first undersubscribed house on $a_i$'s preference list after $f(a_i)$. We will refer to such houses as s-houses. It is important to note that such a house $s(a_i)$ always exists, due to the introduction of $l(a_i)$. In a popular matching, an agent $a_i$ may only be matched to either $f(a_i)$ or $s(a_i)$, as every house in between those two is full according to the defintion of $f(a_i)$ and $s(a_i)$. Manlove and Sng, again prove the following two lemmas \cite{ManlovePopularMatchings}:

\newtheorem{lemma-popular-2}[theorem]{Lemma}
\begin{lemma}\label{lemma-popular2}
    Let $M$ be a popular matching in $I$. Then no agent $a_i \in A$ can be matched in $M$ to a house between $f(a_i)$ and $s(a_i)$ on $a_i$'s preference list.
\end{lemma}

\newtheorem{lemma-popular-3}[theorem]{Lemma}
\begin{lemma}\label{lemma-popular3}
    Let $M$ be a popular matching in $I$. Then no agent $a_i \in A$ can be matched in $M$ to a house worse than $s(a_i)$ on $a_i$'s preference list.
\end{lemma}

To summarize, so far for every applicant $a_i \in A$ we have defined the applicant's most preferred house $f(a_i)$ and his second most-preferred, but available house $s(a_j)$. We have seen that, in a popular matching, applicants can only be matched to either of those houses $f(a_i)$ or $s_(a_i)$. Using this information and a description of an instance as a weighted, bipartite graph $G = (V=(A, H), E)$, we can now construct a subgraph $G'$ of $G$, by removing all edges in $G$ from every applicant $a_i$, except the ones to $f(a_i)$ and $s(a_i)$. We now say that a matching $M$ is agent-complete in $G'$ if it matches all agents in $A$ and no agent $a_i$ is matched to their last-resort house $l(a_i)$ \cite{ManlovePopularMatchings}. Manlove and Sng prove the following theorem to fully characterize popular matchings \cite{ManlovePopularMatchings}:
\newtheorem{theorem-popular-4}[theorem]{Theorem}
\begin{theorem}\label{theorem-popular-4}
    A matching $M$ is popular in $I$ iff:
    \begin{enumerate}
        \item for every f-house $h_j$
        \begin{enumerate}
            \item\label{condition1a} if $f_j \leq c_j$, then $f(h_j) \subseteq M(h_j)$
            \item\label{condition1b} if $f_j > c_j$, then $|M(h_j)| = c_j$ and $M(h_j) \subseteq f(h_j)$
        \end{enumerate}
        \item $M$ is an agent complete matching in the reduced graph $G'$
    \end{enumerate}
\end{theorem}

Intuitively, this means that in every popular matching, each house that would be undersubscribed or just full, based on the applicants who name that house as their first preference, will be matched to at least all of those applicants. Additionally, every house that would be oversubscribed is matched to only a subset of the applicants naming the house as their first choice. For the remaining unmatched applicants, it must now be possible to find an applicant-complete matching so that none of the applicants are matched to their last-resort house.

\subsubsection{Algorithm}
Using Theorem \ref{theorem-popular-4}, the authors develop the algorithm Popular-CHA for finding a maximum popular matching or reporting that none exists \cite{ManlovePopularMatchings}. The algorithm works as follows:
\begin{enumerate}
    \item Reduce $G$ to $G'$.
    \item Match all agents to their first-choice house $h_j$, if $f_j \leq c_j$, i.e. the house would be undersubscribed or just full afterwards. This will satisfy condition \ref{condition1a} of Theorem \ref{theorem-popular-4}.
    \item Remove all applicants and their incident edges, that were matched in the previous step, from $G'$. Additionally, update the capacities for each previously matched house $h_j$ as $c'_j = c_j - f_j$. All full and isolated houses and their incident edges are also removed from $G'$.
    \item Compute a maximum cardinality matching $M'$ on $G'$ using the updated capacities. For this step Manlove and Sng use Gabow's algorithm \cite{Gabow1983}.
    \item If $M'$ is not agent complete, then no popular matchings exists. Otherwise merge the matchings $M$ and $M'$.
    \item As a last step, to fulfill condition \ref{condition1b} of Theorem \ref{theorem-popular-4}, promote any agent $a_i \in M$ who is matched to their s-house to their f-house, if it is undersubscribed.
\end{enumerate}

\subsubsection{Properties}
Due to the fact that step 2, 4 and 6 make the computed matching fulfill the criteria outlined in Theorem \ref{theorem-popular-4}, the algorithm produces a popular matching of maximum cardinality, if it exists. If just one applicant cannot be matched to his f-house or s-house, or is matched to his last-resort house, the algorithm fails and no matching is produced. In that case the popular matching for that instance is not of maximum cardinality and therefore is not computed by the algorithm.

Furthermore, the algorithm's runtime complexity is $\mathcal{O}(\sqrt{C} * n_1 + |E|)$, where $C$ is the sum of the capacities of the houses and $n_1$ the number of applicants. $|E|$ is equivalent to the sum of the agents' preference list lengths. The runtime is dominated by Gabow's algorithm \cite{Gabow1983}, which computes a maximum cardinality matching in $G'$ in $\mathcal{O}(\sqrt{C}n_1)$ \cite{ManlovePopularMatchings}. Alternatively, a modified version of the Hopcroft-Karp algorithm could be used for computing a maximum cardinality matching in $\mathcal{O}(E\sqrt{V})$\cite{Hopcroft} time, which is what Abraham et al. use for the one-to-one case.

If the applicants do not have knowledge over the overall preference distribution, this mechanism is strategy-proof due to the existence of the l-houses. The mechanism does not prioritize students with short preference lists, because it only considers at most two of the students' preferences. However, if students provide short preference lists, it is more likely for this algorithm to not find a matching, as the chances of a student being matched to his last-resort house are increased.


\documentclass[a4paper, 11pt]{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{url}
\usepackage{graphicx}
\usepackage{hyperref}

% Theorem Styles
\newtheorem{theorem}{Theorem}[section]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{corollary}[theorem]{Corollary}
% Definition Styles
\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]
\newtheorem{example}{Example}[section]
\theoremstyle{remark}
\newtheorem{remark}{Remark}

\begin{document}

\title{A predictive model for the Crowds protocol}
\date{\today}
\author{Marco Tinacci}
\maketitle

All the material of this work, included this document, can be found in the public repository \url{https://github.com/marcotinacci/predictive-crowds}.

\section{Introduction} % (fold)
\label{sec:introduction}
In this work we deal with the anonymity protocol CROWDS~\cite{DBLP:journals/tissec/ReiterR98}, we formulate the problem in three different ways extending the canonical set of rules with the possibility to give some inputs to tune the protocol's behavior. In Section~\ref{sec:preliminaries} we will introduce the protocol and the preliminary concepts that we are going to use, in Section~\ref{sec:models} we describe the three models and we show the respective achieved results and finally in Section~\ref{sec:future_work} we conclude discussing possible evolution of the work.
% section introduction (end)

\section{Preliminaries} % (fold)
\label{sec:preliminaries}

\subsection{CROWDS protocol} % (fold)
\label{sub:crowds_protocol}
The crowds protocol has been invented to preserve the anonymity of the sender of a message inside a network of communicating nodes. This approach is useful when corrupted nodes are present in the network and an external attacker may exploit information leaks to trace the origin of the message. For this purpose the protocol intuitively needs to show the sender as the origin of the message not more likely than the other nodes. To do so we use the following randomized algorithm:
\begin{enumerate}
	\item the sender selects a crowd member at random (possibly itself), and forwards the message to it,
	\item the selected router flips a biased coin. With probability $1-p_f$, where $p_f$ (forwarding probability) is given and globally known, it delivers the message directly to the destination. With probability $p_f$, it selects a crowd member at random (possibly itself) as the next router in the path, and forwards the message to it, re-encrypted with the appropriate pairwise key. The next router then repeats this step.
\end{enumerate}
% subsection crowds_protocol (end)

\subsection{Discrete-time Markov chains} % (fold)
\label{sub:discrete_time_markov_chains}

The following definition describe the basic model used in all the three models analyzed in this work.

\begin{definition}\label{def:dtmc}
A discrete-time Markov chain (DTMC) $\mathcal{D}$ is defined as follows
$$ \mathcal{D} = \langle S, \pi, T \rangle $$
where
\begin{itemize}
	\item $S$ is a finite set of states,
	\item $\pi \in \Delta(S)$ is the initial distribution over states,
	\item $T: S \rightarrow \Delta(S)$ is the transition function.
\end{itemize}
\end{definition}

$\Delta(S)$ is used to denotes the set of all the discrete probability distributions with domain in $S$, and we denote the number of states $|S|$ as $n$. A DTMC $\mathcal{D}$ describe an stochastic dynamic system whose dynamics can be summarized with a stochastic matrix $A \in [0,1]^{n \times n}$. A matrix is stochastic when every column is a discrete probability distribution. We can estimate future states of the system by multiplying the current distribution, a Dirac distribution pointing to the current state, by $A$ many times, one for every step forward. If we denote the current distribution $x_0 \in [0,1]^n$ we can describe the canonical dynamic of a DTMC with the following equation:
$$ x(k+1) = A \cdot x(k) $$
Using this rule we are able to predict how the probability mass is going to spread after a number of discrete steps $T \in \mathbb{N}$, indeed this measure, also known as transient probability, can be computed in the following way
$$ x(T) = A^T \cdot x_0 $$
The common target of the three models we considered is to tune the dynamics of the DTMC with some input that can affect state probabilities, edge probabilities or probability flow in order to obtain a transient probability that will preserve the anonymity of the sender.
% subsection discrete_time_markov_chains (end)
% section preliminaries (end)

\section{Models} % (fold)
\label{sec:models}

% section models (end)

\subsection{Input control array} \label{subs:inputcontrol}

\paragraph{Probabilistic behavior}
We model the protocol as a DTMC, states represent nodes of the network and edges represent transitions of the message from a node to another. Message forwarding is a probabilistic choice among nodes of the network. We can describe the dynamics of the protocol by using a stochastic matrix $A \in \mathbb{R}^{n\times n}$, with $n$ the number of nodes of the network. Since $A$ is stochastic the following condition holds

\begin{equation} \label{eq:stoc1}
a_{ij} \in [0,1],\ \forall i,j = 1,\dots,n
\end{equation}
\begin{equation} \label{eq:stoc2}
\sum_{i=1}^n a_{ij} = 1,\ \forall j = 1,\dots,n
\end{equation}

Let $a_{ij}$ be an element of $A$, we can describe how the dynamics modify the state using the following system

\begin{equation} \label{eq:system}
	x_i(k+1) = \sum_{j=1}^n a_{ij} x_j(k), \ \forall i = 1,\dots,n
\end{equation}

where the state $x(k) \in \mathbb{R}^n$ with $k \geq 0$ represents a probability distribution over states, then it holds $\sum_{i=1}^n x_i(k) = 1$ and $x_i(k) \in [0,1]$ for any $i = 1,\dots,n$.

\paragraph{Predictive control} % (fold)
\label{par:predictive_control}
We introduce a vector of parameters $u(k) \in \mathbb{R}^n$ at time $k$ that affect the system dynamics in the following way

\begin{equation} \label{eq:mpc}
		x(k+1) = Ax(k) + Bu(k)
\end{equation}

% TODO correggere dimensione B
for $k \in \mathbb{N}_0$ and $B \in \mathbb{R}^{n\times n}$. $B$ is a constant real square matrix that describe how parameters affect the state evolution.

% paragraph predictive_control (end)

\paragraph{Constraints} % (fold)
\label{par:constraints}
To preserve the probability distribution structure of the state we must ensure that, given any correct $x(k)$, the respective evolution $x(k+1)$ is still correct, that is $x(k+1) \in [0,1]^n$ and $\sum_{i=1}^n x_i(k+1) = 1$. From the former condition we derive the following constraints

\begin{equation} \label{eq:constr1}
	-1 \leq -\sum_j^n a_{ij} x_j(k) \leq \sum_j^m b_{ij} u_j(k) \leq 1 - \sum_j^n a_{ij} x_j \leq 1
\end{equation}
while, from the latter, we can derive
\begin{equation} \label{eq:constr2}
	\sum_{i=1}^n\sum_{j=1}^m b_{ij} u_j(k) = 0
\end{equation}

% paragraph constraints (end)

\paragraph{Objective} % (fold)
\label{par:objective1}
We use the canonical performance index (\ref{eq:performanceindex1}) for quadratic programming and we customize values in $P,Q \in \mathbb{R}^{n\times n}$ and $R \in \mathbb{R}^{m\times m}$ matrices to suit our target.
\begin{equation} \label{eq:performanceindex1}
	J(x(0),U) = x(N)^T P x(N) + \sum_{k=0}^{N-1} x(k)^T R x(k) + u(k)^T Q u(k)
\end{equation}
We are interested in maximizing the probability of being in a healthy node, with a priority for the receiver, and in minimizing the probability of using a corrupted node.
% paragraph objective (end)

\paragraph{Results} % (fold)
\label{par:results}

We developed a MATLAB script that implement the crowds scenario we defined in this section. We used YALMIP~\cite{YALMIP} to formulate the problem, generalizing it on the dimension of the network, on the number of corrupted nodes, on the number of parameters employed, on the length of the horizon and on the forward probability. In Figure~\ref{fig:mpc} the predicted state behavior is represented (above) with the respective control actions (below) over time. We can see that healthy nodes (in blue) will be reached with a higher probability with respect to corrupted nodes (in red). The receiver (in green) has the highest probability due to the relatively high forward probability $p_f$. If we increase the number of control inputs, that in this test is $3$, the blu lines tend to move away from the red lines, still remaining close to each other, while red lines tend to move close to zero.

\begin{figure}[htbp]
	\begin{center}
	\includegraphics[width=.8\textwidth]{images/mpc}
	\end{center}
	\caption{Control of a crowds network of 7 nodes with 3 parameters with model predictive control}
	\label{fig:mpc}
\end{figure}

The results represented in Figure~\ref{fig:mpc} derive from an experiment with the following settings:
\begin{itemize}
	\item the network contains $4$ healthy nodes, $2$ corrupted nodes and $1$ receiver;
	\item the prediction horizon is $10$;
	\item $3$ input parameters are used.
\end{itemize}
To maximize the probability of using a healthy node instead of a corrupted one we set weights in the performance index (\ref{eq:performanceindex1}) in the following way
\begin{equation} \label{eq:pqmatrix}
	P = Q = 10 \cdot I
\end{equation}
\begin{equation} \label{eq:rmatrix}
	r_{ij} =
	\begin{cases}
		1 & \mbox{if } i=j \mbox{ and $i$ is a healthy node} \\
		10 & \mbox{if } i=j \mbox{ and $i$ is a corrupted node} \\
		0 & \mbox{if } i\neq j \\
	\end{cases}
\end{equation}
where $r_{ij}$ is an element of $R$. The MATLAB code used to run this experiment can be found in the github repository \url{https://github.com/marcotinacci/predictive-crowds/blob/master/mpc_crowds_yalmip.m}.
% paragraph results (end)

\subsection{Input control matrix}
We consider a variation of the control applied in the first model from Section~\ref{subs:inputcontrol}, since every input parameter may affect more than one transition probability and, on the other side, every probability could be affected by more than one input parameter. This kind of formulation does not make clear what control input actually are in the concrete scenario, thus we reformulate the problem in order to have a one-to-one relation between control inputs and transition probabilities. We consider the same stochastic matrix $A$ and constraints (\ref{eq:stoc1}) and (\ref{eq:stoc2}) to represent the system dynamics, but we change the control parameters in order to act on the specific transition probabilities.

\paragraph{Predictive control}
We introduce the possibility to tune probabilities over the edges of the DTMC preserving the distribution constraints (\ref{eq:stoc1}) and (\ref{eq:stoc2}). Let $U \in \mathbb{R}^{n\times n}$ be the input matrix that will be used to tune the probabilities.
%In order to preserve the distribution constraints of $x(k)$ the following condition must hold on $U$
%\begin{equation} \label{eq:zerosum}
%	\sum_{j=1}^n u_{ij}(k) = 0
%\end{equation}
%where $u_{ij}(k)$ is an element of $U(k)$ and $k \geq 0$, for any $i = 1,\dots,n$.

Now we can extend the system dynamics (\ref{eq:system}) with parameters given by $U$ in this way
\begin{equation} \label{eq:system_extended}
	x_i(k+1) = \sum_{j=1}^n a_{ij} x_j(k) + u_{ij}(k), \ \forall i = 1,\dots,n
\end{equation}
%It is easy to see that, if (\ref{eq:zerosum}) holds and $x(k)$ is a valid distribution, $x(k+1)$ is a distribution as well.

\paragraph{Constraints}
After applying (\ref{eq:system_extended}) we need to preserve the distribution constraints, thus, for every $k$:
\begin{equation}\label{eq:udistr1}
	a_{ij}x_j(k) + u_{ij}(k) \in [0,1],\ \forall i,j = 1,\dots,n
\end{equation}
\begin{equation}\label{eq:udistr2}
	\sum_{j=1}^n a_{ij}x_j(k) + u_{ij}(k) = 1,\ \forall i = 1,\dots,n
\end{equation}

Since parameters $U(k)$ can increase or decrease transition probabilities $A$ out of the probability range $[0,1]$ we need to fix individual constraints to avoid that. Parameter $u_{ij}(k)$ is a local absolute modification of the probability mass flowing in a specific edge at time $k$, thus, starting from \ref{eq:udistr1} we can derive the following constraints

\begin{equation} \label{eq:constraints}
	-1 \leq -a_{ij}x_j(k) \leq u_{ij}(k) \leq 1-a_{ij}x_{j}(k) \leq 1
\end{equation}

\paragraph{Objective} % (fold)
\label{par:objective2}
We define a performance index with two features: firstly, minimizing the euclidean distance between the current state $x(k)$ and the ideal state $x_{id} \in [0,1]^n$, that is the distribution we want to obtain from the system; secondly, penalizing high modifications of input controls with specific costs. We use a weight vector $w^x$ to realize the former feature, while we use a weight matrix $W^u$ for the latter, to measure the effort of every input control on every probabilistic transition. The performance index is defined as follow
\begin{equation} \label{eq:index_ext}
	J(x(0),U) = \sum_{k=0}^{N-1}\sum_{i=1}^{n}\sum_{j=1}^{n} w_i^x(x_i(k) - x_{id})^2 + w_{ij}^u u^2_{ij}(k)
\end{equation}

Thus the problem can be summarize as the following optimization

\begin{equation} \label{eq:opt2}
	\begin{array}{ll}
		\underset{U}{\mbox{min}} & J(x(0),U) \\
		\mbox{subject to} &
		(\ref{eq:system_extended})(\ref{eq:udistr2})(\ref{eq:constraints}) \\
	\end{array}
\end{equation}

% paragraph objective (end)
\paragraph{Results} % (fold)
\label{par:results2}
Again we used the same environment (MATLAB + YALMIP) to formulate the problem and to solve it with quadratic programming. The result is shown in Figure~\ref{fig:qp-states} using the same notation from Figure~\ref{fig:mpc}. The outcome is clearly more accurate than the previous experiment as expected, since we are using more parameters. To be specific we parametrize weights on edges instead of states. Moreover, we do not give a limit to the number of parameters that can be used. In Figure~\ref{fig:qp-params} are shown the parameters affecting the transition probabilities starting from the first state.

Since the formulation of the problem is different, so it is the parameters setting. In this case we have to chose values for $w^x$, $w^u$ and $x_{id}$, while the rest of the values are the same as the previous experiment. The MATLAB source code to generate this experiment can be found at \url{https://github.com/marcotinacci/predictive-crowds/blob/master/crowds_qp_yalmip.m}.

\begin{equation}
	\begin{array}{l}
		x_{id} = (0.05, 0.05, 0.05, 0.05, 0, 0, 0.8) \\
		w^x = (1, 1, 1, 1, 1, 1, 1) \\
		w^u = \mbox{rand}(N) \\
%		\begin{pmatrix}
%			0.1 & \cdots & 0.1 \\
%			\vdots & \ddots & \vdots \\
%			0.1 & \cdots & 0.1 \\
%		\end{pmatrix} \\
	\end{array}
\end{equation}

We randomized $w^u$ with values in $[0,1]$ in order to distinguish values of the parameters like in Figure~\ref{fig:qp-params} that, in case of uniform costs, would assumed the same value as consequence of (\ref{eq:opt2}).

\begin{figure}[htbp!]
	\begin{center}
	\includegraphics[width=.8\textwidth]{images/qp-states}
	\end{center}
	\caption{Control of a crowds network of 7 nodes with quadratic programming}
	\label{fig:qp-states}
\end{figure}

\begin{figure}[htbp!]
	\begin{center}
	\includegraphics[width=.8\textwidth]{images/qp-params}
	\end{center}
	\caption{Parameter values on edges starting from the first state with quadratic programming}
	\label{fig:qp-params}
\end{figure}

\subsection{Topological control matrix} % (fold)
\label{sub:topological_control_matrix}

We formulate the same problem a third time. In this case we want to tune the weights over transitions before the probability mass is affected by them, like in the work of Bartocci et al.~\cite{DBLP:conf/tacas/2011}. To do so we formulate the problem as follows

\begin{equation}\label{eq:top-system}
	x(k+1) = (A+U(k)) x(k)
\end{equation}

Again, we have the following straightforward constraints:
\begin{itemize}
	\item $A$ is a stochastic matrix,
	\item $x(k)$ is a discrete probability distribution $\forall\ k=1\dots H$, where $H$ is the chosen horizon,
	\item $U$ is the input matrix that must be zero-sum per columns.
\end{itemize}

The performance index is again (\ref{eq:index_ext}). Using MATLAB and YALMIP we implemented the just described problem, the script can be found in the github repository  \url{https://github.com/marcotinacci/predictive-crowds/blob/master/crowds_nlp_yalmip.m}. A nonlinear programming solver was used until the last iteration over 1000 cycles, but the results were quite accurate, as shown in Figure~\ref{fig:nlp-states} and Figure~\ref{fig:nlp-params}.

\begin{figure}[htbp!]
	\begin{center}
	\includegraphics[width=.8\textwidth]{images/nlp-states}
	\end{center}
	\caption{Control of a crowds network of 7 nodes with nonlinear programming}
	\label{fig:nlp-states}
\end{figure}

\begin{figure}[htbp!]
	\begin{center}
	\includegraphics[width=.8\textwidth]{images/nlp-params}
	\end{center}
	\caption{Parameter values on edges starting from the first state with nonlinear programming}
	\label{fig:nlp-params}
\end{figure}

% subsection topological_control_matrix (end)

% paragraph results (end)
\section{Future work} % (fold)
\label{sec:future_work}

At the moment the scenario is not very realistic because we need to know which are the corrupted nodes before, but in that case any path avoiding those corrupted nodes would be a good solution. We could use the concept of partially observable model, like hidden Markov models, to model the perception of a leak of information. In this way penalties given to suspected corrupted nodes may be updated according to the perceived observations. Once the system has been modeled as a hidden Markov model, it can be converted into a DTMC and analyzed as we currently do. This kind of transformation is linear in the number of nodes times the number of observations.

In this work only the classical definition of the crowds protocol has been taken into account and the network is assumed to be strongly connected. Sparsity properties of different kind of topologies, block diagonal or band matrix structures for instance, may be exploited to improve analysis performance.
% section future_work (end)

\bibliographystyle{plain}
\bibliography{biblio}
% TODO repository github

\end{document}


\documentclass[answers]{exam}

\usepackage{graphicx}
\usepackage{float}
\usepackage{amsmath}
\usepackage{subcaption}
\usepackage{framed}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\usepackage{mathtools}
\usepackage{tikz}
\newcommand\numberthis{\addtocounter{equation}{1}\tag{\theequation}}
\newtheorem{lemma}{Lemma}
\newtheorem{claim}{Claim}
\usepackage{etoolbox}
\AtBeginEnvironment{solution}{\setcounter{claim}{0}}

\usetikzlibrary{fit}
\newcommand\addvmargin[1]{
  \node[fit=(current bounding box),inner ysep=#1,inner xsep=0]{};
}

\DeclareMathOperator*{\argmax}{arg\,max}

\newcommand{\nl}{\vspace{0.2cm}\\}
\newcommand{\nln}{\vspace{0.2cm}}

% First we setup the header and footer
\pagestyle{headandfoot}
\runningheadrule
\runningfootrule
\header{COL351: Analysis and Design of Algorithms (CSE, IITD, Semester-I-2020-21)}{}{Homework-3}
\footer{}{\thepage  \, of \numpages}{}

% We want the points for each question displayed on the left
%\pointname{points}
%\pointsinmargin

% Automatically total the points - make sure to compile TWICE
\addpoints

\begin{document}

\begin{center}
\fbox{\parbox{5.5in}{
\vspace{-0.1in}
\begin{itemize}
\item \small{The instructions are the same as in Homework-0, 1, 2.}
\end{itemize}
\vspace{-0.1in}
}}
\end{center}

\vspace{0.1in}

\vspace{0.1in}
% Some general text together with number of questions and total points possible
There are \numquestions\, questions for a total of \numpoints\, points.
\vspace{0.1in}
\hrule
 \vspace{0.2in}
\begin{questions}

\question[20] Design an $O(poly(n) \cdot 2^{n/4})$-time backtracking algorithm for the maximum independent set problem for graphs with bounded degree $3$ (these are graphs where all vertices have degree at most $3$). Give running time analysis and proof of correctness.

\begin{solution}

    Note that the following algorithm also works for graphs with vertices with degree more than $3$, however the exhibited runtime bound matches with the requested runtime bound only for graphs with bounded degree $3$.

    We exhibit an algorithm that works in $O((n + m)^2 2^{n'/4})$ time, where $m$ is the number of edges in the graph, and $n'$ is defined as $$\sum_{i \in \mathbb{N}} i \times\text{number of vertices with degree }(i + 2)$$
    Clearly for a graph with max degree $3$, we have $n' \le n$, and $m = O(n)$, so the algorithm has runtime in $O(n^2 2^{n/4})$.

    \textbf{Note:} The following algorithm can be optimized in a real-world implementation using backtracking and updating the graph instead of creating a new graph each time, and maintaining a heap
    to find the vertex with the least degree. However, the complexity still remains the same and there is no real complexity gain.

    Denote by $N(v)$ the neighbours of a vertex $v$ for the sake of brevity. Edges are denoted as ordered pairs $\{u, v\}$ and not as ordered pairs $(u, v)$ since the graph is undirected.

\paragraph{Algorithm}

\begin{algorithmic}[1]
\Function{MaximumIndependentSet}{Graph $G = (V, E)$}
    \If{$G$ is empty}
        \State \Return $\{\}$
    \EndIf
    \State \textbf{let} $v$ be the vertex with the least degree in $G$
    \If{$deg(v) = 0$}
        \State \Return $\{v\}\, \cup\, $\textsc{MaximumIndependentSet}$(G \setminus \{v\})$
    \ElsIf{$deg(v) = 1$}
        \State \textbf{let} $u$ be the (only) neighbour of $v$
        \State \Return $\{v\}\, \cup\, $\textsc{MaximumIndependentSet}$(G \setminus \{v, u\})$
    \ElsIf{$deg(v) = 2$}
        \State \textbf{let} $u_1, u_2$ be the (only) neighbours of $v$.
        \If{there is an edge between $u_1$ and $u_2$ in $G$}
            \State \Return $\{v\}\, \cup\,$ \textsc{MaximumIndependentSet}$(G \setminus \{v, u_1, u_2\})$
        \Else
            \State \textbf{let} $v'$ be a new vertex not in $G$.
            \State \textbf{let} $E_o = \{\{u_1, u\} \mid u \in N(u_1)\} \cup \{\{u, u_2\} \mid u \in N(u_2)\}$ \Comment{Edges incident to at least one vertex in the set $\{u_1, u_2\}$. Recall that we represent edges as unordered pairs.}
            \State \textbf{let} $E_n = \{\{v', u\} \mid u \in N(u_1) \cup N(u_2) \setminus \{v\}\}$ \Comment{Edges that will join $v'$ to all neighbours of $u_1, u_2$ except $v$}
            \State \textbf{let} $G' = ((V \cup \{v'\}) \setminus \{v, u_1, u_2\}, (E \cup E_n) \setminus E_o)$ \Comment{Merge $v, u_1, u_2$ into `super-node' $v'$}
            \State \textbf{let} $S = \,$ \textsc{MaximumIndependentSet}$(G')$.
            \If{$v'$ is in $S$}
                \State \Return $\{u_1, u_2\}\, \cup\, S \setminus \{v'\}$
            \Else
                \State \Return $\{v\}\, \cup\, S$
            \EndIf
        \EndIf
    \Else \Comment{In this case, $deg(v) \ge 3$}
        \State \textbf{let} $S_1 = \{v\}\, \cup\,$\textsc{MaximumIndependentSet}$(G \setminus (\{v\} \cup N(v)))$
        \State \textbf{let} $S_2 =\, $\textsc{MaximumIndependentSet}$(G \setminus \{v\})$
        \State \Return the larger of $S_1, S_2$.
    \EndIf
\EndFunction
\end{algorithmic}

\paragraph{Proof of Correctness} We prove the correctness of the algorithm using induction on the number of vertices in the graph.\nl
\textbf{Inductive hypothesis:} \textsc{MaximumIndependentSet}$(G)$ returns a maximum independent set of $G$ for all graphs $G$ with number of vertices less than $n$ for some $n > 0$.\nl
\textbf{Base Case:} In the case the graph is empty, the maximum independent set of the graph is the empty set, and hence the algorithm returns the correct answer in the case that the graph is empty.\nl
\textbf{Inductive step:}\nl
Firstly we claim the following:

    \begin{claim}
        For any graph $G$, and a vertex $v$ in it, if there is a maximum independent set in $G$ that contains exactly one vertex in $\{v\} \cup N(v)$, then there is a maximum independent set in
        $G$ containing $v$.
    \end{claim}

    \begin{proof}
        Suppose that there exists a maximum independent set $S$ in $G$ that contains exactly one vertex out of the vertices in $\{v\} \cup N(v)$. If that vertex is $v$, we are trivially done. Now
        assume that $S$ contains a vertex in $N(v)$, say $u$. Consider the set $S \setminus \{u\} \cup \{v\}$. Note that since there is no neighbour of $v$ in this set (which is because $u$ was the
        only neighbour of $v$ in $S$), this set is also a valid independent set by the validity of $S$ and this observation. Since the size of this set is the same as that of $S$, this set is also a
        maximum independent set in $G$, and we have shown the claim, as needed.
    \end{proof}

Now, we make cases on the vertex $v$ with the least degree in the graph.

\begin{enumerate}
    \item $deg(v) = 0$.\nl
    In this case, since this vertex is isolated, we claim that this vertex will always be in any maximum independent set of $G$. Indeed, suppose $S$ is any maximum independent set of $G$ that doesn't
        contain $v$. Then for any vertex $u$ in $S$, since $deg(v) = 0$, $u$ is not adjacent to any vertex in $S$. Hence we can add $u$ to $S$ to get a bigger independent set, which is a
        contradiction. By the inductive hypothesis, the recursive call gives the rest of the independent set, as needed.
    \item $deg(v) = 1$.\nl
    We claim that there is always at least one maximum independent set of $G$ that contains $v$. Let $u$ be the only neighbour of $v$. Consider any maximum independent set $S$ of $G$.
    \begin{enumerate}
        \item $S$ contains at least one of $u, v$.\nl
        Note that it can't contain both of these vertices. Also, by the claim in the beginning, we have a maximum independent set that contains $v$.
        \item $S$ contains none of $u, v$.\nl
        By a completely similar analysis, we can show that $S \cup \{v\}$ is also an independent set, which is larger than $S$, and it contradicts the fact that $S$ is a maximum independent set of $G$.
    \end{enumerate}
    Hence, we have shown our claim, so it is always optimal to choose a degree $1$ vertex. Since $u$ can't be in such an independent set, the remaining part of the set must come from $G \setminus \{u,
        v\}$, and must be independent. Note that the maximum independent set of this subgraph is an independent set by definition, and taking the union of this with $v$ yields an independent set of
        $G$. It is also the maximum independent set of $G$, since if it wasn't, then removing the vertex $v$ from the maximum independent set of $G$ which has $v$ would yield a larger independent set
        of $G \setminus \{u, v\}$. By the induction hypothesis, the recursive call in the function corresponding to this case gives a maximum independent set of this case, and we are done in this
        case.
    \item $deg(v) = 2$.\nl
        Let $u_1, u_2$ be the two neighbours of $v$.
        In this case, we make two subcases:
        \begin{enumerate}
            \item There is an edge between $u_1, u_2$. In this case, we can choose exactly one of $v, u_1, u_2$ in the maximum independent set (if we choose none, the maximum property is violated by
                adding in $v$, and if we choose two or more, the set is no longer independent). By the claim, it is optimal to choose $v$, and the remaining set must come from $G \setminus \{v,
                u_1, u_2\}$. Using a completely analogous argument as in the degree 1 case, we can show that the union of the chosen vertex (which is $v$) and the answer of the recursive call is a
                maximum independent set of the graph.
            \item There is no edge between $u_1, u_2$. In this case, we can choose either one or two vertices from $\{v, u_1, u_2\}$ in the maximum independent set of $G$. We combine the nodes $v,
                u_1, u_2$ into a node $v'$ to get a new graph $G'$. Note that the recursive call computes a maximum independent set of this new graph since it has two less vertices than $G$, call that set $S$.
                Our claim is the following:
                \begin{claim}
                    If $S$ contains $v'$, then it is optimal to choose $u_1, u_2$ in $G$, and if $S$ doesn't contain $v$, then it is optimal to choose $v$ in $G$.
                \end{claim}
                \begin{proof}
                We first show how to construct an independent set of $G$ from $S$ which has exactly one more vertex than $S$.
                We make two cases:
                    \begin{enumerate}
                        \item $S$ contains $v'$.\nl
                            Note that since $S$ contains $v'$, no vertex adjacent to $v'$ in $G'$ is in $S$, and thus no neighbour of $u_1, u_2$ is in $S$. Hence we can add $u_1, u_2$ after removing
                            $v'$ to get an independent set of $G$ with exactly one more vertex than $S$ has.
                        \item $S$ doesn't contain $v'$.\nl
                            In this case, we can add $v$ to $S$, and this is an independent set because no neighbour of $v$ is in $S$. Since we have added one vertex to $S$, the size of this set
                            is exactly one more than that of $S$.
                    \end{enumerate}
                Now we show that the size of any independent set $S'$ of $G$ is at most $1 + |S|$.
                We make two cases again:
                    \begin{enumerate}
                        \item At least two of $v, u_1, u_2$ are in $S'$.\nl
                            In this case, since $v$ is adjacent to $u_1, u_2$, so $v$ can't be in $S'$. Hence $u_1, u_2$ are both in $S$. Consider the corresponding independent set in $G'$ (formed
                            by choosing the vertices in $S'$ except $u_1, u_2$ but possibly also containing $v'$ -- the set without $v'$ is independent because of being a subset of $S'$, but as
                            we show in the next sentence, we can choose $v'$ as well and still have this set as independent in $G'$). Note
                            that since $u_1, u_2$ have both been chosen, $v'$ has been chosen in the corresponding independent set in $G'$ (since the neighbours of $v'$ are precisely the neighbours of
                            $u_1, u_2$ except $v$, and none of them have been chosen in $S'$ because $u_1, u_2$ have been chosen), so we have an independent set of size $|S'| - 1$ in $G'$. Now that we know that the maximum size of an
                            independent set in $G'$ is $|S|$, we have $|S'| - 1 \le |S|$, or $|S'| \le 1 + |S|$, as needed.
                        \item Exactly one of $v, u_1, u_2$ is in $S'$.\nl
                            Again, considering the corresponding independent set in $G'$, either $v'$ is chosen or it is not, and in either case, we have that the size of that set is either
                            $|S'|$ or $|S'| - 1$. Since the size of the corresponding independent set is at most $|S|$ (as $S$ is a maximum independent set of $G'$), $|S'|$ is at most $\max(|S|,
                            |S| + 1)$, so we have $|S'| \le |S| + 1$ in this case as well.
                    \end{enumerate}
                From this analysis, we see that any independent set of $G$ must have at most $|S| + 1$ vertices. Since we have exhibited an independent set of $G$ with exactly these many vertices,
                    it must be a maximum independent set of $G$ in each case.
                \end{proof}
            Note that this finishes the proof of the claim, and thus by the claim (and the construction mentioned in the proof), the independent set returned by the algorithm is indeed a
                maximum independent set of $G$.
      \end{enumerate}
  \item $deg(v) \ge 3$.\nl
      In this case, either we choose $v$ or not. If we choose $v$, the answer is the union of $v$ with the maximum independent set of the graph obtained after we remove $v$ and all its three
      neighbours, and if we don't, then the answer is the union of $v$ with the maximum independent set of the graph obtained after we remove $v$ from $G$, as done in class. Since both of these graphs
      have number of vertices strictly less than that of $G$, using the inductive hypothesis, we are done in this case as well.
\end{enumerate}

By the above case analysis, we can see that the algorithm returns a maximum independent set in all cases, and thus the induction is complete.

\paragraph{Time complexity analysis} Let $n, m$ be the number of nodes, edges in the graph, and let $n'$ be as defined. Let the number of constant-time operations
needed in this algorithm be denoted by $f(n, m, n')$. Then we claim that $f(n, m, n') \le c (n + m + 1)^2 2^{n'/4}$ for any non-empty graph where $c$ is a suitably chosen large enough constant, which will be specified later on.

We first begin with two claims:

\begin{claim}
    $n' = \sum_{v \in V(G)} \max(0, deg(v) - 2)$.
\end{claim}
\begin{proof}
    Note that for each vertex with degree $i$ contributes $i - 2$ to the right hand side if $i \ge 3$ and $0$ otherwise. Hence by grouping vertices by degree, we have the result. More precisely, we
    have:
    \begin{align*}
        \sum_{v \in V(G)} \max(0, deg(v) - 2)
           &= \sum_{j \in \mathbb{N}}\sum_{v \in V(G), deg(v) = j} \max(0, j - 2)\\
           &= \sum_{j \in \mathbb{N}_{\ge 3}}\sum_{v \in V(G), deg(v) = j} (j - 2)\\
           &= \sum_{i \in \mathbb{N}} \sum_{v \in V(G), deg(v) = i + 2} i\\
           &= \sum_{i \in \mathbb{N}} i \times |\{v \in V(G) \mid deg(v) = i + 2\}|\\
           &= n'
    \end{align*}
    as required.
\end{proof}

\begin{claim}
    Whenever we create a graph $G'$ from $G$ in the algorithm, $n'(G') \le n'(G)$.
\end{claim}
\begin{proof}
    Note that in the cases with minimum degree 0, 1, or at least 3, we always remove vertices from the graph, which either reduces the degree of any remaining vertex or keeps it the same, and also removes the non-negative contribution of the removed vertices from the expression of $n'$. Hence the inequality holds in that case. The same holds in the subcase of the case of minimum degree 2 where we have an edge between $u_1, u_2$.\nl
    The more important case is when the minimum degree is $2$ and we replace a group of nodes with a new node.\nl
    In the case when the degree is $2$, the highest possible degree of the combined node is $deg(u_1) + deg(u_2) - 2$ (this is in the case when $N(u_1), N(u_2)$ intersect in only one vertex $v$). The contribution of $v, u_1, u_2$ to $n'(G)$ is $0, \max(deg(u_1) - 2, 0), \max(deg(u_2) - 2, 0)$ respectively. Also, for any vertex adjacent to both $u_1, u_2$, its degree in the new graph is one less than its old degree, and this can't increase the difference between $n'(G'), n'(G)$. Hence we have
    $$n'(G') - n'(G) \le \max(deg(u_1) + deg(u_2) - 4, 0) - \max(deg(u_1) - 2, 0) - \max(deg(u_2) - 2, 0)$$

    Note that the real function $f(a) = \max(a, 0)$ is the point-wise maximum of the identity function and the 0 function, both of which are convex, and hence it is also convex. So it satisfies $f(a) + f(b) \ge 2f\left(\frac{a + b}{2}\right)$ (special case of Jensen's inequality), and thus applying this inequality to $(a, b) = (deg(u_1) - 2, deg(u_2) - 2)$, we have that
    \begin{align*}
        n'(G') - n'(G) &\le \max(deg(u_1) + deg(u_2) - 4, 0) - \max(deg(u_1) - 2, 0) - \max(deg(u_2) - 2, 0)\\
                       &= 2 \max\left(\frac{deg(u_1) + deg(u_2) - 4}{2}, 0\right) - \max(deg(u_1) - 2, 0) - \max(deg(u_2) - 2, 0)\\
                       &\le 0
    \end{align*}
    This completes the proof.
\end{proof}

Now we move to the main proof of the time complexity.

\textbf{Note that all the constants $c_i$ mentioned in the below proof shall be such that the mentioned upper bound holds for all $n$, and not just the particular $n$ considered in the
inductive step.}

\begin{proof}
    The proof goes by strong induction on the lexicographical ordering of tuples $(n, m, n')$ of non-negative integers.\nl
    For the base case, if the number of operations to check if the graph is empty is $c_1$, then if we have $c \ge c_1$, the base case holds.\nl
    The number of operations to find out the vertex with the least degree is at most linear in $n + m$. Suppose it is upper bounded by
    $c_2(n + m)$ for all $n, m$.\nl
    If the degree is $0$, then removing the vertex from the graph takes at most linear operations (wherever operations are mentioned, we mean constant-time operations unless specified otherwise).
    Suppose the time for this and taking the union and returning the set is upper bounded by $c_3(n + m)$ for all $n, m$. So we have
    \begin{align*}
        f(n, m, n') &\le f(n - 1, m, n') + c_3 n + c_2 n\\
        &\le c(n + m)^2 2^{n'/4} + (c_3 + c_2)(n + m)\\
        &\le c(n + m + 1)^2 2^{n' / 4}
    \end{align*}
        where the last inequality is true if we have $c > c_3 + c_2$.\nl
    If the degree is $1$, then removing both vertices and returning the set takes linear time, say upper bounded by $c_4(n + m)$ for all $n, m$. Then we have \begin{align*}
        f(n, m, n') &\le \max_{0 \le i \le n'}f(n - 2, m - \deg(u), n' - i) + (c_4 + c_2)(n + m)\\
        &\le c(n + m - 1)^2 2^{n'/4} + (c_4 + c_2)(n + m)\\
        &\le c(n + m + 1)^2 2^{n'/4}
\end{align*}
    where the last inequality is true if we have $c > c_4 + c_2$.\nl
    If the degree is $2$, the checking of the two subcases takes at most linear number of operations, upper bounded by, say $c_5(n + m)$ for all $n, m$.

    In the first subcase, the removal of the vertices to get a new graph, taking the union and returning takes at most $c_6(n + m)$ operations for all $n, m$, for some constant $c_6$. Then we have
    \begin{align*}
        f(n, m, n') &\le \max_{0 \le i \le n'}f(n - 3, m - 3 - \epsilon, n' - i) + (c_6 n + c_5 + c_2)(n + m)\\
                &\le c(n + m - 5)^2 2^{n'/4} + (c_6 n + c_5 + c_2)(n + m)\\
                &\le c(n + m - 5)^2 2^{n'/4} + (c_6 n + c_5 + c_2)(n + m)\\
                &\le c(n + m + 1)^2 2^{n'/4}
    \end{align*}
    where the last inequality is true if $c > c_5 + c_6 + c_2$.

    In the second subcase, the creation of the new graph takes a linear number of operations, and so does taking the union, searching in the returned set, modifying the returned set and returning it.
    Suppose it is upper bounded by $c_7(n + m)$ for all $n, m$.
    \begin{align*}
        f(n, m, n') &\le \max_{0 \le i \le n'}f(n - 2, m - 2 - \epsilon, n' - i) + (c_7 + c_5 + c_2)(n + m)\\
                &\le c(n + m - 3)^2 2^{n'/4} + (c_7 + c_5 + c_2)(n + m)\\
                &\le c(n + m + 1)^2 2^{n'/4}
    \end{align*}
    where the last inequality is true if $c > c_7 + c_5 + c_2$.

    If none of the above cases is true, then the minimum degree $d$ in the graph must be at least $3$, and as a consequence, all vertices in the graph must have degree at least $d$.
    Comparing sizes of sets and returning the larger one of them takes linear number of operations, and so does creating two graphs with one and two vertices removed.
    Suppose all of this is upper bounded by $c_8 (n + m)$ for all $n, m$.

    We compute the difference in $n'$ in this case. When we remove a vertex with degree $d$, $n'$ decreases by at least $2d - 2$, since the contribution of this vertex is $d - 2$, and the reduction
    in the degree of each other vertex is exactly $1$.
    When we remove this vertex and the set of all neighbours of this vertex, the reduction in $n'$ is at least $(d + 1)(d - 2)$. Let the exact reduction be $d'$.

    We then have:
    \begin{align*}
        f(n, m, n') &\le f(n - 1, m - d, n' - 2d + 2) + f(n - d - 1, m - d - \epsilon, n' - d') + (c_8 + c_2)(n + m)\\
                &\le cn^2 2^{(n' - 2d + 2) / 4} + c(n - d)^2
    2^{(n' - d') / 4} + c_8 n + c_2 n \\
                &\le c(n + m - d)^2 2^{(n' - 2d + 2) / 4} + c(n + m - 2d)^2 2^{(n' - (d+1)(d-2)) / 4} + (c_8 + c_2)(n + m) \\
                &\le c(n + m - 3)^2 2^{(n' - 4) / 4} + c(n + m - 6)^2 2^{(n' - 4) / 4} + (c_8 + c_2)(n + m)\\
                &= \frac{1}{2} \cdot c(n + m - 3)^2 2^{n' / 4} + \frac{1}{2} \cdot c(n + m - 6)^2 2^{n' / 4} + (c_8 + c_2)(n + m)\\
                &\le \frac{1}{2} \cdot c(n + m)^2 2^{n'/4} + \frac{1}{2} \cdot c(n + m)^2 2^{n'/4} + (c_8 + c_2)(n + m)\\
                &= c(n + m)^2 2^{n'/4} + (c_8 + c_2)(n + m)\\
                &\le c(n + m + 1)^2 2^{n'/4}
    \end{align*}
    where the last inequality is true if $c > c_8 + c_2$. We have made use of the facts that $d \ge 3$, $d' \ge 4d - 8$, and that $(d + 1)(d - 2)$ is increasing on $[2, \infty)$.

    Note that if we choose $c = 1 + c_2 + \max(c_1 - c_2, c_3, c_4, c_5 + c_6, c_5 + c_7, c_8)$, all of these claims will be valid, and thus our proof by induction is complete.
\end{proof}

Since our claim is completed, we have that for $n > 0$, our algorithm takes $O((n + m + 1)^2 2^{n'/4})$ time, since it takes the same order of constant time operations.
Note that we have $n' \le n$ as well as $m = O(n)$ for any graph with bounded degree 3, so the runtime of the algorithm is in $O(n^2 2^{n / 4})$, as required.

\end{solution}

\end{questions}
\end{document}


\graphicspath{{Pics/}}

\newpage
\section{Inequalities}

\begin{myitemize}
    \item \href{https://artofproblemsolving.com/articles/files/MildorfInequalities.pdf}
        {Olympiad Inequalities - Thomas J. Mildorf}
    \item \href{https://artofproblemsolving.com/articles/files/KedlayaInequalities.pdf}
        {$ A<B $ - Keran Kedlaya}
    \item \href{http://www.math.cmu.edu/~lohp/docs/math/mop2008/convexity-soln.pdf}
        {Convexity - Po Shen Loh}
    \item \href{https://web.evanchen.cc/handouts/Ineq/en.pdf}
        {Brief Intro to Ineqs - Evan Chen}
\end{myitemize}

\vspace{2em}

\den{Majorizes}{
    Given two sequences of real numbers $ x_1\ge x_2\ge\cdots \ge x_n $ and $
    y_1\ge y_2\ge \cdots\ge y_n  $, we say $ (x_n) $ \emph{majorizes} $ (y_n)
    $, written $ (x_n)\succ(y_n) $ if
    \begin{align*}
        x_1+x_2\cdots +x_n &=y_1+y_2\cdots +y_n\ \text{ and,}\\
        x_1+x_2\cdots +x_k &\ge y_1+y_2\cdots +y_k \ \ \forall 1\le k \le n-1
    \end{align*}
}

\den{Mean Values}{
    Given $ n $ \emph{positive reals} $ a_2 \dots a_n $, we have
    \begin{align*}
        \text{Arithmetic Mean} &:\quad \dfrac{\sum a_i}{n} = \dfrac{a_1 + a_2
        + \dots + a_n}{n}\\[1em]
        \text{Geometric Mean} &:\quad \sqrt[\leftroot{1}\uproot{4}n]{\prod
        a_i} = \sqrt[\leftroot{1}\uproot{4}n]{a_1a_2\dots a_n}\\[1em]
        \text{Quadratic Mean} &:\quad \sqrt{\dfrac{\sum a_i^2}{n}} =
        \sqrt{\dfrac{a_1^2+a_2^2+\dots +a_n^2}{n}}\\[1em]
        \text{Harmonic Mean} &:\quad \dfrac{n}{\sum \tfrac{1}{a_i}} =
        \dfrac{n}{\tfrac{1}{a_1}+\tfrac{1}{a_2} + \dots + \tfrac{1}{a_n}}
    \end{align*}
}

\den{Convexity}{
    The function $f$ is \textbf{convex} in interval $ I $ iff for all $a,b\in I$
    and for all $ t<1 $,
    \[tf(a) + (1-t)f(b) \ge f\left(ta+(1-t)b\right)\]
    Which if put in words, means that the line segment joining $ (a, f(a)) $
    and $ (b, f(b)) $ lies completely above the graph of the function.

    The function is convex in interval $ I $ if $ f' $ is \textbf{increasing} in $ I $
    or $ f'' $ is \textbf{positive} in $ I $.
}

\newpage
\subsection{Basic Inequalities}

\theo{http://mathworld.wolfram.com/TriangleInequality.html}{Triangle Inequality}{ For any complex numbers $ a_1, a_2 \ldots a_n $ the following holds:
\[\left | a_1 + a_2 \dots + a_k \right | \leq |a_1| + |a_2| + \dots + |a_k|\]}

\theo{https://artofproblemsolving.com/wiki/index.php?title=Root-Mean_Square-Arithmetic_Mean-Geometric_Mean-Harmonic_mean_Inequality}
{QM-AM-GM-HM}{
    Given $ n $ positive real numbers $ x_1, x_2, \dots x_n $, the
    following relation holds:

    \begin{align*}
        \impeq{\sqrt{\dfrac{x_1^2+\cdots+x_n^2}{n}}
            \ge\dfrac{x_1+\cdots+x_n}{n}\ge\sqrt[n]{x_1\cdots
        x_n}\ge\dfrac{n}{\dfrac{1}{x_1}+\cdots+\dfrac{1}{x_n}}}
    \end{align*}
    with equality if and only if $x_1=x_2=\cdots=x_n$.
}

\theo{https://artofproblemsolving.com/wiki/index.php?title=Arithmetic_Mean-Geometric_Mean_Inequality}
{Weighted AM-GM}{
    If $a_1, a_2, \dotsc, a_n$ are nonnegative real numbers, and $\lambda_1,
    \lambda_2, \dotsc, \lambda_n$ are nonnegative real numbers (the "weights")
    which sum to $ 1 $, then

    \begin{align*}
        \impeq{\lambda_1a_1 + \lambda_2a_2 +\dots +\lambda_na_n \ge
        a_1^{\lambda_1}a_2^{\lambda_2}\dots a_n^{\lambda_n}}
    \end{align*}

    Equality holds if and only if $a_i = a_j$ for all integers $i, j$ such that
    $\lambda_i \neq 0$ and $\lambda_j \neq 0$. We obtain the unweighted form of
    AM-GM by setting \[\lambda_1 = \lambda_2 = \dotsb = \lambda_n = \dfrac1n\]
}

\theo{}
{Muirhead's Inequality}{
    If $ a_1, a_2,\dots a_n $ are real positive reals, and $ (x_n)\succ(y_n)
    $, then we have,
    \[\impeq{\sum_{\text{sym}}a_1^{x_1}a_2^{x_2}\cdots a_n^{x_n} \ge
    \sum_{\text{sym}}a_1^{y_1}a_2^{y_2}\cdots a_n^{y_n}}\]
}

\theo{https://artofproblemsolving.com/wiki/index.php?title=Jensen's_Inequality}
{Jensen's Inequality}{
    Let $x_1,\dots,x_n\in\R$ and let $\a_1,\dots, \a_n\ge 0$ satisfy
    $\a_1+\dots+\a_n=1$.
    If $ f $ is a Convex Function, we have:
    \[\impeq{\a_1f(x_1)+\a_2f(x_2)\dots+a_nf(x_n) \ge f(\a_1x_1+\a_2x_2\dots+\a_nx_n)}\]
    If $f$ is a Concave Function, the sign of the fuction is reversed.
}

\theo{}
{Karamata's Inequality}{
    If $ f $ is convex and $(x_n)\succ(y_n)$, then
    \[\impeq{f(x_1) + f(x_2) \dots +f(x_n) \ge f(y_1)+f(y_2)\dots + f(x_n)}\]
    The reverse inequality holds if $ f $ is concave.
}

\begin{BoxedTheorem}[Tangent Line Trick: Best-Nice bound for function]{}{}
    Even if a fucntion is not convex or concave for us to use Jensen's
    Inequality, we can still find a number $ a $ such that for our required
    interval $I$, $f$ stays above (or below) the tangent line to $ f $ at
    $(a,f(a))$, that is
    \[\boxed{f(x) \ge f(a) + f'(a)(x-a)}\]
    \figdf{.9}{tangent_line_tricc}{The tangent line at $(1, f(1))$ is
    the best fit line in interval $ [0,2] $}
\end{BoxedTheorem}

\theo{}
{$ n-1 $ Equal Values}{
    Let $ a_1, a_2, \dots a_n $ be real numbers with $ a_1+a_2\dots a_n $
    fixed. Let $ f:\R\to\R $ be a function with exactly one \emph{inflection
    point}. If
    \[f(a_1)+f(a_2)+\dots + f(a_n)\]
    achieves a maximal or minimal value, then $ n-1 $ of the $ a_i $ are equal
    to each other.
}

\lem{Power function convexity}{
    If $ (x_i), (y_i), (m_i) $ be three sequences of real numbers, $ x, y\in
    \R $ and $ p>1 $. Then for $ \a\in(0, 1) $,
    \[\impeq{ (x+y)^p\ \le\ \a^{1-p} x^p \ + \ (1-\a)^{1-p} y^p} \]
    \[\impeq{ \sum (x_i+y_i)^pm_i \ \le\ \a^{1-p}\sum x_i^pm_i \ + \
    (1-\a)^{1-p}\sum y_i^pm_i} \]
    Equality holds iff
    \[\frac{x}{y} = \frac{x_i}{y_i} = \frac{\a}{1-\a}\]
}

\proof{
    Because $ f(x) = x^p $ is a convex function,
    \[(\a a + (1-\a)b)^p < \a a^p + (1-\a)b^p\]
    So setting $ \a a = x $ and $ (1-\a) b = y $, we get the first inequality.
    The second one is just an extention of the first one.
}

\theo{}{Minkowski}{
    If $ x_1, x_2\dots x_n $, $ y_1, y_2,\dots y_n $ and $ m_1, m_2, \dots m_n$
    be three sequence of real numbers and $ p>1 $, then
    \[\impeq{\left(\sum \left(x_i+y_i\right)^p m_i\right)^{\frac{1}{p}} \le \left(\sum
    x_i^pm_i\right)^{\frac{1}{p}} + \left(\sum y_i^pm_i\right)^{\frac{1}{p}}}\]
    Equality holds iff $ (x_i) $ and $ (y_i) $ are proportional.
}

\proof{
    Let us write,
    \begin{align*}
        A = \left(\sum x_i^pm_i\right)^{\frac{1}{p}},\ B = \left(\sum
        y_i^pm_i\right)^{\frac{1}{p}}
    \end{align*}

    From the second equation of \autoref{lemma:Power function convexity}, for
    $ \a\in(0, 1) $ we have
    \begin{align*}
        \sum(x_i+y_i)^pm_i \le \a^{1-p}A^p + (1-\a)^{1-p}B^p
    \end{align*}

    Setting $ \a $ be such that $ \dfrac{A}{B} = \dfrac{\a}{1-\a} $, by the equality case of
    the first equation of the lemma we get,
    \[\sum(x_i+y_i)^pm_i \ \le\ \a^{1-p}A^p + (1-\a)^{1-p}B^p= (A+B)^p\]
}

\theo{}
{Young's Inequality}{
    If $a, b> 0$ and $p, q> 0$ and $\dfrac{1}{p}+\dfrac{1}{q} = 1$, then we
    have \[ab \le \frac{a^p}{p} + \frac{b^q}{q} \]
    Equality occurs when $a^p = b^q $.
}

\proof{
    Consider the function $f(x) = e^x$, it is convex. So we have
    \[e^{\tfrac{1}{p}x + \tfrac{1}{q}y} \le e^{\tfrac{1}{p}x} +
    e^{\tfrac{1}{q}y}\]
    If we let $a=e^{\tfrac{x}{p}}, b= e^{\tfrac{y}{q}} $, we are done. Equality
    occurs when $ x = y $.
}

\thmbox{https://en.wikipedia.org/wiki/Generalized_mean}
{Weighted Power Mean}{
    Let $ a_1, a_2, \dots a_n $ and $ w_1, w_2, \dots + w_n $ be positive real
    numbers with $w_1 + w_2\dots w_n=1$. For real number $ r $, we define,
    \[P(r) = \left\{
            \begin{array}{ll}
                \left(w_1a_1^r + w_2a_2^r \dots +w_na_n^r\right)^{1/r} & r\ne
                0\\[1em]
                a_1^{w_1}a_2^{w_2}\dots a_n^{w_n} & r=0
            \end{array}
    \right.\]
    If $ r> s $, then $ P(r)\ge P(s) $. Equality occurs iff $ a_1 = a_2 =\dots
    = a_n $
}

\proof{
    First we show that, $ \displaystyle \lim_{r\to 0} P(r) = P(0) $. Using
    L'Hopital's law,
    \begin{align*}
        \lim_{r\to 0} \ln P(r) &= \lim_{r\to 0}\frac{\ln \sum w_ia_i^r}{r} =
        \lim_{r\to 0} \frac{\dfrac{\sum w_ia_i^r\ln a_i}{\sum
        w_ia_i^r}}{1}\\[.5em]
        &= \lim_{r\to 0} \frac{\sum w_ia_i^r\ln a_i}{\sum w_ia_i^r}\\[.5em]
        &= \sum\ln a_i^{w_i} \\[.2em]
        &= \ln P(0)
    \end{align*}
    First we prove that for positive $r<s$, we have $P(r)\le P(s)$. Define the
    function
    \[f:\mathbb{R}^+ \to \mathbb{R}^+ \quad f(x) = x^\frac{s}{r}\]
    This is a convex function, and so by Jensen's Inequality we have
    \[\begin{aligned}
        f\left(\sum^{n}_{i=1} w_ia_i^r\right) &\le \sum^{n}_{i=1}w_i f(a_i^r)\\
        \implies \sqrt[\leftroot{-3}\uproot{7}\tfrac{r}{s}]{\sum^{n}_{i=1}
    w_ia_i^r} &\le \sum^{n}_{i=1}w_i a_i^s
    \end{aligned}\]
}

\thmbox{https://artofproblemsolving.com/wiki/index.php?title=Cauchy-Schwarz_Inequality}
{Cauchy-Schward Inequality}{
    For any real numbers $a_1, \ldots, a_n$ and $b_1, \ldots, b_n$,
    \begin{align*}
        \left(a_1^2 + a_2^2 \dots a_n^2 \right) \left(b_1^2 +
        b_2^2\dots b_n^2 \right) \ge \left( a_1b_1 + a_2b_2 \dots a_nb_n \right)^2
    \end{align*}
    with equality when there exist constants $\mu, \lambda$ not both zero such
    that for all $1 \le i \le n$, $\mu a_i = \lambda b_i$.
}

The inequality sometimes appears in the following form.

\theo{https://artofproblemsolving.com/wiki/index.php?title=Cauchy-Schwarz_Inequality}
{Cauchy-Schwarz Inequality Complex form}{
    Let $a_1, a_2 \ldots, a_n$ and $b_1, b_2 \ldots, b_n$ be complex numbers. Then
    \begin{align*}
        \impeq{\left(\left|a_1^2\right| +\left|a_2^2\right| \dots\left|a_n^2\right|\right)
        \left(\left|b_1^2\right| +\left|b_2^2\right| \dots \left|b_n^2\right|\right)
        \ge \left|a_1b_1 + a_2b_2 \dots a_nb_n\right|^2 }
    \end{align*}
}

\theo{https://brilliant.org/wiki/titus-lemma/}{Titu's Lemma}{For positive reals $ a_1, a_2 \ldots a_n $ and $ b_1, b_2 \ldots b_n $ the following holds:
    \begin{align*}
        \impeq{\dfrac{a_1^2}{b_1} + \dfrac{a_2^2}{b_2} +\dots+
        \dfrac{a_n^2}{b_n} \ge \dfrac{\left(a_1 + a_2+\dots+
        a_n\right)^2}{\left(b_1+b_2+\dots+b_n\right)}}
    \end{align*}
}

\theo{https://artofproblemsolving.com/wiki/index.php?title=Hölder's_Inequality}
{Holder's Inequality}{
    If $a_1, a_2, \dotsc, a_n, b_1, b_2, \dotsc, b_n, \dotsc, z_1, z_2,
    \dotsc, z_n$ are nonnegative real numbers and $\lambda_a, \lambda_b,
    \dotsc, \lambda_z$ are nonnegative reals with sum of 1, then
    \[\impeq{a_1^{\lambda_a}b_1^{\lambda_b} \dots z_1^{\lambda_z} + \dots +
    a_n^{\lambda_a} b_n^{\lambda_b} \dots z_n^{\lambda_z}  \leq (a_1 + \dots +
    a_n)^{\lambda_a} (b_1 + \dots + b_n)^{\lambda_b} \dots (z_1 + \dots +
    z_n)^{\lambda_z}}\]
}

\theo{https://en.wikipedia.org/wiki/Popoviciu's_inequality}{Popoviciu's inequality}{
    Let $ f $ be a convex function on and interval $ I\in \R $. Then for any
    numbers $ x, y, z \in I $,
    \[\impeq{f(x)+f(y)+f(z)\ +3f\left(\frac{x+y+z}{3}\right) \ \ge
    2f\left(\frac{x+y}{2}\right)+
    2f\left(\frac{y+z}{2}\right)+
    2f\left(\frac{z+x}{2}\right)}\]
}

\newpage
\subsection{Tricks}

\begin{take_note*}[title={Some tricks to try}]{}
    \begin{enumerate}[left=0pt, label=\bfseries\arabic*., itemsep=5pt]
        \item Replace trigonometric functions by reals, and translate the problem
        \item \textbf{Smoothing}, replace two variable while keeping something
            invariant, to make the inequality sharper.
        \item \textbf{Convexity}, differentiate to check convexity, if the
            second derivative is positive on some interval, then the function
            is convex on that interval except probably at the endpoints, and
            concave otherwise.

            An example is $\ln \dfrac{1-x}{x}$. It is convex in
            $\left(0,\frac{1}{2} \right] $ and concave in $ \left[\frac{1}{2}, 1\right) $
        \item If there is product, and if the problem is `ad-hoc'y, then apply
            $ AM-GM $ and $ \ln $ to see if there is something to play with.
    \end{enumerate}
\end{take_note*}

\den{Homogeneous Expression}{
    Expression $ F(a_1, a_2 \dots a_n) $ is said to be homogeneous of degree $
    k $ if and only if there exists real $ k $ such that for every $ t>0 $ we
    have \[t^kF(a_1, a_2\dots a_n)=F(ta_1, ta_2\dots ta_n)\]
    If an expression is homogeneous, then the following can be assumed (only
    one at a time):
    \begin{align*}
        \boxed{\sum^n_{i=1} a_i = 1}\quad
        \boxed{\prod^n_{i=1} a_i = 1}\quad
        \boxed{\sum^n_{i=1} a_i^2 = 1} \quad
        \boxed{\sum_{Cyc}\ a_ia_{i+1} = 1 }\quad
        \boxed{a_1 = 1 \text{ or for some } i,\  a_i=1}\\
    \end{align*}
}

\theo{}
{Substitutions}{
    Some common substitution techniques:
    \begin{enumerate}[label=\bfseries\arabic*., left=0pt]
        \item For the condition $abc=1$, set $a=\dfrac{x}{y},\
            b=\dfrac{y}{z},\ c=\dfrac{z}{x}$\label{susbstitution:case_abc}
        \item If given $xyx=x+y+z+2 \implies \dfrac{1}{x+1} + \dfrac{1}{y+1} +
                \dfrac{1}{z+1} = 1$ then there exists $ a, b, c $ such
                that \[ x=\dfrac{b+c}{a},\ y=\dfrac{a+c}{b},\ z=\dfrac{b+a}{c}\]
        \item $2xyz+xy+yz+zx=1$ is just the inverse of the previous condition.
        \item $x^2+y^2+z^2=xyz+4  \text{ and }  |x|,\ |y|,\ |z| \geq 2$
            implies the existence of $ a, b, c $ such that
            \[abc=1 \text{ and }  x=a+\dfrac{1}{a},\ y=b+\dfrac{1}{b},\ z=c+\dfrac{1}{c}\]
            In fact even if only $ \max(|x|, |y|, |z|) > 2 $ is given, the
            result still holds.
    \end{enumerate}
}

\lem{}{
    The following inequality holds for every positive integer $ n $
    \[\impeq{2\sqrt{n+1} - 2\sqrt{n} < \sqrt{\dfrac{1}{n}} < 2\sqrt{n} - 2\sqrt{n-1}}\]
}

\lem{}{
    Given $ 4 $ posititve real numbers $ a<b<c<d $. Call the score of a
    permutation $ a_1, a_2, a_3, a_4 $ of the four given reals be equal to the real
    \[\left|\dfrac{a_1}{a_2} - \dfrac{a_3}{a_4}\right| \]
    Then the minimum score over all permutaions is $\quad\left|\dfrac{a}{c} -
    \dfrac{b}{d}\right|$
}

\newpage\subsection{Problems}

\prob{https://artofproblemsolving.com/community/c6h78687}{APMO 1991 P3}{E}{Let $a_1$, $a_2$, $\cdots$, $a_n$, $b_1$, $b_2$, $\cdots$, $b_n$ be positive real numbers such that $a_1 + a_2 + \cdots + a_n = b_1 + b_2 + \cdots + b_n$. Show that
\[ \dfrac{a_1^2}{a_1 + b_1} + \dfrac{a_2^2}{a_2 + b_2} + \cdots + \dfrac{a_n^2}{a_n + b_n} \geq \dfrac{a_1 + a_2 + \cdots + a_n}{2} \]}

\prob{https://artofproblemsolving.com/community/c6h214703p1187159}
{ISL 2007 A3}{EM}{
    Let $ n$ be a positive integer, and let $ x$ and $ y$ be a positive real
    number such that $ x^n + y^n = 1.$ Prove that
    \[\left(\sum^n_{k = 1} \frac {1 + x^{2k}}{1 + x^{4k}} \right) \cdot
    \left( \sum^n_{k = 1} \frac {1 + y^{2k}}{1 + y^{4k}} \right) < \frac
    {1}{(1 - x) \cdot (1 - y)}. \]
}

\begin{solution}
    We need to turn the ugly fraction $\dfrac{1+x^{2k}}{1+x^{4k}}$ into
    something more usable. Also the right hand size looks almost like the sum
    of geometric series $\sum^{n}_{i=1}\frac{1}{x}$. Which motivates us to
    look for a $t$ such that
    \[\frac{1+x^{2k}}{1+x^{4k}} \le \frac{1}{x^t}\]
    But by Karamata's inequality, we know that $t = k$, as $x + x^3 \le 1 +
    x^4$ for all positive $x$. Where equality holds iff $x=1$
    \[\begin{aligned}
     \left(\sum^n_{k = 1} \frac {1 + x^{2k}}{1 + x^{4k}} \right) \cdot
     \left( \sum^n_{k = 1} \frac {1 + y^{2k}}{1 + y^{4k}} \right)&<
     \left(\sum^{n}_{k=1}\frac{1}{x^k}\right)\cdot
     \left(\sum^{n}_{k=1}\frac{1}{y^k}\right)\\[1em]
     &= \frac{1-x^n}{(1-x)x^n} \cdot \frac{1-y^n}{(1-y)y^n}\\
     &= \frac{y^n}{(1-x)x^n} \cdot \frac{x^n}{(1-y)y^n} =
     \frac{1}{\left(1-x\right)\cdot (1-y)}
    \end{aligned}\]
\end{solution}

\prob{https://artofproblemsolving.com/community/c6h355781p1932917}{ISL 2009 A2}{\hl{(Kori nai)}}{Let $a$, $b$, $c$ be positive real numbers such that $\dfrac{1}{a} + \dfrac{1}{b} + \dfrac{1}{c} = a+b+c$. Prove that:
\[\dfrac{1}{(2a+b+c)^2}+\dfrac{1}{(a+2b+c)^2}+\dfrac{1}{(a+b+2c)^2}\leq \dfrac{3}{16}.\]}

\prob{https://artofproblemsolving.com/community/c6h1634929p10278399}
{ARO 2018 P11.2}{M}{
    Let $n\geq 2$ and $x_{1}, x_{2},\dots, x_{n}$ positive real numbers. Prove
    that
    \[\dfrac{1+x_{1}^2}{1+x_{1}x_{2}}+\dfrac{1+x_{2}^2}{1+x_{2}x_{3}}+...+
    \dfrac{1+x_{n}^2}{1+x_{n}x_{1}}\geq n\]
}

\solu{
    The inequality says sum is greater, so if the product is greater, then we
    are done by AM-GM.
}

\prob{https://artofproblemsolving.com/community/c6h1455842p8375615}
{Turkey TST 2017 P5}{M}{
    For all positive real numbers $a,b,c$ with $a+b+c=3$, show
    that \[a^3b+b^3c+c^3a+9\geq 4(ab+bc+ca)\]
}

\solu{Always try the most simple ineq possible, AM-GM}

\prob{https://artofproblemsolving.com/community/c6h488342p2736375}{IMO 2012 P2}{Varies}{Let $n\ge 3$ be an integer, and let $a_2,a_3,\ldots ,a_n$ be positive real numbers such that $a_{2}a_{3}\cdots a_{n}=1$. Prove that
\[(1 + a_2)^2 (1 + a_3)^3 \dotsm (1 + a_n)^n > n^n.\]}\label{problem:imo_2012_p2}

\solu{The main idea is to look for the ans of the ques, $ (1+a_k)^k >= ? $. We have $ k^{th} $ power. So if we can get a $ k $ term sum inside of the brackets, we can get a clean term for $ ? $ from AM-GM. And $ 1 $ seems like it's crying to be partitioned. So we write the term as $ \left(a_k + \dfrac{1}{k-1}+\dots+\dfrac{1}{k-1}\right) $}

\solu{*Looks at the $ a_2a_3\dots a_n=1 $ condition*\\Hey, we have a \hrf{susbstitution:case_abc}{substitution} for this one, why not try it out...\\darn it, i still have to do the partition thing to cancel out the powers $ >( $}

\prob{https://artofproblemsolving.com/community/c6h18486p124417}{ISL 1998 A1}{can't judge now}{Let $a_{1},a_{2},\ldots ,a_{n}$ be positive real numbers such that $a_{1}+a_{2}+\cdots +a_{n}<1$. Prove that

\[ \dfrac{a_{1} a_{2} \cdots a_{n} \left[ 1 - (a_{1} + a_{2} + \cdots + a_{n}) \right] }{(a_{1} + a_{2} + \cdots + a_{n})( 1 - a_{1})(1 - a_{2}) \cdots (1 - a_{n})} \leq \dfrac{1}{ n^{n+1}}. \]}\label{problem:1998A1}

\solu{Simplifying and making it symmetric, we get to the inequality \[\prod_{i=1}{n} \dfrac{1-a_i}{a_i} \ge n^{n+1}\]Now approaching similarly as \hrf{problem:imo_2012_p2}{this} problem, we get to the solution.}

\prob{https://artofproblemsolving.com/community/c6h57603p354109}{ISL 2001 A1}{E}{Let $ a, b, c$ be positive real numbers so that $ abc = 1$. Prove that
\[ \left( a - 1 + \dfrac 1b \right) \left( b - 1 + \dfrac 1c \right) \left( c - 1 + \dfrac 1a \right) \leq 1. \]}

\solu{Substitute.}

\prob{https://artofproblemsolving.com/community/c6h19778p131846}{ISL 1999 A1}{EM}{Let $n \geq 2$ be a fixed integer. Find the least constant $C$ such the inequality

    \[\sum_{i<j} x_{i}x_{j} \left(x^{2}_{i}+x^{2}_{j} \right) \leq C \left(\sum_{i}x_{i} \right)^4\]

holds for any $x_{1}, \ldots ,x_{n} \geq 0$ (the sum on the left consists of $\binom{n}{2}$ summands). For this constant $C$, characterize the instances of equality.}

\solu{Follow the ineq sign and remember AM-GM.}

\prob{https://artofproblemsolving.com/community/c6h1671272p10632289}{ISL 2017 A1}{E}{Let $a_1,a_2,\ldots a_n,k$, and $M$ be positive integers such that
    \[\dfrac{1}{a_1}+\dfrac{1}{a_2}+\cdots+\dfrac{1}{a_n}=k\quad\text{and}\quad a_1a_2\cdots a_n=M\]

    If $M>1$, prove that the polynomial
    \[P(x)=M(x+1)^k-(x+a_1)(x+a_2)\cdots (x+a_n)\]
has no positive roots.}

\solu{The same idea used in \hrf{problem:1998A1}{this} and \hrf{problem:imo_2012_p2}{this}, spreading an expression to perform AM-GM on it.}

\prob{https://artofproblemsolving.com/community/c6h1480690p8639254}{ISL 2016 A1}{M}{Let $a$, $b$, $c$ be positive real numbers such that $\min(ab,bc,ca) \ge 1$. Prove that \[\sqrt[3]{(a^2+1)(b^2+1)(c^2+1)} \le \left(\dfrac{a+b+c}{3}\right)^2 + 1.\]}

\solu{Try the simpler version with two variables first. Now you can use this discovery with a little bit of cleverness to solve the problem. The clever part is to notice that 4 variable ineq is more solvable than a 3 variable one.}

\prob{https://artofproblemsolving.com/community/c6h1480705p8639281}{ISL 2016 A2}{M}{Find the smallest constant $C > 0$ for which the following statement holds: among any five positive real numbers $a_1,a_2,a_3,a_4,a_5$ (not necessarily distinct), one can always choose distinct subscripts $i,j,k,l$ such that
\[ \left| \dfrac{a_i}{a_j} - \dfrac {a_k}{a_l} \right| \le C. \]}

\solu{Simplify the problem to get the ans first. Think about what is the smallest such value for any given $ 4 $ positive reals.}

\prob{https://artofproblemsolving.com/community/c6h14091p99756}{ISL 2004 A1}{E}{Let $n \geq 3$ be an integer. Let $t_1$, $t_2$, ..., $t_n$ be positive real numbers such that \[n^2 + 1 > \left( t_1 + t_2 + \cdots + t_n \right) \left( \dfrac{1}{t_1} + \dfrac{1}{t_2} + \cdots + \dfrac{1}{t_n} \right).\] Show that $t_i$, $t_j$, $t_k$ are side lengths of a triangle for all $i$, $j$, $k$ with $1 \leq i < j < k \leq n$.}

\solu{Easy solution by induction. For a more elegant solution, write the right side as sum of paired factors. Finding when the inequality breaks and relating it to the end statement.}

\prob{https://artofproblemsolving.com/community/c6h219647p1218538}{ISL 1996 A2}{EM}{Let $ a_1 \geq a_2 \geq \ldots \geq a_n$ be real numbers such that for all integers $ k > 0,$

    \[ a^k_1 + a^k_2 + \ldots + a^k_n \geq 0.\]

    Let $ p =\max\{|a_1|, \ldots, |a_n|\}.$ Prove that $ p = a_1$ and that

\[ (x - a_1) \cdot (x - a_2) \cdots (x - a_n) \leq x^n - a^n_1\] for all $ x > a_1.$}

\solu{After the first part, apply AM-GM on the whole left side, this not gonna work, since we can't bound $ \sum a_i $ wrt $ a_1 $. So what if we divide both side by $ (x-a_1) $ and then apply AM-GM?}

\newpage
\subsubsection{Smoothing And Convexity}

\begin{take_note*}[title={Some usual tricks}]{}
    \begin{enumerate}
        \item Bring $ x, y $ closer, keeping $ x+y $ constant.
        \item If we need to smoothen up the value $ xy $, then take $ \ln $ on both side.
        \item Work with different variables.
    \end{enumerate}
\end{take_note*}

\prob{https://artofproblemsolving.com/community/c6h55341p343867}
{USAMO 1998 P3}{H}{
    Let $a_0,a_1,\cdots ,a_n$ be numbers from the interval $(0,\pi/2)$ such that
    \[\tan\left(a_0-\dfrac{\pi}{4}\right)+ \tan \left(a_1-\dfrac{\pi}{4}\right)+\cdots
    +\tan\left(a_n-\dfrac{\pi}{4}\right)\geq n-1\]
    Prove that
    \[\tan a_0\tan a_1 \cdots \tan a_n\geq n^{n+1}\]
}

\solu{
    Setting $b_i = \tan(a_i)$, we get
    \[\sum^{n}_{i=0} \frac{b_i-1}{b_i+1} \ge n-1 \implies \sum^{n}_{i=0}
    \frac{1}{b_i+1} \le 1\]
    We need to show that geometric mean of $b_i$ is greater than $n$. But we
    know that the harmonic mean is smaller than geometric mean. So we want to
    prove
    \[n \le \frac{n+1}{\sum \tfrac{1}{b_i}} \implies \sum^{n}_{i=0}
    \frac{1}{b_i} \le \frac{n+1}{n}\]
    If we let $\dfrac{1}{b_i +1} = c_i$, then $\dfrac{1}{b_i} =
    \dfrac{c_i}{1-c_i}$, which is a convex function, since
    \[f(x) = \frac{x}{1-x}, \quad f'(x) = \frac{1}{(x-1)^2}\quad \text{which is
    increasing for } x \in [0, 1)\]
    So $\displaystyle\sum^{n}_{i=0}\frac{1}{b_i}$ is maximized when all the
    $c_i$'s are equal to $\dfrac{1}{n}$.
}

\prob{https://artofproblemsolving.com/community/c6h337923p1808247}
{USAMO 1974 P2}{E}{
    Prove that if $ a,b,$ and $ c$ are positive real numbers, then
    \[a^ab^bc^c \ge (abc)^{(a+b+c)/3}\]
}

\solu{Do $\ln$, and obvious by Jensen.}

\prob{https://artofproblemsolving.com/community/q1h61144p368290}
{India 1995}{E}{
    Let $x_{1},x_{2},...,x_{n}>0$ be real numbers such that
    $x_{1}+x_{2}+x_{3}+...+x_{n}=1$. Prove the inequality
    \[\frac{x_{1}}{\sqrt{1-x_{1}}}+\frac{x_{2}}{\sqrt{1-x_{2}}}+....+
    \frac{x_{n}}{\sqrt{1-x_{n}}}\geq \sqrt{\frac{n}{n-1}}\]
}

\solu{easy smoothing}

\prob{https://artofproblemsolving.com/community/q1h355706p1932030}
{Vietnam 1998}{E}{
    $ x_1,x_2...x_n$ are real numbers such that
    \[\dfrac{1}{x_1+1998} +\dots +\dfrac{1}{x_n+1998}= \dfrac{1}{1998}\]
    Prove that
    \[\frac{\sqrt[n]{x_1\dots x_n}}{n-1} \ge 1998\]
}

\solu{Translate the given expression in a nicer way with new variables...}

\prob{https://artofproblemsolving.com/community/c6h58608p358019}{IMO 1974 P2}{M}{The variables $a,b,c,d,$ traverse, independently from each other, the set of positive real values. What are the values which the expression \[ S= \dfrac{a}{a+b+d} + \dfrac{b}{a+b+c} + \dfrac{c}{b+c+d} + \dfrac{d}{a+c+d} \] takes?}

\solu{$ \dfrac{x}{y+c} \le \dfrac{x}{y} \le \dfrac{x}{y-c} $}

\prob{https://artofproblemsolving.com/community/c6h45679p289327}{Bulgaria 1995}{E}{Given $n$ real number $x_1,x_2,...,x_n\in [0,1]$ . Prove the following inequality
\[(x_1 + x_2 +\dots+ x_n) - \left( x_1x_2 + x_2x_3 +\dots+ x_nx_1\right) \le \left\lfloor\frac{n}{2}\right\rfloor\]}


%\documentclass[12pt]{article}
%\usepackage{amsmath}
%\usepackage{graphicx, color}
%\usepackage{amssymb}
%\usepackage{listings} %source code listing
%\usepackage{multirow}
%%\usepackage[version=2]{mhchem}
%\usepackage{subfig}
%\usepackage{hyperref}
%\usepackage{units}
%\usepackage{gensymb}
%\usepackage{adjustbox}
%\usepackage{listings}
%\usepackage{color}
%\usepackage{tcolorbox}
%
%\definecolor{codegreen}{rgb}{0,0.6,0}
%\definecolor{codegray}{rgb}{0.5,0.5,0.5}
%\definecolor{codepurple}{rgb}{0.58,0,0.82}
%\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
%
%\newcommand{\specialcell}[2][c]{%
%  \begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}
%
%
%
%\title{Reactor physics with Python \\ Lecture Notes}
%
%
%\author{Zs.~Elter. E. Branger, M. Preston \\ Uppsala University \\
%        Division of Applied Nuclear Physics}%\corref{cja}}
%%
%\date{2021.}
%\begin{document}

\section{Appendix}

\subsection{Solid angle}

When describing the movement of neutrons, it is often not only the speed of neutrons but also the velocity $\mathbf{v}$ of neutrons which is of interest. Nevertheless, instead of handling the velocity directly, it is rather the energy $E$ and the direction unit vector $\mathbf{\Omega}$:

\begin{equation}
\mathbf{\Omega}=\frac{\mathbf{v}}{|\mathbf{v}|}=\mathbf{e_x}\sin\theta\cos\phi+\mathbf{e_y}\sin\theta\sin\phi+\mathbf{e_z}\cos\theta
\end{equation}

\noindent where we introduce the spherical space coordinates, the polar angle $\theta$ and the azimuthal angle $\phi$.

\begin{figure}[ht!]
\protect \centering{
\includegraphics[scale=0.44] {figures/A-solidangle.png}}\protect
\caption{\label{fig:solidangle} \footnotesize{Representation of direction and solid angle.}}
\end{figure}

Thus when integrating a function over velocity, we can substitute

\begin{equation}
\int f(\mathbf{v})d^3v=\int\limits_0^\infty v^2 dv\int\limits_0^{2\pi}d\phi\int\limits_0^\pi \sin\theta f(\mathbf{v})d\theta
\end{equation}

\noindent and we can further define that

\begin{equation}
\int\limits_{4\pi}d\mathbf{\Omega}=\int\limits_0^{2\pi}d\phi\int\limits_0^\pi \sin\theta d\theta
\end{equation}

\noindent and we can notice that in fact the differential $d\mathbf{\Omega}$ is a solid angle (an area illustrated with the small patch in Figure \ref{fig:solidangle})

\[
d\mathbf{\Omega}=\sin\theta d\theta d\phi
\]

We will frequently encounter integrals over this small solid angle.

\subsection{Monte Carlo methods}

Monte Carlo methods provide a direct solution to the transport equation, since it allows for faithful simulation of neutron trajectories while they move around in matter. Of course it still means that we need to implement the correct physics (for example scattering kernel), and have the right cross sections, but we do not need to discretize space, angle or energy. Thus, the geometry can be arbitrarily complex and handled in 3D.

The idea is that one tracks neutrons as from birth to death, and randomly samples the collisions and the locations of the collisions the neutron enters. If one could simulate every neutron than the accuracy would be the same as performing a measurement. Nevertheless, due to the lack in computational power, usually we are not able to simulate every neutron, therefore our results (k-eff,  reaction rates, flux) will be average over the population of neutrons sampled. An other source of accuracy in this methods comes from the uncertainty in the measured cross section data, which propagates into the Monte Carlo simulations.

Covering Monte Carlo particle transport could be in itself a full course, so here we cover only some very basic ideas we will be using. In order to faithfully simulate neutrons, we will need

\begin{itemize}
\item To implement physics (for example scattering kernels)
\item Sample coordinates, angles, reactions, energies. So in general: sample probability density functions
\item Track neutrons and perform coordinate transformations.
\end{itemize}

Therefore, in this Appendix we briefly review pseudo random numbers, and  probability density functions sampling.

\subsubsection{Random numbers}

In order to sample probability density functions, we need random numbers. What is random? Something which lacks pattern and predictability. It doesn't make sense to discuss the randomness of a single number or event, only for a sequence of number of events. There are several methods to decide whether a sequence of numbers can be considered random.

One could use measurement of random fluctuations of nature to generate random numbers. But this needs the data to be tabulated. Similarly we could use irrational numbers, such as $\pi$ as shown in Fig. \ref{fig:randompi}. In several applications in fact such tabulated data is being used.

\begin{figure}[ht!]
\protect \centering{
\includegraphics[scale=0.44] {figures/randomized_pi_rect.png}}\protect
\caption{\label{fig:randompi} \footnotesize{The digits of $\pi$, each digit 0-9 is assigned to a different color.}}
\end{figure}

However, it is often more practical to use a computer to generate random numbers. Now of course a computer is deterministic, so these will only be \textit{pseudo random numbers}. The great advantages is, that if using the same seed, we can get the same sequence, which is useful for tests and debugging.

For example a simple algorithm to generate pseudo random numbers can be $X_{n+1}=\text{k middle digit of }\: X_n^2$. But algorithms like that will have a period (once the middle digits repeat we will reproduce the same sequence of numbers over and over again). A slightly improved algorithm:

\[
X_{n+1}=(aX_n+c)\%m
\]

\noindent for example with $a=1664525$, $c=1013904223$, $m=2^{32}$. This will produce longer periods.

Nevertheless, we don't need to worry about about pseudo random numbers, since for our applications the basic pseudo random number generators of numpy will be perfect.

\subsubsection{discrete distributions}

Consider we have three events with the following probabilities:

\begin{table}
\centering\begin{tabular}{c | c}
event & probability \\
\hline
A & 0.6 \\
B & 0.3 \\
C & 0.1
\end{tabular}
\end{table}

In order to sample random events from this distrubution we can follow the algorithm in Fig. \ref{fig:discrete}, which can be generalized for cases when there are more possible events. We perform the cumulative sum of probabilities, and get event j if $\sum_{i=1}^{j-1}p_i\leq r <\sum_{i=1}^{j}p_i$

\begin{figure}[ht!]
\protect \centering{
\includegraphics[scale=0.44] {figures/discreterandom.png}}\protect
\caption{\label{fig:discrete} \footnotesize{Algorithm to sample discrete events.}}
\end{figure}

In neutron transport, such discrete events will be important when we need to decide which reaction is going to happen with a neutron (with a probability $\Sigma_i/\Sigma_t$, or when we want to sample the number of neutrons emitted in a fission event.

\subsubsection{Sampling continuous distributions}

Let's consider that the distribution of a random value $x$ is described by a probability density function $p(x)$, and the related cumulative distribution function is $F(x)=\int_{-\infty}^xp(t)dt$ as shown in Fig. \ref{fig:pdfcdf}

\begin{figure}[ht!]
\protect \centering{
\includegraphics[scale=0.44] {figures/cont_distr1n.png}}\protect
\caption{\label{fig:pdfcdf} \footnotesize{Illustration of a probability density function and the related cumulative distribution function.}}
\end{figure}

The cumulative distribution function is going to take values between 0 and 1. So if one can have uniformly distributed random numbers between 0 and 1, it is possible to convert the random number $r$ to get a random value $x$:

\[
x=F^{-1}(r)
\]

This method can be applied however only, when it possible to easily integrate the probability density function. This is for example the case in neutron transport when we need to sample random distances between collisions, since as we saw before, the distribution of the distance between collisions follows an exponential distribution.

However, if the given distribution is less well-behaving, and it is difficult to obtain the cumulative distribution function, we still have several options, from which here we review only one, the so called rejection sampling. We draw a random number, convert it to be between a and b, we draw an other one to create a y value based on the maximum of the pdf. If we are under the curve we accept the value, otherwise we reject it, and draw a new number. The algorithm is summarized in Fig. \ref{fig:rejection} Such a method will be useful, when for example sampling the birth neutron spectrum.

\begin{figure}[ht!]
\protect \centering{
\includegraphics[scale=0.44] {figures/rejection.png}}\protect
\caption{\label{fig:rejection} \footnotesize{Algorithm of rejection based sampling.}}
\end{figure}

\subsubsection{Coordinates after scattering}

During elastic scattering we derived that both the energy and the direction of the neutron is going to change, and we saw that there is a connection between the two. Usually we will sample the new energy from the scattering kernel, and use it to figure out the scattering angle. However, then we still need to update the direction of the neutron. For transforming the directions we can use the following formulae (from \url{https://docs.openmc.org/en/v0.10.0/methods/physics.html}), which is based on coordinate transformation considerations not derived here.

\begin{equation}
u' = \mu u + \frac{\sqrt{1 - \mu^2} ( uw \cos\phi - v \sin\phi )}{\sqrt{1 -
w^2}}
\end{equation}
\begin{equation}
v' = \mu v + \frac{\sqrt{1 - \mu^2} ( vw \cos\phi + u \sin\phi )}{\sqrt{1 -
w^2}}
\end{equation}
\begin{equation}
w' = \mu w - \sqrt{1 - \mu^2} \sqrt{1 - w^2} \cos\phi
\end{equation}

%\end{document}