Remove louvain and connectivity modules (#160)

This commit is contained in:
Josip Seljan 2021-06-02 11:10:16 +02:00 committed by GitHub
parent 560eb04f67
commit 839d45b3f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 0 additions and 1358 deletions

View File

@ -35,8 +35,3 @@ install(FILES graph_analyzer.py DESTINATION lib/memgraph/query_modules)
install(FILES mgp_networkx.py DESTINATION lib/memgraph/query_modules)
install(FILES nxalg.py DESTINATION lib/memgraph/query_modules)
install(FILES wcc.py DESTINATION lib/memgraph/query_modules)
if (MG_ENTERPRISE)
add_subdirectory(louvain)
add_subdirectory(connectivity)
endif()

View File

@ -1,18 +0,0 @@
set(MODULE src/connectivity_module.cpp)
include_directories(src)
add_library(connectivity SHARED ${MODULE})
target_include_directories(connectivity PRIVATE ${CMAKE_SOURCE_DIR}/include)
# Strip the library in release build.
string(TOLOWER ${CMAKE_BUILD_TYPE} lower_build_type)
if (lower_build_type STREQUAL "release")
add_custom_command(TARGET connectivity POST_BUILD
COMMAND strip -s $<TARGET_FILE:connectivity>
COMMENT "Stripping symbols and sections from connectivity module")
endif()
install(PROGRAMS $<TARGET_FILE:connectivity>
DESTINATION lib/memgraph/query_modules
RENAME connectivity.so)

View File

@ -1,131 +0,0 @@
#include "mg_procedure.h"
#include <queue>
#include <unordered_map>
// Finds weakly connected components of a graph.
// Time complexity: O(|V|+|E|)
static void weak(const mgp_list *args, const mgp_graph *graph,
mgp_result *result, mgp_memory *memory) {
std::unordered_map<int64_t, int64_t> vertex_component;
mgp_vertices_iterator *vertices_iterator =
mgp_graph_iter_vertices(graph, memory);
if (vertices_iterator == nullptr) {
mgp_result_set_error_msg(result, "Not enough memory");
return;
}
int64_t curr_component = 0;
for (const mgp_vertex *vertex = mgp_vertices_iterator_get(vertices_iterator);
vertex != nullptr;
vertex = mgp_vertices_iterator_next(vertices_iterator)) {
mgp_vertex_id vertex_id = mgp_vertex_get_id(vertex);
if (vertex_component.find(vertex_id.as_int) != vertex_component.end())
continue;
// run bfs from current vertex
std::queue<int64_t> q;
q.push(vertex_id.as_int);
vertex_component[vertex_id.as_int] = curr_component;
while (!q.empty()) {
mgp_vertex *v = mgp_graph_get_vertex_by_id(graph, {q.front()}, memory);
if (v == nullptr) {
mgp_vertices_iterator_destroy(vertices_iterator);
mgp_result_set_error_msg(result, "Not enough memory");
return;
}
q.pop();
// iterate over inbound edges
mgp_edges_iterator *edges_iterator = mgp_vertex_iter_in_edges(v, memory);
if (edges_iterator == nullptr) {
mgp_vertex_destroy(v);
mgp_vertices_iterator_destroy(vertices_iterator);
mgp_result_set_error_msg(result, "Not enough memory");
return;
}
for (const mgp_edge *edge = mgp_edges_iterator_get(edges_iterator);
edge != nullptr; edge = mgp_edges_iterator_next(edges_iterator)) {
mgp_vertex_id next_id = mgp_vertex_get_id(mgp_edge_get_from(edge));
if (vertex_component.find(next_id.as_int) != vertex_component.end())
continue;
vertex_component[next_id.as_int] = curr_component;
q.push(next_id.as_int);
}
// iterate over outbound edges
mgp_edges_iterator_destroy(edges_iterator);
edges_iterator = mgp_vertex_iter_out_edges(v, memory);
if (edges_iterator == nullptr) {
mgp_vertex_destroy(v);
mgp_vertices_iterator_destroy(vertices_iterator);
mgp_result_set_error_msg(result, "Not enough memory");
return;
}
for (const mgp_edge *edge = mgp_edges_iterator_get(edges_iterator);
edge != nullptr; edge = mgp_edges_iterator_next(edges_iterator)) {
mgp_vertex_id next_id = mgp_vertex_get_id(mgp_edge_get_to(edge));
if (vertex_component.find(next_id.as_int) != vertex_component.end())
continue;
vertex_component[next_id.as_int] = curr_component;
q.push(next_id.as_int);
}
mgp_vertex_destroy(v);
mgp_edges_iterator_destroy(edges_iterator);
}
++curr_component;
}
mgp_vertices_iterator_destroy(vertices_iterator);
for (const auto &p : vertex_component) {
mgp_result_record *record = mgp_result_new_record(result);
if (record == nullptr) {
mgp_result_set_error_msg(result, "Not enough memory");
return;
}
mgp_value *mem_id_value = mgp_value_make_int(p.first, memory);
if (mem_id_value == nullptr) {
mgp_result_set_error_msg(result, "Not enough memory");
return;
}
mgp_value *comp_value = mgp_value_make_int(p.second, memory);
if (comp_value == nullptr) {
mgp_value_destroy(mem_id_value);
mgp_result_set_error_msg(result, "Not enough memory");
return;
}
int mem_id_inserted = mgp_result_record_insert(record, "id", mem_id_value);
int comp_inserted =
mgp_result_record_insert(record, "component", comp_value);
mgp_value_destroy(mem_id_value);
mgp_value_destroy(comp_value);
if (!mem_id_inserted || !comp_inserted) {
mgp_result_set_error_msg(result, "Not enough memory");
return;
}
}
}
extern "C" int mgp_init_module(struct mgp_module *module,
struct mgp_memory *memory) {
struct mgp_proc *wcc_proc =
mgp_module_add_read_procedure(module, "weak", weak);
if (!mgp_proc_add_result(wcc_proc, "id", mgp_type_int())) return 1;
if (!mgp_proc_add_result(wcc_proc, "component", mgp_type_int())) return 1;
return 0;
}
extern "C" int mgp_shutdown_module() {
return 0;
}

View File

@ -1,33 +0,0 @@
set(MAIN src/main.cpp)
set(MODULE src/louvain_module.cpp)
set(SOURCES src/algorithms/louvain.cpp
src/data_structures/graph.cpp)
include_directories(src)
add_library(louvain-core STATIC ${SOURCES})
set_target_properties(louvain-core PROPERTIES POSITION_INDEPENDENT_CODE ON)
add_executable(louvain-main ${MAIN})
target_link_libraries(louvain-main louvain-core)
enable_testing()
add_subdirectory(test)
add_library(louvain SHARED ${MODULE})
target_link_libraries(louvain louvain-core)
target_include_directories(louvain PRIVATE ${CMAKE_SOURCE_DIR}/include)
# Strip the library in release build.
string(TOLOWER ${CMAKE_BUILD_TYPE} lower_build_type)
if (lower_build_type STREQUAL "release")
add_custom_command(TARGET louvain POST_BUILD
COMMAND strip -s $<TARGET_FILE:louvain>
COMMENT "Stripping symbols and sections from louvain module")
endif()
if (NOT MG_COMMUNITY)
install(PROGRAMS $<TARGET_FILE:louvain>
DESTINATION lib/memgraph/query_modules
RENAME louvain.so)
endif()

View File

@ -1,18 +0,0 @@
/// @file
///
/// The file contains function declarations of several community-detection
/// graph algorithms.
#pragma once
#include "data_structures/graph.hpp"
namespace algorithms {
/// Detects communities of an unidrected, weighted graph using the Louvain
/// algorithm. The algorithm attempts to maximze the modularity of a weighted
/// graph.
///
/// @param graph pointer to an undirected, weighted graph which may contain
/// self-loops.
void Louvain(comdata::Graph *graph);
} // namespace algorithms

View File

@ -1,163 +0,0 @@
#include "algorithms/algorithms.hpp"
#include <algorithm>
#include <map>
#include <random>
#include <unordered_map>
namespace {
void OptimizeLocally(comdata::Graph *graph) {
// We will consider local optimizations uniformly at random.
std::random_device rd;
std::mt19937 g(rd());
std::vector<uint32_t> p(graph->Size());
std::iota(p.begin(), p.end(), 0);
std::shuffle(p.begin(), p.end(), g);
// Modularity of a graph can be expressed as:
//
// Q = 1 / (2m) * sum_over_pairs_of_nodes[(Aij - ki * kj / 2m) * delta(ci, cj)]
//
// where m is the sum of all weights in the graph,
// Aij is the weight on edge that connects i and j (i=j for a self-loop)
// ki is the sum of weights incident to node i
// ci is the community of node i
// delta(a, b) is the Kronecker delta function.
//
// With some simple algebraic manipulations, we can transform the formula into:
//
// Q = sum_over_components[M * ((sum_over_pairs(Aij + M * ki * kj)))] =
// = sum_over_components[M * (sum_over_pairs(Aij) + M * sum_over_nodes^2(ki))] =
// = sum_over_components[M * (w_contrib(ci) + M * k_contrib^2(ci))]
//
// where M = 1 / (2m)
//
// Therefore, we could store for each community the following:
// * Weight contribution (w_contrib)
// * Weighted degree contribution (k_contrib)
//
// This allows us to efficiently remove a node from one community and insert
// it into a community of its neighbour without the need to recalculate
// modularity from scratch.
std::unordered_map<uint32_t, double> w_contrib;
std::unordered_map<uint32_t, double> k_contrib;
for (uint32_t node_id = 0; node_id < graph->Size(); ++node_id) {
k_contrib[graph->Community(node_id)] += graph->IncidentWeight(node_id);
for (const auto &neigh : graph->Neighbours(node_id)) {
uint32_t nxt_id = neigh.dest;
double w = neigh.weight;
if (graph->Community(node_id) == graph->Community(nxt_id))
w_contrib[graph->Community(node_id)] += w;
}
}
bool stable = false;
double total_w = graph->TotalWeight();
while (!stable) {
stable = true;
for (uint32_t node_id : p) {
std::unordered_map<uint32_t, double> sum_w;
double self_loop = 0;
sum_w[graph->Community(node_id)] = 0;
for (const auto &neigh : graph->Neighbours(node_id)) {
uint32_t nxt_id = neigh.dest;
double weight = neigh.weight;
if (nxt_id == node_id) {
self_loop += weight;
continue;
}
sum_w[graph->Community(nxt_id)] += weight;
}
uint32_t my_c = graph->Community(node_id);
uint32_t best_c = my_c;
double best_dq = 0;
for (const auto &p : sum_w) {
if (p.first == my_c) continue;
uint32_t nxt_c = p.first;
double dq = 0;
// contributions before swap (dq = d_after - d_before)
for (uint32_t c : {my_c, nxt_c})
dq -= w_contrib[c] - k_contrib[c] * k_contrib[c] / (2.0 * total_w);
// leave the current community
dq += (w_contrib[my_c] - 2.0 * sum_w[my_c] - self_loop) -
(k_contrib[my_c] - graph->IncidentWeight(node_id)) *
(k_contrib[my_c] - graph->IncidentWeight(node_id)) /
(2.0 * total_w);
// join a new community
dq += (w_contrib[nxt_c] + 2.0 * sum_w[nxt_c] + self_loop) -
(k_contrib[nxt_c] + graph->IncidentWeight(node_id)) *
(k_contrib[nxt_c] + graph->IncidentWeight(node_id)) /
(2.0 * total_w);
if (dq > best_dq) {
best_dq = dq;
best_c = nxt_c;
}
}
if (best_c != my_c) {
graph->SetCommunity(node_id, best_c);
w_contrib[my_c] -= 2.0 * sum_w[my_c] + self_loop;
k_contrib[my_c] -= graph->IncidentWeight(node_id);
w_contrib[best_c] += 2.0 * sum_w[best_c] + self_loop;
k_contrib[best_c] += graph->IncidentWeight(node_id);
stable = false;
}
}
}
}
} // anonymous namespace
namespace algorithms {
void Louvain(comdata::Graph *graph) {
OptimizeLocally(graph);
// Collapse the locally optimized graph.
uint32_t collapsed_nodes = graph->NormalizeCommunities();
if (collapsed_nodes == graph->Size()) return;
comdata::Graph collapsed_graph(collapsed_nodes);
std::map<std::pair<uint32_t, uint32_t>, double> collapsed_edges;
for (uint32_t node_id = 0; node_id < graph->Size(); ++node_id) {
std::unordered_map<uint32_t, double> edges;
for (const auto &neigh : graph->Neighbours(node_id)) {
uint32_t nxt_id = neigh.dest;
double weight = neigh.weight;
if (graph->Community(nxt_id) < graph->Community(node_id)) continue;
edges[graph->Community(nxt_id)] += weight;
}
for (const auto &neigh : edges) {
uint32_t a = std::min(graph->Community(node_id), neigh.first);
uint32_t b = std::max(graph->Community(node_id), neigh.first);
collapsed_edges[{a, b}] += neigh.second;
}
}
for (const auto &p : collapsed_edges)
collapsed_graph.AddEdge(p.first.first, p.first.second, p.second);
// Repeat until no local optimizations can be found.
Louvain(&collapsed_graph);
// Propagate results from collapsed graph.
for (uint32_t node_id = 0; node_id < graph->Size(); ++node_id) {
graph->SetCommunity(node_id,
collapsed_graph.Community(graph->Community(node_id)));
}
graph->NormalizeCommunities();
}
} // namespace algorithms

View File

@ -1,99 +0,0 @@
#include "data_structures/graph.hpp"
#include <exception>
#include <numeric>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace comdata {
Graph::Graph(uint32_t n_nodes) : n_nodes_(n_nodes), total_w_(0) {
adj_list_.resize(n_nodes, {});
inc_w_.resize(n_nodes, 0);
// each node starts as its own separate community.
community_.resize(n_nodes);
std::iota(community_.begin(), community_.end(), 0);
}
uint32_t Graph::Size() const { return n_nodes_; }
uint32_t Graph::Community(uint32_t node) const { return community_.at(node); }
void Graph::SetCommunity(uint32_t node, uint32_t c) { community_.at(node) = c; }
uint32_t Graph::NormalizeCommunities() {
std::set<uint32_t> c_id(community_.begin(), community_.end());
std::unordered_map<uint32_t, uint32_t> cmap;
uint32_t id = 0;
for (uint32_t c : c_id) {
cmap[c] = id;
++id;
}
for (uint32_t node_id = 0; node_id < n_nodes_; ++node_id)
community_[node_id] = cmap[community_[node_id]];
return id;
}
void Graph::AddEdge(uint32_t node1, uint32_t node2, double weight) {
if (node1 >= n_nodes_ || node2 >= n_nodes_)
throw std::out_of_range("Node index out of range");
if (weight <= 0) throw std::out_of_range("Weights must be positive");
if (edges_.find({node1, node2}) != edges_.end())
throw std::invalid_argument("Edge already exists");
edges_.emplace(node1, node2);
edges_.emplace(node2, node1);
total_w_ += weight;
adj_list_[node1].emplace_back(node2, weight);
inc_w_[node1] += weight;
if (node1 != node2) {
adj_list_[node2].emplace_back(node1, weight);
inc_w_[node2] += weight;
}
}
uint32_t Graph::Degree(uint32_t node) const {
return static_cast<uint32_t>(adj_list_.at(node).size());
}
double Graph::IncidentWeight(uint32_t node) const { return inc_w_.at(node); }
double Graph::TotalWeight() const { return total_w_; }
double Graph::Modularity() const {
double ret = 0;
// Since all weights should be positive, this implies that our graph has
// no edges.
if (total_w_ == 0) return 0;
std::unordered_map<uint32_t, double> weight_c;
std::unordered_map<uint32_t, double> degree_c;
for (uint32_t i = 0; i < n_nodes_; ++i) {
degree_c[Community(i)] += IncidentWeight(i);
for (const auto &neigh : adj_list_[i]) {
uint32_t j = neigh.dest;
double w = neigh.weight;
if (Community(i) != Community(j)) continue;
weight_c[Community(i)] += w;
}
}
for (const auto &p : degree_c)
ret += weight_c[p.first] - (p.second * p.second) / (2 * total_w_);
ret /= 2 * total_w_;
return ret;
}
const std::vector<Neighbour> &Graph::Neighbours(uint32_t node) const {
return adj_list_.at(node);
}
} // namespace comdata

