void scn::GetDiameterAndAverageDistance(size_t &Diamter,double &APL,UGraph::pGraph graph) { size_t sum = 0; size_t count = 0; size_t diameter = 0; std::unordered_map<size_t,size_t> distance; for(auto node = graph->begin(); node != graph->end(); node++) { RunSPFA(graph,*node,distance); //add distance except NaF for(auto iter = distance.begin(); iter != distance.end(); iter++) { if(iter->first == *node) continue; if(iter->second < Graph::NaF) { sum += iter->second; count++; if(iter->second > diameter) diameter = iter->second; } } } //return make_pair(diameter, double(sum) / count); Diamter=diameter; APL=double(sum) / count; }
double scn::ComputeAverageDegree(UGraph::pGraph graph) { double sum = 0; for(auto node = graph->begin(); node != graph->end(); node++) { sum += node->GetDegree(); } return sum / graph->GetNumberOfNodes(); }
double scn::GetBetweennessCentrality(UGraph::pGraph graph,size_t indexOfNode) { double sum = 0; for(auto node1 = graph->begin(); node1 != graph->end(); node1++) { if(*node1 == indexOfNode) continue; for(auto node2 = graph->begin(); node2 != graph->end(); node2++) { if(*node2 == indexOfNode || node1 == node2) continue; //compute auto result = GetNumberOfShortestPath(graph,*node1, *node2, indexOfNode); sum += static_cast<double>(result.second) / static_cast<double>(result.first); } } return sum; }
double scn::GetHideInfo(UGraph::pGraph graph,size_t indexOfNode) { double sum = 0; for(auto node = graph->begin(); node != graph->end(); node++) { if(*node == indexOfNode) continue; sum += GetSearchInfo(graph,*node, indexOfNode); } return sum / static_cast<double>(graph->GetNumberOfNodes()); }
size_t scn::GetDiameter(UGraph::pGraph graph) { //auto& distance = distance_sssp; std::unordered_map<size_t,size_t> distance; size_t diameter = 0; distance.clear(); for(auto node = graph->begin(); node != graph->end(); node++) { RunSPFA(graph,*node,distance); //search for the max distance except NaF for(auto other = graph->begin(); other != graph->end(); other++) { if(distance[*other] > diameter && distance[*other] < Graph::NaF) { diameter = distance[*other]; } } } return diameter; }
void scn::RunDjikstra(UGraph::pGraph graph,size_t indexOfSource,std::unordered_map<size_t,size_t> &distance) { //auto& distance = distance_sssp;//using distance_sssp eariler assert(graph->HasNode(indexOfSource)); //init //distance.reserve(graph->GetNumberOfNodes()); for(auto node = graph->begin(); node != graph->end(); node++) { distance[*node] = Graph::NaF; } distance[indexOfSource] = 0; list<size_t> queue; //fill index of nodes into queue for(size_t i = 0; i < graph->GetNumberOfNodes(); i++) { queue.push_back(i); } //begin size_t next_distance; while(!queue.empty()) { //get min one auto min = min_element(queue.begin(), queue.end(), [&](const size_t &one, const size_t &two)->bool { if(distance[one] < distance[two]) return true; else return false; }); auto node = graph->find(*min); if(distance[*node] < Graph::NaF) next_distance = distance[*node] + 1; else next_distance = Graph::NaF; //relax neighbors for(auto other = node->begin(); other != node->end(); other++) { if(distance[*other] > next_distance) { distance[*other] = next_distance; } } queue.erase(min); } }
void scn::GetDegreeDistribution(vector<pair<size_t, size_t>> &pairs,UGraph::pGraph graph) { std::unordered_map<size_t,size_t> distribution; for(auto node = graph->begin(); node != graph->end(); node++) { distribution[node->GetDegree()]++; } //copy and sort pairs.assign(distribution.begin(), distribution.end()); sort(pairs.begin(), pairs.end(), [&](const pair<size_t, size_t> &one, const pair<size_t, size_t> &two)->bool { if(one.first < two.first) return true; else return false; }); }
double scn::GetEntropyOfDegreeDist(UGraph::pGraph graph) { //get degree distribution std::unordered_map<size_t,size_t> distribution; for(auto node = graph->begin(); node != graph->end(); node++) { distribution[node->GetDegree()]++; } //compute the entropy double sum = 0; double pk = 0; for(auto iter = distribution.begin(); iter != distribution.end(); iter++) { pk = static_cast<double>(iter->second) / graph->GetNumberOfNodes(); sum -= pk * log(pk)/log(2.0); } return sum; }
double scn::GetClusteringCoeff(UGraph::pGraph graph,size_t indexOfNode) { if(indexOfNode == UGraph::NaF) {//the whole network double coefficient = 0; for(auto node = graph->begin(); node != graph->end(); node++) { size_t numberOfTriangles = 0; for(auto other1 = node->begin(); other1 != node->end(); other1++) { for(auto other2 = other1 + 1; other2 != node->end(); other2++) { if(graph->HasEdge(*other1, *other2)) numberOfTriangles++; } } if(node->GetDegree()>1) { coefficient += 2 * static_cast<double>(numberOfTriangles) / (node->GetDegree() * (node->GetDegree() - 1)); } } return coefficient / graph->GetNumberOfNodes(); } else {//one vertex auto node = graph->find(indexOfNode); double numberOfTriangles = 0; for(auto other1 = node->begin(); other1 != node->end(); other1++) { for(auto other2 = other1 + 1; other2 != node->end(); other2++) { if(graph->HasEdge(*other1, *other2)) numberOfTriangles++; } } if(node->GetDegree()>1) return 2 * numberOfTriangles / (node->GetDegree() * (node->GetDegree() - 1)); else return 0.0; } }
double scn::GetPearsonCorrCoeff(UGraph::pGraph graph) { size_t sum = 0; size_t product = 0; size_t square_sum = 0; for(auto i = graph->begin(); i != graph->end(); i++) { for(auto j = i + 1; j != graph->end(); j++) { if(graph->HasEdge(*i, *j)) { sum += i->GetDegree() + j->GetDegree(); product += i->GetDegree() * j->GetDegree(); square_sum += i->GetDegree() * i->GetDegree() + j->GetDegree() * j->GetDegree(); } } } double tmp = 1.0 / static_cast<double>(graph->GetNumberOfEdges()); return (tmp * product - (tmp * sum) * (tmp * sum) / 4) / (tmp * square_sum / 2 - (tmp * sum) * (tmp * sum) / 4); }
double scn::GetTransitivity(UGraph::pGraph graph) { size_t numberOfTriangles = 0; size_t numberOfTriples = 0; for(auto node = graph->begin(); node != graph->end(); node++) { for(auto other1 = node->begin(); other1 != node->end(); other1++) { for(auto other2= other1 + 1; other2 != node->end(); other2++) { if(graph->HasEdge(*other1, *other2)) { numberOfTriangles++; } } } numberOfTriples += node->GetDegree() * (node->GetDegree() - 1) / 2; } return static_cast<double>(numberOfTriangles) / static_cast<double>(numberOfTriples); }
double scn::GetGlobalEfficiency(UGraph::pGraph graph) { double sum = 0; std::unordered_map<size_t,size_t> distance; //auto& distance = distance_sssp; distance.clear(); for(auto node = graph->begin(); node != graph->end(); node++) { RunSPFA(graph,*node,distance); //add distance for(auto iter = distance.begin(); iter != distance.end(); iter++) { if(iter->first == *node) continue; sum += 1.0 / static_cast<double>(iter->second); } } double size = static_cast<double>(graph->GetNumberOfNodes()); return size * (size - 1) / sum; }
double scn::GetRichClubCoeff(UGraph::pGraph graph,size_t degree) { IndexList setOfHighNode;//whose degree is greater than //argument degree for(auto node = graph->begin(); node != graph->end(); node++) { if(node->GetDegree() > degree) { setOfHighNode.push_back(*node); } } double sum = 0; for(auto one = setOfHighNode.begin(); one != setOfHighNode.end(); one++) { for(auto two = one + 1; two != setOfHighNode.end(); two++) { if(graph->HasEdge(*one, *two)) sum++; } } return 2 * sum / static_cast<double>(setOfHighNode.size() * (setOfHighNode.size() - 1)); }
void scn::GetShortestDistanceDistribution(vector<pair<size_t, size_t>> &distribution,UGraph::pGraph graph) { std::unordered_map<size_t,size_t> distance; std::unordered_map<size_t,size_t> tempdist; for(auto node = graph->begin(); node != graph->end(); node++) { RunSPFA(graph,*node,distance); //add distance except NaF for(auto iter = distance.begin(); iter !=distance.end(); iter++) { if(iter->first == *node) continue; if(iter->second < Graph::NaF) { tempdist[iter->second]++; } } } distribution.assign(tempdist.begin(),tempdist.end()); sort(distribution.begin(), distribution.end()); }
void scn::GetClusteringDegreeCorre(pair<double, vector<pair<size_t, double>>> &correlation,UGraph::pGraph graph) { unordered_map<size_t, double> degree_dist, degree_cc; //accumulate degree and clustering coefficient for(auto node = graph->begin(); node != graph->end(); node++) { degree_dist[node->GetDegree()]++; degree_cc[node->GetDegree()] += GetClusteringCoeff(graph,*node); } //get average double degree_average = 0;//1-order moment of degree double degree_sqr_average = 0;//2-order moment of degree for(auto iter = degree_dist.begin(); iter != degree_dist.end(); iter++) { //average clustering coefficient in each degree degree_cc[iter->first] /= iter->second; //probability of degree iter->second /= graph->GetNumberOfNodes(); //1-order moment of degree degree_average += iter->first * iter->second; //2-order moment of degree degree_sqr_average += iter->first * iter->first * iter->second; } //compute global clustering degree correlation double clustering_degree_corre = 0; for(auto iter = degree_dist.begin(); iter != degree_dist.end(); iter++) { clustering_degree_corre += iter->first * (iter->first - 1) * iter->second * degree_cc[iter->first]; } clustering_degree_corre /= (degree_sqr_average - degree_average); //sort and get result correlation.second.assign(degree_cc.begin(), degree_cc.end()); sort(correlation.second.begin(), correlation.second.end()); correlation.first=clustering_degree_corre; }