186
Appendix F. Example MATLAB functions
%
3: E
[n,m] = size(W);
din = sum(W,1)’;
dout = sum(W,2);
X = sparse(diag((2 * dout+din).ˆ(-0.5))) * (W+diag(din+dout)) * ...
sparse(diag((dout+2 * din).ˆ(-0.5)));
if k == n
[U, E, V] = svd(X);
else
[U, E, V] = svds(X, k);
end
e = 1 - diag(E);
Vout = diag((2 * dout+din).ˆ(-0.5)) * U;
Vin = diag((dout+2 * din).ˆ(-0.5)) * V;
varargout{1} = Vout;
varargout{2} = Vin;
varargout{3} = e;
function [varargout] = DirLaplacianChung(W, epsilon, k)
% Spectral embedding of a directed graph using the method of Fan Chung
%
% W is the weighted adjacency matrix of a directed graph.
% epsilon: is a value between 0 and 1, used to avoid the problem
%
of reducibility of directed graph (default is 0)
% k = number of eigenvectors desired
% Vector = the eigenvector matrix for the embedding
% E = the diagonal matrix of Laplacian eigenvalues
% importance = importance value of the random walk matrix
% varargout = cell array of results
%
1: Vector
%
2: E
%
3: importance
n = size(W,1);
D = diag(sum(W,2));
RW = sparse(n,n); % RW is the random walk matrix of W
for i = 1:n
if D(i,i) ˜= 0
RW(i,:) = W(i,:)./D(i,i);
else
RW(i,i) = 1;
end
end
if epsilon > 0
RW = (1-epsilon) * reshape(RW,n,n)
Appendix F. Example MATLAB functions
%
3: E
[n,m] = size(W);
din = sum(W,1)’;
dout = sum(W,2);
X = sparse(diag((2 * dout+din).ˆ(-0.5))) * (W+diag(din+dout)) * ...
sparse(diag((dout+2 * din).ˆ(-0.5)));
if k == n
[U, E, V] = svd(X);
else
[U, E, V] = svds(X, k);
end
e = 1 - diag(E);
Vout = diag((2 * dout+din).ˆ(-0.5)) * U;
Vin = diag((dout+2 * din).ˆ(-0.5)) * V;
varargout{1} = Vout;
varargout{2} = Vin;
varargout{3} = e;
function [varargout] = DirLaplacianChung(W, epsilon, k)
% Spectral embedding of a directed graph using the method of Fan Chung
%
% W is the weighted adjacency matrix of a directed graph.
% epsilon: is a value between 0 and 1, used to avoid the problem
%
of reducibility of directed graph (default is 0)
% k = number of eigenvectors desired
% Vector = the eigenvector matrix for the embedding
% E = the diagonal matrix of Laplacian eigenvalues
% importance = importance value of the random walk matrix
% varargout = cell array of results
%
1: Vector
%
2: E
%
3: importance
n = size(W,1);
D = diag(sum(W,2));
RW = sparse(n,n); % RW is the random walk matrix of W
for i = 1:n
if D(i,i) ˜= 0
RW(i,:) = W(i,:)./D(i,i);
else
RW(i,i) = 1;
end
end
if epsilon > 0
RW = (1-epsilon) * reshape(RW,n,n)
