% Please note that a large LaTeX memory limit is needed to compile this file.
% Edit /usr/local/texlive/<year-version>/texmf.cnf and add/edit the following lines:
%
% main_memory = 12000000
% extra_mem_bot = 30000000
% extra_mem_top = 2000000000

\documentclass[unicode,11pt,a4paper,oneside,numbers=endperiod,openany]{scrartcl}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{amsmath}
\input{assignment.sty}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{plotmarks}
\usetikzlibrary{arrows.meta}
\usepgfplotslibrary{patchplots}
\usepackage{grffile}
\usepackage{amsmath}
\usepackage{subcaption}
\usepgfplotslibrary{external}
\tikzexternalize

\hyphenation{PageRank}
\hyphenation{PageRanks}

\begin{document}


\setassignment
\setduedate{Wednesday, 4 November 2020, 11:55 PM}

\serieheader{Numerical Computing}{2020}{Student: Claudio Maggioni}{Discussed with: --}{Solution for Project 3}{}
\newline

\assignmentpolicy

Please note all scripts used can be found under the folder \texttt{Project\_3\_Maggioni\_Claudio/src}.

\section*{Note on collaboration}

Please note that in the spirit of collaboration I shared with some of my classmates the helper
function \texttt{drawgraph(...)} used in exercise 3 and included in the file \texttt{bisection\_spectral.m}, and the functions \texttt{runtest(...)} and \texttt{recurse(...)} in file \texttt{bench\_rec\_bisection.m} for exercise 4. I am therefore claiming here I am the sole author of these functions.

Please also note this sharing was done with Edoardo Vecchi's supervision.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Install METIS 5.0.2, and the corresponding Matlab mex interface}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Implement various graph partitioning algorithms \punkte{60}}
I summarize the various benchmark results in table \ref{table:bisection}. Please note that this table
can be generated at will with the script \texttt{ex2\_bisection\_table.m}.

\section{Visualize the Fiedler eigenvector\punkte{10}}

In figure \ref{fig:run1} there are graph outputs respectively from \textit{Grid9}, \textit{Small}, and \textit{Eppstein}. Please note that these results can be reproduced using script \texttt{ex3\_plots.m}.

Colors to represent the partitions and the eigenvector components are the same as the ones used in the the assignment's Figure 3. It is quite easy to evince from these plots that spectral partitioning indeed partitions the graph verticies based on the sign of their respective Fiedler eigenvector component: the only intersection between the Graph plane and the surface the eigenvector entries lay corresponds to the edgecut produced by spectral partitioning. This is due to the fact that in the figure the graph is positioned at $z = 0$.

\begin{figure}[h]
\begin{subfigure}{0.5\textwidth}
\centering
%	\input{ex3_grid9}
\includegraphics[trim=50 200 50 200,clip,width=\textwidth]{ex3_grid9.pdf}
	\caption{Plot for \textit{Grid9}}
\end{subfigure}
\begin{subfigure}{0.5\textwidth}
\centering
%	\input{ex3_small}
\includegraphics[trim=50 200 50 200,clip,width=\textwidth]{ex3_small.pdf}
	\caption{Plot for \textit{Small}}
\end{subfigure}
\begin{subfigure}{0.5\textwidth}
%	\input{ex3_eppstein)
\includegraphics[trim=50 200 50 200,clip,width=\textwidth]{ex3_eppstein.pdf}
	\caption{Plot for \textit{Eppstein}}
\end{subfigure}
\caption{Graph outputs for the 3 adjacency matrices.}
\label{fig:run1}
\end{figure}

\section{Recursively bisecting meshes \punkte{20}}

I summarize my results in table \ref{table:Rec_bisection}. Additionally, the graph plots
for a recursive partition in 16 parts of \textit{Crack} are avaliable in figure \ref{fig:bicrack}.

The figure shows the partitions and edge cuts with colors randomly assigned by MATLAB.

Again the data shown here can reproduced using a MATLAB script (\texttt{Bench\_rec\_bisection.m}). Please note that the given recursion helper function \texttt{rec\_bisection} was not used. Instead, the function \texttt{runtest} was implemented to allow for partitioning graphs to be generated at will.

Please note that while computing spectral partitioning for \textit{barth4} MATLAB emits a warning
in the computation of the adjacency matrix eigenvalues stating that the matrix is ill conditioned.
This warning was suppressed so as not to interfere with the table printout.

\begin{figure}[h]
\begin{subfigure}{0.5\textwidth}
\centering
	\resizebox{0.8\linewidth}{!}{
		\input{ex4_Spectral}}
\caption{Spectral bisection}
\end{subfigure}
\begin{subfigure}{0.5\textwidth}
\centering
	\resizebox{0.8\linewidth}{!}{
		\input{ex4_Coordinate}}
\caption{Coordinate bisection}
\end{subfigure}
\begin{subfigure}{0.5\textwidth}
\centering
	\resizebox{0.8\linewidth}{!}{
		\input{ex4_METIS}}
