簡體   English   中英

boost bgl write_graphviz VertexPropertyWriter和EdgePropertyWriter

[英]boost bgl write_graphviz VertexPropertyWriter and EdgePropertyWriter

嗨,任何一個點跟隨代碼, make_edge_writer函數無法推斷gcc4.9中的類型。 我的代碼基於以下答案: 如何在graphviz中打印圖表,其中顯示了多個屬性

#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)
        )
    );
}

您有注釋中提到的語法問題,並且插入了具有相同未初始化源和ele所有邊緣(這應該是std::pair<int, int> ?)。

生成隨機圖...簡單的方法

如果要生成隨機圖,可以使用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

例如 在此輸入圖像描述

要么 在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM