-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.cpp
170 lines (145 loc) · 4.63 KB
/
graph.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// File: graph.cpp
// -- simple graph handling source file
//-----------------------------------------------------------------------------
// 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 file is part of Louvain algorithm.
//
// Louvain algorithm is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Louvain algorithm is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Louvain algorithm. If not, see <http://www.gnu.org/licenses/>.
//-----------------------------------------------------------------------------
// Author : E. Lefebvre, adapted by J.-L. Guillaume
// Email : [email protected]
// Location : Paris, France
// Time : February 2008
//-----------------------------------------------------------------------------
// see readme.txt for more details
#include "graph.h"
using namespace std;
Graph::Graph(char *filename, int type) {
ifstream finput;
finput.open(filename,fstream::in);
int nb_links=0;
while (!finput.eof()) {
unsigned int src, dest;
double weight=1.;
if (type==WEIGHTED) {
finput >> src >> dest >> weight;
} else {
finput >> src >> dest;
}
if (finput) {
if (links.size()<=max(src,dest)+1) {
links.resize(max(src,dest)+1);
}
links[src].push_back(make_pair(dest,weight));
if (src!=dest)
links[dest].push_back(make_pair(src,weight));
nb_links++;
}
}
finput.close();
}
void
Graph::renumber(int type) {
vector<int> linked(links.size(),-1);
vector<int> renum(links.size(),-1);
int nb=0;
for (unsigned int i=0 ; i<links.size() ; i++) {
for (unsigned int j=0 ; j<links[i].size() ; j++) {
linked[i]=1;
linked[links[i][j].first]=1;
}
}
for (unsigned int i=0 ; i<links.size() ; i++) {
if (linked[i]==1)
renum[i]=nb++;
}
for (unsigned int i=0 ; i<links.size() ; i++) {
if (linked[i]==1) {
for (unsigned int j=0 ; j<links[i].size() ; j++) {
links[i][j].first = renum[links[i][j].first];
}
links[renum[i]]=links[i];
}
}
links.resize(nb);
}
void
Graph::clean(int type) {
for (unsigned int i=0 ; i<links.size() ; i++) {
map<int, float> m;
map<int, float>::iterator it;
for (unsigned int j=0 ; j<links[i].size() ; j++) {
it = m.find(links[i][j].first);
if (it==m.end())
m.insert(make_pair(links[i][j].first, links[i][j].second));
else if (type==WEIGHTED)
it->second+=links[i][j].second;
}
vector<pair<int,float> > v;
for (it = m.begin() ; it!=m.end() ; it++)
v.push_back(*it);
links[i].clear();
links[i]=v;
}
}
void
Graph::display(int type) {
for (unsigned int i=0 ; i<links.size() ; i++) {
for (unsigned int j=0 ; j<links[i].size() ; j++) {
int dest = links[i][j].first;
float weight = links[i][j].second;
if (type==WEIGHTED)
cout << i << " " << dest << " " << weight << endl;
else
cout << i << " " << dest << endl;
}
}
}
void
Graph::display_binary(char *filename, char *filename_w, int type) {
ofstream foutput;
foutput.open(filename, fstream::out | fstream::binary);
unsigned int s = links.size();
// outputs number of nodes
foutput.write((char *)(&s),4);
// outputs cumulative degree sequence
long tot=0;
for (unsigned int i=0 ; i<s ; i++) {
tot+=(long)links[i].size();
foutput.write((char *)(&tot),8);
}
// outputs links
for (unsigned int i=0 ; i<s ; i++) {
for (unsigned int j=0 ; j<links[i].size() ; j++) {
int dest = links[i][j].first;
foutput.write((char *)(&dest),4);
}
}
foutput.close();
// outputs weights in a separate file
if (type==WEIGHTED) {
ofstream foutput_w;
foutput_w.open(filename_w,fstream::out | fstream::binary);
for (unsigned int i=0 ; i<s ; i++) {
for (unsigned int j=0 ; j<links[i].size() ; j++) {
float weight = links[i][j].second;
foutput_w.write((char *)(&weight),4);
}
}
foutput_w.close();
}
}