Ticket #13474: Boost Betweennness Centrality .cpp

File Boost Betweennness Centrality .cpp, 3.4 KB (added by abertzouanis@…, 5 years ago)

Calculating Betweenness Centrality

Line 
1#include <string>
2#include <iostream>
3#include <fstream>
4#include <sstream>
5
6#include <boost/graph/adjacency_list.hpp>
7#include <boost/graph/graphviz.hpp>
8
9#include <boost/graph/bc_clustering.hpp>
10#include <boost/graph/iteration_macros.hpp>
11
12
13// Graph edge properties (bundled properties)
14struct EdgeProperties
15{
16 int weight;
17};
18
19typedef boost::property<boost::edge_weight_t, float> EdgeWeightProperty;
20
21typedef boost::adjacency_list< boost::setS, boost::vecS, boost::undirectedS, boost::no_property, EdgeWeightProperty > Graph;
22
23typedef Graph::vertex_descriptor Vertex;
24typedef Graph::edge_descriptor Edge;
25
26void WriteGraph(const Graph& g, const std::string& filename);
27
28int addition(int a, int b);
29int betweenness(int*, int*, int);
30
31int main()//std::vector<int> arr)
32{
33 //provide array of connections from graph
34 //from https://www.google.co.uk/search?q=between+centrality+calculation&client=firefox-b-ab&dcr=0&source=lnms&tbm=isch&sa=X&ved=0ahUKEwjeg8W7tdvZAhXMBZoKHbEnCjkQ_AUICigB&biw=1376&bih=692#imgrc=H9wvPMTEbg6bKM:
35 int arr [] = { 1,2, 1,3, 2,3, 3,4, 3,5, 2,5, 5,7, 5,6 };
36 //weights of the above connections
37 int arr_weights[] = { 10,10,10,10,10,1,10,10};
38 int size_nodes = 8;
39 int a;
40 int out = 1;
41 a = betweenness(arr, arr_weights, size_nodes);//works
42
43 return 0;
44}
45
46int findMax(int array[], int arraySize) {
47 int maxPosition = 0; //assume the first element is maximum
48 for (int i = 1; i < arraySize; i++)
49 if (array[i] > array[maxPosition]) //compare the current element with the known max
50 maxPosition = i; //update maxPosition
51 return array[maxPosition];
52}
53
54int betweenness(int *param, int *weights, int size_nodes)
55{
56
57 int ii = boost::lexical_cast<int>("1");
58 double max_centrality = 14;
59
60 //// Create a graph
61 Graph g;
62 int paramSize = size_nodes;
63
64 static int Vert[50000000];
65
66 int biggest = findMax(param, 16);
67
68 //create the vetrices
69 for (int a1=0; a1 < biggest+1; a1++)
70 {
71 Vert[a1] = boost::add_vertex(g);
72 }
73
74 //create the edges (with the given weights included)
75 for (int a2=0; a2 < paramSize; a2++)
76 {
77 //EdgeWeightProperty weight0 = 5;
78 boost::add_edge(Vert[param[a2*2]],Vert[param[a2*2+1]], EdgeWeightProperty(weights[a2]), g);
79 }
80
81
82 // Define VertexCentralityMap
83 typedef boost::property_map< Graph, boost::vertex_index_t>::type VertexIndexMap;
84
85
86
87 VertexIndexMap v_index = get(boost::vertex_index, g);
88 std::vector< double > v_centrality_vec(boost::num_vertices(g), 0.0);
89
90 // Create the external property map
91 boost::iterator_property_map< std::vector< double >::iterator, VertexIndexMap >
92 v_centrality_map(v_centrality_vec.begin(), v_index);
93
94
95
96
97 // Write to graphviz -> illustrate the graph via 'neato -Tps before.dot > before.ps'
98 WriteGraph(g, "before.dot");
99
100 // Calculate the vertex and edge centralites
101 // Can be used to get an initial impression about the edge centrality values for the graph
102 //brandes_betweenness_centrality( g, v_centrality_map, e_centrality_map );
103 brandes_betweenness_centrality(g, v_centrality_map);
104
105
106 //BGL_FORALL_VERTICES(vertex, g, Graph)
107 for (int vertex=1;vertex<size_nodes;vertex++)
108 {
109 std::cout << vertex << ": " << v_centrality_map[vertex] << std::endl;
110 }
111
112 WriteGraph(g, "after.dot");
113
114 return 0;
115}
116
117void WriteGraph(const Graph& g, const std::string& filename)
118{
119 std::ofstream graphStream;
120 graphStream.open(filename.c_str());
121 boost::write_graphviz(graphStream, g);
122 graphStream.close();
123}