-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_hierarchy.cpp
executable file
·116 lines (102 loc) · 3.42 KB
/
main_hierarchy.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
// File: main_hierarchy.cpp
// -- output community structure handling (number of levels, communities of one level)
//-----------------------------------------------------------------------------
// 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 <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
int display_level = -1;
char *filename = NULL;
void
usage(char *prog_name, char *more) {
cerr << more;
cerr << "usage: " << prog_name << " input_file [options]" << endl << endl;
cerr << "input_file: read the community tree from this file." << endl;
cerr << "-l xx\t display the community structure for the level xx." << endl;
cerr << "\t outputs the community for each node." << endl;
cerr << "\t xx must belong to [-1,N] if N is the number of levels." << endl;
cerr << "-n\t displays the number of levels and the size of each level." << endl;
cerr << "\t equivalent to -l -1." << endl;
cerr << "-h\tshow this usage message." << endl;
exit(0);
}
void
parse_args(int argc, char **argv) {
if (argc<2)
usage(argv[0], "Bad arguments number\n");
for (int i = 1; i < argc; i++) {
if(argv[i][0] == '-') {
switch(argv[i][1]) {
case 'l':
display_level = atoi(argv[i+1]);
i++;
break;
case 'n':
display_level = -1;
break;
default:
usage(argv[0], "Unknown option\n");
}
} else {
if (filename==NULL)
filename = argv[i];
else
usage(argv[0], "More than one filename\n");
}
}
if (filename==NULL)
usage(argv[0], "No input file has been provided.\n");
}
int
main(int argc, char **argv) {
parse_args(argc, argv);
vector<vector<int> >levels;
ifstream finput;
finput.open(filename,fstream::in);
int l=-1;
while (!finput.eof()) {
int node, nodecomm;
finput >> node >> nodecomm;
if (finput) {
if (node==0) {
l++;
levels.resize(l+1);
}
levels[l].push_back(nodecomm);
}
}
if (display_level==-1) {
cout << "Number of levels: " << levels.size() << endl;
for (unsigned int i=0 ; i<levels.size();i++)
cout << "level " << i << ": " << levels[i].size() << " nodes" << endl;
} else if (display_level<0 || (unsigned)display_level>=levels.size()) {
cerr << "Incorrect level\n";
} else {
vector<int> n2c(levels[0].size());
for (unsigned int i=0 ; i<levels[0].size() ; i++)
n2c[i]=i;
for (l=0 ; l<display_level ; l++)
for (unsigned int node=0 ; node<levels[0].size() ; node++)
n2c[node] = levels[l][n2c[node]];
for (unsigned int node=0 ; node<levels[0].size() ; node++)
cout << node << " " << n2c[node] << endl;
}
}