Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (3.34 KB)

1
//
2
// Created by quynh on 12/15/15.
3
//
4

    
5
#include "centrality.h"
6

    
7
void simpleBetweennessCentrality(Graph g, string fileSuffix) {
8
    // 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
    typedef std::map<Vertex, size_t> StdVertexIndexMap;
28
    StdVertexIndexMap idxMap;
29

    
30
    // This property map is an adaptor that converts any type that is a model of Unique Associative Container such as std::map into a mutable Lvalue Property Map.
31
    typedef boost::associative_property_map<StdVertexIndexMap> VertexIndexMap;
32
    VertexIndexMap v_index(idxMap);
33

    
34
    // Populate the indexMap
35
    Viter v_iter, v_iter_end;
36
    size_t i = 0;
37
    for (boost::tie(v_iter, v_iter_end) = boost::vertices(g); v_iter != v_iter_end; ++v_iter) {
38
        boost::put(v_index, *v_iter, i);
39
        ++i;
40
    }
41

    
42
    typedef std::vector<double> CentralityVec;
43
    CentralityVec v_centrality_vec(boost::num_vertices(g), 0);
44

    
45
    typedef boost::iterator_property_map<CentralityVec::iterator, VertexIndexMap> CentralityMap;
46
    CentralityMap v_centrality_map(v_centrality_vec.begin(), v_index);
47

    
48
    // Nov 26, try out the normal call to centrality().This version is not working.
49
//    brandes_betweenness_centrality( g, boost::centrality_map(v_centrality_map));
50

    
51
    // http://stackoverflow.com/questions/30263594/adding-a-vertex-index-to-lists-graph-on-the-fly-for-betweenness-centrality
52
    // Use named-parameter
53
    brandes_betweenness_centrality(g, boost::centrality_map(v_centrality_map).vertex_index_map(v_index));
54
    relative_betweenness_centrality(g, v_centrality_map);
55

    
56

    
57
    // Print result of v_centrality_map to console
58
    cout << "Vertex betweenness" << endl;
59
    i = 0;
60
    for (boost::tie(v_iter, v_iter_end) = boost::vertices(g); v_iter != v_iter_end; ++v_iter) {
61
        cout << g[*v_iter].id << "\t" << v_centrality_vec.at(i) << endl;
62
        ++i;
63
    }
64

    
65
    // Write result of v_centrality_map to file.
66
    writeBetweennessCentrality(g, v_centrality_vec, fileSuffix);
67

    
68
}
69

    
70

    
71
void writeBetweennessCentrality(Graph &g, std::vector<double> v_centrality_vec, string fileSuffix) {
72
    cout << "XXX Writing to File";
73
    string filePath = "../output/boost_" + fileSuffix + ".csv";
74
    ofstream outFile(filePath);
75

    
76
    Viter vi, ve;
77
    size_t i = 0;
78
    if (outFile.is_open()) {
79
        for (boost::tie(vi, ve) = boost::vertices(g); vi != ve; ++vi) {
80
            outFile << g[*vi].id << ", " << v_centrality_vec.at(i) << endl;
81
            ++i;
82
        }
83
    }
84
    outFile.close();
85

    
86
    cout << "XXX Writing to File 2";
87
}