You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Boost Graph Library中sequential_vertex_coloring算法自定义最大度顶点顺序的实现示例请求

Got it, let's walk through how to implement the Welsh-Powell ordering for Boost Graph Library's sequential vertex coloring. Here's a minimal, runnable example that shows you exactly how to create the required vertex order and pass it to the algorithm:

Step-by-Step Example Code

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/sequential_vertex_coloring.hpp>
#include <iostream>
#include <vector>
#include <algorithm>

// Define our graph type (using vector-based vertices/edges for simplicity)
using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS>;
using Vertex = typename boost::graph_traits<Graph>::vertex_descriptor;

int main() {
    // 1. Create a sample undirected graph (replace this with your own graph structure)
    Graph g(5);
    add_edge(0, 1, g);
    add_edge(0, 2, g);
    add_edge(1, 2, g);
    add_edge(1, 3, g);
    add_edge(2, 4, g);
    add_edge(3, 4, g);

    // 2. Collect all vertices from the graph
    std::vector<Vertex> vertices;
    auto [v_begin, v_end] = vertices(g);
    vertices.assign(v_begin, v_end);

    // 3. Sort vertices in DESCENDING order of degree (this is the Welsh-Powell ordering)
    std::sort(vertices.begin(), vertices.end(), [&g](Vertex a, Vertex b) {
        return boost::degree(a, g) > boost::degree(b, g);
    });

    // 4. Create a color map to store the resulting colors for each vertex
    std::vector<int> color_values(num_vertices(g));
    auto color_map = boost::make_iterator_property_map(
        color_values.begin(), 
        boost::get(boost::vertex_index, g)
    );

    // 5. Run sequential coloring with the custom Welsh-Powell order
    boost::sequential_vertex_coloring(g, vertices.begin(), vertices.end(), color_map);

    // 6. Print the results
    std::cout << "Vertex colors (using Welsh-Powell ordering):\n";
    for (std::size_t i = 0; i < num_vertices(g); ++i) {
        std::cout << "Vertex " << i << ": Color " << color_values[i] << "\n";
    }

    return 0;
}

Key Details Explained

  • Welsh-Powell Ordering: The core part is the std::sort call, where we compare vertices by their degree in descending order. This ensures we process the highest-degree vertices first, which is the defining feature of Welsh-Powell.
  • Passing the Order: The sequential_vertex_coloring function accepts an iterator range for the vertex order. We just pass the begin/end of our sorted vertices vector to use our custom order.
  • Color Map: We use a std::vector to store color values, wrapped with make_iterator_property_map to work with Boost's graph APIs. This is a standard pattern for property maps in BGL.

If you're using a different vertex container type (like listS instead of vecS), you'll need to ensure you have a valid vertex index map, but vecS (the default) works seamlessly here since vertices are 0-indexed.

内容的提问来源于stack exchange,提问作者Jake

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 20:03:11