annotate louvain/graph_binary.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: graph_binary.h
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
2 // -- graph handling 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 GRAPH_H
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
18 #define GRAPH_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 <assert.h>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
23 #include <malloc.h>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
24 #include <iostream>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
25 #include <iomanip>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
26 #include <fstream>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
27 #include <vector>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
28 #include <map>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
29 #include <algorithm>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
30
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
31 #define WEIGHTED 0
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
32 #define UNWEIGHTED 1
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
33
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
34 using namespace std;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
35
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
36 class Graph {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
37 public:
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
38 unsigned int nb_nodes;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
39 unsigned long nb_links;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
40 double total_weight;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
41
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
42 vector<unsigned long> degrees;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
43 vector<unsigned int> links;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
44 vector<float> weights;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
45
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
46 Graph();
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
47
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
48 // binary file format is
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
49 // 4 bytes for the number of nodes in the graph
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
50 // 8*(nb_nodes) bytes for the cumulative degree for each node:
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
51 // deg(0)=degrees[0]
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
52 // deg(k)=degrees[k]-degrees[k-1]
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
53 // 4*(sum_degrees) bytes for the links
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
54 // IF WEIGHTED 4*(sum_degrees) bytes for the weights in a separate file
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
55 Graph(char *filename, char *filename_w, int type);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
56
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
57 Graph(int nb_nodes, int nb_links, double total_weight, int *degrees, int *links, float *weights);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
58
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
59 void display(void);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
60 void display_reverse(void);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
61 void display_binary(char *outfile);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
62 bool check_symmetry();
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
63
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
64
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
65 // return the number of neighbors (degree) of the node
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
66 inline unsigned int nb_neighbors(unsigned int node);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
67
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
68 // return the number of self loops of the node
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
69 inline double nb_selfloops(unsigned int node);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
70
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
71 // return the weighted degree of the node
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
72 inline double weighted_degree(unsigned int node);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
73
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
74 // return pointers to the first neighbor and first weight of the node
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
75 inline pair<vector<unsigned int>::iterator, vector<float>::iterator > neighbors(unsigned int node);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
76 };
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
77
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
78
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
79 inline unsigned int
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
80 Graph::nb_neighbors(unsigned int node) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
81 assert(node>=0 && node<nb_nodes);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
82
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
83 if (node==0)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
84 return degrees[0];
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
85 else
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
86 return degrees[node]-degrees[node-1];
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
87 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
88
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
89 inline double
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
90 Graph::nb_selfloops(unsigned int node) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
91 assert(node>=0 && node<nb_nodes);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
92
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
93 pair<vector<unsigned int>::iterator, vector<float>::iterator > p = neighbors(node);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
94 for (unsigned int i=0 ; i<nb_neighbors(node) ; i++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
95 if (*(p.first+i)==node) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
96 if (weights.size()!=0)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
97 return (double)*(p.second+i);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
98 else
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
99 return 1.;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
100 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
101 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
102 return 0.;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
103 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
104
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
105 inline double
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
106 Graph::weighted_degree(unsigned int node) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
107 assert(node>=0 && node<nb_nodes);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
108
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
109 if (weights.size()==0)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
110 return (double)nb_neighbors(node);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
111 else {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
112 pair<vector<unsigned int>::iterator, vector<float>::iterator > p = neighbors(node);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
113 double res = 0;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
114 for (unsigned int i=0 ; i<nb_neighbors(node) ; i++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
115 res += (double)*(p.second+i);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
116 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
117 return res;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
118 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
119 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
120
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
121 inline pair<vector<unsigned int>::iterator, vector<float>::iterator >
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
122 Graph::neighbors(unsigned int node) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
123 assert(node>=0 && node<nb_nodes);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
124
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
125 if (node==0)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
126 return make_pair(links.begin(), weights.begin());
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
127 else if (weights.size()!=0)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
128 return make_pair(links.begin()+degrees[node-1], weights.begin()+degrees[node-1]);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
129 else
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
130 return make_pair(links.begin()+degrees[node-1], weights.begin());
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
131 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
132
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
133
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
134 #endif // GRAPH_H