196
Appendix F. Example MATLAB functions
posW(1:n,n+2) = ew(1) * Y(:,2);
posW(n+2,1:n) = ew(1) * Y(:,2)’;
% build the modified total degree matrices.
Dpos = diag(sum(posW,2));
Dneg = diag(sum(negW,2));
D = diag(sum(W,2));
D(n+1,n+1) = Dpos(n+1,n+1) + Dneg(n+1,n+1);
D(n+2,n+2) = Dpos(n+2,n+2) + Dneg(n+2,n+2);
D2 = sparse(n+2,n+2);
for i = 1:n+2
if D(i,i) ˜= 0
D2(i,i) = 1/D(i,i)ˆ0.5;
end
end
% signed Laplacian embedding
L = (Dpos-posW) - (Dneg-negW);
Lsym = max(sum(abs(D2 * L * D2),2)) * speye(n+2) - D2 * L * D2;
%- max(sum(abs(D2 * L * D2),2)) * D3 * D3’;
Lsym = Lsym + Lsym’;
[V,e] = eigs(Lsym,2);
if abs(e(1,1) - 2 * max(sum(abs(D2 * L * D2),2))) < 1e-10;
U = V(:,2);
else
U = V(:,1);
end
% make the output vector consistent with the labels.
if U(n+1,1) > 0
F = U(1:n,1);
else
F = -U(1:n,1);
end
function F = GBE(posW,label,ew)
% Implements our GBE semi-supervised learning algorithm
%
% posW: is the n * n weighted adjacency matrix of a undirected graph.
% label: is a n * c label indication matrix, with value {-1, 0 1} if
%
label is a vector, {1,0} if label is a matrix; and 0 row for
% unlabelled nodes.
% ew: is a [apw,anw] vector for positive and negative added edge
%
weight between labeled points; default value is [1,1].
[n,m] = size(label);
if m <= 2 % two classes
if m==1
Y(:,1) = label;
Y(:,2) = -label;
Y(Y<0) = 0;
Appendix F. Example MATLAB functions
posW(1:n,n+2) = ew(1) * Y(:,2);
posW(n+2,1:n) = ew(1) * Y(:,2)’;
% build the modified total degree matrices.
Dpos = diag(sum(posW,2));
Dneg = diag(sum(negW,2));
D = diag(sum(W,2));
D(n+1,n+1) = Dpos(n+1,n+1) + Dneg(n+1,n+1);
D(n+2,n+2) = Dpos(n+2,n+2) + Dneg(n+2,n+2);
D2 = sparse(n+2,n+2);
for i = 1:n+2
if D(i,i) ˜= 0
D2(i,i) = 1/D(i,i)ˆ0.5;
end
end
% signed Laplacian embedding
L = (Dpos-posW) - (Dneg-negW);
Lsym = max(sum(abs(D2 * L * D2),2)) * speye(n+2) - D2 * L * D2;
%- max(sum(abs(D2 * L * D2),2)) * D3 * D3’;
Lsym = Lsym + Lsym’;
[V,e] = eigs(Lsym,2);
if abs(e(1,1) - 2 * max(sum(abs(D2 * L * D2),2))) < 1e-10;
U = V(:,2);
else
U = V(:,1);
end
% make the output vector consistent with the labels.
if U(n+1,1) > 0
F = U(1:n,1);
else
F = -U(1:n,1);
end
function F = GBE(posW,label,ew)
% Implements our GBE semi-supervised learning algorithm
%
% posW: is the n * n weighted adjacency matrix of a undirected graph.
% label: is a n * c label indication matrix, with value {-1, 0 1} if
%
label is a vector, {1,0} if label is a matrix; and 0 row for
% unlabelled nodes.
% ew: is a [apw,anw] vector for positive and negative added edge
%
weight between labeled points; default value is [1,1].
[n,m] = size(label);
if m <= 2 % two classes
if m==1
Y(:,1) = label;
Y(:,2) = -label;
Y(Y<0) = 0;
