Commit e16b8cd3 authored by pmla's avatar pmla

removed deleted files from repository

parent 8f67e27b
Pipeline #24471456 failed with stage
// File: main_louvain.cpp
// -- community detection, sample main file
//-----------------------------------------------------------------------------
// Community detection
// Based on the article "Fast unfolding of community hierarchies in large
// networks"
// Copyright (C) 2008 V. Blondel, J.-L. Guillaume, R. Lambiotte, E. Lefebvre
//
// And based on the article "A Generalized and Adaptive Method for Community
// Detection"
// Copyright (C) 2014 R. Campigotto, P. Conde Céspedes, J.-L. Guillaume
//
// This file is part of Louvain algorithm.
//
// Louvain algorithm is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Louvain algorithm is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Louvain algorithm. If not, see <http://www.gnu.org/licenses/>.
//-----------------------------------------------------------------------------
// Author : E. Lefebvre, adapted by J.-L. Guillaume and R. Campigotto
// Email : jean-loup.guillaume@lip6.fr
// Location : Paris, France
// Time : July 2014
//-----------------------------------------------------------------------------
// see README.txt for more details
#include <random>
#include <limits>
#include "graph_binary.h"
#include "louvain.h"
using namespace std;
int assign_communities(vector<vector<pair<int, double> > > &glinks, double total_weight, vector< int > &n2c)
{
//thread-safe random number generation
mt19937_64 mt_rand(0);
int num_vertices = glinks.size();
Graph g(glinks, total_weight);
Quality *q = new Quality(g);
Louvain c(q);
double quality = (c.qual)->quality();
for (int i=0;i<num_vertices;i++)
n2c[i] = i;
int numc = (c.qual)->g.nb_nodes;
bool improvement = true;
do {
//cerr << "network size: " << (c.qual)->g.nb_nodes << " nodes" << endl;
improvement = c.one_level(mt_rand);
double new_qual = (c.qual)->quality();
vector<int> partition = c.get_partition();
for (int i=0;i<num_vertices;i++)
n2c[i] = partition[n2c[i]];
g = c.partition2graph_binary(total_weight);
delete q;
q = new Quality(g);
c = Louvain(q);
numc = (c.qual)->g.nb_nodes;
//cerr << "quality increased from " << quality << " to " << new_qual << endl << endl;
quality = new_qual;
} while (improvement);
delete q;
return numc;
}
int assign_communities_halfedge(size_t num_vertices, std::vector<size_t>::iterator adjdegrees, size_t num_edges, std::vector<size_t>::iterator adjlist, std::vector<double>::iterator adjweight, double total_weight, vector< int > &n2c)
{
//thread-safe random number generation
mt19937_64 mt_rand(0);
Graph g(num_vertices, adjdegrees, num_edges, adjlist, adjweight, total_weight);
Quality *q = new Quality(g);
Louvain c(q);
double quality = (c.qual)->quality();
for (int i=0;i<num_vertices;i++)
n2c[i] = i;
int numc = (c.qual)->g.nb_nodes;
bool improvement = true;
do {
//cerr << "network size: " << (c.qual)->g.nb_nodes << " nodes" << endl;
improvement = c.one_level(mt_rand);
double new_qual = (c.qual)->quality();
vector<int> partition = c.get_partition();
for (int i=0;i<num_vertices;i++)
n2c[i] = partition[n2c[i]];
g = c.partition2graph_binary(total_weight);
delete q;
q = new Quality(g);
c = Louvain(q);
numc = (c.qual)->g.nb_nodes;
//cerr << "quality increased from " << quality << " to " << new_qual << endl << endl;
quality = new_qual;
} while (improvement);
delete q;
return numc;
}
#ifndef COMMUNITY_H
#define COMMUNITY_H
using namespace std;
int assign_communities(vector<vector<pair<int, double> > > &glinks, 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, vector< int > &n2c);
#endif
// File: graph_binary.cpp
// -- graph handling source
//-----------------------------------------------------------------------------
// Community detection
// Based on the article "Fast unfolding of community hierarchies in large
// networks"
// Copyright (C) 2008 V. Blondel, J.-L. Guillaume, R. Lambiotte, E. Lefebvre
//
// And based on the article "A Generalized and Adaptive Method for Community
// Detection"
// Copyright (C) 2014 R. Campigotto, P. Conde Céspedes, J.-L. Guillaume
//
// This file is part of Louvain algorithm.
//
// Louvain algorithm is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Louvain algorithm is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Louvain algorithm. If not, see <http://www.gnu.org/licenses/>.
//-----------------------------------------------------------------------------
// Author : E. Lefebvre, adapted by J.-L. Guillaume and R. Campigotto
// Email : jean-loup.guillaume@lip6.fr
// Location : Paris, France
// Time : July 2014
//-----------------------------------------------------------------------------
// see README.txt for more details
#include "graph_binary.h"
Graph::Graph(vector<vector<pair<int, double> > > &glinks, double _total_weight)
{
nb_nodes = glinks.size();
total_weight = _total_weight;
for (int i = 0; i < nb_nodes; i++) {
for (unsigned int j = 0; j < glinks[i].size(); j++) {
links.push_back(glinks[i][j].first);
weights.push_back(glinks[i][j].second);
}
}
unsigned long long tot = 0ULL;
for (int i = 0; i < nb_nodes; i++) {
tot += (unsigned long long)glinks[i].size();
degrees.push_back(tot);
}
nodes_w.assign(nb_nodes, 1);
sum_nodes_w = nb_nodes;
}
Graph::Graph(size_t num_vertices, std::vector<size_t>::iterator adjdegrees, size_t num_edges, std::vector<size_t>::iterator adjlist, std::vector<double>::iterator adjweight, double _total_weight)
{
nb_nodes = num_vertices;
total_weight = _total_weight;
for (int i = 0; i < num_edges; i++) {
links.push_back(adjlist[i]);
weights.push_back(adjweight[i]);
}
unsigned long long tot = 0ULL;
for (int i = 0; i < nb_nodes; i++) {
tot += (unsigned long long)adjdegrees[i];
degrees.push_back(tot);
}
nodes_w.assign(nb_nodes, 1);
sum_nodes_w = nb_nodes;
}
// File: graph_binary.h
// -- graph handling header file
//-----------------------------------------------------------------------------
// Community detection
// Based on the article "Fast unfolding of community hierarchies in large
// networks"
// Copyright (C) 2008 V. Blondel, J.-L. Guillaume, R. Lambiotte, E. Lefebvre
//
// And based on the article "A Generalized and Adaptive Method for Community
// Detection"
// Copyright (C) 2014 R. Campigotto, P. Conde Céspedes, J.-L. Guillaume
//
// This file is part of Louvain algorithm.
//
// Louvain algorithm is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Louvain algorithm is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Louvain algorithm. If not, see <http://www.gnu.org/licenses/>.
//-----------------------------------------------------------------------------
// Author : E. Lefebvre, adapted by J.-L. Guillaume and R. Campigotto
// Email : jean-loup.guillaume@lip6.fr
// Location : Paris, France
// Time : July 2014
//-----------------------------------------------------------------------------
// see README.txt for more details
#ifndef GRAPH_H
#define GRAPH_H
#include <algorithm>
#include <cassert>
#include <map>
#include <vector>
using namespace std;
class Graph {
public:
int nb_nodes;
double total_weight;
int sum_nodes_w;
vector<unsigned long long> degrees;
vector<int> links;
vector<double> weights;
vector<int> nodes_w;
Graph(int nbc, double _total_weight) {
total_weight = _total_weight;
sum_nodes_w = 0;
nb_nodes = nbc;
degrees.resize(nbc);
nodes_w.resize(nbc);
};
Graph(size_t num_vertices, std::vector<size_t>::iterator adjdegrees, size_t num_edges, std::vector<size_t>::iterator adjlist, std::vector<double>::iterator adjweight, double _total_weight)
{
nb_nodes = num_vertices;
total_weight = _total_weight;
for (int i = 0; i < num_edges; i++) {
links.push_back(adjlist[i]);
weights.push_back(adjweight[i]);
}
unsigned long long tot = 0ULL;
for (int i = 0; i < nb_nodes; i++) {
tot += (unsigned long long)adjdegrees[i];
degrees.push_back(tot);
}
nodes_w.assign(nb_nodes, 1);
sum_nodes_w = nb_nodes;
}
// assign a weight to a node (needed after the first level)
void assign_weight(int node, int weight) {
sum_nodes_w -= nodes_w[node];
nodes_w[node] = weight;
sum_nodes_w += weight;
}
int nb_neighbors(int node) {
assert(node >= 0 && node < nb_nodes);
if (node == 0)
return degrees[0];
else
return (int)(degrees[node] - degrees[node - 1]);
}
double nb_selfloops(int node) {
assert(node >= 0 && node < nb_nodes);
pair<vector<int>::iterator, vector<double>::iterator> p = neighbors(node);
for (int i = 0; i < nb_neighbors(node); i++)
if (*(p.first + i) == node)
return (double)*(p.second + i);
return 0.0L;
}
double weighted_degree(int node) {
assert(node >= 0 && node < nb_nodes);
pair<vector<int>::iterator, vector<double>::iterator> p = neighbors(node);
double res = 0.0L;
for (int i = 0; i < nb_neighbors(node); i++) {
res += (double)*(p.second + i);
}
return res;
}
pair<vector<int>::iterator, vector<double>::iterator> neighbors(int node) {
assert(node >= 0 && node < nb_nodes);
if (node == 0)
return make_pair(links.begin(), weights.begin());
else
return make_pair(links.begin() + degrees[node - 1], weights.begin() + degrees[node - 1]);
}
};
#endif // GRAPH_H
// File: quality.h
// -- quality functions header file
//-----------------------------------------------------------------------------
// Community detection
// Based on the article "Fast unfolding of community hierarchies in large networks"
// Copyright (C) 2008 V. Blondel, J.-L. Guillaume, R. Lambiotte, E. Lefebvre
//
// And based on the article "A Generalized and Adaptive Method for Community Detection"
// Copyright (C) 2014 R. Campigotto, P. Conde Céspedes, J.-L. Guillaume
//
// This file is part of Louvain algorithm.
//
// Louvain algorithm is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Louvain algorithm is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Louvain algorithm. If not, see <http://www.gnu.org/licenses/>.
//-----------------------------------------------------------------------------
// Author : E. Lefebvre, adapted by J.-L. Guillaume and R. Campigotto
// Email : jean-loup.guillaume@lip6.fr
// Location : Paris, France
// Time : July 2014
//-----------------------------------------------------------------------------
// see README.txt for more details
#ifndef QUALITY_H
#define QUALITY_H
#include "graph_binary.h"
using namespace std;
class Quality {
public:
Graph& g; // network to compute communities for
int size; // nummber of nodes in the network and size of all vectors
vector<int> n2c; // community to which each node belongs
vector<double> in;
vector<double> tot;
Quality(Graph& gr) : g(gr),size(gr.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);
}
};
~Quality() {
n2c.clear();
in.clear();
tot.clear();
}
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;
}
};
#endif // QUALITY_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