void RunCombo(Graph& G, int max_comunities) { G.CalcModMtrix(); G.SetCommunities(vector<int>(G.Size(), 0)); double currentMod = G.Modularity(); //printf("Initial modularity: %6f\n", currentMod); vector< vector<double> > moves(2, vector<double>(2, 0)); //results of splitting communities //vectors of boolean meaning that corresponding vertex should be moved to dest vector< vector<int> > splits_communities(2, vector<int>(G.Size(), 0)); //best split vectors int origin, dest; for(origin = 0; origin < G.CommunityNumber(); ++ origin) for(dest = 0; dest < G.CommunityNumber() + (G.CommunityNumber() < max_comunities); ++dest) reCalc(G, moves, splits_communities, origin, dest); best_gain = BestGain(moves, origin, dest); while(best_gain > THRESHOLD) { bool comunityAdded = dest >= G.CommunityNumber(); G.PerformSplit(origin, dest, splits_communities[dest]); if(debug_verify) { double oldMod = currentMod; currentMod = G.Modularity(); if(fabs(currentMod - oldMod - best_gain) > THRESHOLD) printf("ERROR\n"); } if(comunityAdded && dest < max_comunities - 1) { if(dest >= moves.size() - 1) { for(int i = 0; i < moves.size(); ++i) moves[i].push_back(0); moves.push_back(vector<double>(moves.size() + 1, 0)); splits_communities.push_back(vector<int>(G.Size(), 0)); } for(int i = 0; i < dest; ++i) { moves[i][dest+1] = moves[i][dest]; splits_communities[dest+1] = splits_communities[dest]; } } for(int i = 0; i < G.CommunityNumber() + (G.CommunityNumber() < max_comunities); ++i) { reCalc(G, moves, splits_communities, origin, i); reCalc(G, moves, splits_communities, dest, i); if(i != dest && i < G.CommunityNumber()) reCalc(G, moves, splits_communities, i, origin); if(i != origin && i < G.CommunityNumber()) reCalc(G, moves, splits_communities, i, dest); } DeleteEmptyCommunities(G, moves, splits_communities, origin); //remove origin community if empty best_gain = BestGain(moves, origin, dest); } }
void DeleteEmptyCommunities(Graph& G, vector< vector<double> >& moves, vector< vector<int> >& splits_communities, int origin) { if(G.DeleteCommunityIfEmpty(origin)) { int commNumber = G.CommunityNumber(); for(int i = origin; i < commNumber; ++i) moves[i] = moves[i+1]; moves[commNumber].assign(commNumber+2, 0); for(int i = 0; i < moves.size(); ++i) { for(int j = origin; j < commNumber+1; ++j) moves[i][j] = moves[i][j+1]; moves[i][commNumber+1] = 0; } for(int i = origin; i < commNumber+1; ++i) splits_communities[i] = splits_communities[i+1]; } }