annotate louvain/graph.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.cpp
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
2 // -- simple graph handling source 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 #include "graph.h"
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
18
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
19 using namespace std;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
20
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
21 Graph::Graph(char *filename, int type) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
22 ifstream finput;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
23 finput.open(filename,fstream::in);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
24
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
25 int nb_links=0;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
26
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
27 while (!finput.eof()) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
28 unsigned int src, dest;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
29 double weight=1.;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
30
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
31 if (type==WEIGHTED) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
32 finput >> src >> dest >> weight;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
33 } else {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
34 finput >> src >> dest;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
35 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
36
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
37 if (finput) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
38 if (links.size()<=max(src,dest)+1) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
39 links.resize(max(src,dest)+1);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
40 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
41
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
42 links[src].push_back(make_pair(dest,weight));
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
43 if (src!=dest)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
44 links[dest].push_back(make_pair(src,weight));
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
45
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
46 nb_links++;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
47 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
48 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
49
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
50 finput.close();
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
51 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
52
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
53 void
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
54 Graph::renumber(int type) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
55 vector<int> linked(links.size(),-1);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
56 vector<int> renum(links.size(),-1);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
57 int nb=0;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
58
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
59 for (unsigned int i=0 ; i<links.size() ; i++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
60 for (unsigned int j=0 ; j<links[i].size() ; j++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
61 linked[i]=1;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
62 linked[links[i][j].first]=1;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
63 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
64 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
65
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
66 for (unsigned int i=0 ; i<links.size() ; i++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
67 if (linked[i]==1)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
68 renum[i]=nb++;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
69 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
70
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
71 for (unsigned int i=0 ; i<links.size() ; i++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
72 if (linked[i]==1) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
73 for (unsigned int j=0 ; j<links[i].size() ; j++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
74 links[i][j].first = renum[links[i][j].first];
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
75 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
76 links[renum[i]]=links[i];
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
77 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
78 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
79 links.resize(nb);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
80 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
81
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
82 void
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
83 Graph::clean(int type) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
84 for (unsigned int i=0 ; i<links.size() ; i++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
85 map<int, float> m;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
86 map<int, float>::iterator it;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
87
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
88 for (unsigned int j=0 ; j<links[i].size() ; j++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
89 it = m.find(links[i][j].first);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
90 if (it==m.end())
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
91 m.insert(make_pair(links[i][j].first, links[i][j].second));
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
92 else if (type==WEIGHTED)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
93 it->second+=links[i][j].second;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
94 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
95
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
96 vector<pair<int,float> > v;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
97 for (it = m.begin() ; it!=m.end() ; it++)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
98 v.push_back(*it);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
99 links[i].clear();
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
100 links[i]=v;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
101 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
102 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
103
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
104 void
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
105 Graph::display(int type) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
106 for (unsigned int i=0 ; i<links.size() ; i++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
107 for (unsigned int j=0 ; j<links[i].size() ; j++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
108 int dest = links[i][j].first;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
109 float weight = links[i][j].second;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
110 if (type==WEIGHTED)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
111 cout << i << " " << dest << " " << weight << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
112 else
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
113 cout << i << " " << dest << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
114 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
115 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
116 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
117
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
118 void
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
119 Graph::display_binary(char *filename, char *filename_w, int type) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
120 ofstream foutput;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
121 foutput.open(filename, fstream::out | fstream::binary);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
122
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
123 unsigned int s = links.size();
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
124
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
125 // outputs number of nodes
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
126 foutput.write((char *)(&s),4);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
127
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
128 // outputs cumulative degree sequence
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
129 long tot=0;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
130 for (unsigned int i=0 ; i<s ; i++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
131 tot+=(long)links[i].size();
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
132 foutput.write((char *)(&tot),8);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
133 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
134
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
135 // outputs links
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
136 for (unsigned int i=0 ; i<s ; i++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
137 for (unsigned int j=0 ; j<links[i].size() ; j++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
138 int dest = links[i][j].first;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
139 foutput.write((char *)(&dest),4);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
140 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
141 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
142 foutput.close();
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
143
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
144 // outputs weights in a separate file
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
145 if (type==WEIGHTED) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
146 ofstream foutput_w;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
147 foutput_w.open(filename_w,fstream::out | fstream::binary);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
148 for (unsigned int i=0 ; i<s ; i++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
149 for (unsigned int j=0 ; j<links[i].size() ; j++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
150 float weight = links[i][j].second;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
151 foutput_w.write((char *)(&weight),4);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
152 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
153 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
154 foutput_w.close();
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
155 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
156 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
157