仅将std :: vector属性的元素传递给BGL算法

时间:2010-10-29 16:41:09

标签: c++ boost boost-graph

我有一个多边缘权重的图表存储为

namespace boost {
    enum edge_weightvector_t {
        edge_weightvector = 1337
    };
    BOOST_INSTALL_PROPERTY(edge, weightvector);
}

typedef boost::adjacency_list<
    boost::vecS,
    boost::vecS,
    boost::undirectedS,
    boost::no_property,
    boost::property<boost::edge_weightvector_t, std::vector<int> >
> graph_t;

权重全部被推到矢量上。

现在我想在图表上调用prim_minimum_spanning_tree()函数,向量中的第一个元素用作加权。

如何执行正确的函数调用?

2 个答案:

答案 0 :(得分:0)

我现在已经做到了,首先将所需的权重复制到附加属性,然后运行算法并在之后复制。这很丑陋,但在我的情况下它可以解决问题。

答案 1 :(得分:0)

我最近尝试过这样做(使用矢量属性)并且只能使用其中一个值运行算法。但是,我发现使用exterior properties是一种很好的方法,不会导致不必要的复制操作,并将属性映射明确地传递给算法。

如果您使用随机访问容器,则可以使用boost::iterator_property_map来包装该容器并将其设为property_map。它不需要边缘描述符,而是需要基于0的边缘索引来实现边缘和属性值之间的有效映射。这是一个妙语,你进一步找到了完整的例子:

// ... 
EdgeIndexMap edgeIds = get(edge_index, g);
// ...
typedef std::vector<int> Weights;
typedef std::vector<Weights> WeightsVector;
typedef iterator_property_map <Weights::iterator, EdgeIndexMap> WeightMap;
// ...
Weights weights; // = ...
WeightMap wm(weights.begin(), edgeIds);
// ...
some_bgl_algorithm(g, wm);

这里有一个完整的例子:

  using namespace boost;

  void sampleExteriorProperties()
  {
     typedef adjacency_list<vecS, vecS, undirectedS,
                            no_property,
                            //property<edge_index_t, int, property<edge_weight_t, int> >
                            property<edge_index_t, std::size_t>
                            > Graph;
     typedef graph_traits<Graph>::edge_descriptor Edge;
     typedef graph_traits<Graph>::edge_iterator EdgeIterator;
     typedef property_map<Graph, edge_index_t>::type EdgeIndexMap;
     //typedef property_map<Graph, edge_weight_t>::type WeightMap;

     const int NVERTICES = 5;
     const int NEDGES = 8;

     Graph g(NVERTICES);

     // Add edges WITH indexes.
     int edgeIndex = 0;
     add_edge(0, 1, edgeIndex++, g);
     add_edge(0, 2, edgeIndex++, g);
     add_edge(0, 3, edgeIndex++, g);
     add_edge(1, 2, edgeIndex++, g);
     add_edge(1, 4, edgeIndex++, g);
     add_edge(2, 3, edgeIndex++, g);
     add_edge(2, 4, edgeIndex++, g);
     add_edge(3, 4, edgeIndex++, g);

     // Weights: there must be a weight for every edge.
     // Weights will be later on accessed by edge index.
     assert(num_edges(g) == NEDGES);
     typedef std::vector<int> Weights;
     typedef std::vector<Weights> WeightsVector;
     WeightsVector weightVector({ { 2, 3, 5, 7, 9, 11, 13, 17 },
                                  { 8, 7, 6, 5, 4, 3, 2, 1 }
                                });

     EdgeIndexMap edgeIds = get(edge_index, g);

     for (Weights &weights : weightVector)
     {
        // Use the iterator_property_map to read the properties from a
        // random access container. Remember: Edge ids are used to access
        // the correct value from the container!
        typedef iterator_property_map <Weights::iterator, EdgeIndexMap> WeightMap;
        WeightMap wm(weights.begin(), edgeIds);

        EdgeIterator eIt, eItEnd;
        tie(eIt, eItEnd) = edges(g);
        while (eIt!=eItEnd)
        {
           std::cout << *eIt << ": " << wm[*eIt] << " ";
           ++eIt;
        }
        std::cout << std::endl;

        // Explicitly pass the exterior map to the algorithm.
        std::vector<Edge> mstEdges;
        kruskal_minimum_spanning_tree(g, std::back_inserter(mstEdges),
                                      weight_map(wm));

        std::for_each(mstEdges.begin(), mstEdges.end(),
                      [](const Edge &val){std::cout << val << " ";});
        std::cout << std::endl;
     }

  }