Statistics
| Branch: | Revision:

root / custompackages / graph-parser / src / centrality.cpp @ ee0dd796

History | View | Annotate | Download (3.07 KB)

1 c6961065 Quynh PX Nguyen
//
2
// Created by quynh on 12/15/15.
3
//
4
5
#include "centrality.h"
6
7 946f4f73 Quynh PX Nguyen
void simpleBetweennessCentrality(const Graph &g, string fileSuffix) {
8 c6961065 Quynh PX Nguyen
    // One way to create centrality_map
9
    //    boost::shared_array_property_map<double, boost::property_map<Graph, vertex_index_t>::const_type>
10
    //            centrality_map(num_vertices(g), get(boost::vertex_index, g));
11
12
13
    // Define VertexCentralityMap
14
//    typedef boost::property_map< Graph, boost::vertex_index_t>::type VertexIndexMap;
15
//    VertexIndexMap v_index = get(boost::vertex_index, g);
16
//    // Constructs a container with n elements. Each element is a copy of val.
17
//    std::vector< double > v_centrality_vec(boost::num_vertices(g), 0.0);
18
//    // Create the external property map
19
//    boost::iterator_property_map< std::vector< double >::iterator, VertexIndexMap >
20
//            v_centrality_map(v_centrality_vec.begin(), v_index);
21
//
22
//    brandes_betweenness_centrality( g, v_centrality_map);
23
24
25
    // Nov 20, 2015
26
    // http://stackoverflow.com/questions/15432104/how-to-create-a-propertymap-for-a-boost-graph-using-lists-as-vertex-container
27
28 ee0dd796 Quynh PX Nguyen
    StdVertexIndexMap v_index_std_map;
29
    VertexIndexMap v_index_map(v_index_std_map);
30 c6961065 Quynh PX Nguyen
31
    // Populate the indexMap
32 ee0dd796 Quynh PX Nguyen
    Viter vi, ve; // vertex_iterator, vertex_iterator_end
33 c6961065 Quynh PX Nguyen
    size_t i = 0;
34 ee0dd796 Quynh PX Nguyen
    for (boost::tie(vi, ve) = boost::vertices(g); vi != ve; ++vi) {
35
        boost::put(v_index_map, *vi, i);
36 c6961065 Quynh PX Nguyen
        ++i;
37
    }
38
39
    typedef std::vector<double> CentralityVec;
40
    CentralityVec v_centrality_vec(boost::num_vertices(g), 0);
41
42
    typedef boost::iterator_property_map<CentralityVec::iterator, VertexIndexMap> CentralityMap;
43 ee0dd796 Quynh PX Nguyen
    CentralityMap v_centrality_map(v_centrality_vec.begin(), v_index_map);
44 c6961065 Quynh PX Nguyen
45
    // Nov 26, try out the normal call to centrality().This version is not working.
46
//    brandes_betweenness_centrality( g, boost::centrality_map(v_centrality_map));
47
48
    // http://stackoverflow.com/questions/30263594/adding-a-vertex-index-to-lists-graph-on-the-fly-for-betweenness-centrality
49
    // Use named-parameter
50 ee0dd796 Quynh PX Nguyen
    brandes_betweenness_centrality(g, boost::centrality_map(v_centrality_map).vertex_index_map(v_index_map));
51 c6961065 Quynh PX Nguyen
    relative_betweenness_centrality(g, v_centrality_map);
52
53
54
    // Print result of v_centrality_map to console
55
    cout << "Vertex betweenness" << endl;
56
    i = 0;
57 ee0dd796 Quynh PX Nguyen
    for (boost::tie(vi, ve) = boost::vertices(g); vi != ve; ++vi) {
58
        cout << g[*vi].id << "\t" << v_centrality_vec.at(i) << endl;
59 c6961065 Quynh PX Nguyen
        ++i;
60
    }
61
62
    // Write result of v_centrality_map to file.
63
    writeBetweennessCentrality(g, v_centrality_vec, fileSuffix);
64
65
}
66
67
68 946f4f73 Quynh PX Nguyen
void writeBetweennessCentrality(const Graph &g, std::vector<double> v_centrality_vec, string fileSuffix) {
69 c6961065 Quynh PX Nguyen
    cout << "XXX Writing to File";
70
    string filePath = "../output/boost_" + fileSuffix + ".csv";
71 ee0dd796 Quynh PX Nguyen
    ofstream outFile(filePath.c_str());
72 c6961065 Quynh PX Nguyen
73
    Viter vi, ve;
74
    size_t i = 0;
75
    if (outFile.is_open()) {
76
        for (boost::tie(vi, ve) = boost::vertices(g); vi != ve; ++vi) {
77
            outFile << g[*vi].id << ", " << v_centrality_vec.at(i) << endl;
78
            ++i;
79
        }
80
    }
81
    outFile.close();
82
83
    cout << "XXX Writing to File 2";
84 ee0dd796 Quynh PX Nguyen
}