Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

c++ - How to calculate betweenness using boostlib for adjacency list?

I'm trying to write a simple program to calculate betweeness using brandes_betweenness_centrality from boostlib. I got stuck at getting an output (CentralityMap). I've been reading the documentation but I can't figure out how to put it all together.

Here is my simple code:

#include <iostream> // std::cout
#include <utility>  // std::pair
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/betweenness_centrality.hpp>

using namespace boost;

int main()
{
  int nVertices = 100;
  srand ( time(NULL) );

  typedef std::pair<int, int> Edge;
  std::vector<Edge> edges;
  for(int i=0; i<nVertices; i++){
    std::cout << i << " :  ";
    for(int j=0; j<nVertices; j++){
      if(rand() % 100 < 9){ /// chances of making a connection is 9 out of 100. may not be accurate
    std::cout << j << "  ";
        edges.push_back(std::make_pair(i,j));
      }
    }
    std::cout << std::endl;
  }

  typedef adjacency_list<vecS, vecS, bidirectionalS, 
    property<vertex_color_t, default_color_type>
  > Graph;
  Graph g(edges.begin(), edges.end(), edges.size());

  brandes_betweenness_centrality(g,?????? );

  return 0;
}

From my understanding, I need define centrality map where the result will be written. It's related to read/write property map but I can't figure out how to define one.

Eventually I need to output betweenness.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The simplest way to fill in the missing parts is:

boost::shared_array_property_map<double, boost::property_map<Graph, vertex_index_t>::const_type>
  centrality_map(num_vertices(g), get(boost::vertex_index, g));

then pass centrality_map as the centrality map to brandes_betweenness_centrality.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...