Core Numbers
Computes the k-core decomposition of a graph. Each vertex is assigned a core number: the largest k such that the vertex belongs to a subgraph where every vertex has degree >= k. Higher core numbers indicate denser, more tightly connected substructures.
Complexity: O(V + E)
Defined in: <boost/graph/core_numbers.hpp>
Example
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/core_numbers.hpp>
#include <iostream>
#include <vector>
// Bundled vertex property
struct VertexProps {
std::string label;
};
int main() {
// Undirected graph with bundled vertex properties
using Graph = boost::adjacency_list<
boost::vecS, boost::vecS, boost::undirectedS, VertexProps>;
using Vertex = boost::graph_traits<Graph>::vertex_descriptor;
using SizeType = boost::graph_traits<Graph>::vertices_size_type;
Graph g(7);
g[0].label = "a";
g[1].label = "b";
g[2].label = "c";
g[3].label = "d";
g[4].label = "e";
g[5].label = "f";
g[6].label = "g";
// Dense 4-clique: a-b-c-d (vertices 0,1,2,3)
add_edge(0, 1, g);
add_edge(0, 2, g);
add_edge(0, 3, g);
add_edge(1, 2, g);
add_edge(1, 3, g);
add_edge(2, 3, g);
// Peripheral vertices attached loosely
add_edge(3, 4, g); // d -- e
add_edge(4, 5, g); // e -- f
add_edge(5, 6, g); // f -- g
// Compute core numbers into a vector property map
std::vector<SizeType> core_nums(boost::num_vertices(g));
auto core_map = boost::make_iterator_property_map(
core_nums.begin(), boost::get(boost::vertex_index, g));
boost::core_numbers(g, core_map);
std::cout << "Core numbers (k-core decomposition):\n";
for (Vertex v = 0; v < boost::num_vertices(g); ++v) {
std::cout << " vertex " << v
<< " (" << g[v].label << "): "
<< core_nums[v] << "\n";
}
}
Core numbers (k-core decomposition):
vertex 0 (a): 3
vertex 1 (b): 3
vertex 2 (c): 3
vertex 3 (d): 3
vertex 4 (e): 1
vertex 5 (f): 1
vertex 6 (g): 1
Vertices a-d form a 4-clique (K4), so each has core number 3 (every vertex in the clique has degree 3 within it). Vertices e-f-g form a chain attached to the clique, so they have core number 1.
(1) core_numbers (unweighted, default visitor)
template <typename Graph, typename CoreMap>
typename property_traits<CoreMap>::value_type
core_numbers(Graph& g, CoreMap c);
(2) core_numbers (unweighted, custom visitor)
template <typename Graph, typename CoreMap, typename CoreNumVisitor>
typename property_traits<CoreMap>::value_type
core_numbers(Graph& g, CoreMap c, CoreNumVisitor vis);
(3) core_numbers (weighted)
template <typename Graph, typename CoreMap, typename EdgeWeightMap,
typename VertexIndexMap, typename CoreNumVisitor>
typename property_traits<CoreMap>::value_type
core_numbers(Graph& g, CoreMap c,
EdgeWeightMap wm, VertexIndexMap vim, CoreNumVisitor vis);
When edge weights are provided, the core number of a vertex is based on the weighted degree (sum of incident edge weights) rather than the unweighted degree.
(4) weighted_core_numbers (default visitor)
template <typename Graph, typename CoreMap>
typename property_traits<CoreMap>::value_type
weighted_core_numbers(Graph& g, CoreMap c);
Convenience wrapper over (3) that uses get(edge_weight, g) as the weight map, get(vertex_index, g) as the index map, and a null_visitor.
(5) weighted_core_numbers (custom visitor)
template <typename Graph, typename CoreMap, typename CoreNumVisitor>
typename property_traits<CoreMap>::value_type
weighted_core_numbers(Graph& g, CoreMap c, CoreNumVisitor vis);
Same as (4) with an explicit visitor.
(6) core_numbers_visitor
template <class Visitors = null_visitor>
class core_numbers_visitor : public bfs_visitor<Visitors>;
template <class Visitors>
core_numbers_visitor<Visitors> make_core_numbers_visitor(Visitors vis);
The visitor type used internally. Model of CoreNumbersVisitorConcept. Derives from bfs_visitor, adding the examine_vertex(v, g) event each time a vertex is removed during k-core stripping. Use make_core_numbers_visitor(…) to construct one from an adapted visitor.
Parameters
| Direction | Parameter | Description |
|---|---|---|
IN |
|
Must model Incidence Graph and Vertex List Graph. For the weighted overloads, must also model Edge List Graph. |
OUT |
|
ReadWritePropertyMap. Vertex descriptor → core number. |
IN |
|
(weighted) ReadablePropertyMap from edge descriptor to weight. |
IN |
|
(weighted) ReadablePropertyMap from vertex to integer in |
IN |
|
Model of |
The function returns the largest core number assigned.