184
Appendix F. Example MATLAB functions
The versions of the MATLAB functions below are simplified by omitting the
simple, but space-consuming, code that checks input arguments. Full versions are
in the Github repository: https://github.com/PHDZheng/Social-Networks-withRich-Edge-Semantics.
function [varargout] = Laplacian(W, type, k)
% Spectral embedding of an undirected graph
%
% W is the weighted adjacency matrix of a graph.
% type = un and empty: unnormalized graph Laplacian
%
sym: symmetric normalized graph Laplacian
%
rw: random walk normalized graph Laplacian
% k = number of groups to cluster into (default is disabled)
%
% Vector = eigenvector matrix of the embedding
% E = the vector of Laplacian eigenvalues
%
% varargout = cellarray
%
1: Vector
%
2: E
[n,m] = size(W);
tempvalue = max(max(W-W’));
if tempvalue ˜= 0
W = (W + W’)/2;
end
D = sparse(1:n,1:n,sum(W,2));
L = D - W;
%unnormalized graph Laplacian
if (nargin < 2) || isempty(type) || strcmp(type,’un’)
|| strcmp(type,’Un’) ...
|| strcmp(type,’UN’)
display(’Unnormalized Laplacian decomposition’);
tempvalue = max(D(:));
if k == n
[Vector,eigenvalue] = eig(L);
e = diag(eigenvalue);
[e, IXY] = sort(e,1,’ascend’);
Vector = Vector(:,IXY);
else
[Vector,eigenvalue] = eigs(2 * tempvalue * eye(n)-L,k);
e = 2 * tempvalue - diag(eigenvalue);
end
varargout{1} = Vector;
varargout{2} = e;
% symmetric normalized graph-Laplacian
elseif strcmp(type,’sym’) || strcmp(type,’Sym’) || strcmp(type,’SYM’)
% Normalized spectral clustering according to Ng, Jordan, and Weiss
% (2002)
display(’Symmetric Laplacian decomposition’);
Appendix F. Example MATLAB functions
The versions of the MATLAB functions below are simplified by omitting the
simple, but space-consuming, code that checks input arguments. Full versions are
in the Github repository: https://github.com/PHDZheng/Social-Networks-withRich-Edge-Semantics.
function [varargout] = Laplacian(W, type, k)
% Spectral embedding of an undirected graph
%
% W is the weighted adjacency matrix of a graph.
% type = un and empty: unnormalized graph Laplacian
%
sym: symmetric normalized graph Laplacian
%
rw: random walk normalized graph Laplacian
% k = number of groups to cluster into (default is disabled)
%
% Vector = eigenvector matrix of the embedding
% E = the vector of Laplacian eigenvalues
%
% varargout = cellarray
%
1: Vector
%
2: E
[n,m] = size(W);
tempvalue = max(max(W-W’));
if tempvalue ˜= 0
W = (W + W’)/2;
end
D = sparse(1:n,1:n,sum(W,2));
L = D - W;
%unnormalized graph Laplacian
if (nargin < 2) || isempty(type) || strcmp(type,’un’)
|| strcmp(type,’Un’) ...
|| strcmp(type,’UN’)
display(’Unnormalized Laplacian decomposition’);
tempvalue = max(D(:));
if k == n
[Vector,eigenvalue] = eig(L);
e = diag(eigenvalue);
[e, IXY] = sort(e,1,’ascend’);
Vector = Vector(:,IXY);
else
[Vector,eigenvalue] = eigs(2 * tempvalue * eye(n)-L,k);
e = 2 * tempvalue - diag(eigenvalue);
end
varargout{1} = Vector;
varargout{2} = e;
% symmetric normalized graph-Laplacian
elseif strcmp(type,’sym’) || strcmp(type,’Sym’) || strcmp(type,’SYM’)
% Normalized spectral clustering according to Ng, Jordan, and Weiss
% (2002)
display(’Symmetric Laplacian decomposition’);
