-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweighedClique.cpp
270 lines (219 loc) · 6.75 KB
/
weighedClique.cpp
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "weighedClique.h"
clique::clique()
{
nodes.resize(0);
}
std::ostream& operator <<(std::ostream &os,const clique &obj)
{
//os<<obj.strVal;
return os;
}
const size_t clique::size() const
{
return nodes.size();
}
const size_t clique::at(const size_t i) const
{
return nodes.at(i);
}
/*void clique::addNodes(const std::vector<size_t> & unsorted_nodes)
{
for (std::vector<size_t>::const_iterator iter = unsorted_nodes.begin(); iter != unsorted_nodes.end(); iter++)
nodes.push_back(*iter);
std::sort( nodes.begin(), nodes.end() ); // sort nodes
}*/
void clique::copyClique(const clique & old_clique)
{
const size_t newSize = old_clique.size();
nodes.resize(newSize);
for (size_t i = 0; i < newSize; i++)
nodes.at(i) = old_clique.at(i);
}
/*
clique( clique & old_clique) {
copyClique(old_clique);
}
clique( const clique & old_clique) {
copyClique(old_clique);
}
*/
void clique::replaceNodes( const std::vector<size_t> & unsorted_nodes )
{
nodes = unsorted_nodes;
std::sort( nodes.begin(), nodes.end() ); // sort nodes
}
void clique::printClique() const
{
for (std::vector<size_t>::const_iterator i = nodes.begin(); i != nodes.end(); i++)
std::cout << *i << " ";
std::cout << std::endl;
}
clique::clique(const std::vector<size_t> & unsorted_nodes)
{
replaceNodes(unsorted_nodes);
}
bool clique::operator== ( const clique & clique2 ) const
{
const size_t cliqueSize = size();
if ( cliqueSize == clique2.size() )
{
bool allEqual = true;
for ( size_t i = 0; allEqual && i < cliqueSize; i++)
{
if ( nodes.at(i) != clique2.at(i) ) allEqual = false;
}
return allEqual;
}
else return false;
}
bool clique::operator!= ( const clique & clique2 ) const // tän vois korvata operator==:n avulla
{
const size_t cliqueSize = size();
if ( cliqueSize == clique2.size() )
{
bool anyDifferent = false;
for ( size_t i = 0; !anyDifferent && i < cliqueSize; i++)
{
if ( nodes.at(i) != clique2.at(i) ) anyDifferent = true;
}
return anyDifferent;
}
else return true;
}
bool clique::operator< ( const clique & clique2) const
{
const size_t cliqueSize = size();
if ( cliqueSize == clique2.size() )
{
bool definetelySmaller = false, know_answer = false;
for (size_t i = 0; i < cliqueSize && !know_answer; ++i)
{
if ( (*this).at(i) < clique2.at(i) )
{
definetelySmaller = true;
know_answer = true;
}
else if ( (*this).at(i) > clique2.at(i) )
{
definetelySmaller = false;
know_answer = true;
}
}
return definetelySmaller;
}
else
{
std::cerr << "comparing cliques of different sizes!\n";
return false;
}
}
//gets link weight for every link in the clique
/*void weighedClique::getLinks(const NetType & net )
{
linkWs.clear();
for (size_t i = 0; i < size()-1; ++i)
{
for (size_t j = i+1; j < size(); ++j)
{
if ( net(nodes.at(i))[nodes.at(j)] > 0 )
{
linkWs.push_back( net(nodes.at(i))[nodes.at(j)] );
// std::cerr << nodes.at(i) << "\t" << nodes.at(j) << "\t" << net(nodes.at(i))[nodes.at(j)] << "\n";
}
else
{
std::cerr << "link weight was zero or less!\n";
std::cerr << nodes.at(i) << "\t" << nodes.at(j) << "\t" << net(nodes.at(i))[nodes.at(j)] << "\n";
}
}
}
if ( linkWs.size() != size()*(size()-1)/2 )
{
std::cerr << "obtained link number does not match clique link number!\n";
std::cerr << linkWs.size() << "\n";
}
}*/
void weighedClique::addNodes(const std::vector<size_t> & unsorted_nodes, const NetType & net)
{
for (std::vector<size_t>::const_iterator iter = unsorted_nodes.begin(); iter != unsorted_nodes.end(); iter++)
nodes.push_back(*iter);
std::sort( nodes.begin(), nodes.end() ); // sort nodes
weight = (this->*weightFunc)(net); // update weight
}
float weighedClique::minWeight(const NetType & net) const
{
float min = FLT_MAX;
for (std::vector<size_t>::const_iterator i = nodes.begin(); i != nodes.end(); i++)
{
std::vector<size_t>::const_iterator j = i;
for (j++; j != nodes.end(); j++)
if (net[*i][*j] < min)
min = net[*i][*j];
}
return min;
}
/*float weighedClique::maxWeight() const
{
const size_t size = weights.size();
float minWeight = weights.at(0);
for (size_t i = 1; i < size; ++i)
if ( weights.at(i) > minWeight ) minWeight = weights.at(i);
return minWeight;
}*/
float weighedClique::intensity(const NetType & net) const
{
float intensity = 1;
for (std::vector<size_t>::const_iterator i = nodes.begin(); i != nodes.end(); i++)
{
std::vector<size_t>::const_iterator j = i;
for (j++; j != nodes.end(); j++)
intensity *= net[*i][*j];
}
intensity = pow(intensity, 2.0/(nodes.size() * (nodes.size() - 1)));
return intensity;
}
weighedClique::weighedClique() // uses the default constructor
{
weight = 0;
weightFunc = &weighedClique::minWeight;
}
weighedClique::weighedClique( const std::vector<size_t> & unsorted_nodes, const NetType & net, size_t funcType ) : clique( unsorted_nodes ) // copy nodes using parent constructor
{
// nodes should now be ok, loop over them and build list of link weights as well as the weight
//getLinks( net );
// std::cerr << "funcType: " << funcType << "\n";
switch ( funcType )
{
case 0:
weightFunc = &weighedClique::minWeight;
break;
case 2:
// weightFunc = &weighedClique::maxWeight;
break;
case 1:
weightFunc = &weighedClique::intensity;
break;
default:
weightFunc = &weighedClique::minWeight;
}
weight = (this->*weightFunc)(net); // update weight
}
void weighedClique::copyClique(const weighedClique & old_clique)
{
std::vector<size_t> tmp_nodes;
for (size_t i = 0; i < old_clique.size(); ++i) tmp_nodes.push_back( old_clique.at(i) ); //
clique::replaceNodes( tmp_nodes );
weight = old_clique.getWeight();
}
void weighedClique::replaceNodes( const std::vector<size_t> & unsorted_nodes, const NetType & net )
{
clique::replaceNodes(unsorted_nodes);
//getLinks( net );
weight = (this->*weightFunc)(net); // update weight
}
void weighedClique::printWeightedClique(std::ostream & file) const
{
for (std::vector<size_t>::const_iterator i = nodes.begin(); i != nodes.end(); i++)
file << *i << " ";
file << "weight: " << getWeight() << std::endl;
}