Revision c6961065

View differences:

custompackages/graph-parser/src/centrality.cpp
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
}
custompackages/graph-parser/src/centrality.h
1
//
2
// Created by quynh on 12/15/15.
3
//
4

  
5
#ifndef GRAPH_PARSER_CENTRALITY_H
6
#define GRAPH_PARSER_CENTRALITY_H
7

  
8
#include <iostream>
9
#include <fstream>
10
#include <boost/graph/betweenness_centrality.hpp>
11
#include "common.h"
12

  
13
void simpleBetweennessCentrality(Graph g, string fileSuffix);
14
void writeBetweennessCentrality(Graph &g, std::vector<double> v_centrality_vec, string fileSuffix);
15

  
16
#endif //GRAPH_PARSER_CENTRALITY_H
custompackages/graph-parser/src/common.h
5 5
#ifndef GRAPH_PARSER_COMMON_H
6 6
#define GRAPH_PARSER_COMMON_H
7 7

  
8
#include <string>
9
#include <array>
8
#include <boost/graph/adjacency_list.hpp>
9

  
10 10
using namespace std;
11 11

  
12 12
struct Router {
......
27 27
};
28 28

  
29 29
// List typedefs
30
typedef array<string, 3> graphDataType;
31 30
typedef boost::adjacency_list<boost::listS, boost::listS, boost::undirectedS,
32 31
        Router, Link> Graph;
33 32
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
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
custompackages/graph-parser/src/parser.cpp
70 70
}
71 71

  
72 72
void readJson(string filePath, Graph &g) {
73
    vector<graphDataType> contents;
74 73
    boost::property_tree::ptree pt;
75 74
    boost::property_tree::read_json(filePath, pt);
76 75

  
......
91 90
}
92 91

  
93 92
void readComplexJson(string filePath, Graph &g) {
94
    vector<graphDataType> contents;
95 93
    boost::property_tree::ptree pt;
96 94
    boost::property_tree::read_json(filePath, pt);
97 95

  
custompackages/graph-parser/src/parser.h
6 6
#define GRAPH_PARSER_PARSER_H
7 7

  
8 8
#include <iostream>
9
#include <fstream>
10
#include <vector>
11
#include <map>
12 9
#include <boost/algorithm/string.hpp>
13 10
#include <boost/property_tree/json_parser.hpp>
14
#include <boost/graph/undirected_graph.hpp>// A subclass to provide reasonable arguments to adjacency_list for a typical undirected graph
15 11
#include <boost/foreach.hpp>
16 12
#include "common.h"
17
using namespace std;
18 13

  
19 14
template<typename NameVertexMap>
20 15
void addLinkToGraph(string s1, string s2, double cost, Graph &g, NameVertexMap &routers);
......
24 19
void readComplexJson(string filePath, Graph &g);
25 20

  
26 21

  
27

  
28 22
#endif //GRAPH_PARSER_PARSER_H
29 23

  
30 24

  

Also available in: Unified diff