-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathECPN_chainred_pol_v3.m
208 lines (167 loc) · 8.08 KB
/
ECPN_chainred_pol_v3.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
function P = ECPN_chainred_pol_v3(rel,E,Wpol)
%ECPN_chainred_pol Calculates ECPN for a graph with chain inside
% Detailed explanation goes here
%% assert: no hnodes, not a cycle
n = length(E);
[ index, c0, cend, c_first, c_last ] = GetIndexedChain(E);
%reordering nodes, maybe need to avoid this
rel = rel(index);
E = E(index,index);
Wpol = Wpol(index,:);
%if nnz(rel(c_first:c_last)) ~= 0
% BGView = biograph(triu(E));
% set(BGView,'ShowArrows','off');%,'LayoutType','equilibrium');
% view(BGView)
% rel
%end
%assert(nnz(rel(c_first:c_last)) == 0, '[ERROR] have reliable nodes in the chain!');
%Es = Es(index);
%now we have biggest chain on nodes c_first:c_last
%nodes c0:c_first and (c_last):cend are adjacent
%need to optimize chain reductions
%mult = prod(V(c_first:c_last));
%mult = [1 zeros(1,nnz(~rel(c_first:c_last)))];
%cum_nonrel = [0,cumsum(~rel(c_first:end))]; % look into making array shorter
cum_nonrel(c_first:n) = cumsum(~rel(c_first:n)); % look into making array shorter
[comps_br([c0 c_first c_last+1:n]), c_lens] = components_mex(E([c0 c_first c_last+1:n],[c0 c_first c_last+1:n])); % shortcut, need to place MEX-file to the MATLAB path
chainbridge = length(c_lens);
comps_br(c_first) = 0; % important!
assert(chainbridge < 3, '[ERROR] Assertion failed: Unbelieveable! First node of the chain breaks graph into >2 conn comps!');
assert(comps_br(c0) == 1,'[ERROR] Left part of graph has index 2!');
%cnext = [2:c_last cend];
% calculating chain
%cum_r = get_cumsum_ord_chain_right(rel(3:c_last),Wpol(3:c_last,:));
if chainbridge == 2 % c0 ~= cend
ind1 = find(comps_br == 1);
ind2 = find(comps_br == 2);
if rel(c_first) % unfolding loop
P = 0;
else
ind_q_l = ind1;
ind_q_r = [c_last ind2];
tmp = ECPN_C_pol(rel(ind_q_l),E(ind_q_l,ind_q_l),Wpol(ind_q_l,:));
tmp_q_r = ECPN_ordered_chain_pol(rel(c_first+1:c_last), Wpol2VWpol( rel(c_first+1:c_last), Wpol(c_first+1:c_last,:)));
tmp = poly_add(tmp,tmp_q_r);
Wlast = get_cum_Wlast(rel(c_first+1:c_last),Wpol(c_first+1:c_last,:));
k = length(Wlast)-length(Wpol(1,:));
tmp = poly_add(tmp, ECPN_C_pol(rel(ind_q_r),E(ind_q_r,ind_q_r),[Wlast ; zeros(length(ind_q_r)-1,k) Wpol(ind_q_r(2:end),:)]));
P = -[tmp 0] + [0 tmp];
rel(c_first) = 1;
end
WpolF = Wpol(c_first,:);
tmp_p = zeros(1,2*length(Wpol(1,:))-1); % for elimination of poly_add
%now we can reduce resulting reliable node in chain to node 2 in each iteration
for i=c_first+1:c_last % add one to c_max_len because of the starting node!
%tmp_p = tmp_p + conv2(Wpol(c_first,:),Wpol(i,:));
tmp_p = tmp_p + conv2(WpolF,Wpol(i,:));
%P = poly_add(P,[conv2(Wpol(c_first,:),Wpol(i,:)) zeros(1,cum_nonrel(i-1))]);
WpolF = WpolF + Wpol(i,:);
% Wpol(c_first,:) = WpolF;
%Wpol(c_first,:) = Wpol(c_first,:) + Wpol(i,:);
if ~rel(i)
ind_q_l = [ind1(1) c_first:(i-1) ind1(2:end)];
ind_q_r = [c_last ind2];
tmp = ECPN_C_pol(rel(ind_q_l),E(ind_q_l,ind_q_l),Wpol(ind_q_l,:));
if i < c_last
tmp_q_r = ECPN_ordered_chain_pol(rel(i+1:c_last), Wpol2VWpol( rel(i+1:c_last), Wpol(i+1:c_last,:)));
tmp = poly_add(tmp,tmp_q_r);
Wlast = get_cum_Wlast(rel(i+1:c_last),Wpol(i+1:c_last,:));
k = length(Wlast)-length(Wpol(1,:));
tmp = poly_add(tmp, ECPN_C_pol(rel(ind_q_r),E(ind_q_r,ind_q_r),[Wlast ; zeros(length(ind_q_r)-1,k) Wpol(ind_q_r(2:end),:)]));
else
tmp = poly_add(tmp, ECPN_C_pol(rel(ind_q_r(2:end)),E(ind_q_r(2:end),ind_q_r(2:end)), Wpol(ind_q_r(2:end),:)));
end
%tmp = poly_add(tmp, tmp_p);
tmp = -[tmp 0] + [0 tmp];
P = poly_add(P,[tmp zeros(1,cum_nonrel(i-1))]);
rel(i) = 1;
end
end
%P = P + mult*ECPN_full(W(c_first:c_last));
tmp_p = ECPN_ordered_chain_pol(rel(c_first:c_last), Wpol2VWpol( rel(c_first:c_last), Wpol(c_first:c_last,:)));
P = poly_add(P,[tmp_p zeros(1,cum_nonrel(c_last))]);
% P = poly_add(P,tmp_p);
%Wpol(c_first,:) = sum(Wpol(c_first:c_last,:),1); %!!! CAREFUL !!!
Wpol(c_first,:) = WpolF;
assert (cend ~= c0, 'Assertion failed: unbelieveable, cend==c0 while chainbridge==2 !');
E(c_first,cend) = 1; E(cend,c_first) = 1;
%need to rewrite the algorhitm to enable next line
% E(c0,cend) = 1; E(cend,) = 1; % contracting c0 with cend
ind_p = [c0 c_first c_last+1:n];
%P = P + mult*ECPN_C(V(ind_p),E(ind_p,ind_p),W(ind_p));
tmp = ECPN_C_pol(rel(ind_p),E(ind_p,ind_p),Wpol(ind_p,:));
P = poly_add(P,[tmp zeros(1,cum_nonrel(c_last))]);
else % chainbridge = 1
if ~rel(c_first) % unfolding loop
ind_q = [c0 c_last:n];
Wlast = get_cum_Wlast(rel(c_first+1:c_last),Wpol(c_first+1:c_last,:));
k = length(Wlast)-length(Wpol(1,:));
Wcomp = [zeros(length(ind_q),k) Wpol(ind_q,:)];
Wcomp(2,:) = Wlast;
tmp = ECPN_C_pol(rel(ind_q),E(ind_q,ind_q),Wcomp);
tmp_q_r = ECPN_ordered_chain_pol(rel(c_first+1:c_last), Wpol2VWpol( rel(c_first+1:c_last), Wpol(c_first+1:c_last,:)));
tmp = poly_add(tmp,tmp_q_r);
tmp = -[tmp 0] + [0 tmp];
P = [tmp zeros(1,cum_nonrel(1))];
rel(c_first) = 1;
else
P = 0;
end
WpolF = Wpol(c_first,:);
tmp_p = zeros(1,2*length(Wpol(1,:))-1); % for elimination of poly_add
%now we can reduce resulting reliable node in chain to node 2 in each iteration
for i=c_first+1:c_last % add one to c_max_len because of the starting node!
if ~rel(i)
if i < c_last
ind_q = [c0 c_first:i-1 c_last:n];
Wlast = get_cum_Wlast(rel(i+1:c_last),Wpol(i+1:c_last,:));
k = length(Wlast)-length(Wpol(1,:));
Wcomp = [zeros(length(ind_q),k) Wpol(ind_q,:)];
Wcomp(i-c_first+2,:) = Wlast;
tmp = ECPN_C_pol(rel(ind_q),E(ind_q,ind_q), Wcomp);
tmp_q_r = ECPN_ordered_chain_pol(rel(i+1:c_last), Wpol2VWpol( rel(i+1:c_last), Wpol(i+1:c_last,:)));
tmp = poly_add(tmp,tmp_q_r);
else %i == c_last
ind_q = [c0 c_first:i-1 c_last+1:n];
tmp = ECPN_C_pol(rel(ind_q),E(ind_q,ind_q), Wpol(ind_q,:));
end
tmp = -[tmp 0] + [0 tmp];
P = poly_add(P,[tmp zeros(1,cum_nonrel(i-1))]);
rel(i) = 1;
end
%tmp_p = tmp_p + conv2(Wpol(2,:),Wpol(i,:));
tmp_p = tmp_p + conv2(WpolF,Wpol(i,:));
%P = poly_add(P,[conv2(Wpol(2,:),Wpol(i,:)) zeros(1,cum_nonrel(i-1))]);
WpolF = WpolF + Wpol(i,:);
%Wpol(c_first,:) = Wpol(c_first,:) + Wpol(i,:);
%E(c_first,cnext(i)) = 1;
%E(cnext(i),c_first) = 1;
end
%P = P + mult*ECPN_full(W(c_first:c_last));
%tmp = ECPN_full_pol(Wpol(c_first:c_last,:));
%P = poly_add(P,[tmp zeros(1,cum_nonrel(c_last))]);
%P = poly_add(P,[tmp_p zeros(1,cum_nonrel(c_last))]);
%P = poly_add(P,tmp_p);
%Wpol(c_first,:) = sum(Wpol(c_first:c_last,:),1); %!!! CAREFUL !!!
Wpol(c_first,:) = WpolF;
assert (cend ~= c_first, 'Assertion failed: unbelieveable, cend ~=c_first !');
if cend == c0 %c0 == cend
if rel(c0)
% tmp_p = poly_add(tmp_p,conv(Wpol(c0,:),Wpol(c_first,:)));
tmp_p = tmp_p + conv(Wpol(c0,:),Wpol(c_first,:));
else
tmp_p = [0 tmp_p] + [conv(Wpol(c0,:),Wpol(c_first,:)) 0];
end
Wpol(c0,:) = Wpol(c0,:) + Wpol(c_first,:);
ind_p = [c0 c_last+1:n];
else
E(c_first,cend) = 1; E(cend,c_first) = 1;
%need to rewrite the algorhitm to enable next line
% E(c0,cend) = 1; E(cend,c0) = 1; % contracting c0 with cend
ind_p = [c0 c_first c_last+1:n];
end
%P = P + mult*ECPN_C(V(ind_p),E(ind_p,ind_p),W(ind_p));
tmp_p = poly_add(tmp_p,ECPN_C_pol(rel(ind_p),E(ind_p,ind_p),Wpol(ind_p,:)));
P = poly_add(P,[tmp_p zeros(1,cum_nonrel(c_last))]);
end
end