2
0
mirror of https://github.com/boostorg/graph.git synced 2026-01-19 04:12:11 +00:00
Files
graph/example/degree_centrality.cpp
2025-07-20 17:51:15 +02:00

67 lines
1.9 KiB
C++

// (C) Copyright Andrew Sutton 2007
//
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0 (See accompanying file
// LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
//[degree_centrality_example
#include <iostream>
#include <iomanip>
#include <boost/graph/undirected_graph.hpp>
#include <boost/graph/exterior_property.hpp>
#include <boost/graph/degree_centrality.hpp>
#include "helper.hpp"
using namespace std;
using namespace boost;
// The Actor type stores the name of each vertex in the graph.
struct Actor
{
string name;
};
// Declare the graph type and its vertex and edge types.
using Graph = undirected_graph< Actor >;
using Vertex = graph_traits< Graph >::vertex_descriptor;
using Edge = graph_traits< Graph >::edge_descriptor;
// The name map provides an abstract accessor for the names of
// each vertex. This is used during graph creation.
using NameMap = property_map< Graph, string Actor::* >::type;
// Declare a container type for degree centralities and its
// corresponding property map.
using CentralityProperty = exterior_vertex_property< Graph, unsigned >;
using CentralityContainer = CentralityProperty::container_type;
using CentralityMap = CentralityProperty::map_type;
int main(int argc, char* argv[])
{
// Create the graph and a property map that provides access
// to the actor names.
Graph g;
NameMap nm(get(&Actor::name, g));
// Read the graph from standard input.
read_graph(g, nm, cin);
// Compute the degree centrality for graph.
CentralityContainer cents(num_vertices(g));
CentralityMap cm(cents, g);
all_degree_centralities(g, cm);
// Print the degree centrality of each vertex.
graph_traits< Graph >::vertex_iterator i, end;
for (boost::tie(i, end) = vertices(g); i != end; ++i)
{
cout << setiosflags(ios::left) << setw(12) << g[*i].name << cm[*i]
<< endl;
}
return 0;
}
//]