mp2: all done

This commit is contained in:
Claudio Maggioni 2020-10-07 15:39:10 +02:00
parent ec187be71a
commit 00971fbb87
10 changed files with 1000 additions and 6 deletions

View file

@ -0,0 +1,21 @@
n = 5;
A = sparse(n, n);
A(1,:) = 1;
A(end,:) = 1;
A(:,1) = 1;
A(:,end) = 1;
% make sure diagonal is 0
A(1,1) = 0;
A(n,n) = 0;
A = A + diag(n:(2*n-1));
spy(A);
matlab2tikz('../ex2_2_spy.tex');
figure;
subplot(1,2,1)
spy(A);
subplot(1,2,2)
spy(chol(A));
matlab2tikz('../ex2_3_spy.tex');

View file

@ -42,14 +42,16 @@ deg = sum(A);
L = full(diag(deg) - A); L = full(diag(deg) - A);
[V, D] = eig(L); [V, D] = eig(L);
fprintf("lambda_2: %d\n", D(2,2)); fprintf("lambda_2: %d\n", D(2,2));
plot(sort(V(:,2)), '.-'); [sv, p] = sort(V(:,2));
figure;
[ignore, p] = sort(V(:,2)); plot(sv, '.-');
%matlab2tikz('../ex6_6_ev.tex');
figure; figure;
subplot(1,2,1) subplot(1,2,1)
spy(A); spy(A);
subplot(1,2,2) subplot(1,2,2)
spy(A(p,p)); spy(A(p,p));
%matlab2tikz('../ex6_6_spy.tex');
fprintf("Group 1: "); fprintf("Group 1: ");
display(sort(p(1:16))'); display(sort(p(1:16))');
fprintf("Group 2: "); fprintf("Group 2: ");
@ -57,3 +59,4 @@ display(sort(p(17:end))');

48
mp2/ex2_2_spy.tex Normal file
View file

@ -0,0 +1,48 @@
% This file was created by matlab2tikz.
%
%The latest updates can be retrieved from
% http://www.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz-matlab2tikz
%where you can also make suggestions and rate matlab2tikz.
%
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
%
\begin{tikzpicture}
\begin{axis}[%
width=4.754in,
height=4.754in,
at={(1.648in,0.642in)},
scale only axis,
xmin=0,
xmax=6,
xlabel style={font=\color{white!15!black}},
xlabel={nz = 19},
y dir=reverse,
ymin=0,
ymax=6,
axis background/.style={fill=white}
]
\addplot [color=mycolor1, only marks, mark size=2.3pt, mark=*, mark options={solid, mycolor1}, forget plot]
table[row sep=crcr]{%
1 1\\
1 2\\
1 3\\
1 4\\
1 5\\
2 1\\
2 2\\
2 5\\
3 1\\
3 3\\
3 5\\
4 1\\
4 4\\
4 5\\
5 1\\
5 2\\
5 3\\
5 4\\
5 5\\
};
\end{axis}
\end{tikzpicture}%

82
mp2/ex2_3_spy.tex Normal file
View file

@ -0,0 +1,82 @@
% This file was created by matlab2tikz.
%
%The latest updates can be retrieved from
% http://www.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz-matlab2tikz
%where you can also make suggestions and rate matlab2tikz.
%
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
%
\begin{tikzpicture}
\begin{axis}[%
width=2.603in,
height=2.603in,
at={(1.011in,1.717in)},
scale only axis,
xmin=0,
xmax=6,
xlabel style={font=\color{white!15!black}},
xlabel={nz = 19},
y dir=reverse,
ymin=0,
ymax=6,
axis background/.style={fill=white}
]
\addplot [color=mycolor1, only marks, mark size=2.3pt, mark=*, mark options={solid, mycolor1}, forget plot]
table[row sep=crcr]{%
1 1\\
1 2\\
1 3\\
1 4\\
1 5\\
2 1\\
2 2\\
2 5\\
3 1\\
3 3\\
3 5\\
4 1\\
4 4\\
4 5\\
5 1\\
5 2\\
5 3\\
5 4\\
5 5\\
};
\end{axis}
\begin{axis}[%
width=2.603in,
height=2.603in,
at={(4.436in,1.717in)},
scale only axis,
xmin=0,
xmax=6,
xlabel style={font=\color{white!15!black}},
xlabel={nz = 15},
y dir=reverse,
ymin=0,
ymax=6,
axis background/.style={fill=white}
]
\addplot [color=mycolor1, only marks, mark size=2.3pt, mark=*, mark options={solid, mycolor1}, forget plot]
table[row sep=crcr]{%
1 1\\
2 1\\
2 2\\
3 1\\
3 2\\
3 3\\
4 1\\
4 2\\
4 3\\
4 4\\
5 1\\
5 2\\
5 3\\
5 4\\
5 5\\
};
\end{axis}
\end{tikzpicture}%

60
mp2/ex6_6_ev.tex Normal file
View file

@ -0,0 +1,60 @@
% This file was created by matlab2tikz.
%
%The latest updates can be retrieved from
% http://www.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz-matlab2tikz
%where you can also make suggestions and rate matlab2tikz.
%
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
%
\begin{tikzpicture}
\begin{axis}[%
width=6.028in,
height=4.754in,
at={(1.011in,0.642in)},
scale only axis,
xmin=0,
xmax=35,
ymin=-0.5,
ymax=0.2,
axis background/.style={fill=white}
]
\addplot [color=mycolor1, mark=*, mark options={solid, mycolor1}, forget plot]
table[row sep=crcr]{%
1 -0.422765329195372\\
2 -0.323727218343997\\
3 -0.323727218343997\\
4 -0.284604525162929\\
5 -0.284604525162929\\
6 -0.210992953839888\\
7 -0.112137432309658\\
8 -0.109461295834945\\
9 -0.100181421923354\\
10 -0.100181421923353\\
11 -0.0554997809141353\\
12 -0.0525860037236362\\
13 -0.0412878881191417\\
14 -0.0147419738270838\\
15 -0.0136371326786982\\
16 0.0232189557643265\\
17 0.0516012759453287\\
18 0.0734996356043595\\
19 0.0928008879511365\\
20 0.0951522995236114\\
21 0.0987534270285001\\
22 0.11890326307255\\
23 0.127664048518892\\
24 0.1303454580847\\
25 0.153025580600806\\
26 0.155694567363955\\
27 0.160962921412923\\
28 0.162750784735682\\
29 0.162750784735682\\
30 0.162750784735682\\
31 0.162750784735682\\
32 0.162750784735682\\
33 0.167650313260338\\
34 0.18710956349329\\
};
\end{axis}
\end{tikzpicture}%

360
mp2/ex6_6_spy.tex Normal file
View file

@ -0,0 +1,360 @@
% This file was created by matlab2tikz.
%
%The latest updates can be retrieved from
% http://www.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz-matlab2tikz
%where you can also make suggestions and rate matlab2tikz.
%
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
%
\begin{tikzpicture}
\begin{axis}[%
width=2.603in,
height=2.603in,
at={(1.011in,1.717in)},
scale only axis,
xmin=0,
xmax=35,
xlabel style={font=\color{white!15!black}},
xlabel={nz = 156},
y dir=reverse,
ymin=0,
ymax=35,
axis background/.style={fill=white}
]
\addplot [color=mycolor1, only marks, mark size=2.3pt, mark=*, mark options={solid, mycolor1}, forget plot]
table[row sep=crcr]{%
1 2\\
1 3\\
1 4\\
1 5\\
1 6\\
1 7\\
1 8\\
1 9\\
1 11\\
1 12\\
1 13\\
1 14\\
1 18\\
1 20\\
1 22\\
1 32\\
2 1\\
2 3\\
2 4\\
2 8\\
2 14\\
2 18\\
2 20\\
2 22\\
2 31\\
3 1\\
3 2\\
3 4\\
3 8\\
3 9\\
3 10\\
3 14\\
3 28\\
3 29\\
3 33\\
4 1\\
4 2\\
4 3\\
4 8\\
4 13\\
4 14\\
5 1\\
5 7\\
5 11\\
6 1\\
6 7\\
6 11\\
6 17\\
7 1\\
7 5\\
7 6\\
7 17\\
8 1\\
8 2\\
8 3\\
8 4\\
9 1\\
9 3\\
9 31\\
9 33\\
9 34\\
10 3\\
10 34\\
11 1\\
11 5\\
11 6\\
12 1\\
13 1\\
13 4\\
14 1\\
14 2\\
14 3\\
14 4\\
14 34\\
15 33\\
15 34\\
16 33\\
16 34\\
17 6\\
17 7\\
18 1\\
18 2\\
19 33\\
19 34\\
20 1\\
20 2\\
20 34\\
21 33\\
21 34\\
22 1\\
22 2\\
23 33\\
23 34\\
24 26\\
24 28\\
24 30\\
24 33\\
24 34\\
25 26\\
25 28\\
25 32\\
26 24\\
26 25\\
26 32\\
27 30\\
27 34\\
28 3\\
28 24\\
28 25\\
28 34\\
29 3\\
29 32\\
29 34\\
30 24\\
30 27\\
30 33\\
30 34\\
31 2\\
31 9\\
31 33\\
31 34\\
32 1\\
32 25\\
32 26\\
32 29\\
32 33\\
32 34\\
33 3\\
33 9\\
33 15\\
33 16\\
33 19\\
33 21\\
33 23\\
33 24\\
33 30\\
33 31\\
33 32\\
33 34\\
34 9\\
34 10\\
34 14\\
34 15\\
34 16\\
34 19\\
34 20\\
34 21\\
34 23\\
34 24\\
34 27\\
34 28\\
34 29\\
34 30\\
34 31\\
34 32\\
34 33\\
};
\end{axis}
\begin{axis}[%
width=2.603in,
height=2.603in,
at={(4.436in,1.717in)},
scale only axis,
xmin=0,
xmax=35,
xlabel style={font=\color{white!15!black}},
xlabel={nz = 156},
y dir=reverse,
ymin=0,
ymax=35,
axis background/.style={fill=white}
]
\addplot [color=mycolor1, only marks, mark size=2.3pt, mark=*, mark options={solid, mycolor1}, forget plot]
table[row sep=crcr]{%
1 2\\
1 3\\
2 1\\
2 3\\
2 5\\
2 7\\
3 1\\
3 2\\
3 4\\
3 7\\
4 3\\
4 5\\
4 7\\
5 2\\
5 4\\
5 7\\
6 7\\
7 2\\
7 3\\
7 4\\
7 5\\
7 6\\
7 8\\
7 9\\
7 10\\
7 11\\
7 12\\
7 13\\
7 14\\
7 15\\
7 16\\
7 17\\
7 21\\
8 7\\
8 11\\
9 7\\
9 13\\
10 7\\
10 13\\
11 7\\
11 8\\
11 12\\
11 13\\
11 14\\
11 16\\
12 7\\
12 11\\
12 13\\
12 16\\
13 7\\
13 9\\
13 10\\
13 11\\
13 12\\
13 14\\
13 15\\
13 16\\
13 18\\
14 7\\
14 11\\
14 13\\
14 16\\
14 22\\
15 7\\
15 13\\
15 22\\
16 7\\
16 11\\
16 12\\
16 13\\
16 14\\
16 17\\
16 19\\
16 20\\
16 23\\
16 24\\
17 7\\
17 16\\
17 18\\
17 22\\
17 24\\
18 13\\
18 17\\
18 22\\
18 24\\
19 16\\
19 22\\
20 16\\
20 21\\
20 22\\
21 7\\
21 20\\
21 22\\
21 24\\
21 25\\
21 27\\
22 14\\
22 15\\
22 17\\
22 18\\
22 19\\
22 20\\
22 21\\
22 23\\
22 24\\
22 26\\
22 28\\
22 29\\
22 30\\
22 31\\
22 32\\
22 33\\
22 34\\
23 16\\
23 22\\
23 25\\
23 26\\
24 16\\
24 17\\
24 18\\
24 21\\
24 22\\
24 26\\
24 28\\
24 29\\
24 30\\
24 31\\
24 32\\
24 33\\
25 21\\
25 23\\
25 27\\
26 22\\
26 23\\
26 24\\
26 27\\
26 33\\
27 21\\
27 25\\
27 26\\
28 22\\
28 24\\
29 22\\
29 24\\
30 22\\
30 24\\
31 22\\
31 24\\
32 22\\
32 24\\
33 22\\
33 24\\
33 26\\
33 34\\
34 22\\
34 33\\
};
\end{axis}
\end{tikzpicture}%

360
mp2/ex6_plot.tex Normal file
View file

@ -0,0 +1,360 @@
% This file was created by matlab2tikz.
%
%The latest updates can be retrieved from
% http://www.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz-matlab2tikz
%where you can also make suggestions and rate matlab2tikz.
%
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
%
\begin{tikzpicture}
\begin{axis}[%
width=2.603in,
height=2.603in,
at={(1.011in,1.717in)},
scale only axis,
xmin=0,
xmax=35,
xlabel style={font=\color{white!15!black}},
xlabel={nz = 156},
y dir=reverse,
ymin=0,
ymax=35,
axis background/.style={fill=white}
]
\addplot [color=mycolor1, only marks, mark size=2.3pt, mark=*, mark options={solid, mycolor1}, forget plot]
table[row sep=crcr]{%
1 2\\
1 3\\
1 4\\
1 5\\
1 6\\
1 7\\
1 8\\
1 9\\
1 11\\
1 12\\
1 13\\
1 14\\
1 18\\
1 20\\
1 22\\
1 32\\
2 1\\
2 3\\
2 4\\
2 8\\
2 14\\
2 18\\
2 20\\
2 22\\
2 31\\
3 1\\
3 2\\
3 4\\
3 8\\
3 9\\
3 10\\
3 14\\
3 28\\
3 29\\
3 33\\
4 1\\
4 2\\
4 3\\
4 8\\
4 13\\
4 14\\
5 1\\
5 7\\
5 11\\
6 1\\
6 7\\
6 11\\
6 17\\
7 1\\
7 5\\
7 6\\
7 17\\
8 1\\
8 2\\
8 3\\
8 4\\
9 1\\
9 3\\
9 31\\
9 33\\
9 34\\
10 3\\
10 34\\
11 1\\
11 5\\
11 6\\
12 1\\
13 1\\
13 4\\
14 1\\
14 2\\
14 3\\
14 4\\
14 34\\
15 33\\
15 34\\
16 33\\
16 34\\
17 6\\
17 7\\
18 1\\
18 2\\
19 33\\
19 34\\
20 1\\
20 2\\
20 34\\
21 33\\
21 34\\
22 1\\
22 2\\
23 33\\
23 34\\
24 26\\
24 28\\
24 30\\
24 33\\
24 34\\
25 26\\
25 28\\
25 32\\
26 24\\
26 25\\
26 32\\
27 30\\
27 34\\
28 3\\
28 24\\
28 25\\
28 34\\
29 3\\
29 32\\
29 34\\
30 24\\
30 27\\
30 33\\
30 34\\
31 2\\
31 9\\
31 33\\
31 34\\
32 1\\
32 25\\
32 26\\
32 29\\
32 33\\
32 34\\
33 3\\
33 9\\
33 15\\
33 16\\
33 19\\
33 21\\
33 23\\
33 24\\
33 30\\
33 31\\
33 32\\
33 34\\
34 9\\
34 10\\
34 14\\
34 15\\
34 16\\
34 19\\
34 20\\
34 21\\
34 23\\
34 24\\
34 27\\
34 28\\
34 29\\
34 30\\
34 31\\
34 32\\
34 33\\
};
\end{axis}
\begin{axis}[%
width=2.603in,
height=2.603in,
at={(4.436in,1.717in)},
scale only axis,
xmin=0,
xmax=35,
xlabel style={font=\color{white!15!black}},
xlabel={nz = 156},
y dir=reverse,
ymin=0,
ymax=35,
axis background/.style={fill=white}
]
\addplot [color=mycolor1, only marks, mark size=2.3pt, mark=*, mark options={solid, mycolor1}, forget plot]
table[row sep=crcr]{%
1 2\\
1 3\\
2 1\\
2 3\\
2 5\\
2 7\\
3 1\\
3 2\\
3 4\\
3 7\\
4 3\\
4 5\\
4 7\\
5 2\\
5 4\\
5 7\\
6 7\\
7 2\\
7 3\\
7 4\\
7 5\\
7 6\\
7 8\\
7 9\\
7 10\\
7 11\\
7 12\\
7 13\\
7 14\\
7 15\\
7 16\\
7 17\\
7 21\\
8 7\\
8 11\\
9 7\\
9 13\\
10 7\\
10 13\\
11 7\\
11 8\\
11 12\\
11 13\\
11 14\\
11 16\\
12 7\\
12 11\\
12 13\\
12 16\\
13 7\\
13 9\\
13 10\\
13 11\\
13 12\\
13 14\\
13 15\\
13 16\\
13 18\\
14 7\\
14 11\\
14 13\\
14 16\\
14 22\\
15 7\\
15 13\\
15 22\\
16 7\\
16 11\\
16 12\\
16 13\\
16 14\\
16 17\\
16 19\\
16 20\\
16 23\\
16 24\\
17 7\\
17 16\\
17 18\\
17 22\\
17 24\\
18 13\\
18 17\\
18 22\\
18 24\\
19 16\\
19 22\\
20 16\\
20 21\\
20 22\\
21 7\\
21 20\\
21 22\\
21 24\\
21 25\\
21 27\\
22 14\\
22 15\\
22 17\\
22 18\\
22 19\\
22 20\\
22 21\\
22 23\\
22 24\\
22 26\\
22 28\\
22 29\\
22 30\\
22 31\\
22 32\\
22 33\\
22 34\\
23 16\\
23 22\\
23 25\\
23 26\\
24 16\\
24 17\\
24 18\\
24 21\\
24 22\\
24 26\\
24 28\\
24 29\\
24 30\\
24 31\\
24 32\\
24 33\\
25 21\\
25 23\\
25 27\\
26 22\\
26 23\\
26 24\\
26 27\\
26 33\\
27 21\\
27 25\\
27 26\\
28 22\\
28 24\\
29 22\\
29 24\\
30 22\\
30 24\\
31 22\\
31 24\\
32 22\\
32 24\\
33 22\\
33 24\\
33 26\\
33 34\\
34 22\\
34 33\\
};
\end{axis}
\end{tikzpicture}%

Binary file not shown.

View file

@ -3,6 +3,13 @@
\usepackage{subcaption} \usepackage{subcaption}
\usepackage{amsmath} \usepackage{amsmath}
\input{assignment.sty} \input{assignment.sty}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{plotmarks}
\usetikzlibrary{arrows.meta}
\usepgfplotslibrary{patchplots}
\usepackage{grffile}
\usepackage{amsmath}
\hyphenation{PageRank} \hyphenation{PageRank}
@ -70,7 +77,9 @@ And the spy plots of the corresponding Cholesky factor are listed in figure~\ref
\label{fig:1chol} \label{fig:1chol}
\end{figure} \end{figure}
The number of nonzero elements in the Cholesky factor of the RCM optimized matrix are significantly lower (circa 0.1x) of the ones in the vanilla process. The respective nonzero counts can be found in figure~\ref{fig:1chol}. The number of nonzero elements in the Cholesky factor of the RCM optimized
matrix are significantly lower (circa 0.1x) of the ones in the vanilla process.
The respective nonzero counts can be found in figure~\ref{fig:1chol}.
\section{Sparse Matrix Factorization [10 points]} \section{Sparse Matrix Factorization [10 points]}
@ -84,7 +93,14 @@ corresponding to all edges and the main diagonal. Therefore:
\[4 + 5 \dot (n - 2) = 5n - 6\] \[4 + 5 \dot (n - 2) = 5n - 6\]
\subsection{Construct this matrix and visualize its non-zero structure.} \subsection{Write a short Matlab script to construct this matrix and visualize
its non-zero structure(you can use, e.g., the command \texttt{spy()}).}
The MATLAB script can be found in file \texttt{ex3.m}.
Here is a spy plot of the nonzero values of $A$, for $n = 5$:
\centering{\input{ex2_2_spy.tex}}
The matrix $A \in R^{n x n}$ looks like this (zero entries are represented as The matrix $A \in R^{n x n}$ looks like this (zero entries are represented as
blanks): blanks):
@ -99,10 +115,17 @@ A := \begin{bmatrix}
\end{bmatrix} \end{bmatrix}
\] \]
\subsection{Explain why for n = $100000$ using Matlabs \texttt{chol(\ldots)} \subsection{Using again the \texttt{spy()} command, visualize side by side the
original matrix $A$ and the result of the Cholesky factorization (\texttt{chol()}
in Matlab). Then explain why for n = $100000$ using Matlabs \texttt{chol(\ldots)}
to solve $Ax = b$ to solve $Ax = b$
for a given righthand-side vector would be problematic.} for a given righthand-side vector would be problematic.}
Here is the plot of \texttt{spy(A)} (on the left) and \texttt{chol(spy(A))} (on
the right).
\centering{\input{ex2_3_spy.tex}}
Solving $Ax = b$ would be a costly operation since the a Cholesky Solving $Ax = b$ would be a costly operation since the a Cholesky
decomposition of matrix $A$ (performed using MATLAB's \texttt{chol(\ldots)}) decomposition of matrix $A$ (performed using MATLAB's \texttt{chol(\ldots)})
would drastically reduce the number of zero elements in the matrix in the very would drastically reduce the number of zero elements in the matrix in the very
@ -422,6 +445,43 @@ network into two groups of 16 and 18 nodes, respectively. List the nodes in the
two groups. How does spectral bisection compare to the real split observed by two groups. How does spectral bisection compare to the real split observed by
Zachary?} Zachary?}
The spectral bisection of the matrix a in two groups of 16 and 18 members
respectively is identical to the real split observed by Zachary. To compute the
split, the script \texttt{ex6.m} was used.
Here are the (sorted) two groups found:
\begin{gather*}
G_1 = [1, 2, 3, 4, 5, 6, 7, 8, 11, 12, 13, 14, 17, 18, 20, 22] \\
G_2 = [9, 10, 15, 16, 19, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34]
\end{gather*}
Here are the spy plots of the original matrix A (to the left) and the spectral
bisected permutated matrix (to the right):
\centering{\input{ex6_6_spy.tex}}
Here is a plot of the sorted elements of the second eigenvector $\lambda_2$:
\centering{\input{ex6_6_ev.tex}}
and here are the actual (sorted) values of $\lambda_2$:
\begin{align*}
sort(\lambda_2) = [-0.4228, -0.3237, -0.3237, -0.2846,
-0.2846, -0.2110, -0.1121, -0.1095, -0.1002, \\ -0.1002, -0.0555,
-0.0526, -0.0413, -0.0147, -0.0136, 0.0232, 0.0516, 0.0735, \\
0.0928, 0.0952, 0.0988, 0.1189, 0.1277, 0.1303, 0.1530,
0.1557, 0.1610, \\ 0.1628, 0.1628, 0.1628, 0.1628, 0.1628,
0.1677, 0.1871]^T
\end{align*}
As it can be seen above, there are only 15 negative values out the 16 we would
need to obtain a perfect 16/18 partition. We therefore add the index corresponding to
the smallest positive value in $\lambda_2$ in the set of indexes of group 1.
This seems to be a good approximation since indeed we get the same partitioning
as the original Zachary's one.
\begin{thebibliography}{99} \begin{thebibliography}{99}
\bibitem{karate} The social network of a karate club at a US university, M.~E.~J. Newman and M. Girvan, Phys. Rev. E 69,026113 (2004) \bibitem{karate} The social network of a karate club at a US university, M.~E.~J. Newman and M. Girvan, Phys. Rev. E 69,026113 (2004)
pp. 219-229. pp. 219-229.