【问题标题】:boost bgl write_graphviz VertexPropertyWriter and EdgePropertyWriterboost bgl write_graphviz VertexPropertyWriter 和 EdgePropertyWriter
【发布时间】:2024-04-20 17:25:01
【问题描述】:

您好,以下代码中的任何一处,make_edge_writer 函数无法推断 gcc4.9 中的类型。我的代码基于此处找到的以下答案How to print a graph in graphviz with multiple properties displayed

#include <boost/graph/graphviz.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <string>
#include <tuple>
#include <iostream>
#include <iomanip>
#include <map>
#include <random>
#include <cmath>
#include <fstream>
#include <utility>
#include <boost/graph/iteration_macros.hpp>
#include <boost/graph/graph_utility.hpp>
using namespace boost;

struct vert{
    std::string name;
};

struct edge{
    int capacity;
    int weight;
}; 

template <class WeightMap,class CapacityMap>
class edge_writer
{
    public:
    edge_writer(WeightMap w, CapacityMap c) : wm(w),cm(c) {}

    template <class Edge>
    void operator()(ostream &out, const Edge& e) const {
        out << "[label=\"" << wm[e] << "\", taillabel=\"" << cm[e] << "\"]";
    }
    private:
    WeightMap wm;
    CapacityMap cm;
};

template <class WeightMap, class CapacityMap>
inline edge_writer<WeightMap,CapacityMap> 
make_edge_writer(WeightMap w,CapacityMap c)
{
    return edge_writer<WeightMap,CapacityMap>(w,c);
}

int main(int a,char **b)
{
    typedef adjacency_list<listS, vecS, undirectedS, vert, edge> Graph;
    Graph g;
    vector<int,int> ele;
    edge prop;
    prop.weight = 5;
    prop.capacity = 4;
    add_edge(ele.first,ele.second, prop, g);
    std::random_device rd;
// Choose a random mean between 1 and 100
    std::default_random_engine e1(rd());
    std::uniform_int_distribution<int> uniform_dist(1, 100);
    for (int a=0;a<20;++a){
        edge prop1;
        prop1.weight = uniform_dist(e1);
        prop1.capacity = uniform_dist(e1); 
        add_edge(ele.first,ele.second, prop1, g);
    }
    std::ofstream dot("graph.dot");
    write_graphviz(
        dot,
        g,
        boost::make_label_writer(
            boost::get(&vert::name, g)
        ),
        make_edge_writer(
            boost::get(&edge::weight,g),
            boost::get(&edge::capacity,g)
        )
    );
}

【问题讨论】:

  • 您可以删除using namespace boost(在必要时添加boost::)或重命名edge 结构(并更改使用它的每个实例)。 vector&lt;int,int&gt; 没有意义,应该是vector&lt;pair&lt;int,int&gt; &gt;

标签: c++ boost graphviz boost-graph


【解决方案1】:

您遇到了评论中提到的语法问题,并且正在从ele(应该是std::pair&lt;int, int&gt;?)插入具有相同未初始化源和目标的所有边。

生成随机图...简单的方法

如果您想生成随机图,可以使用boost::generate_random_graph

int main() {
    typedef b::adjacency_list<b::listS, b::vecS, b::undirectedS, vert, edge> Graph;
    Graph g;

    b::generate_random_graph(g, 10 /*100*/, 5 /*20*/, rng);

    std::ofstream dot("graph.dot");
    write_graphviz(dot, g, boost::make_label_writer(boost::get(&vert::name, g)),
                make_edge_writer(boost::get(&edge::weight, g), boost::get(&edge::capacity, g)));
}

为简单起见,我使用类内初始化器来生成随机权重/容量:

struct edge {
    int capacity = uniform_dist(rng);
    int weight   = uniform_dist(rng);
};

Live On Coliru

例如

【讨论】: