Statistics
| Branch: | Revision:

root / custompackages / graph-parser / src / count_bcc.cpp @ 6575aa2e

History | View | Annotate | Download (3.31 KB)

1
/*
2
    Output the number of nodes for each biconnected.
3
*/
4
#include <iostream>
5
#include <vector>
6
#include <dirent.h>
7
#include "utility.h"
8
#include "bi_connected_components.h"
9
#include "graph_manager.h"
10

    
11
using namespace std;
12

    
13
void get_all_files_in_directory(string dir_path, vector<string>& files, string with_extension) {
14
    DIR*    dir;
15
    dirent* pdir;
16

    
17
    dir = opendir(dir_path.c_str());
18

    
19
    while (pdir = readdir(dir)) {
20
        string filename = pdir->d_name;
21
        string::size_type idx;
22
        idx = filename.find('.');
23
        string extension = "";
24
        if (idx != string::npos) {
25
            extension = filename.substr(idx + 1);
26
        }
27

    
28
        if (extension.compare(with_extension) == 0) {
29
            files.push_back(filename);
30
            cout << filename << endl;
31
        }
32
    }
33
}
34

    
35
void count_bcc(string input_filepath, vector<int>& number_of_nodes_in_bcc, bool is_weighted_graph) {
36
    streambuf* orig_buf = cout.rdbuf(); // get underlying buffer
37
    cout.rdbuf(0); // set null
38

    
39
    GraphManager gm(is_weighted_graph);
40
    readEdgeFileGraphManager(input_filepath, gm);
41
    BiConnectedComponents bcc = BiConnectedComponents(gm);
42
    bcc.FindBiConnectedComponents();
43

    
44
    // Counting number of nodes in each bi-connected component
45
    for (int i = 0; i < bcc.num_of_bcc(); ++i) {
46
        int num_of_vertices = bcc.BCCs[i].num_of_vertices();
47
        number_of_nodes_in_bcc.push_back(num_of_vertices);
48
    }
49
    cout.rdbuf(orig_buf);
50
}
51

    
52
void write_counting_result(string output_filepath, string filename, const vector<int>& number_of_nodes_in_bcc) {
53
    ofstream out_file(output_filepath.c_str(), std::ofstream::out | std::ofstream::app);
54

    
55
    // Extract information from file name
56
    string graph_type = filename.substr(0, 2);
57

    
58
    string::size_type sep_pos = filename.find('_');
59
    string number_of_nodes = filename.substr(2, sep_pos - 2);
60

    
61
    string topo_type = graph_type + number_of_nodes;
62

    
63
    // Number of bi-connected components
64
    int num_bccs = number_of_nodes_in_bcc.size();
65

    
66
    // String containing the number of nodes
67
    std::stringstream result;
68
    std::copy(number_of_nodes_in_bcc.begin(), number_of_nodes_in_bcc.end(), std::ostream_iterator<int>(result, " "));
69
    string result_str = result.str();
70

    
71
    string separator = ", ";
72

    
73
    if (out_file.is_open()) {
74
        out_file << filename << separator;
75
        out_file << topo_type << separator;
76
        out_file << num_bccs << separator;
77
        out_file << result_str << endl;
78
    }
79
    out_file.close();
80
}
81

    
82
int main(int argc, char * argv[]) {
83
    if (argc < 2) {
84
        cout << "Provide the input directory\n";
85
        exit(1);
86
    }
87

    
88
    string input_dir = string(argv[1]);
89

    
90
    string output_filepath = "../output/counting_bcc.out";
91
    if (argc >= 3) {
92
        output_filepath = string(argv[2]);
93
    }
94

    
95
    bool is_weighted_graph = false;
96
    if (argc >= 4) {
97
        if (string(argv[3]).compare("false") != 0) {
98
            is_weighted_graph = true;
99
        }
100
    }
101

    
102

    
103
    // Find all the files
104
    vector<string> files;
105
    get_all_files_in_directory(input_dir, files, "edges");
106

    
107
    for (string file : files) {
108
        string input_filepath = input_dir + "/" + file;
109
        vector<int> number_of_nodes_in_bcc;
110
        count_bcc(input_filepath, number_of_nodes_in_bcc, is_weighted_graph);
111

    
112
        write_counting_result(output_filepath, file, number_of_nodes_in_bcc);
113
    }
114
}