annotate louvain/main_community.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: main_community.cpp
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
2 // -- community detection, sample main 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 <stdlib.h>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
18 #include <math.h>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
19 #include <string>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
20 #include <iostream>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
21 #include <fstream>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
22 #include <sstream>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
23 #include <vector>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
24 #include <algorithm>
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
25
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
26 #include "graph_binary.h"
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
27 #include "community.h"
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
28
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
29 using namespace std;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
30
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
31 char *filename = NULL;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
32 char *filename_w = NULL;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
33 char *filename_part = NULL;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
34 int type = UNWEIGHTED;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
35 int nb_pass = 0;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
36 double precision = 0.000001;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
37 int display_level = -2;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
38 int k1 = 16;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
39 int seed = 123;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
40
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
41 bool verbose = false;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
42
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
43 void
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
44 usage(char *prog_name, const char *more) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
45 cerr << more;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
46 cerr << "usage: " << prog_name << " input_file [-w weight_file] [-p part_file] [-q epsilon] [-l display_level] [-v] [-h]" << endl << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
47 cerr << "input_file: file containing the graph to decompose in communities." << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
48 cerr << "-w file\tread the graph as a weighted one (weights are set to 1 otherwise)." << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
49 cerr << "-p file\tstart the computation with a given partition instead of the trivial partition." << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
50 cerr << "\tfile must contain lines \"node community\"." << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
51 cerr << "-q eps\ta given pass stops when the modularity is increased by less than epsilon." << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
52 cerr << "-l k\tdisplays the graph of level k rather than the hierachical structure." << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
53 cerr << "\tif k=-1 then displays the hierarchical structure rather than the graph at a given level." << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
54 cerr << "-v\tverbose mode: gives computation time, information about the hierarchy and modularity." << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
55 cerr << "-s\tseed for rundom number generator setting, integer(123 deafault)" << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
56 cerr << "-h\tshow this usage message." << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
57 exit(0);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
58 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
59
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
60 void
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
61 parse_args(int argc, char **argv) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
62 if (argc<2)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
63 usage(argv[0], "Bad arguments number\n");
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
64
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
65 for (int i = 1; i < argc; i++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
66 if(argv[i][0] == '-') {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
67 switch(argv[i][1]) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
68 case 'w':
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
69 type = WEIGHTED;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
70 filename_w = argv[i+1];
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
71 i++;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
72 break;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
73 case 'p':
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
74 filename_part = argv[i+1];
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
75 i++;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
76 break;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
77 case 'q':
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
78 precision = atof(argv[i+1]);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
79 i++;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
80 break;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
81 case 'l':
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
82 display_level = atoi(argv[i+1]);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
83 i++;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
84 break;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
85 case 's':
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
86 seed = atoi(argv[i+1]);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
87 i++;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
88 break;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
89 case 'k':
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
90 k1 = atoi(argv[i+1]);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
91 i++;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
92 break;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
93 case 'v':
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
94 verbose=true;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
95 break;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
96 default:
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
97 usage(argv[0], "Unknown option\n");
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
98 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
99 } else {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
100 if (filename==NULL)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
101 filename = argv[i];
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
102 else
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
103 usage(argv[0], "More than one filename\n");
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
104 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
105 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
106 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
107
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
108 void
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
109 display_time(const char *str) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
110 time_t rawtime;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
111 time ( &rawtime );
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
112 cerr << str << ": " << ctime (&rawtime);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
113 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
114
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
115 int
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
116 main(int argc, char **argv) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
117 parse_args(argc, argv);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
118 srand(seed);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
119 time_t time_begin, time_end;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
120 time(&time_begin);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
121 if (verbose)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
122 display_time("Begin");
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
123
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
124 Community c(filename, filename_w, type, -1, precision);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
125 if (filename_part!=NULL)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
126 c.init_partition(filename_part);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
127 Graph g;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
128 bool improvement=true;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
129 double mod=c.modularity(), new_mod;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
130 int level=0;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
131
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
132 do {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
133 if (verbose) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
134 cerr << "level " << level << ":\n";
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
135 display_time(" start computation");
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
136 cerr << " network size: "
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
137 << c.g.nb_nodes << " nodes, "
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
138 << c.g.nb_links << " links, "
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
139 << c.g.total_weight << " weight." << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
140 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
141
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
142 improvement = c.one_level();
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
143 new_mod = c.modularity();
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
144 if (++level==display_level)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
145 g.display();
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
146 if (display_level==-1)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
147 c.display_partition();
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
148 g = c.partition2graph_binary();
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
149 c = Community(g, -1, precision);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
150
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
151 if (verbose)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
152 cerr << " modularity increased from " << mod << " to " << new_mod << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
153
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
154 mod=new_mod;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
155 if (verbose)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
156 display_time(" end computation");
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
157
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
158 if (filename_part!=NULL && level==1) // do at least one more computation if partition is provided
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
159 improvement=true;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
160 } while(improvement);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
161
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
162 time(&time_end);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
163 if (verbose) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
164 display_time("End");
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
165 cerr << "Total duration: " << (time_end-time_begin) << " sec." << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
166 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
167 cerr << new_mod << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
168 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
169