Revision c6961065 custompackages/graph-parser/src/main.cpp

View differences:

custompackages/graph-parser/src/main.cpp
1
#include <fstream>
2 1
#include <iostream>
3

  
4

  
5
#include <boost/graph/betweenness_centrality.hpp>
6

  
7
#include <boost/tuple/tuple.hpp>
8

  
9 2
#include "parser.h"
10 3
#include "utility.h"
4
#include "centrality.h"
11 5

  
12
using namespace std;
13

  
14

  
15
void writeBetweennessCentrality(Graph &g, std::vector<double> v_centrality_vec, string fileSuffix) {
16
    cout << "XXX Writing to File";
17
    string filePath = "../output/boost_" + fileSuffix + ".csv";
18
    ofstream outFile(filePath);
19

  
20
    // Reading to vector<graphDataType>
21
    Viter vi, ve;
22
    size_t i = 0;
23
    if (outFile.is_open()) {
24
        for (boost::tie(vi, ve) = boost::vertices(g); vi != ve; ++vi) {
25
            outFile << g[*vi].id << ", " << v_centrality_vec.at(i) << endl;
26
            ++i;
27
        }
28
    }
29
    outFile.close();
30

  
31
    cout << "XXX Writing to File 2";
32
}
33

  
34

  
35
void simpleBetweennessCentrality(Graph g, string fileSuffix) {
36
    // One way to create centrality_map
37
    //    boost::shared_array_property_map<double, boost::property_map<Graph, vertex_index_t>::const_type>
38
    //            centrality_map(num_vertices(g), get(boost::vertex_index, g));
39

  
40

  
41
    // Define VertexCentralityMap
42
//    typedef boost::property_map< Graph, boost::vertex_index_t>::type VertexIndexMap;
43
//    VertexIndexMap v_index = get(boost::vertex_index, g);
44
//    // Constructs a container with n elements. Each element is a copy of val.
45
//    std::vector< double > v_centrality_vec(boost::num_vertices(g), 0.0);
46
//    // Create the external property map
47
//    boost::iterator_property_map< std::vector< double >::iterator, VertexIndexMap >
48
//            v_centrality_map(v_centrality_vec.begin(), v_index);
49
//
50
//    brandes_betweenness_centrality( g, v_centrality_map);
51

  
52

  
53
    // Nov 20, 2015
54
    // http://stackoverflow.com/questions/15432104/how-to-create-a-propertymap-for-a-boost-graph-using-lists-as-vertex-container
55
    typedef std::map<Vertex, size_t> StdVertexIndexMap;
56
    StdVertexIndexMap idxMap;
57

  
58
    // 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.
59
    typedef boost::associative_property_map<StdVertexIndexMap> VertexIndexMap;
60
    VertexIndexMap v_index(idxMap);
61

  
62
    // Populate the indexMap
63
    Viter v_iter, v_iter_end;
64
    size_t i = 0;
65
    for (boost::tie(v_iter, v_iter_end) = boost::vertices(g); v_iter != v_iter_end; ++v_iter) {
66
        boost::put(v_index, *v_iter, i);
67
        ++i;
68
    }
69

  
70
    typedef std::vector<double> CentralityVec;
71
    CentralityVec v_centrality_vec(boost::num_vertices(g), 0);
72

  
73
    typedef boost::iterator_property_map<CentralityVec::iterator, VertexIndexMap> CentralityMap;
74
    CentralityMap v_centrality_map(v_centrality_vec.begin(), v_index);
75

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

  
79
    // http://stackoverflow.com/questions/30263594/adding-a-vertex-index-to-lists-graph-on-the-fly-for-betweenness-centrality
80
    // Use named-parameter
81
    brandes_betweenness_centrality(g, boost::centrality_map(v_centrality_map).vertex_index_map(v_index));
82
    relative_betweenness_centrality(g, v_centrality_map);
83

  
84

  
85
    // Print result of v_centrality_map to console
86
    cout << "Vertex betweenness" << endl;
87
    i = 0;
88
    for (boost::tie(v_iter, v_iter_end) = boost::vertices(g); v_iter != v_iter_end; ++v_iter) {
89
        cout << g[*v_iter].id << "\t" << v_centrality_vec.at(i) << endl;
90
        ++i;
91
    }
92

  
93
    // Write result of v_centrality_map to file.
94
    writeBetweennessCentrality(g, v_centrality_vec, fileSuffix);
95

  
96
}
97 6

  
98 7
void handleSimpleInput(string filePath) {
99 8
    // Read the input.edges

Also available in: Unified diff