-
Notifications
You must be signed in to change notification settings - Fork 0
/
SAFT.m
77 lines (72 loc) · 1.76 KB
/
SAFT.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
seed='6';
dataset='Epinions';
edges = load(strcat('Data\', dataset, '\seed=', seed, '\train_edges.txt'));
order = 20;
sources = edges(:, 1) + 1; % Our edge list is generated by Py script, such that index starts from 0. But in Matlab, index starts from 1.
targets = edges(:, 2) + 1;
weights = edges(:, 3);
G = graph();
G = addedge(G, sources, targets, weights);
G = simplify(G);
numNodes = G.numnodes;
A = full(adjacency(G,'weighted'));
I = speye(numNodes);
vecD = abs(A) * ones(numNodes, 1);
D = speye(numNodes);
for i = 1: numNodes
D(i, i) = vecD(i);
end
invD = sparse(numNodes, numNodes);
for i = 1: numNodes
if D(i, i) == 0
continue
end
invD(i, i) = 1 / D(i, i);
end
P = invD * A;
P1 = P;
P1(P1<0) = 0;
P2 = P;
P2(P1>0) = 0;
P2 = -P2;
positive_SAFT = zeros(numNodes, numNodes);
negative_SAFT = zeros(numNodes, numNodes);
tic
parfor i = 1: numNodes
disp(i);
X1 = P1(:, i);
X2 = P2(:, i);
Q1 = P1;
Q1(:, i) = 0;
Q1(i, :) = 0;
Q2 = P2;
Q2(:, i) = 0;
Q2(i, :) = 0;
R1 = Q1 + Q2;
R2 = Q1 - Q2;
tmp1 = X1;
tmp2 = X1;
tmp3 = X2;
tmp4 = X2;
psaft = (tmp1 + tmp2 + tmp3 - tmp4);
nsaft = (tmp3 + tmp4 + tmp1 - tmp2);
d1 = (tmp1 + tmp2 + tmp3 - tmp4);
d2 = (tmp3 + tmp4 + tmp1 - tmp2);
for n = 2: order
tmp1 = R1 * tmp1;
tmp2 = R2 * tmp2;
tmp3 = R1 * tmp3;
tmp4 = R2 * tmp4;
tmp5 = tmp1 + tmp2 + tmp3 - tmp4;
tmp6 = tmp3 + tmp4 + tmp1 - tmp2;
psaft = psaft + n * tmp5;
nsaft = nsaft + n * tmp6;
d1 = d1 + tmp5;
d2 = d2 + tmp6;
end
psaft = psaft ./ d1;
nsaft = nsaft ./ d2;
positive_SAFT(:, i) = psaft;
negative_SAFT(:, i) = nsaft;
end
toc