NewmanGirvan.xs

filter: Statementstmt 0.0% 0 / 206 Branchbran 0.0% 0 / 286 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 492 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Graph/NewmanGirvan.pm ↑ Summary
1 // Quick and very dirty port of of the clustering algorithm implementation
2 // from Andreas Noack's <http://code.google.com/p/linloglayout/> released
3 // under the terms of the LGPLv2.1+.
4
5 #include <list>
6 #include <map>
7 #include <istream>
8 #include <ostream>
9 #include <string>
10 #include <algorithm>
11 #include <iostream>
12 #include <sstream>
13 #include <fstream>
14 #include <vector>
15 #include <limits>
16
17 extern "C"
18 {
19 #include "EXTERN.h"
20 #include "perl.h"
21 #include "XSUB.h"
22 }
23
24 #undef max
25
26 class node {
27 public:
28 0 node() : name(-1), weight(0) {
29 0 }
30 0 node(long aName, double aWeight) : weight(aWeight), name(aName) {
31 0 }
32 0 node(std::map< std::string, long>& string_map, std::string aName, double aWeight)
33 0 : weight(aWeight) {
34 0 ▶ if (string_map.find(aName) != string_map.end()) {
Branch: 2 branches
BranchTrueFalseTotal
if (string_map.find(aName) != string_map.end()) { 0 0 0
if (string_map.find(aName) != string_map.end()) { 0 0 0
35 0 ▶ name = string_map.find(aName)->second;
Branch: 1 branch
BranchTrueFalseTotal
name = string_map.find(aName)->second; 0 0 0
36 } else {
37 0 name = string_map.size();
38 0 ▶ string_map.insert(std::pair<std::string, long>(aName, name));
Branch: 2 branches
BranchTrueFalseTotal
string_map.insert(std::pair<std::string, long>(aName, name)); 0 0 0
string_map.insert(std::pair<std::string, long>(aName, name)); 0 0 0
39 }
40 0 }
41 long name;
42 double weight;
43 0 int operator==(const node& right) const {
44 0 return name == right.name;
45 }
46 0 bool operator<(const node& right) const {
47 0 return name < right.name;
48 }
49 };
50
51 class edge {
52 public:
53 0 edge(node aSrc, node aDst, double aWeight)
54 0 : src(aSrc), dst(aDst), weight(aWeight) {
55 0 }
56 class node src;
57 class node dst;
58 double weight;
59 int operator==(const edge& right) const {
60 return src == right.src && dst == right.dst && weight == right.weight;
61 }
62 };
63
64 class NewmanGirvan {
65 public:
66 std::map< std::string, long> string_map;
67 std::map< std::string, node > nameToNode;
68 std::map< long, double > nameToWeight;
69 std::map< node, std::string > nodeToName;
70 std::map< std::pair< node, node >, double> edgeToWeight;
71 std::map< std::pair< node, node >, double>::iterator edgeToWeight_iter;
72
73 0 void add_edge(std::string src, std::string dst, double weight) {
74 0 ▶ if (nameToNode.find(src) == nameToNode.end()) {
Branch: 2 branches
BranchTrueFalseTotal
if (nameToNode.find(src) == nameToNode.end()) { 0 0 0
if (nameToNode.find(src) == nameToNode.end()) { 0 0 0
75 0 ▶ node new_node = createNode(src, 0);
Branch: 2 branches
BranchTrueFalseTotal
node new_node = createNode(src, 0); 0 0 0
node new_node = createNode(src, 0); 0 0 0
76 0 ▶ nameToNode.insert(std::pair<std::string, node>(src, new_node));
Branch: 2 branches
BranchTrueFalseTotal
nameToNode.insert(std::pair<std::string, node>(src, new_node)); 0 0 0
nameToNode.insert(std::pair<std::string, node>(src, new_node)); 0 0 0
77 0 ▶ nodeToName.insert(std::pair<node, std::string>(new_node, src));
Branch: 2 branches
BranchTrueFalseTotal
nodeToName.insert(std::pair<node, std::string>(new_node, src)); 0 0 0
nodeToName.insert(std::pair<node, std::string>(new_node, src)); 0 0 0
78 }
79 0 ▶ if (nameToNode.find(dst) == nameToNode.end()) {
Branch: 2 branches
BranchTrueFalseTotal
if (nameToNode.find(dst) == nameToNode.end()) { 0 0 0
if (nameToNode.find(dst) == nameToNode.end()) { 0 0 0
80 0 ▶ node new_node = createNode(dst, 0);
Branch: 2 branches
BranchTrueFalseTotal
node new_node = createNode(dst, 0); 0 0 0
node new_node = createNode(dst, 0); 0 0 0
81 0 ▶ nameToNode.insert(std::pair<std::string, node>(dst, new_node));
Branch: 2 branches
BranchTrueFalseTotal
nameToNode.insert(std::pair<std::string, node>(dst, new_node)); 0 0 0
nameToNode.insert(std::pair<std::string, node>(dst, new_node)); 0 0 0
82 0 ▶ nodeToName.insert(std::pair<node, std::string>(new_node, dst));
Branch: 2 branches
BranchTrueFalseTotal
nodeToName.insert(std::pair<node, std::string>(new_node, dst)); 0 0 0
nodeToName.insert(std::pair<node, std::string>(new_node, dst)); 0 0 0
83 }
84 0 ▶ edgeToWeight.insert(std::pair< std::pair<node, node>, double>(
Branch: 1 branch
BranchTrueFalseTotal
edgeToWeight.insert(std::pair< std::pair<node, node>, double>( 0 0 0
85 0 ▶ std::pair<node, node>(nameToNode[src], nameToNode[dst]),
Branch: 2 branches
BranchTrueFalseTotal
std::pair<node, node>(nameToNode[src], nameToNode[dst]), 0 0 0
std::pair<node, node>(nameToNode[src], nameToNode[dst]), 0 0 0
86 weight
87 ));
88 0 }
89
90 0 void set_vertex_weight(std::string vertex, double weight) {
91 0 nameToWeight[ string_map[vertex] ] = weight;
92 0 }
93
94 0 std::map<std::string, int> run() {
95 0 std::list< edge > edges;
96 0 ▶ for (edgeToWeight_iter = edgeToWeight.begin(); edgeToWeight_iter != edgeToWeight.end(); ++edgeToWeight_iter) {
Branch: 1 branch
BranchTrueFalseTotal
for (edgeToWeight_iter = edgeToWeight.begin(); edgeToWeight_iter != edgeToWeight.end(); ++edgeToWeight_iter) { 0 0 0
97 0 node src = edgeToWeight_iter->first.first;
98 0 node dst = edgeToWeight_iter->first.second;
99 0 double weight = edgeToWeight_iter->second;
100 0 double revWeight = 0;
101 0 std::pair<node, node> rev = std::pair<node,node>(dst, src);
102 0 ▶ if (edgeToWeight.find(rev) != edgeToWeight.end()) {
Branch: 2 branches
BranchTrueFalseTotal
if (edgeToWeight.find(rev) != edgeToWeight.end()) { 0 0 0
if (edgeToWeight.find(rev) != edgeToWeight.end()) { 0 0 0
103 0 ▶ revWeight = edgeToWeight[rev];
Branch: 1 branch
BranchTrueFalseTotal
revWeight = edgeToWeight[rev]; 0 0 0
104 } else {
105 0 ▶ edges.push_back(createEdge(dst, src, weight + revWeight));
Branch: 1 branch
BranchTrueFalseTotal
edges.push_back(createEdge(dst, src, weight + revWeight)); 0 0 0
106 }
107 0 ▶ edges.push_back(createEdge(src, dst, weight + revWeight));
Branch: 1 branch
BranchTrueFalseTotal
edges.push_back(createEdge(src, dst, weight + revWeight)); 0 0 0
108 }
109
110 0 std::list<edge> edges2;
111 0 ▶ for (std::list< edge >::iterator i = edges.begin(); i != edges.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::list< edge >::iterator i = edges.begin(); i != edges.end(); ++i) { 0 0 0
112 0 ▶ edges2.push_back(createEdge(createNode(i->src.name, nameToWeight[ i->src.name ]),
Branch: 2 branches
BranchTrueFalseTotal
edges2.push_back(createEdge(createNode(i->src.name, nameToWeight[ i->src.name ]), 0 0 0
edges2.push_back(createEdge(createNode(i->src.name, nameToWeight[ i->src.name ]), 0 0 0
113 0 ▶ createNode(i->dst.name, nameToWeight[ i->dst.name ]),
Branch: 1 branch
BranchTrueFalseTotal
createNode(i->dst.name, nameToWeight[ i->dst.name ]), 0 0 0
114 0 i->weight));
115 }
116
117 0 ▶ edges = edges2;
Branch: 1 branch
BranchTrueFalseTotal
edges = edges2; 0 0 0
118
119 0 std::list<node> nodes;
120
121 0 ▶ for (std::map< std::string, node >::iterator i = nameToNode.begin(); i != nameToNode.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::map< std::string, node >::iterator i = nameToNode.begin(); i != nameToNode.end(); ++i) { 0 0 0
122 0 ▶ nodes.push_back(createNode(i->second.name, nameToWeight[i->second.name]));
Branch: 2 branches
BranchTrueFalseTotal
nodes.push_back(createNode(i->second.name, nameToWeight[i->second.name])); 0 0 0
nodes.push_back(createNode(i->second.name, nameToWeight[i->second.name])); 0 0 0
123 }
124
125 0 ▶ std::map<node, int> temp = execute(nodes, edges, false);
Branch: 1 branch
BranchTrueFalseTotal
std::map<node, int> temp = execute(nodes, edges, false); 0 0 0
126 0 std::map<std::string, int> result;
127 0 ▶ for (std::map<node, int>::const_iterator i = temp.begin(); i != temp.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::map<node, int>::const_iterator i = temp.begin(); i != temp.end(); ++i) { 0 0 0
128 0 ▶ result[ nodeToName[ i->first ] ] = i->second;
Branch: 2 branches
BranchTrueFalseTotal
result[ nodeToName[ i->first ] ] = i->second; 0 0 0
result[ nodeToName[ i->first ] ] = i->second; 0 0 0
129 }
130 0 return result;
131 0 }
132
133 0 node createNode(long aName, double aWeight) {
134 0 return node(aName, aWeight);
135 }
136 0 node createNode(std::string aName, double aWeight) {
137 0 ▶ return node(string_map, aName, aWeight);
Branch: 2 branches
BranchTrueFalseTotal
return node(string_map, aName, aWeight); 0 0 0
return node(string_map, aName, aWeight); 0 0 0
138 }
139 0 edge createEdge(node aSrc, node aDst, double aWeight) {
140 0 return edge(aSrc, aDst, aWeight);
141 }
142 void refine(std::map<node, int> nodeToCluster, std::map< node, std::list<edge> > nodeToEdges, double atedges, double atpairs);
143 std::map<node, int> cluster(std::list<node>& nodes, std::list<edge>& edges, double atedges, double atpairs);
144 std::map<node, int> execute(std::list<node>& nodes, std::list<edge>& edges, bool ignoreLoops);
145
146 };
147
148 double
149 0 edge_density(class edge& edge) {
150 0 return edge.weight / (edge.src.weight * edge.dst.weight);
151 }
152
153 0 double qualityFunc(double interAtedges, double interAtpairs, double atedges, double atpairs) {
154 0 return interAtedges/atedges - interAtpairs/atpairs;
155 }
156
157 void
158 0 NewmanGirvan::refine(std::map<node, int> nodeToCluster, std::map< node, std::list<edge> > nodeToEdges, double atedges, double atpairs) {
159 0 int maxCluster = 0;
160
161 0 ▶ for (std::map< node, int >::const_iterator i = nodeToCluster.begin(); i != nodeToCluster.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::map< node, int >::const_iterator i = nodeToCluster.begin(); i != nodeToCluster.end(); ++i) { 0 0 0
162 0 maxCluster = std::max(maxCluster, i->second);
163 }
164
165 // compute clusterToAtnodes, interAtedges, interAtpairs
166 0 ▶ std::vector<double> clusterToAtnodes( nodeToCluster.size() + 1 );
Branch: 1 branch
BranchTrueFalseTotal
std::vector<double> clusterToAtnodes( nodeToCluster.size() + 1 ); 0 0 0
167
168 0 ▶ for (std::map< node, int >::const_iterator i = nodeToCluster.begin(); i != nodeToCluster.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::map< node, int >::const_iterator i = nodeToCluster.begin(); i != nodeToCluster.end(); ++i) { 0 0 0
169 0 ▶ clusterToAtnodes[nodeToCluster[i->first]] += i->first.weight;
Branch: 1 branch
BranchTrueFalseTotal
clusterToAtnodes[nodeToCluster[i->first]] += i->first.weight; 0 0 0
170 }
171
172 0 double interAtedges = 0.0;
173 0 ▶ for (std::map< node, std::list<edge> >::const_iterator i = nodeToEdges.begin(); i != nodeToEdges.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::map< node, std::list<edge> >::const_iterator i = nodeToEdges.begin(); i != nodeToEdges.end(); ++i) { 0 0 0
174 0 ▶ for (std::list<edge>::const_iterator i2 = i->second.begin(); i2 != i->second.end(); ++i2) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::list<edge>::const_iterator i2 = i->second.begin(); i2 != i->second.end(); ++i2) { 0 0 0
175 0 ▶ if ( !nodeToCluster[i2->src] == nodeToCluster[i2->dst] ) {
Branch: 3 branches
BranchTrueFalseTotal
if ( !nodeToCluster[i2->src] == nodeToCluster[i2->dst] ) { 0 0 0
if ( !nodeToCluster[i2->src] == nodeToCluster[i2->dst] ) { 0 0 0
if ( !nodeToCluster[i2->src] == nodeToCluster[i2->dst] ) { 0 0 0
176 0 interAtedges += i2->weight;
177 }
178 }
179 }
180 0 double interAtpairs = 0.0;
181
182 0 ▶ for (std::map< node, int >::const_iterator i = nodeToCluster.begin(); i != nodeToCluster.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::map< node, int >::const_iterator i = nodeToCluster.begin(); i != nodeToCluster.end(); ++i) { 0 0 0
183 0 interAtpairs += i->first.weight;
184 }
185
186 0 interAtpairs *= interAtpairs;
187
188 0 ▶ for (std::vector<double>::const_iterator i = clusterToAtnodes.begin(); i != clusterToAtnodes.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::vector<double>::const_iterator i = clusterToAtnodes.begin(); i != clusterToAtnodes.end(); ++i) { 0 0 0
189 0 interAtpairs -= *i * *i;
190 }
191
192 // greedily move nodes between clusters
193 0 double prevQuality = std::numeric_limits<double>::max();
194 0 ▶ double quality = qualityFunc(interAtedges, interAtpairs, atedges, atpairs);
Branch: 1 branch
BranchTrueFalseTotal
double quality = qualityFunc(interAtedges, interAtpairs, atedges, atpairs); 0 0 0
195
196 0 ▶ while (quality < prevQuality) {
Branch: 1 branch
BranchTrueFalseTotal
while (quality < prevQuality) { 0 0 0
197 0 prevQuality = quality;
198 0 ▶ for (std::map< node, int >::const_iterator i = nodeToCluster.begin(); i != nodeToCluster.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::map< node, int >::const_iterator i = nodeToCluster.begin(); i != nodeToCluster.end(); ++i) { 0 0 0
199 0 node node = i->first;
200 0 int bestCluster = 0;
201 0 double bestQuality = quality, bestInterAtedges = interAtedges, bestInterAtpairs = interAtpairs;
202 0 ▶ std::vector<double> clusterToAtedges(nodeToCluster.size() + 1);
Branch: 1 branch
BranchTrueFalseTotal
std::vector<double> clusterToAtedges(nodeToCluster.size() + 1); 0 0 0
203
204 0 ▶ for (std::list<edge>::const_iterator i = nodeToEdges[node].begin(); i != nodeToEdges[node].end(); ++i) {
Branch: 3 branches
BranchTrueFalseTotal
for (std::list<edge>::const_iterator i = nodeToEdges[node].begin(); i != nodeToEdges[node].end(); ++i) { 0 0 0
for (std::list<edge>::const_iterator i = nodeToEdges[node].begin(); i != nodeToEdges[node].end(); ++i) { 0 0 0
for (std::list<edge>::const_iterator i = nodeToEdges[node].begin(); i != nodeToEdges[node].end(); ++i) { 0 0 0
205 0 ▶ if (!(i->dst == node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(i->dst == node)) { 0 0 0
206 // count weight twice to include reverse edge
207 0 ▶ clusterToAtedges[nodeToCluster[i->dst]] += 2 * i->weight;
Branch: 1 branch
BranchTrueFalseTotal
clusterToAtedges[nodeToCluster[i->dst]] += 2 * i->weight; 0 0 0
208 }
209 }
210 0 ▶ int cluster = nodeToCluster[node];
Branch: 1 branch
BranchTrueFalseTotal
int cluster = nodeToCluster[node]; 0 0 0
211 0 ▶ for (int newCluster = 0; newCluster <= maxCluster+1; newCluster++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int newCluster = 0; newCluster <= maxCluster+1; newCluster++) { 0 0 0
212 0 ▶ if (cluster == newCluster) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (cluster == newCluster) continue; 0 0 0
213 double newInterPairs = interAtpairs
214 0 + clusterToAtnodes[cluster] * clusterToAtnodes[cluster]
215 0 - (clusterToAtnodes[cluster]-node.weight) * (clusterToAtnodes[cluster]-node.weight)
216 0 + clusterToAtnodes[newCluster] * clusterToAtnodes[newCluster]
217 0 - (clusterToAtnodes[newCluster]+node.weight) * (clusterToAtnodes[newCluster]+node.weight);
218 double newInterEdges = interAtedges
219 0 + clusterToAtedges[cluster]
220 0 - clusterToAtedges[newCluster];
221 0 ▶ double newQuality = qualityFunc(newInterEdges, newInterPairs, atedges, atpairs);
Branch: 1 branch
BranchTrueFalseTotal
double newQuality = qualityFunc(newInterEdges, newInterPairs, atedges, atpairs); 0 0 0
222 0 ▶ if (bestQuality - newQuality > 1e-8) {
Branch: 1 branch
BranchTrueFalseTotal
if (bestQuality - newQuality > 1e-8) { 0 0 0
223 0 bestCluster = newCluster;
224 0 bestQuality = newQuality;
225 0 bestInterAtedges = newInterEdges;
226 0 bestInterAtpairs = newInterPairs;
227 }
228 }
229 0 ▶ if (bestQuality < quality) {
Branch: 1 branch
BranchTrueFalseTotal
if (bestQuality < quality) { 0 0 0
230 0 clusterToAtnodes[cluster] -= node.weight;
231 0 clusterToAtnodes[bestCluster] += node.weight;
232 0 ▶ nodeToCluster[node] = bestCluster;
Branch: 1 branch
BranchTrueFalseTotal
nodeToCluster[node] = bestCluster; 0 0 0
233 0 maxCluster = std::max(maxCluster, bestCluster);
234 0 quality = bestQuality;
235 0 interAtedges = bestInterAtedges;
236 0 interAtpairs = bestInterAtpairs;
237 }
238 0 }
239 }
240 0 }
241
242 bool
243 0 compare_density(edge& first, edge& second) {
244 0 return edge_density(first) > edge_density(second);
245 }
246
247 std::map<node, int>
248 0 NewmanGirvan::cluster(std::list<node>& nodes, std::list<edge>& edges, double atedges, double atpairs) {
249 0 ▶ edges.sort(compare_density);
Branch: 1 branch
BranchTrueFalseTotal
edges.sort(compare_density); 0 0 0
250
251 0 std::map<node, node> nodeToContr;
252 0 std::list<node> contrNodes;
253
254 0 ▶ for (std::list<edge>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::list<edge>::const_iterator i = edges.begin(); i != edges.end(); ++i) { 0 0 0
255 0 edge edge = *i;
256 0 ▶ if (edge_density(edge) < atedges/atpairs) break;
Branch: 2 branches
BranchTrueFalseTotal
if (edge_density(edge) < atedges/atpairs) break; 0 0 0
if (edge_density(edge) < atedges/atpairs) break; 0 0 0
257 0 ▶ if (edge.src == edge.dst) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (edge.src == edge.dst) continue; 0 0 0
258 0 ▶ if (nodeToContr.find(edge.src) != nodeToContr.end() || nodeToContr.find(edge.dst) != nodeToContr.end()) continue;
Branch: 5 branches
BranchTrueFalseTotal
if (nodeToContr.find(edge.src) != nodeToContr.end() || nodeToContr.find(edge.dst) != nodeToContr.end()) continue; 0 0 0
if (nodeToContr.find(edge.src) != nodeToContr.end() || nodeToContr.find(edge.dst) != nodeToContr.end()) continue; 0 0 0
if (nodeToContr.find(edge.src) != nodeToContr.end() || nodeToContr.find(edge.dst) != nodeToContr.end()) continue; 0 0 0
if (nodeToContr.find(edge.src) != nodeToContr.end() || nodeToContr.find(edge.dst) != nodeToContr.end()) continue; 0 0 0
if (nodeToContr.find(edge.src) != nodeToContr.end() || nodeToContr.find(edge.dst) != nodeToContr.end()) continue; 0 0 0
259 // randomize contraction
260 // if (!nodeToContr.isEmpty() && Math.random() < 0.5) continue;
261
262 0 ▶ std::stringstream ss;
Branch: 1 branch
BranchTrueFalseTotal
std::stringstream ss; 0 0 0
263 0 ▶ ss << edge.src.name << " " << edge.dst.name;
Branch: 3 branches
BranchTrueFalseTotal
ss << edge.src.name << " " << edge.dst.name; 0 0 0
ss << edge.src.name << " " << edge.dst.name; 0 0 0
ss << edge.src.name << " " << edge.dst.name; 0 0 0
264
265 0 ▶ node contrNode = createNode(ss.str(), edge.src.weight + edge.dst.weight);
Branch: 2 branches
BranchTrueFalseTotal
node contrNode = createNode(ss.str(), edge.src.weight + edge.dst.weight); 0 0 0
node contrNode = createNode(ss.str(), edge.src.weight + edge.dst.weight); 0 0 0
266
267 0 ▶ nodeToContr.insert(std::pair<node,node>(edge.src, contrNode));
Branch: 1 branch
BranchTrueFalseTotal
nodeToContr.insert(std::pair<node,node>(edge.src, contrNode)); 0 0 0
268 0 ▶ nodeToContr.insert(std::pair<node,node>(edge.dst, contrNode));
Branch: 1 branch
BranchTrueFalseTotal
nodeToContr.insert(std::pair<node,node>(edge.dst, contrNode)); 0 0 0
269 0 ▶ contrNodes.push_back(contrNode);
Branch: 1 branch
BranchTrueFalseTotal
contrNodes.push_back(contrNode); 0 0 0
270 0 }
271 // terminal case: no nodes to contract
272 0 ▶ if (nodeToContr.empty()) {
Branch: 1 branch
BranchTrueFalseTotal
if (nodeToContr.empty()) { 0 0 0
273 0 std::map<node, int> nodeToCluster;
274 0 int clusterId = 0;
275 0 ▶ for (std::list<node>::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::list<node>::const_iterator i = nodes.begin(); i != nodes.end(); ++i) { 0 0 0
276 0 ▶ nodeToCluster.insert(std::pair<node, int>(*i, clusterId++));
Branch: 1 branch
BranchTrueFalseTotal
nodeToCluster.insert(std::pair<node, int>(*i, clusterId++)); 0 0 0
277 }
278 0 return nodeToCluster;
279 0 }
280
281 // "contract" singleton clusters
282 0 ▶ for (std::list<node>::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::list<node>::const_iterator i = nodes.begin(); i != nodes.end(); ++i) { 0 0 0
283 0 class node node = *i;
284 0 ▶ if (nodeToContr.find(node) == nodeToContr.end()) {
Branch: 2 branches
BranchTrueFalseTotal
if (nodeToContr.find(node) == nodeToContr.end()) { 0 0 0
if (nodeToContr.find(node) == nodeToContr.end()) { 0 0 0
285 0 class node contrNode = createNode(node.name, node.weight);
286 0 ▶ nodeToContr.insert(std::pair<class node, class node>(node, contrNode));
Branch: 1 branch
BranchTrueFalseTotal
nodeToContr.insert(std::pair<class node, class node>(node, contrNode)); 0 0 0
287 0 ▶ contrNodes.push_back(contrNode);
Branch: 1 branch
BranchTrueFalseTotal
contrNodes.push_back(contrNode); 0 0 0
288 }
289 }
290
291 // contract edges
292 0 std::map< node, std::map< node, double > > startToEndToWeight;
293 0 std::map< node, std::map< node, double > >::const_iterator stetw_iter;
294
295 0 ▶ for (std::list<node>::const_iterator i = contrNodes.begin(); i != contrNodes.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::list<node>::const_iterator i = contrNodes.begin(); i != contrNodes.end(); ++i) { 0 0 0
296 0 ▶ startToEndToWeight.insert(std::pair<node, std::map<node, double> >(*i, std::map<node, double>()));
Branch: 1 branch
BranchTrueFalseTotal
startToEndToWeight.insert(std::pair<node, std::map<node, double> >(*i, std::map<node, double>())); 0 0 0
297 }
298 0 ▶ for (std::list<edge>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::list<edge>::const_iterator i = edges.begin(); i != edges.end(); ++i) { 0 0 0
299 0 class edge edge = *i;
300 0 ▶ node contrStart = nodeToContr[edge.src];
Branch: 1 branch
BranchTrueFalseTotal
node contrStart = nodeToContr[edge.src]; 0 0 0
301 0 ▶ node contrEnd = nodeToContr[edge.dst];
Branch: 1 branch
BranchTrueFalseTotal
node contrEnd = nodeToContr[edge.dst]; 0 0 0
302 0 double contrWeight = 0.0;
303 0 ▶ std::map<node, double>& endToWeight = startToEndToWeight[contrStart];
Branch: 1 branch
BranchTrueFalseTotal
std::map<node, double>& endToWeight = startToEndToWeight[contrStart]; 0 0 0
304 0 ▶ if (endToWeight.find(contrEnd) != endToWeight.end()) {
Branch: 2 branches
BranchTrueFalseTotal
if (endToWeight.find(contrEnd) != endToWeight.end()) { 0 0 0
if (endToWeight.find(contrEnd) != endToWeight.end()) { 0 0 0
305 0 ▶ contrWeight = endToWeight[contrEnd];
Branch: 1 branch
BranchTrueFalseTotal
contrWeight = endToWeight[contrEnd]; 0 0 0
306 }
307 0 ▶ endToWeight[contrEnd] = contrWeight + edge.weight;
Branch: 1 branch
BranchTrueFalseTotal
endToWeight[contrEnd] = contrWeight + edge.weight; 0 0 0
308 }
309
310 0 std::list<edge> contrEdges;
311 0 ▶ for (stetw_iter = startToEndToWeight.begin(); stetw_iter != startToEndToWeight.end(); ++stetw_iter) {
Branch: 1 branch
BranchTrueFalseTotal
for (stetw_iter = startToEndToWeight.begin(); stetw_iter != startToEndToWeight.end(); ++stetw_iter) { 0 0 0
312 0 class node contrStart = stetw_iter->first;
313 0 ▶ std::map<node, double>& endToWeight = startToEndToWeight[contrStart];
Branch: 1 branch
BranchTrueFalseTotal
std::map<node, double>& endToWeight = startToEndToWeight[contrStart]; 0 0 0
314 0 ▶ for (std::map<node, double>::const_iterator i2 = endToWeight.begin(); i2 != endToWeight.end(); ++i2) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::map<node, double>::const_iterator i2 = endToWeight.begin(); i2 != endToWeight.end(); ++i2) { 0 0 0
315 0 class node contrEnd = i2->first;
316 0 ▶ class edge contrEdge = createEdge(contrStart, contrEnd, endToWeight[contrEnd]);
Branch: 1 branch
BranchTrueFalseTotal
class edge contrEdge = createEdge(contrStart, contrEnd, endToWeight[contrEnd]); 0 0 0
317 0 ▶ contrEdges.push_back(contrEdge);
Branch: 1 branch
BranchTrueFalseTotal
contrEdges.push_back(contrEdge); 0 0 0
318 }
319 }
320
321 // cluster contracted graph
322 std::map<node, int> contrNodeToCluster
323 0 ▶ = cluster(contrNodes, contrEdges, atedges, atpairs);
Branch: 1 branch
BranchTrueFalseTotal
= cluster(contrNodes, contrEdges, atedges, atpairs); 0 0 0
324
325 // decontract clustering
326 0 std::map<node, int> nodeToCluster;
327 0 ▶ for (std::map<node, node>::const_iterator i = nodeToContr.begin(); i != nodeToContr.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::map<node, node>::const_iterator i = nodeToContr.begin(); i != nodeToContr.end(); ++i) { 0 0 0
328 0 ▶ nodeToCluster.insert(std::pair<node, int>(i->first, contrNodeToCluster[nodeToContr[i->first]]));
Branch: 3 branches
BranchTrueFalseTotal
nodeToCluster.insert(std::pair<node, int>(i->first, contrNodeToCluster[nodeToContr[i->first]])); 0 0 0
nodeToCluster.insert(std::pair<node, int>(i->first, contrNodeToCluster[nodeToContr[i->first]])); 0 0 0
nodeToCluster.insert(std::pair<node, int>(i->first, contrNodeToCluster[nodeToContr[i->first]])); 0 0 0
329 }
330
331 // refine decontracted clustering
332 0 std::map< node, std::list<edge> > nodeToEdge;
333
334 0 ▶ for (std::list<node>::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::list<node>::const_iterator i = nodes.begin(); i != nodes.end(); ++i) { 0 0 0
335 0 ▶ nodeToEdge.insert(std::pair<node, std::list<edge> >(*i, std::list<edge>()));
Branch: 1 branch
BranchTrueFalseTotal
nodeToEdge.insert(std::pair<node, std::list<edge> >(*i, std::list<edge>())); 0 0 0
336 }
337 0 ▶ for (std::list<edge>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::list<edge>::const_iterator i = edges.begin(); i != edges.end(); ++i) { 0 0 0
338 0 ▶ nodeToEdge[i->src].push_back(*i);
Branch: 2 branches
BranchTrueFalseTotal
nodeToEdge[i->src].push_back(*i); 0 0 0
nodeToEdge[i->src].push_back(*i); 0 0 0
339 }
340
341 0 ▶ refine(nodeToCluster, nodeToEdge, atedges, atpairs);
Branch: 3 branches
BranchTrueFalseTotal
refine(nodeToCluster, nodeToEdge, atedges, atpairs); 0 0 0
refine(nodeToCluster, nodeToEdge, atedges, atpairs); 0 0 0
refine(nodeToCluster, nodeToEdge, atedges, atpairs); 0 0 0
342
343 0 return nodeToCluster;
344 0 }
345
346 std::map<node, int>
347 0 NewmanGirvan::execute(std::list<node>& nodes, std::list<edge>& edges, bool ignoreLoops) {
348
349 // compute atedgeCnt and atpairCnt
350 0 double atedgeCnt = 0.0;
351 0 ▶ for (std::list<edge>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::list<edge>::const_iterator i = edges.begin(); i != edges.end(); ++i) { 0 0 0
352 0 ▶ if (!ignoreLoops || !(i->src == i->dst)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!ignoreLoops || !(i->src == i->dst)) { 0 0 0
if (!ignoreLoops || !(i->src == i->dst)) { 0 0 0
if (!ignoreLoops || !(i->src == i->dst)) { 0 0 0
353 0 atedgeCnt += i->weight;
354 }
355 }
356
357 0 double atpairCnt = 0.0;
358 0 ▶ for (std::list<node>::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::list<node>::const_iterator i = nodes.begin(); i != nodes.end(); ++i) { 0 0 0
359 0 atpairCnt += i->weight;
360 }
361 0 atpairCnt *= atpairCnt;
362 0 ▶ if (ignoreLoops) {
Branch: 1 branch
BranchTrueFalseTotal
if (ignoreLoops) { 0 0 0
363 0 ▶ for (std::list<node>::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::list<node>::const_iterator i = nodes.begin(); i != nodes.end(); ++i) { 0 0 0
364 0 atpairCnt -= i->weight * i->weight;
365 }
366 }
367
368 // compute clustering
369 0 return cluster(nodes, edges, atedgeCnt, atpairCnt);
370 }
371
372
373
374 MODULE = Graph::NewmanGirvan PACKAGE = Graph::NewmanGirvan
375
376 NewmanGirvan *
377 NewmanGirvan::new()
378
379 void
380 NewmanGirvan::add_edge(const char* src, const char* dst, double weight)
381
382 void
383 NewmanGirvan::set_vertex_weight(const char* vertex, double weight)
384
385 void
386 NewmanGirvan::compute()
387 PREINIT:
388 PPCODE:
389 0 ▶ std::map<std::string, int> result = THIS->run();
Branch: 1 branch
BranchTrueFalseTotal
std::map<std::string, int> result = THIS->run(); 0 0 0
390 0 ▶ for (std::map<std::string, int>::const_iterator i = result.begin(); i != result.end(); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (std::map<std::string, int>::const_iterator i = result.begin(); i != result.end(); ++i) { 0 0 0
391 0 ▶ mXPUSHs(newSVpvn(i->first.c_str(), i->first.size()));
Branch: 4 branches
BranchTrueFalseTotal
mXPUSHs(newSVpvn(i->first.c_str(), i->first.size())); 0 0 0
mXPUSHs(newSVpvn(i->first.c_str(), i->first.size())); 0 0 0
mXPUSHs(newSVpvn(i->first.c_str(), i->first.size())); 0 0 0
mXPUSHs(newSVpvn(i->first.c_str(), i->first.size())); 0 0 0
392 0 ▶ mXPUSHs(newSVnv(i->second));
Branch: 4 branches
BranchTrueFalseTotal
mXPUSHs(newSVnv(i->second)); 0 0 0
mXPUSHs(newSVnv(i->second)); 0 0 0
mXPUSHs(newSVnv(i->second)); 0 0 0
mXPUSHs(newSVnv(i->second)); 0 0 0
393 }
394
395 void
396 NewmanGirvan::DESTROY()
« blib/lib/Graph/NewmanGirvan.pm ↑ Summary