View File

@ -1,125 +0,0 @@
/// @file
#pragma once
#include <cstdint>
#include <set>
#include <vector>
namespace comdata {
struct Neighbour {
uint32_t dest;
double weight;
Neighbour(uint32_t d, double w) : dest(d), weight(w) {}
};
/// Class which models a weighted, undirected graph with necessary
/// functionalities for community detection algorithms.
class Graph {
public:
/// Constructs a new graph with a given number of nodes and no edges between
/// them.
///
/// The implementation assumes (and enforces) that all nodes
/// are indexed from 0 to n_nodes.
///
/// @param n_nodes Number of nodes in the graph.
explicit Graph(uint32_t n_nodes);
/// @return number of nodes in the graph.
uint32_t Size() const;
/// Adds a bidirectional, weighted edge to the graph between the given
/// nodes. If both given nodes are the same, the method inserts a weighted
/// self-loop.
///
/// There should be no edges between the given nodes when before invoking
/// this method.
///
/// @param node1 index of an incident node.
/// @param node2 index of an incident node.
/// @param weight real value which represents the weight of the edge.
///
/// @throw std::out_of_range
/// @throw std::invalid_argument
void AddEdge(uint32_t node1, uint32_t node2, double weight);
/// @param node index of node.
///
/// @return community where the node belongs to.
///
/// @throw std::out_of_range
uint32_t Community(uint32_t node) const;
/// Adds a given node to a given community.
///
/// @param node index of node.
/// @param c community where the given node should go in.
///
/// @throw std::out_of_range
void SetCommunity(uint32_t node, uint32_t c);
/// Normalizes the values of communities. More precisely, after invoking this
/// method communities will be indexed by successive integers starting from 0.
///
/// Note: this method is computationally expensive and takes O(|V|)
/// time, i.e., it traverses all nodes in the graph.
///
/// @return number of communities in the graph
uint32_t NormalizeCommunities();
/// Returns the number of incident edges to a given node. Self-loops
/// contribute a single edge to the degree.
///
/// @param node index of node.
///
/// @return degree of given node.
///
/// @throw std::out_of_range
uint32_t Degree(uint32_t node) const;
/// Returns the total weight of incident edges to a given node. Weight
/// of a self loop contributes once to the total sum.
///
/// @param node index of node.
///
/// @return total incident weight of a given node.
///
/// @throw std::out_of_range
double IncidentWeight(uint32_t node) const;
/// @return total weight of all edges in a graph.
double TotalWeight() const;
/// Calculates the modularity of the graph which is defined as a real value
/// between -1 and 1 that measures the density of links inside communities
/// compared to links between communities.
///
/// Note: this method is computationally expensive and takes O(|V| + |E|)
/// time, i.e., it traverses the entire graph.
///
/// @return modularity of the graph.
double Modularity() const;
/// Returns nodes adjacent to a given node.
///
/// @param node index of node.
///
/// @return list of neighbouring nodes.
///
/// @throw std::out_of_range
const std::vector<Neighbour>& Neighbours(uint32_t node) const;
private:
uint32_t n_nodes_;
double total_w_;
std::vector<std::vector<Neighbour>> adj_list_;
std::set<std::pair<uint32_t, uint32_t>> edges_;
std::vector<double> inc_w_;
std::vector<uint32_t> community_;
};
} // namespace comdata

