【问题标题】:How to add custom properties to the edges of a grid_graph in Boost Graph Library?如何在 Boost Graph Library 中向 grid_graph 的边缘添加自定义属性?
【发布时间】:2017-05-06 17:50:01
【问题描述】:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/chrono.hpp>
#include <algorithm>
#include <ctime>
#include <boost/graph/graphviz.hpp>
#include <boost/graph/grid_graph.hpp>
#include <boost/array.hpp>
#include <boost/graph/grid_graph.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/test/unit_test.hpp>
#include <iostream>                  // for std::cout
#include <utility>                   // for std::pair
#include <algorithm>                 // for std::for_each
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/config.hpp>
#include <iostream>
#include <fstream>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/graphviz.hpp>

using namespace std;
using namespace boost;
using namespace boost::gregorian;


#define DIMENSIONS 2

typedef grid_graph<2> Graph;
typedef graph_traits<Graph> Traits;

// Define a simple function to print vertices
void print_vertex(Traits::vertex_descriptor vertex_to_print) {
    std::cout << "(" << vertex_to_print[0] << ", " << vertex_to_print[1] << ")" << std::endl;
}

int main(int argc, char* argv[]) {

    // Define a 3x5 grid_graph where the second dimension doesn't wrap
//  boost::array<std::size_t, 2> lengths = { { 3, 5 } };
    boost::array<size_t, 2> lengths = { { 3, 5 } };
    Graph graph(lengths);

    // Do a round-trip test of the vertex index functions
    for (Traits::vertices_size_type v_index = 0;
        v_index < num_vertices(graph); ++v_index) {

        // The two indicies should always be equal
        std::cout << "Index of vertex " << v_index << " is " <<
            get(boost::vertex_index, graph, vertex(v_index, graph)) << std::endl;

    }

    // Do a round-trip test of the edge index functions
    for (Traits::edges_size_type e_index = 0;
        e_index < num_edges(graph); ++e_index) {

        // The two indicies should always be equal
        std::cout << "Index of edge " << e_index << " is " <<
            get(boost::edge_index, graph, edge_at(e_index, graph)) << std::endl;
    }


    // Print number of dimensions
    std::cout << graph.dimensions() << std::endl; // prints "3"

                                                  // Print dimension lengths (same order as in the lengths array)
    std::cout << graph.length(0) << "x" << graph.length(1) << std::endl; // prints "3x5x7"


                                                     // Start with the first vertex in the graph
    Traits::vertex_descriptor first_vertex = vertex(0, graph);
    print_vertex(first_vertex); // prints "(0, 0)"

                                // Print the next vertex in dimension 0
    print_vertex(graph.next(first_vertex, 0)); // prints "(1, 0)"

                                               // Print the next vertex in dimension 1
    print_vertex(graph.next(first_vertex, 1)); // prints "(0, 1)"


}

这是 Boost 库的网格图。但我不知道如何用 int 权重实现边缘。我应该使用什么属性来做到这一点? 我可以遍历边缘和节点,但只能遍历它们的缺陷,而不是它们的值。

【问题讨论】:

    标签: c++ boost edges


    【解决方案1】:

    因此,您可以使用访问器通过索引获取边缘,参见例如

    要附加属性,请参见例如

    【讨论】:

      【解决方案2】:

      执行此操作的方法是通过exterior propertiesgrid_graph 支持将 edge_descriptors 和 vertex_descriptors 分别映射到基于 0 的索引的 edge_indexvertex_index 属性。要定义外部边缘属性映射,您可以利用iterator_property_mapgrid_graph 的边缘索引属性映射与随机访问迭代器结合起来。

      这是一个完整的例子:

      #include <cstdlib>
      #include <iostream>
      #include <numeric>
      #include <vector>
      
      #include <boost/graph/grid_graph.hpp>
      #include <boost/graph/properties.hpp>
      #include <boost/graph/write_dimacs.hpp>
      #include <boost/property_map/property_map.hpp>
      
      int main() {
      
        typedef boost::grid_graph<2> Graph;
        typedef boost::graph_traits<Graph> Traits;
      
        Graph graph {{3, 5}};
      
        // Create a vector to store the capacity of each edge.
        std::vector<int> capacities(num_edges(graph));
        // Set the edge capacities. Here, we'll just use iota().
        std::iota(capacities.begin(), capacities.end(), 10);
      
        // Utilize iterator_property_map to make a property map for the edge capacities.
        boost::property_map<Graph, boost::edge_index_t>::type edge_index_map = get(boost::edge_index, graph);
        auto capacities_map = boost::make_iterator_property_map(capacities.begin(), edge_index_map);
      
        // Select the first and the last vertices as the source and sink nodes, respectively.
        Traits::vertex_descriptor s = vertex(0, graph),
                                  t = vertex(num_vertices(graph) - 1, graph);
      
        boost::write_dimacs_max_flow(
            graph,
            capacities_map,
            get(boost::vertex_index, graph),
            s,
            t,
            std::cout);
      
        return EXIT_SUCCESS;
      }
      

      【讨论】:

        猜你喜欢
        • 2016-03-11
        • 1970-01-01
        • 2012-05-01
        • 1970-01-01
        • 2011-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-18
        相关资源
        最近更新 更多