forked from RapidsAtHKUST/CommunityDetectionCodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_binary.cpp
More file actions
150 lines (131 loc) · 4.45 KB
/
Copy pathgraph_binary.cpp
File metadata and controls
150 lines (131 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// File: graph_binary.cpp
// -- graph handling source
//-----------------------------------------------------------------------------
// Community detection
// Based on the article "Fast unfolding of community hierarchies in large networks"
// Copyright (C) 2008 V. Blondel, J.-L. Guillaume, R. Lambiotte, E. Lefebvre
//
// This program must not be distributed without agreement of the above mentionned authors.
//-----------------------------------------------------------------------------
// Author : E. Lefebvre, adapted by J.-L. Guillaume
// Email : jean-loup.guillaume@lip6.fr
// Location : Paris, France
// Time : February 2008
//-----------------------------------------------------------------------------
// see readme.txt for more details
#include <sys/mman.h>
#include <fstream>
#include "graph_binary.h"
#include "math.h"
Graph::Graph() {
nb_nodes = 0;
nb_links = 0;
total_weight = 0;
}
Graph::Graph(char *filename, char *filename_w, int type) {
ifstream finput;
finput.open(filename,fstream::in | fstream::binary);
// Read number of nodes on 4 bytes
finput.read((char *)&nb_nodes, 4);
assert(finput.rdstate() == ios::goodbit);
// Read cumulative degree sequence: 8 bytes for each node
// cum_degree[0]=degree(0); cum_degree[1]=degree(0)+degree(1), etc.
degrees.resize(nb_nodes);
finput.read((char *)°rees[0], nb_nodes*8);
// Read links: 4 bytes for each link (each link is counted twice)
nb_links=degrees[nb_nodes-1];
links.resize(nb_links);
finput.read((char *)(&links[0]), (long)nb_links*8);
// IF WEIGHTED : read weights: 4 bytes for each link (each link is counted twice)
weights.resize(0);
total_weight=0;
if (type==WEIGHTED) {
ifstream finput_w;
finput_w.open(filename_w,fstream::in | fstream::binary);
weights.resize(nb_links);
finput_w.read((char *)&weights[0], (long)nb_links*4);
}
// Compute total weight
for (unsigned int i=0 ; i<nb_nodes ; i++) {
total_weight += (double)weighted_degree(i);
}
}
Graph::Graph(int n, int m, double t, int *d, int *l, float *w) {
/* nb_nodes = n;
nb_links = m;
total_weight = t;
degrees = d;
links = l;
weights = w;*/
}
void
Graph::display() {
/* for (unsigned int node=0 ; node<nb_nodes ; node++) {
pair<vector<unsigned int>::iterator, vector<float>::iterator > p = neighbors(node);
for (unsigned int i=0 ; i<nb_neighbors(node) ; i++) {
if (node<=*(p.first+i)) {
if (weights.size()!=0)
cout << node << " " << *(p.first+i) << " " << *(p.second+i) << endl;
else
cout << node << " " << *(p.first+i) << endl;
}
}
}*/
for (unsigned int node=0 ; node<nb_nodes ; node++) {
pair<vector<unsigned int>::iterator, vector<float>::iterator > p = neighbors(node);
cout << node << ":" ;
for (unsigned int i=0 ; i<nb_neighbors(node) ; i++) {
if (true) {
if (weights.size()!=0)
cout << " (" << *(p.first+i) << " " << *(p.second+i) << ")";
else
cout << " " << *(p.first+i);
}
}
cout << endl;
}
}
void
Graph::display_reverse() {
for (unsigned int node=0 ; node<nb_nodes ; node++) {
pair<vector<unsigned int>::iterator, vector<float>::iterator > p = neighbors(node);
for (unsigned int i=0 ; i<nb_neighbors(node) ; i++) {
if (node>*(p.first+i)) {
if (weights.size()!=0)
cout << *(p.first+i) << " " << node << " " << *(p.second+i) << endl;
else
cout << *(p.first+i) << " " << node << endl;
}
}
}
}
bool
Graph::check_symmetry() {
int error=0;
for (unsigned int node=0 ; node<nb_nodes ; node++) {
pair<vector<unsigned int>::iterator, vector<float>::iterator > p = neighbors(node);
for (unsigned int i=0 ; i<nb_neighbors(node) ; i++) {
unsigned int neigh = *(p.first+i);
float weight = *(p.second+i);
pair<vector<unsigned int>::iterator, vector<float>::iterator > p_neigh = neighbors(neigh);
for (unsigned int j=0 ; j<nb_neighbors(neigh) ; j++) {
unsigned int neigh_neigh = *(p_neigh.first+j);
float neigh_weight = *(p_neigh.second+j);
if (node==neigh_neigh && weight!=neigh_weight) {
cout << node << " " << neigh << " " << weight << " " << neigh_weight << endl;
if (error++==10)
exit(0);
}
}
}
}
return (error==0);
}
void
Graph::display_binary(char *outfile) {
ofstream foutput;
foutput.open(outfile ,fstream::out | fstream::binary);
foutput.write((char *)(&nb_nodes),4);
foutput.write((char *)(°rees[0]),4*nb_nodes);
foutput.write((char *)(&links[0]),8*nb_links);
}