【问题标题】:Create edge descriptor without graph创建没有图的边描述符
【发布时间】:2015-12-03 15:44:53
【问题描述】:

在我的项目中,我使用一个文件来存储所有边;另一个存储边缘概率。我想将Boost 库用于图形,unordered_map 用于概率。我有以下代码。

typedef boost::adjacency_list <boost::vecS, boost::vecS, boost::bidirectionalS> DiGraph;
typedef boost::graph_traits<SubGraph>::vertex_descriptor vertex_t;
typedef boost::graph_traits<SubGraph>::edge_descriptor edge_t;

unordered_map<edge_t, double> P;

ifstream infile(prob_filename);
double p;
int u, v;
while (infile >> u >> v >> p) {
    P[make_pair(u, v)] = p;
}

但是,我不需要将一对作为键,而是将边缘描述符 edge_t。如何使用给定的两个值 uv 创建边缘描述符。

【问题讨论】:

    标签: c++ boost


    【解决方案1】:

    From boost graph concepts

    DirectedGraph digraph(V);
      {
        boost::graph_traits<DirectedGraph>::vertex_descriptor u, v;
        u = vertex(0, digraph); // read these instead 0,1 from from your file
        v = vertex(1, digraph);
        // populate graph
        add_edge(digraph, u, v, Weight(1.2)); // read weight (prob) instead from from your file
    
        boost::graph_traits<DirectedGraph>::edge_descriptor e;
        bool found;
        boost::tie(e, found) = edge(u, v, digraph);
    
        //use property map
        property_map<DirectedGraph, edge_weight_t>::type
          weight = get(edge_weight, digraph);
    
        cout << "weight[(u,v)] = " << get(weight, e) << endl;
    
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-24
      • 2012-03-27
      • 2022-06-15
      • 1970-01-01
      相关资源
      最近更新 更多