Skip to content

Commit

Permalink
Louvain Implementation in-progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Cry1is committed Apr 12, 2022
1 parent f8596ad commit 4f13bc1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
52 changes: 52 additions & 0 deletions src/Graph_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,58 @@ void Graph_helper::girvan_newman_helper() {
}
}

void Graph_helper::louvain() {
// keeps track of total initial edges and nodes for later
numNodes = (int)boost::num_vertices(graph);
numEdges = (int)boost::num_edges(graph);

Graph temp = graph;
boost::remove_edges(graph);

while(true) {
//put each node of G in its own community.
num_communities = boost::connected_components(graph, boost::make_assoc_property_map(max_comp));

louvain_helper(temp);
if (curr_mod > best_mod) {
best_mod = curr_mod;
}
else
break;
}
}

void Graph_helper::louvain_helper(Graph& temp) {
bool nodesMoved = false;
do {
auto vpair = boost::vertices(temp);
for(vertexIt iter = vpair.first; iter != vpair.second; iter++) {
if (join_nodes(temp, iter))
nodesMoved = true;
}
} while(nodesMoved);
}

bool Graph_helper::join_nodes(Graph& temp, vertexIt iter) {
bool result = false;
auto neighbors = boost::adjacent_vertices(iter, temp);
ed* e = nullptr;
for (auto it = neighbors.first; it != neighbors.second; it++) {
boost::add_edge(it, iter, graph);
double mod = get_modularity();
if (mod > curr_mod) {
curr_mod = mod;
if (e)
boost::remove_edge(e->m_source, e->m_target, graph);
e = &boost::edge(it, iter, graph);
result = true;
}
else
boost::remove_edge(it, iter, graph);
}
return result;
}

double Graph_helper::get_modularity () {
set_degree(); // to set current degrees for each vertex

Expand Down
21 changes: 20 additions & 1 deletion src/Graph_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <cxxabi.h>
#include <fstream>
#include <queue>
#include <float.h>



Expand All @@ -42,6 +43,7 @@ struct component {
typedef boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS, VertexProperty, EdgeProperty> Graph;
typedef boost::range_detail::integer_iterator<unsigned long> vertexIt;
typedef boost::graph_traits<Graph>::vertex_descriptor vd;
typedef boost::graph_traits<Graph>::edge_descriptor ed;


class Graph_helper {
Expand All @@ -50,7 +52,7 @@ class Graph_helper {
int numNodes;
int numEdges;
std::map<vd, int> max_comp;
double best_mod;
double best_mod, curr_mod;
int num_communities;

public:
Expand Down Expand Up @@ -90,6 +92,23 @@ class Graph_helper {
*/
void girvan_newman_helper();

/**
* The master function for the Louvain community detection algorithm. Uses change in
* modularity to determine which community each node belongs to.
*/
void louvain();

/**
* Loop for Louvain algorithm. Joins communities together based on modularity, leading to new
* partitions.
*/
void louvain_helper(Graph&);

/**
* Join communities that create a higher modularity.
*/
bool join_nodes(Graph&, vertexIt);

/**
* Implementation of Breadth First Search in order to visit the graph by visiting each adjacent node
* before moving further. Keeps track of each previously visited node for every node.
Expand Down

0 comments on commit 4f13bc1

Please sign in to comment.