如果有人了解Graphchi并试图理解communitydetection.cpp代码,那么我需要帮助来逐步了解这段代码的工作方式:
for(int i=0; i < vertex.num_edges(); i++) {
bidirectional_label edgelabel = vertex.edge(i)->get_data();
vid_t nblabel = neighbor_label(edgelabel, vertex.id(), vertex.edge(i)->vertex_id());
std::map<vid_t, int>::iterator existing = counts.find(nblabel);
int newcount = 0;
if(existing == counts.end()) {
counts.insert(std::pair<vid_t,int>(nblabel, 1));
newcount = 1;
} else {
existing->second++;//map iterator
newcount = existing->second;
}
if (newcount > maxcount || (maxcount == newcount && nblabel > maxlabel)) {
maxlabel = nblabel;
maxcount = newcount;
}
}
newlabel = maxlabel;
}
最佳答案
公平地说,我在社区检测示例中添加了代码注释:
http://code.google.com/p/graphchi/source/browse/example_apps/communitydetection.cpp
/* The basic idea is to find the label that is most popular among
this vertex's neighbors. This label will be chosen as the new label
of this vertex. */
// This part could be optimized: STL map is quite slow.
std::map<vid_t, int> counts;
int maxcount=0;
vid_t maxlabel=0;
/* Iterate over all the edges */
for(int i=0; i < vertex.num_edges(); i++) {
/* Extract neighbor's current label. The edge contains the labels of
both vertices it connects, so we need to use the right one.
(See comment for bidirectional_label above) */
bidirectional_label edgelabel = vertex.edge(i)->get_data();
vid_t nblabel = neighbor_label(edgelabel, vertex.id(), vertex.edge(i)->vertex_id());
/* Check if this label (nblabel) has been encountered before ... */
std::map<vid_t, int>::iterator existing = counts.find(nblabel);
int newcount = 0;
if(existing == counts.end()) {
/* ... if not, we add this label with count of one to the map */
counts.insert(std::pair<vid_t,int>(nblabel, 1));
newcount = 1;
} else {
/* ... if yes, we increment the counter for this label by 1 */
existing->second++;
newcount = existing->second;
}
/* Finally, we keep track of the most frequent label */
if (newcount > maxcount || (maxcount == newcount && nblabel > maxlabel)) {
maxlabel = nblabel;
maxcount = newcount;
}
}
关于graph - 关于Graphlabs中的Graphchi:社区检测示例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12081773/