View File

@ -1,228 +0,0 @@
#include "mg_procedure.h"
#include <exception>
#include <string>
#include <unordered_map>
#include "algorithms/algorithms.hpp"
#include "data_structures/graph.hpp"
namespace {
std::optional<std::unordered_map<int64_t, uint32_t>> NormalizeVertexIds(
const mgp_graph *graph, mgp_result *result, mgp_memory *memory) {
std::unordered_map<int64_t, uint32_t> mem_to_louv_id;
mgp_vertices_iterator *vertices_iterator =
mgp_graph_iter_vertices(graph, memory);
if (vertices_iterator == nullptr) {
mgp_result_set_error_msg(result, "Not enough memory!");
return std::nullopt;
}
uint32_t louv_id = 0;
for (const mgp_vertex *vertex = mgp_vertices_iterator_get(vertices_iterator);
vertex != nullptr;
vertex = mgp_vertices_iterator_next(vertices_iterator)) {
mgp_vertex_id mem_id = mgp_vertex_get_id(vertex);
mem_to_louv_id[mem_id.as_int] = louv_id;
++louv_id;
}
mgp_vertices_iterator_destroy(vertices_iterator);
return mem_to_louv_id;
}
std::optional<comdata::Graph> RunLouvain(
const mgp_graph *graph, mgp_result *result, mgp_memory *memory,
const std::unordered_map<int64_t, uint32_t> &mem_to_louv_id) {
comdata::Graph louvain_graph(mem_to_louv_id.size());
// Extract the graph structure
// TODO(ipaljak): consider filtering nodes and edges by labels.
for (const auto &p : mem_to_louv_id) {
mgp_vertex *vertex = mgp_graph_get_vertex_by_id(graph, {p.first}, memory);
if (!vertex) {
mgp_result_set_error_msg(result, "Not enough memory!");
return std::nullopt;
}
// iterate over inbound edges. This is enough because we will eventually
// iterate over outbound edges in another direction.
mgp_edges_iterator *edges_iterator =
mgp_vertex_iter_in_edges(vertex, memory);
if (edges_iterator == nullptr) {
mgp_vertex_destroy(vertex);
mgp_result_set_error_msg(result, "Not enough memory!");
return std::nullopt;
}
for (const mgp_edge *edge = mgp_edges_iterator_get(edges_iterator);
edge != nullptr; edge = mgp_edges_iterator_next(edges_iterator)) {
const mgp_vertex *next_vertex = mgp_edge_get_from(edge);
mgp_vertex_id next_mem_id = mgp_vertex_get_id(next_vertex);
uint32_t next_louv_id;
try {
next_louv_id = mem_to_louv_id.at(next_mem_id.as_int);
} catch (const std::exception &e) {
const auto msg = std::string("[Internal error] ") + e.what();
mgp_result_set_error_msg(result, msg.c_str());
return std::nullopt;
}
// retrieve edge weight (default to 1)
mgp_value *weight_prop = mgp_edge_get_property(edge, "weight", memory);
if (!weight_prop) {
mgp_vertex_destroy(vertex);
mgp_edges_iterator_destroy(edges_iterator);
mgp_result_set_error_msg(result, "Not enough memory");
}
double weight = 1;
if (mgp_value_is_double(weight_prop))
weight = mgp_value_get_double(weight_prop);
if (mgp_value_is_int(weight_prop))
weight = static_cast<double>(mgp_value_get_int(weight_prop));
mgp_value_destroy(weight_prop);
try {
louvain_graph.AddEdge(p.second, next_louv_id, weight);
} catch (const std::exception &e) {
mgp_vertex_destroy(vertex);
mgp_edges_iterator_destroy(edges_iterator);
mgp_result_set_error_msg(result, e.what());
return std::nullopt;
}
}
mgp_vertex_destroy(vertex);
mgp_edges_iterator_destroy(edges_iterator);
}
try {
algorithms::Louvain(&louvain_graph);
} catch (const std::exception &e) {
const auto msg = std::string("[Internal error] ") + e.what();
mgp_result_set_error_msg(result, msg.c_str());
return std::nullopt;
}
return louvain_graph;
}
void communities(const mgp_list *args, const mgp_graph *graph,
mgp_result *result, mgp_memory *memory) {
try {
// Normalize vertex ids
auto mem_to_louv_id = NormalizeVertexIds(graph, result, memory);
if (!mem_to_louv_id) return;
// Run louvain
auto louvain_graph = RunLouvain(graph, result, memory, *mem_to_louv_id);
if (!louvain_graph) return;
// Return node ids and their corresponding communities.
for (const auto &p : *mem_to_louv_id) {
mgp_result_record *record = mgp_result_new_record(result);
if (record == nullptr) {
mgp_result_set_error_msg(result, "Not enough memory!");
return;
}
mgp_value *mem_id_value = mgp_value_make_int(p.first, memory);
if (mem_id_value == nullptr) {
mgp_result_set_error_msg(result, "Not enough memory!");
return;
}
mgp_value *com_value =
mgp_value_make_int(louvain_graph->Community(p.second), memory);
if (com_value == nullptr) {
mgp_value_destroy(mem_id_value);
mgp_result_set_error_msg(result, "Not enough memory!");
return;
}
int mem_id_inserted =
mgp_result_record_insert(record, "id", mem_id_value);
int com_inserted =
mgp_result_record_insert(record, "community", com_value);
mgp_value_destroy(mem_id_value);
mgp_value_destroy(com_value);
if (!mem_id_inserted || !com_inserted) {
mgp_result_set_error_msg(result, "Not enough memory!");
return;
}
}
} catch (const std::exception &e) {
mgp_result_set_error_msg(result, e.what());
return;
}
}
void modularity(const mgp_list *args, const mgp_graph *graph,
mgp_result *result, mgp_memory *memory) {
try {
// Normalize vertex ids
auto mem_to_louv_id = NormalizeVertexIds(graph, result, memory);
if (!mem_to_louv_id) return;
// Run louvain
auto louvain_graph = RunLouvain(graph, result, memory, *mem_to_louv_id);
if (!louvain_graph) return;
// Return graph modularity after Louvain
// TODO(ipaljak) - consider allowing the user to specify seed communities
// and
// yield modularity values both before and after running
// louvain.
mgp_result_record *record = mgp_result_new_record(result);
if (record == nullptr) {
mgp_result_set_error_msg(result, "Not enough memory!");
return;
}
mgp_value *modularity_value =
mgp_value_make_double(louvain_graph->Modularity(), memory);
if (modularity_value == nullptr) {
mgp_result_set_error_msg(result, "Not enough memory!");
return;
}
int value_inserted =
mgp_result_record_insert(record, "modularity", modularity_value);
mgp_value_destroy(modularity_value);
if (!value_inserted) {
mgp_result_set_error_msg(result, "Not enough memory!");
return;
}
} catch (const std::exception &e) {
mgp_result_set_error_msg(result, e.what());
return;
}
}
} // namespace
extern "C" int mgp_init_module(struct mgp_module *module,
struct mgp_memory *memory) {
struct mgp_proc *community_proc =
mgp_module_add_read_procedure(module, "communities", communities);
if (!community_proc) return 1;
if (!mgp_proc_add_result(community_proc, "id", mgp_type_int())) return 1;
if (!mgp_proc_add_result(community_proc, "community", mgp_type_int()))
return 1;
struct mgp_proc *modularity_proc =
mgp_module_add_read_procedure(module, "modularity", modularity);
if (!modularity_proc) return 1;
if (!mgp_proc_add_result(modularity_proc, "modularity", mgp_type_float()))
return 1;
return 0;
}
extern "C" int mgp_shutdown_module() { return 0; }

View File

@ -1,28 +0,0 @@
#include <iostream>
#include "algorithms/algorithms.hpp"
#include "data_structures/graph.hpp"
// A simple program that reads the graph from STDIN and
// outputs the detected communities from louvain along with
// its modularity measure on STDOUT.
int main() {
int n;
int m;
std::cin >> n >> m;
comdata::Graph graph(n);
for (int i = 0; i < m; ++i) {
int a;
int b;
double c;
std::cin >> a >> b >> c;
graph.AddEdge(a, b, c);
}
algorithms::Louvain(&graph);
for (int i = 0; i < n; ++i)
std::cout << i << " " << graph.Community(i) << "\n";
std::cout << graph.Modularity() << "\n";
return 0;
}

View File

@ -1,80 +0,0 @@
---
Checks: '*,
-android-*,
-cert-err58-cpp,
-cppcoreguidelines-avoid-c-arrays,
-cppcoreguidelines-avoid-goto,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-macro-usage,
-cppcoreguidelines-no-malloc,
-cppcoreguidelines-non-private-member-variables-in-classes,
-cppcoreguidelines-owning-memory,
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
-cppcoreguidelines-pro-bounds-constant-array-index,
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
-cppcoreguidelines-pro-type-member-init,
-cppcoreguidelines-pro-type-reinterpret-cast,
-cppcoreguidelines-pro-type-static-cast-downcast,
-cppcoreguidelines-pro-type-union-access,
-cppcoreguidelines-pro-type-vararg,
-cppcoreguidelines-special-member-functions,
-fuchsia-default-arguments,
-fuchsia-default-arguments-calls,
-fuchsia-default-arguments-declarations,
-fuchsia-overloaded-operator,
-fuchsia-statically-constructed-objects,
-fuchsia-trailing-return,
-fuchsia-virtual-inheritance,
-google-explicit-constructor,
-google-readability-*,
-hicpp-avoid-c-arrays,
-hicpp-avoid-goto,
-hicpp-braces-around-statements,
-hicpp-member-init,
-hicpp-no-array-decay,
-hicpp-no-assembler,
-hicpp-no-malloc,
-hicpp-special-member-functions,
-hicpp-use-equals-default,
-hicpp-vararg,
-llvm-header-guard,
-misc-non-private-member-variables-in-classes,
-misc-unused-parameters,
-modernize-avoid-c-arrays,
-modernize-concat-nested-namespaces,
-modernize-pass-by-value,
-modernize-use-equals-default,
-modernize-use-nodiscard,
-modernize-use-trailing-return-type,
-performance-unnecessary-value-param,
-readability-braces-around-statements,
-readability-else-after-return,
-readability-implicit-bool-conversion,
-readability-magic-numbers,
-readability-named-parameter'
WarningsAsErrors: ''
HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false
FormatStyle: none
CheckOptions:
- key: google-readability-braces-around-statements.ShortStatementLines
value: '1'
- key: google-readability-function-size.StatementThreshold
value: '800'
- key: google-readability-namespace-comments.ShortNamespaceLines
value: '10'
- key: google-readability-namespace-comments.SpacesBeforeComments
value: '2'
- key: modernize-loop-convert.MaxCopySize
value: '16'
- key: modernize-loop-convert.MinConfidence
value: reasonable
- key: modernize-loop-convert.NamingStyle
value: CamelCase
- key: modernize-pass-by-value.IncludeStyle
value: llvm
- key: modernize-replace-auto-ptr.IncludeStyle
value: llvm
- key: modernize-use-nullptr.NullMacros
value: 'NULL'
...

View File

@ -1,3 +0,0 @@
include_directories(${GTEST_INCLUDE_DIR})
add_subdirectory(unit)

View File

@ -1,28 +0,0 @@
set(test_prefix louvain__unit__)
add_custom_target(louvain__unit)
add_library(louvain-test STATIC utils.cpp)
set_target_properties(louvain-test PROPERTIES POSITION_INDEPENDENT_CODE ON)
function(add_unit_test test_cpp)
# get exec name (remove extension from the abs path)
get_filename_component(exec_name ${test_cpp} NAME_WE)
set(target_name ${test_prefix}${exec_name})
add_executable(${target_name} ${test_cpp})
# OUTPUT_NAME sets the real name of a target when it is built and can be
# used to help create two targets of the same name even though CMake
# requires unique logical target names
set_target_properties(${target_name} PROPERTIES OUTPUT_NAME ${exec_name})
# TODO: this is a temporary workaround the test build warnings
target_compile_options(${target_name} PRIVATE -Wno-comment -Wno-sign-compare
-Wno-unused-variable)
target_link_libraries(${target_name} spdlog gflags gtest gtest_main Threads::Threads
louvain-core louvain-test)
# register test
add_test(${target_name} ${exec_name})
# add to unit target
add_dependencies(louvain__unit ${target_name})
endfunction(add_unit_test)
add_unit_test(graph.cpp)

