forked from RapidsAtHKUST/CommunityDetectionCodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.cpp
157 lines (132 loc) · 3.86 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
// 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 program must not be distributed without agreement of the above mentionned authors.
//-----------------------------------------------------------------------------
// 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();
}
}