annotate louvain/community.h @ 0:1d1b9e1b2e2f draft

Uploaded
author petr-novak
date Thu, 19 Dec 2019 10:24:45 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
1 // File: community.h
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
2 // -- community detection header file
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
3 //-----------------------------------------------------------------------------
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
4 // Community detection
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
5 // Based on the article "Fast unfolding of community hierarchies in large networks"
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
6 // Copyright (C) 2008 V. Blondel, J.-L. Guillaume, R. Lambiotte, E. Lefebvre
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
7 //
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
8 // This program must not be distributed without agreement of the above mentionned authors.
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
9 //-----------------------------------------------------------------------------
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
10 // Author : E. Lefebvre, adapted by J.-L. Guillaume
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
11 // Email : jean-loup.guillaume@lip6.fr
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
12 // Location : Paris, France
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
13 // Time : February 2008
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
14 //-----------------------------------------------------------------------------
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
15 // see readme.txt for more details
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
16
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
17 #ifndef COMMUNITY_H
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
18 #define COMMUNITY_H
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
19
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
20 #include <stdlib.h>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
21 #include <stdio.h>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
22 #include <iostream>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
23 #include <iomanip>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
24 #include <fstream>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
25 #include <vector>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
26 #include <map>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
27
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
28 #include "graph_binary.h"
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
29
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
30 using namespace std;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
31
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
32 class Community {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
33 public:
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
34 vector<double> neigh_weight;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
35 vector<unsigned int> neigh_pos;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
36 unsigned int neigh_last;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
37
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
38 Graph g; // network to compute communities for
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
39 int size; // nummber of nodes in the network and size of all vectors
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
40 vector<int> n2c; // community to which each node belongs
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
41 vector<double> in,tot; // used to compute the modularity participation of each community
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
42
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
43 // number of pass for one level computation
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
44 // if -1, compute as many pass as needed to increase modularity
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
45 int nb_pass;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
46
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
47 // a new pass is computed if the last one has generated an increase
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
48 // greater than min_modularity
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
49 // if 0. even a minor increase is enough to go for one more pass
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
50 double min_modularity;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
51
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
52 // constructors:
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
53 // reads graph from file using graph constructor
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
54 // type defined the weighted/unweighted status of the graph file
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
55 Community (char *filename, char *filename_w, int type, int nb_pass, double min_modularity);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
56 // copy graph
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
57 Community (Graph g, int nb_pass, double min_modularity);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
58
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
59 // initiliazes the partition with something else than all nodes alone
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
60 void init_partition(char *filename_part);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
61
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
62 // display the community of each node
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
63 void display();
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
64
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
65 // remove the node from its current community with which it has dnodecomm links
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
66 inline void remove(int node, int comm, double dnodecomm);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
67
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
68 // insert the node in comm with which it shares dnodecomm links
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
69 inline void insert(int node, int comm, double dnodecomm);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
70
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
71 // compute the gain of modularity if node where inserted in comm
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
72 // given that node has dnodecomm links to comm. The formula is:
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
73 // [(In(comm)+2d(node,comm))/2m - ((tot(comm)+deg(node))/2m)^2]-
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
74 // [In(comm)/2m - (tot(comm)/2m)^2 - (deg(node)/2m)^2]
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
75 // where In(comm) = number of half-links strictly inside comm
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
76 // Tot(comm) = number of half-links inside or outside comm (sum(degrees))
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
77 // d(node,com) = number of links from node to comm
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
78 // deg(node) = node degree
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
79 // m = number of links
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
80 inline double modularity_gain(int node, int comm, double dnodecomm, double w_degree);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
81
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
82 // compute the set of neighboring communities of node
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
83 // for each community, gives the number of links from node to comm
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
84 void neigh_comm(unsigned int node);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
85
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
86 // compute the modularity of the current partition
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
87 double modularity();
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
88
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
89 // displays the graph of communities as computed by one_level
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
90 void partition2graph();
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
91 // displays the current partition (with communities renumbered from 0 to k-1)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
92 void display_partition();
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
93
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
94 // generates the binary graph of communities as computed by one_level
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
95 Graph partition2graph_binary();
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
96
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
97 // compute communities of the graph for one level
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
98 // return true if some nodes have been moved
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
99 bool one_level();
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
100 };
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
101
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
102 inline void
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
103 Community::remove(int node, int comm, double dnodecomm) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
104 assert(node>=0 && node<size);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
105
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
106 tot[comm] -= g.weighted_degree(node);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
107 in[comm] -= 2*dnodecomm + g.nb_selfloops(node);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
108 n2c[node] = -1;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
109 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
110
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
111 inline void
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
112 Community::insert(int node, int comm, double dnodecomm) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
113 assert(node>=0 && node<size);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
114
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
115 tot[comm] += g.weighted_degree(node);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
116 in[comm] += 2*dnodecomm + g.nb_selfloops(node);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
117 n2c[node]=comm;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
118 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
119
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
120 inline double
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
121 Community::modularity_gain(int node, int comm, double dnodecomm, double w_degree) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
122 assert(node>=0 && node<size);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
123
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
124 double totc = (double)tot[comm];
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
125 double degc = (double)w_degree;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
126 double m2 = (double)g.total_weight;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
127 double dnc = (double)dnodecomm;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
128
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
129 return (dnc - totc*degc/m2);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
130 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
131
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
132
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
133 #endif // COMMUNITY_H