View File

@ -1,349 +0,0 @@
#include <gtest/gtest.h>
#include "data_structures/graph.hpp"
#include "utils.hpp"
// Checks if commmunities of nodes in graph correspond to a given community
// vector.
bool CommunityCheck(const comdata::Graph &graph,
const std::vector<uint32_t> &c) {
if (graph.Size() != c.size()) return false;
for (uint32_t node_id = 0; node_id < graph.Size(); ++node_id)
if (graph.Community(node_id) != c[node_id]) return false;
return true;
}
// Checks if degrees of nodes in graph correspond to a given degree vector.
bool DegreeCheck(const comdata::Graph &graph,
const std::vector<uint32_t> &deg) {
if (graph.Size() != deg.size()) return false;
for (uint32_t node_id = 0; node_id < graph.Size(); ++node_id)
if (graph.Degree(node_id) != deg[node_id]) return false;
return true;
}
// Checks if incident weights of nodes in graph correspond to a given weight
// vector.
bool IncidentWeightCheck(const comdata::Graph &graph,
const std::vector<double> &inc_w) {
if (graph.Size() != inc_w.size()) return false;
for (uint32_t node_id = 0; node_id < graph.Size(); ++node_id)
if (std::abs(graph.IncidentWeight(node_id) - inc_w[node_id]) > 1e-6)
return false;
return true;
}
// Sets communities of nodes in graph. Returns true on success.
bool SetCommunities(comdata::Graph *graph, const std::vector<uint32_t> &c) {
if (graph->Size() != c.size()) return false;
for (uint32_t node_id = 0; node_id < graph->Size(); ++node_id)
graph->SetCommunity(node_id, c[node_id]);
return true;
}
TEST(Graph, Constructor) {
uint32_t nodes = 100;
comdata::Graph graph(nodes);
ASSERT_EQ(graph.Size(), nodes);
for (uint32_t node_id = 0; node_id < nodes; ++node_id) {
ASSERT_EQ(graph.IncidentWeight(node_id), 0);
ASSERT_EQ(graph.Community(node_id), node_id);
}
}
TEST(Graph, Size) {
comdata::Graph graph1 = GenRandomUnweightedGraph(0, 0);
comdata::Graph graph2 = GenRandomUnweightedGraph(42, 41);
comdata::Graph graph3 = GenRandomUnweightedGraph(100, 250);
ASSERT_EQ(graph1.Size(), 0);
ASSERT_EQ(graph2.Size(), 42);
ASSERT_EQ(graph3.Size(), 100);
}
TEST(Graph, Communities) {
comdata::Graph graph = GenRandomUnweightedGraph(100, 250);
for (int i = 0; i < 100; ++i) graph.SetCommunity(i, i % 5);
for (int i = 0; i < 100; ++i) ASSERT_EQ(graph.Community(i), i % 5);
// Try to set communities on non-existing nodes
EXPECT_THROW({ graph.SetCommunity(100, 2); }, std::out_of_range);
EXPECT_THROW({ graph.SetCommunity(150, 0); }, std::out_of_range);
// Try to get a the community of a non-existing node
EXPECT_THROW({ graph.Community(100); }, std::out_of_range);
EXPECT_THROW({ graph.Community(150); }, std::out_of_range);
}
TEST(Graph, CommunityNormalization) {
// Communities are already normalized.
comdata::Graph graph = GenRandomUnweightedGraph(5, 10);
std::vector<uint32_t> init_c = {0, 2, 1, 3, 4};
std::vector<uint32_t> final_c = {0, 2, 1, 3, 4};
ASSERT_TRUE(SetCommunities(&graph, init_c));
graph.NormalizeCommunities();
ASSERT_TRUE(CommunityCheck(graph, final_c));
// Each node in its own community.
graph = GenRandomUnweightedGraph(5, 10);
init_c = {20, 30, 10, 40, 50};
final_c = {1, 2, 0, 3, 4};
ASSERT_TRUE(SetCommunities(&graph, init_c));
graph.NormalizeCommunities();
ASSERT_TRUE(CommunityCheck(graph, final_c));
// Multiple nodes in the same community
graph = GenRandomUnweightedGraph(7, 10);
init_c = {13, 99, 13, 13, 1, 99, 1};
final_c = {1, 2, 1, 1, 0, 2, 0};
ASSERT_TRUE(SetCommunities(&graph, init_c));
graph.NormalizeCommunities();
ASSERT_TRUE(CommunityCheck(graph, final_c));
}
TEST(Graph, AddEdge) {
comdata::Graph graph = GenRandomUnweightedGraph(5, 0);
// Node out of bounds.
EXPECT_THROW({ graph.AddEdge(1, 5, 7); }, std::out_of_range);
// Repeated edge
graph.AddEdge(1, 2, 1);
EXPECT_THROW({ graph.AddEdge(1, 2, 7); }, std::invalid_argument);
// Non-positive edge weight
EXPECT_THROW({ graph.AddEdge(2, 3, -7); }, std::out_of_range);
EXPECT_THROW({ graph.AddEdge(3, 4, 0); }, std::out_of_range);
}
TEST(Graph, Degrees) {
// Graph without edges
comdata::Graph graph = GenRandomUnweightedGraph(5, 0);
std::vector<uint32_t> deg = {0, 0, 0, 0, 0};
ASSERT_TRUE(DegreeCheck(graph, deg));
// Chain
// (0)--(1)--(2)--(3)--(4)
graph = BuildGraph(5, {{0, 1, 1}, {1, 2, 1}, {2, 3, 1}, {3, 4, 1}});
deg = {1, 2, 2, 2, 1};
ASSERT_TRUE(DegreeCheck(graph, deg));
// Tree
// (0)--(3)
// / \
// (1) (2)
// | / \
// (4) (5) (6)
graph = BuildGraph(
7, {{0, 1, 1}, {0, 2, 1}, {0, 3, 1}, {1, 4, 1}, {2, 5, 1}, {2, 6, 1}});
deg = {3, 2, 3, 1, 1, 1, 1};
ASSERT_TRUE(DegreeCheck(graph, deg));
// Graph without self-loops
// (0)--(1)
// | \ | \
// | \ | \
// (2)--(3)-(4)
graph = BuildGraph(5, {{0, 1, 1},
{0, 2, 1},
{0, 3, 1},
{1, 3, 1},
{1, 4, 1},
{2, 3, 1},
{3, 4, 1}});
deg = {3, 3, 2, 4, 2};
ASSERT_TRUE(DegreeCheck(graph, deg));
// Graph with self loop [*nodes have self loops]
// (0)--(1*)
// | \ | \
// | \ | \
// (2*)--(3)-(4*)
graph = BuildGraph(5, {{0, 1, 1},
{0, 2, 1},
{0, 3, 1},
{1, 3, 1},
{1, 4, 1},
{2, 3, 1},
{3, 4, 1},
{1, 1, 1},
{2, 2, 2},
{4, 4, 4}});
deg = {3, 4, 3, 4, 3};
ASSERT_TRUE(DegreeCheck(graph, deg));
// Try to get degree of non-existing nodes
EXPECT_THROW({ graph.Degree(5); }, std::out_of_range);
EXPECT_THROW({ graph.Degree(100); }, std::out_of_range);
}
TEST(Graph, Weights) {
// Graph without edges
comdata::Graph graph = GenRandomUnweightedGraph(5, 0);
std::vector<double> inc_w = {0, 0, 0, 0, 0};
ASSERT_TRUE(IncidentWeightCheck(graph, inc_w));
ASSERT_EQ(graph.TotalWeight(), 0);
// Chain
// (0)--(1)--(2)--(3)--(4)
graph = BuildGraph(5, {{0, 1, 0.1}, {1, 2, 0.5}, {2, 3, 2.3}, {3, 4, 4.2}});
inc_w = {0.1, 0.6, 2.8, 6.5, 4.2};
ASSERT_TRUE(IncidentWeightCheck(graph, inc_w));
ASSERT_NEAR(graph.TotalWeight(), 7.1, 1e-6);
// Tree
// (0)--(3)
// / \
// (1) (2)
// | / \
// (4) (5) (6)
graph = BuildGraph(7, {{0, 1, 1.3},
{0, 2, 0.2},
{0, 3, 1},
{1, 4, 3.2},
{2, 5, 4.2},
{2, 6, 0.7}});
inc_w = {2.5, 4.5, 5.1, 1, 3.2, 4.2, 0.7};
ASSERT_TRUE(IncidentWeightCheck(graph, inc_w));
EXPECT_NEAR(graph.TotalWeight(), 10.6, 1e-6);
// Graph without self-loops
// (0)--(1)
// | \ | \
// | \ | \
// (2)--(3)-(4)
graph = BuildGraph(5, {{0, 1, 0.1},
{0, 2, 0.2},
{0, 3, 0.3},
{1, 3, 0.4},
{1, 4, 0.5},
{2, 3, 0.6},
{3, 4, 0.7}});
inc_w = {0.6, 1, 0.8, 2, 1.2};
ASSERT_TRUE(IncidentWeightCheck(graph, inc_w));
EXPECT_NEAR(graph.TotalWeight(), 2.8, 1e-6);
// Graph with self loop [*nodes have self loops]
// (0)--(1*)
// | \ | \
// | \ | \
// (2*)--(3)-(4*)
graph = BuildGraph(5, {{0, 1, 0.1},
{0, 2, 0.2},
{0, 3, 0.3},
{1, 3, 0.4},
{1, 4, 0.5},
{2, 3, 0.6},
{3, 4, 0.7},
{1, 1, 0.8},
{2, 2, 0.9},
{4, 4, 1}});
inc_w = {0.6, 1.8, 1.7, 2, 2.2};
ASSERT_TRUE(IncidentWeightCheck(graph, inc_w));
EXPECT_NEAR(graph.TotalWeight(), 5.5, 1e-6);
// Try to get incident weight of non-existing node
EXPECT_THROW({ graph.IncidentWeight(5); }, std::out_of_range);
EXPECT_THROW({ graph.IncidentWeight(100); }, std::out_of_range);
}
TEST(Graph, Modularity) {
// Graph without edges
comdata::Graph graph = GenRandomUnweightedGraph(5, 0);
ASSERT_EQ(graph.Modularity(), 0);
// Chain
// (0)--(1)--(2)--(3)--(4)
graph = BuildGraph(5, {{0, 1, 0.1}, {1, 2, 0.5}, {2, 3, 2.3}, {3, 4, 4.2}});
std::vector<uint32_t> c = {0, 1, 1, 2, 2};
SetCommunities(&graph, c);
EXPECT_NEAR(graph.Modularity(), 0.036798254314620096, 1e-6);
// Tree
// (0)--(3)
// / \
// (1) (2)
// | / \
// (4) (5) (6)
graph = BuildGraph(7, {{0, 1, 1.3},
{0, 2, 0.2},
{0, 3, 1},
{1, 4, 3.2},
{2, 5, 4.2},
{2, 6, 0.7}});
c = {0, 0, 1, 0, 0, 1, 2};
SetCommunities(&graph, c);
EXPECT_NEAR(graph.Modularity(), 0.4424617301530794, 1e-6);
// Graph without self-loops
// (0)--(1)
// | \ | \
// | \ | \
// (2)--(3)-(4)
graph = BuildGraph(5, {{0, 1, 0.1},
{0, 2, 0.2},
{0, 3, 0.3},
{1, 3, 0.4},
{1, 4, 0.5},
{2, 3, 0.6},
{3, 4, 0.7}});
c = {0, 1, 1, 1, 1};
SetCommunities(&graph, c);
EXPECT_NEAR(graph.Modularity(), -0.022959183673469507, 1e-6);
// Graph with self loop [*nodes have self loops]
// (0)--(1*)
// | \ | \
// | \ | \
// (2*)--(3)-(4*)
graph = BuildGraph(5, {{0, 1, 0.1},
{0, 2, 0.2},
{0, 3, 0.3},
{1, 3, 0.4},
{1, 4, 0.5},
{2, 3, 0.6},
{3, 4, 0.7},
{1, 1, 0.8},
{2, 2, 0.9},
{4, 4, 1}});
c = {0, 0, 0, 0, 1};
SetCommunities(&graph, c);
EXPECT_NEAR(graph.Modularity(), 0.188842975206611, 1e-6);
// Neo4j example graph
// (0)--(1)---(3)--(4)
// \ / \ /
// (2) (5)
graph = BuildGraph(6, {{0, 1, 1},
{1, 2, 1},
{0, 2, 1},
{1, 3, 1},
{3, 5, 1},
{5, 4, 1},
{3, 4, 1}});
c = {0, 0, 0, 1, 1, 1};
SetCommunities(&graph, c);
EXPECT_NEAR(graph.Modularity(), 0.3571428571428571, 1e-6);
// Example graph from wikipedia
// (0)--(1)--(3)--(4)--(5)
// \ / | \ /
// (2) (7) (6)
// / \
// (8)--(9)
graph = BuildGraph(10, {{0, 1, 1},
{1, 2, 1},
{0, 2, 1},
{1, 3, 1},
{3, 4, 1},
{4, 5, 1},
{5, 6, 1},
{6, 4, 1},
{3, 7, 1},
{7, 8, 1},
{7, 9, 1},
{8, 9, 1}});
c = {0, 0, 0, 0, 1, 1, 1, 2, 2, 2};
SetCommunities(&graph, c);
EXPECT_NEAR(graph.Modularity(), 0.4896, 1e-4);
}

