Commit 8f67e27b authored by pmla's avatar pmla

more refactoring

parent 42fd879a
Pipeline #24471435 canceled with stage
......@@ -34,121 +34,8 @@
#define LOUVAIN_H
#include <vector>
#include "graph_binary.h"
//#include "quality.h"
using namespace std;
class Louvain {
public:
vector<double> neigh_weight;
vector<int> neigh_pos;
int neigh_last;
// number of pass for one level computation
// if -1, compute as many pass as needed to increase quality
int nb_pass;
// a new pass is computed if the last one has generated an increase
// better than eps_impr
// if 0.0L even a minor increase is enough to go for one more pass
double eps_impr;
// Quality functions used to compute communities
//Quality* qual;
Graph* g;
// constructors:
// reads graph from file using graph constructor
// type defined the weighted/unweighted status of the graph file
Louvain(Graph* _g) {
g = _g;
size = g->nb_nodes;
n2c.resize(size);
in.resize(size);
tot.resize(size);
for (int i=0 ; i<size ; i++) {
n2c[i] = i;
in[i] = g->nb_selfloops(i);
tot[i] = g->weighted_degree(i);
}
neigh_weight.resize(size, -1);
neigh_pos.resize(size);
neigh_last = 0;
nb_pass = -1;
eps_impr = 1E-6;
}
// compute the set of neighboring communities of node
// for each community, gives the number of links from node to comm
void neigh_comm(int node);
vector< int> get_partition();
// generates the binary graph of communities as computed by one_level
Graph partition2graph_binary(double total_weight);
// compute communities of the graph for one level
// return true if some nodes have been moved
bool one_level(mt19937_64& mt_rand, std::vector< int > &prev_n2c);
vector<int> n2c; // community to which each node belongs
vector<double> in;
vector<double> tot;
size_t size;
void remove(int node, int comm, double dnodecomm) {
assert(node >= 0 && node < size);
in[comm] -= 2.0L*dnodecomm + g->nb_selfloops(node);
tot[comm] -= g->weighted_degree(node);
n2c[node] = -1;
}
void insert(int node, int comm, double dnodecomm) {
assert(node>=0 && node<size);
in[comm] += 2.0L*dnodecomm + g->nb_selfloops(node);
tot[comm] += g->weighted_degree(node);
n2c[node] = comm;
}
double gain(int node, int comm, double dnc, double degc) {
assert(node>=0 && node<size);
double totc = tot[comm];
double m2 = g->total_weight;
return (dnc - totc * degc / m2);
}
double quality() {
double q = 0.0L;
double m2 = g->total_weight;
for (int i=0;i<size;i++)
if (tot[i] > 0.0L)
q += in[i] - (tot[i] * tot[i]) / m2;
q /= m2;
return q;
}
};
int assign_communities_halfedge(size_t numVertices, std::vector<size_t>::iterator adjdegrees, size_t numEdges, std::vector<size_t>::iterator adjlist, std::vector<double>::iterator adjweight, double total_weight, vector< int > &n2c);
int assign_communities_halfedge(size_t numVertices, std::vector<size_t>::iterator adjdegrees, size_t numEdges, std::vector<size_t>::iterator adjlist, std::vector<double>::iterator adjweight, double total_weight, std::vector< int > &n2c);
#endif // LOUVAIN_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment