-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompute_TW.m
135 lines (97 loc) · 3.39 KB
/
compute_TW.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
%
% compute tree-Wasserstein distance matrix (with random tree metric from graph structure)
%
% Choose:
% (1) typeGG = 'RandLLE' (G_Log) or typeGG = 'RandSLE' (G_Sqrt)
%
clear all
clc
typeGG = 'RandLLE'; % log-linear #edges
% typeGG = 'RandSLE'; % sqrt-linear #edges
dsName = 'twitter';
maxKC = 100;
nSS = 20; % #tree (average for TW)
load([dsName '_' num2str(maxKC) '_' typeGG '_Graph.mat']);
DD_SS = cell(nSS, 1);
runTime_Prep = zeros(nSS, 1);
runTime_Dist = zeros(nSS, 1);
for idSS = 1:nSS
disp('... randomly sampling tree');
[minTR_EdgeID, minTR_EdgeWW] = RandomlySamplingTree(nGG, GG.Edges.EndNodes, GG.Edges.Weight);
minTRGG = graph(minTR_EdgeID(:, 1), minTR_EdgeID(:, 2), minTR_EdgeWW);
s0=1;
tic
disp(['... compute the tree path']);
% tree path!!!
[trPP, trDD, trEP] = shortestpathtree(minTRGG, s0, 'OutputForm', 'cell');
disp(['...vector representation for each vertex']);
% ---------------
% ===For TREE===
% vector representation for each vertex 1 --> nGG
disp('......vector representation for each vertex');
% length(wwGG): #edges in graph GG (can be reduced into #edges in tree)
vecGG_VV = zeros(nGG, length(minTR_EdgeWW));
for ii = 1:nGG % each vertex in graph/tree
vecGG_VV(ii, trEP{ii}) = 1;
end
disp('......vector representation for each distribution');
% ===For Data===
% N: #samples (input data)
XX_SI = zeros(N, length(minTR_EdgeWW));
for ii = 1:N % each distribution
tmpWW = WW{ii}/sum(WW{ii}); % normalization for weight!!!
tmpXX = XX_ID{ii};
tmpXX_GG = vecGG_VV(tmpXX, :);
tmpWW_GG = repmat(tmpWW, 1, length(minTR_EdgeWW));
tmpWWXX = tmpXX_GG .* tmpWW_GG;
XX_SI(ii, :) = sum(tmpWWXX, 1);
end
runTime_Prep_II = toc;
tic
% compute the Lp distance matrix
DD_TW = zeros(N, N);
for ii = 1:(N-1)
% ii --> (ii+1):N
tmpII_vec = XX_SI(ii, :);
tmpJJ_mat = XX_SI((ii+1):N, :);
tmpII_mat = repmat(tmpII_vec, N-ii, 1);
tmpAbsDD_mat = abs(tmpII_mat - tmpJJ_mat);
wwGG_mat = repmat(minTR_EdgeWW', N-ii, 1);
tmpWW_AbsDD_mat = wwGG_mat .* tmpAbsDD_mat;
tmpDD_vec = sum(tmpWW_AbsDD_mat, 2); % sum over rows --> column
DD_TW(ii, (ii+1):N) = tmpDD_vec';
DD_TW((ii+1):N, ii) = tmpDD_vec;
end
runTime_Dist_II = toc;
% saving !!!
runTime_Dist(idSS) = runTime_Dist_II;
runTime_Prep(idSS) = runTime_Dist_II;
DD_SS{idSS} = DD_TW;
end
runTime_Prep_Avg = sum(runTime_Prep) / nSS;
runTime_Dist_Avg = sum(runTime_Dist) / nSS;
runTime_Dist_ALL = runTime_Prep + runTime_Dist;
runTime_Dist_ALL_Avg = sum(runTime_Dist_ALL) / nSS;
% Average
tmpNN = [1, 5, 10, 20];
tmpDDSS_Cell = cell(length(tmpNN), 1);
for iiRR = 1:length(tmpNN)
tmpDDSS = zeros(N, N);
for ii = 1:tmpNN(iiRR)
tmpDDSS = tmpDDSS + DD_SS{ii};
end
tmpDDSS = tmpDDSS / tmpNN(iiRR);
tmpDDSS_Cell{iiRR} = tmpDDSS;
end
DD_TW1 = tmpDDSS_Cell{1};
DD_TW5 = tmpDDSS_Cell{2};
DD_TW10 = tmpDDSS_Cell{3};
DD_TW20 = tmpDDSS_Cell{4};
DD_TW = DD_SS;
outName = [dsName '_TW_Random_' num2str(maxKC) '_' typeGG '_S' num2str(nSS) '.mat'];
save(outName, 'DD_TW1', 'DD_TW5', 'DD_TW10', 'DD_TW20', ...
'runTime_Dist', 'runTime_Prep', 'runTime_Dist_ALL', ...
'runTime_Dist_Avg', 'runTime_Prep_Avg', 'runTime_Dist_ALL_Avg', ...
'nSS', ...
'YY');
disp('FINISH !!!');