annotate louvain/main_convert.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_convert.cpp
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
2 // -- conversion of a graph from ascii to binary, 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 "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 char *infile = NULL;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
22 char *outfile = NULL;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
23 char *outfile_w = NULL;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
24 int type = UNWEIGHTED;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
25 bool do_renumber = false;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
26
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
27 void
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
28 usage(char *prog_name, const char *more) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
29 cerr << more;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
30 cerr << "usage: " << prog_name << " -i input_file -o outfile [-r] [-w outfile_weight]" << endl << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
31 cerr << "read the graph and convert it to binary format." << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
32 cerr << "-r\tnodes are renumbered from 0 to nb_nodes-1 (the order is kept)." << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
33 cerr << "-w filename\tread the graph as a weighted one and writes the weights in a separate file." << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
34 cerr << "-h\tshow this usage message." << endl;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
35 exit(0);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
36 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
37
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
38 void
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
39 parse_args(int argc, char **argv) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
40 for (int i = 1; i < argc; i++) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
41 if(argv[i][0] == '-') {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
42 switch(argv[i][1]) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
43 case 'i':
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
44 if (i==argc-1)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
45 usage(argv[0], "Infile missing\n");
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
46 infile = argv[i+1];
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
47 i++;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
48 break;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
49 case 'o':
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
50 if (i==argc-1)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
51 usage(argv[0], "Outfile missing\n");
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
52 outfile = argv[i+1];
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
53 i++;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
54 break;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
55 case 'w' :
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
56 type = WEIGHTED;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
57 outfile_w = argv[i+1];
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
58 i++;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
59 break;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
60 case 'r' :
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
61 do_renumber=true;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
62 break;
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
63 default:
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
64 usage(argv[0], "Unknown option\n");
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
65 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
66 } else {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
67 usage(argv[0], "More than one filename\n");
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
68 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
69 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
70 if (infile==NULL || outfile==NULL)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
71 usage(argv[0], "In or outfile missing\n");
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
72 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
73
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
74 int
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
75 main(int argc, char **argv) {
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
76 parse_args(argc, argv);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
77
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
78 Graph g(infile, type);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
79
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
80 g.clean(type);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
81
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
82 if (do_renumber)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
83 g.renumber(type);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
84
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
85 g.display_binary(outfile, outfile_w, type);
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
86
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
87 }