View File

@ -1,32 +0,0 @@
#include "utils.hpp"
#include <random>
comdata::Graph BuildGraph(
uint32_t nodes, std::vector<std::tuple<uint32_t, uint32_t, double>> edges) {
comdata::Graph G(nodes);
for (auto &edge : edges)
G.AddEdge(std::get<0>(edge), std::get<1>(edge), std::get<2>(edge));
return G;
}
comdata::Graph GenRandomUnweightedGraph(uint32_t nodes, uint32_t edges) {
auto seed =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
std::mt19937 rng(seed);
std::uniform_int_distribution<uint32_t> dist(0, nodes - 1);
std::set<std::tuple<uint32_t, uint32_t, double>> E;
for (uint32_t i = 0; i < edges; ++i) {
int u;
int v;
do {
u = dist(rng);
v = dist(rng);
if (u > v) std::swap(u, v);
} while (u == v || E.find({u, v, 1}) != E.end());
E.insert({u, v, 1});
}
return BuildGraph(nodes, std::vector<std::tuple<uint32_t, uint32_t, double>>(
E.begin(), E.end()));
}

View File

@ -1,18 +0,0 @@
#pragma once
#include <chrono>
#include <random>
#include <set>
#include <tuple>
#include "data_structures/graph.hpp"
/// Builds the graph from a given number of nodes and a list of edges.
/// Nodes should be 0-indexed and each edge should be provided only once.
comdata::Graph BuildGraph(
uint32_t nodes, std::vector<std::tuple<uint32_t, uint32_t, double>> edges);
/// Generates random undirected graph with a given number of nodes and edges.
/// The generated graph is not picked out of a uniform distribution. All weights
/// are the same and equal to one.
comdata::Graph GenRandomUnweightedGraph(uint32_t nodes, uint32_t edges);