mp2: done 1-7.3 and MATLAB's part of 7.4

This commit is contained in:
Claudio Maggioni 2020-10-06 17:00:12 +02:00
parent 5eea5d6feb
commit ec187be71a
5 changed files with 130 additions and 12 deletions

View file

@ -0,0 +1,14 @@
function degcentrality(names, A)
counts = full(sum(A, 2));
ranks = sortrows([counts, (1:size(counts,1))'], 'descend');
for i = 1:size(ranks, 1)
fprintf("%14s %2d: ", names(ranks(i, 2)), ranks(i, 1)-1);
for j = 1:size(A, 2)
if ranks(i, 2) ~= j && A(ranks(i, 2), j) > 0
fprintf("%s, ", names(j));
end
end
fprintf("\n");
end
end

View file

@ -2,15 +2,4 @@ clear;
clc;
load('householder/housegraph.mat')
names = split(strtrim(convertCharsToStrings(name')));
counts = full(sum(A, 2));
ranks = sortrows([counts, (1:size(counts,1))'], 'descend');
for i = 1:size(ranks, 1)
fprintf("%14s %2d: ", names(ranks(i, 2)), ranks(i, 1)-1);
for j = 1:size(A, 2)
if ranks(i, 2) ~= j && A(ranks(i, 2), j) > 0
fprintf("%s, ", names(j));
end
end
fprintf("\n");
end
degcentrality(names, A);

View file

@ -0,0 +1,59 @@
clc;
clear;
fileID = fopen('karate.adj','r');
row = split(strtrim(fgetl(fileID)));
n = size(row,1);
frewind(fileID);
ii = [];
jj = [];
vv = [];
for i = 1:n
row = split(strtrim(fgetl(fileID)));
for j = 1:n
num = str2double(row(j));
if num ~= 0
ii(end+1) = i;
jj(end+1) = j;
vv(end+1) = num;
end
end
end
fclose(fileID);
A = sparse(ii,jj,vv,n,n);
names = string(1:n);
disp("Exercise 6.1:");
degcentrality(names,A);
disp("Exercise 6.2:");
pagerank(names,A);
disp("Exercise 6.4:");
x = randperm(n);
gs = 450;
%group1 = [1 2 3 4 5 6 7 8 11 12 13 14 17 18 20 22];
%group2 = [9 10 15 16 19 21 23:34];
deg = sum(A);
L = full(diag(deg) - A);
[V, D] = eig(L);
fprintf("lambda_2: %d\n", D(2,2));
plot(sort(V(:,2)), '.-');
[ignore, p] = sort(V(:,2));
figure;
subplot(1,2,1)
spy(A);
subplot(1,2,2)
spy(A(p,p));
fprintf("Group 1: ");
display(sort(p(1:16))');
fprintf("Group 2: ");
display(sort(p(17:end))');

Binary file not shown.

View file

@ -366,6 +366,62 @@ The PageRank values for all authors were computing by using the scripts
\section{Zachary's karate club: social network of friendships between 34 members [50 points]}
\subsection{Write a Matlab code that ranks the five nodes with the largest
degree centrality? What are their degrees?}
Results found here can be computed using the file \texttt{ex6.m}.
Please find the top 5 nodes by degree centrality, with their degree and their
neighbours listed below:
\begin{verbatim}
Node Degree: Neighbours...
34 16: 9, 10, 14, 15, 16, 19, 20, 21, 23, 24, 27, 28, 29, 30, 31, 32, 33,
1 15: 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 18, 20, 22, 32,
33 11: 3, 9, 15, 16, 19, 21, 23, 24, 30, 31, 32, 34,
3 9: 1, 2, 4, 8, 9, 10, 14, 28, 29, 33,
2 8: 1, 3, 4, 8, 14, 18, 20, 22, 31,
\end{verbatim}
\subsection{Rank the five nodes with the largest eigenvector centrality. What are
their (properly normalized) eigenvector centralities?}
Results found here can be computed using the file \texttt{ex6.m}.
Please find the top 5 nodes by eigenvector centrality (page-rank column)
listed below:
\begin{verbatim}
page-rank in out author
34 0.1009 17 17 34
1 0.0970 16 16 1
33 0.0717 12 12 33
3 0.0571 10 10 3
2 0.0529 9 9 2
\end{verbatim}
\subsection{Are the rankings in (a) and (b) identical? Give a brief verbal
explanation of the similarities and differences.}
The rankings found are identical, even though if we normalize the degree
centrality to the greatest eigenvector centrality we find slighly different
values ($[0.1009, 0.0946, 0.0694, 0.0568, 0.0505]$) w.r.t the actual eigenvector
centrality.
The identical rankings may be explained by the fact that by computing the
eigenvector centrality we are effectively applying PageRank to a symmetrical
matrix, i.e. to a graph with bidirectional links. Since the links are
bidirectional, we effectively make all the nodes in the graph of the same
``importance'' to the eyes of PageRank, thus avoiding a case where a node has
high PageRank thank to connections with few, but very ``important'' nodes.
Therefore PageRank is simply reduced to a priotarization of nodes with many
edges, i.e. the degree centrality ranking.
\subsection{Use spectral graph partitioning to find a near-optimal split of the
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
Zachary?}
\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)
pp. 219-229.