\caption{METIS bisection}
\end{subfigure}
\begin{subfigure}{0.5\textwidth}
\centering
	\resizebox{0.8\linewidth}{!}{
		\input{ex4_Inertial}}
\caption{Inertial bisection}
\end{subfigure}
	\caption{Graph outputs for \textit{Crack} graph with $n=16$}
\label{fig:bicrack}
\end{figure}

\section{Compare recursive bisection to direct $k$-way partitioning\punkte{10}}

I summarize my results in table \ref{table:Compare_Metis}. Additionally, plots for the $n = 32$ cases can be found in figure \ref{fig:bikmetis}. Please note that both the table and the plots can be reproduced using script \texttt{Bench\_metis.m}.

The figure shows the resulting partitions with colors randomly assigned by MATLAB. All edge cuts are colored in black.

I had the suspicion that k-way partitioning would perform better in all cases, since in this the partitioning algorithm has a chance to see the entire problem and to perform decision that could affect all partitions. Instead, for the \texttt{Crack} mesh, recursive partitioning surprisedly performed better than k-way for both $n = 16$ and $n = 32$, producing slighly smaller edge cuts.

\begin{figure}[h]
\begin{subfigure}{0.5\textwidth}
\centering
	\resizebox{0.8\linewidth}{!}{
		\input{ex5_air_n}}
	\caption{\textit{Airfoil} with recursive partitioning}
\end{subfigure}
\begin{subfigure}{0.5\textwidth}
\centering
	\resizebox{0.8\linewidth}{!}{
		\input{ex5_air_k}}
	\caption{\textit{Airfoil} with k-way partitioning}
\end{subfigure}
\begin{subfigure}{0.5\textwidth}
\centering
	\resizebox{0.8\linewidth}{!}{
		\input{ex5_crack_n}}
	\caption{\textit{Crack} with recursive partitioning}
\end{subfigure}
\begin{subfigure}{0.5\textwidth}
\centering
	\resizebox{0.8\linewidth}{!}{
		\input{ex5_crack_k}}
	\caption{\textit{Crack} with k-way partitioning}
\end{subfigure}
	\caption{Graph outputs for \textit{METIS} recursive and k-way partitioning with $n=32$. Edge cuts are displayed in black}
\label{fig:bikmetis}
\end{figure}




\begin{table}[h]
	\caption{Bisection results}
	\centering
	\begin{tabular}{|l|r|r|r|r|} \hline\hline
		Mesh             &  Coordinate & Metis 5.0.2  & Spectral & Inertial  \\ \hline
		grid5rect(10,100)&   10 &        10 &           10  &      10 \\
		grid5rect(100,10)&   10 &        10 &           10  &      10 \\
       grid5recRotate(100,10,-45)&   18 &        10 &           10  &      10 \\
		gridt(40)        &   58 &        58 &           58  &      58 \\
		grid9(30)        &   88 &        92 &           102 &      88 \\
		Smallmesh        &   25 &        13 &           12  &      30 \\
		Tapir            &   55 &        34 &           18  &      49 \\
		Eppstein         &   42 &        48 &           42  &      45 \\  \hline \hline
	\end{tabular}
	\label{table:bisection}
\end{table}


\begin{table}[h]
	\caption{Edge-cut results for recursive bi-partitioning (data for $n=8$ on the left and $n=16$ on the right).}
	\centering
	\begin{tabular}{|l|r|r|r|r|p{4cm}|} \hline\hline
		Case & Spectral & Metis 5.0.2 & Coordinate & Inertial \\ \hline
airfoil1 & 327 \hfill 578  & 320 \hfill 563  & 516  \hfill  819 &  577 \hfill  897\\
3elt     & 372 \hfill 671  & 395 \hfill 651  & 733  \hfill 1168 &  880 \hfill 1342\\
barth4   & 505 \hfill 758  & 405 \hfill 689  & 875  \hfill 1306 &  891 \hfill 1350\\
mesh3e1  & 75  \hfill 124  & 75 \hfill  117  & 75   \hfill  122 &   67 \hfill  102\\
crack    & 804 \hfill 1303 & 784 \hfill 1290 & 1343 \hfill 1860 & 1061 \hfill 1618\\\hline\hline
	\end{tabular}
	\label{table:Rec_bisection}
\end{table}


\begin{table}[h]
\caption{Comparing the number of cut edges for recursive bisection and direct multiway partitioning in Metis 5.0.2. Results for recursive partitioning shown on the left, k-way on the right.}
\centering
\begin{tabular}{|l|r|r|r|r|} \hline\hline
Partitions       &   crack          & airfoil1  \\ \hline
 16              &   10055 \hfill 10077 & 580 \hfill 564 \\
 32              &   10824 \hfill 10943 & 967 \hfill 947 \\ \hline \hline
\end{tabular}
\label{table:Compare_Metis}
\end{table}



\end{document}
