annotate louvain/graph_binary.cpp @ 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.cpp
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
2 // -- graph handling source
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 #include <sys/mman.h>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
18 #include <fstream>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
19 #include "graph_binary.h"
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
20 #include "math.h"
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
21
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
22 Graph::Graph() {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
23 nb_nodes = 0;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
24 nb_links = 0;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
25 total_weight = 0;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
26 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
27
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
28 Graph::Graph(char *filename, char *filename_w, int type) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
29 ifstream finput;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
30 finput.open(filename,fstream::in | fstream::binary);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
31
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
32 // Read number of nodes on 4 bytes
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
33 finput.read((char *)&nb_nodes, 4);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
34 assert(finput.rdstate() == ios::goodbit);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
35
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
36 // Read cumulative degree sequence: 8 bytes for each node
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
37 // cum_degree[0]=degree(0); cum_degree[1]=degree(0)+degree(1), etc.
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
38 degrees.resize(nb_nodes);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
39 finput.read((char *)&degrees[0], nb_nodes*8);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
40
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
41 // Read links: 4 bytes for each link (each link is counted twice)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
42 nb_links=degrees[nb_nodes-1];
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
43 links.resize(nb_links);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
44 finput.read((char *)(&links[0]), (long)nb_links*4);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
45
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
46 // IF WEIGHTED : read weights: 4 bytes for each link (each link is counted twice)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
47 weights.resize(0);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
48 total_weight=0;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
49 if (type==WEIGHTED) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
50 ifstream finput_w;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
51 finput_w.open(filename_w,fstream::in | fstream::binary);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
52 weights.resize(nb_links);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
53 finput_w.read((char *)&weights[0], (long)nb_links*4);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
54 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
55
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
56 // Compute total weight
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
57 for (unsigned int i=0 ; i<nb_nodes ; i++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
58 total_weight += (double)weighted_degree(i);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
59 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
60 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
61
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
62 Graph::Graph(int n, int m, double t, int *d, int *l, float *w) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
63 /* nb_nodes = n;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
64 nb_links = m;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
65 total_weight = t;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
66 degrees = d;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
67 links = l;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
68 weights = w;*/
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
69 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
70
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
71
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
72 void
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
73 Graph::display() {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
74 /* for (unsigned int node=0 ; node<nb_nodes ; node++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
75 pair<vector<unsigned int>::iterator, vector<float>::iterator > p = neighbors(node);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
76 for (unsigned int i=0 ; i<nb_neighbors(node) ; i++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
77 if (node<=*(p.first+i)) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
78 if (weights.size()!=0)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
79 cout << node << " " << *(p.first+i) << " " << *(p.second+i) << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
80 else
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
81 cout << node << " " << *(p.first+i) << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
82 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
83 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
84 }*/
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
85 for (unsigned int node=0 ; node<nb_nodes ; node++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
86 pair<vector<unsigned int>::iterator, vector<float>::iterator > p = neighbors(node);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
87 cout << node << ":" ;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
88 for (unsigned int i=0 ; i<nb_neighbors(node) ; i++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
89 if (true) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
90 if (weights.size()!=0)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
91 cout << " (" << *(p.first+i) << " " << *(p.second+i) << ")";
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
92 else
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
93 cout << " " << *(p.first+i);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
94 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
95 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
96 cout << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
97 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
98 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
99
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
100 void
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
101 Graph::display_reverse() {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
102 for (unsigned int node=0 ; node<nb_nodes ; node++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
103 pair<vector<unsigned int>::iterator, vector<float>::iterator > p = neighbors(node);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
104 for (unsigned int i=0 ; i<nb_neighbors(node) ; i++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
105 if (node>*(p.first+i)) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
106 if (weights.size()!=0)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
107 cout << *(p.first+i) << " " << node << " " << *(p.second+i) << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
108 else
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
109 cout << *(p.first+i) << " " << node << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
110 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
111 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
112 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
113 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
114
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
115
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
116 bool
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
117 Graph::check_symmetry() {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
118 int error=0;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
119 for (unsigned int node=0 ; node<nb_nodes ; node++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
120 pair<vector<unsigned int>::iterator, vector<float>::iterator > p = neighbors(node);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
121 for (unsigned int i=0 ; i<nb_neighbors(node) ; i++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
122 unsigned int neigh = *(p.first+i);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
123 float weight = *(p.second+i);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
124
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
125 pair<vector<unsigned int>::iterator, vector<float>::iterator > p_neigh = neighbors(neigh);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
126 for (unsigned int j=0 ; j<nb_neighbors(neigh) ; j++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
127 unsigned int neigh_neigh = *(p_neigh.first+j);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
128 float neigh_weight = *(p_neigh.second+j);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
129
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
130 if (node==neigh_neigh && weight!=neigh_weight) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
131 cout << node << " " << neigh << " " << weight << " " << neigh_weight << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
132 if (error++==10)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
133 exit(0);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
134 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
135 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
136 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
137 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
138 return (error==0);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
139 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
140
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
141
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
142 void
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
143 Graph::display_binary(char *outfile) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
144 ofstream foutput;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
145 foutput.open(outfile ,fstream::out | fstream::binary);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
146
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
147 foutput.write((char *)(&nb_nodes),4);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
148 foutput.write((char *)(&degrees[0]),4*nb_nodes);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
149 foutput.write((char *)(&links[0]),8*nb_links);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
150 }