如何在boost :: grid_graph中使用给定的vertex_descriptor访问edge_descriptor

时间:2017-04-03 13:44:11

标签: c++ boost

我正在尝试更改grid_graph中边缘的权重,但无法访问edge_descriptor:

std::pair<bEdgeDescriptor, bool> ed_right = boost::edge(bVertexDescriptor {{i - 1, j, k}}, bVertexDescriptor {{i, j, k}}, grid);

其中gridboost::grid_graph<3>

然后我发现grid_graph不支持这个。为了使用astar_search,是否可以通过edge_descriptor而非vertex_descriptor使用boost::grid_graph来访问adjacency_list

1 个答案:

答案 0 :(得分:2)

您可以获取任何特定节点的输入/输出边缘:

http://www.boost.org/doc/libs/1_63_0/libs/graph/doc/grid_graph.html#indexing

// Get the out-edge associated with vertex and out_edge_index
Traits::edge_descriptor
out_edge_at(Traits::vertex_descriptor vertex,
            Traits::degree_size_type out_edge_index,
            const Graph& graph);

// Get the out-edge associated with vertex and in_edge_index
Traits::edge_descriptor
in_edge_at(Traits::vertex_descriptor vertex,
           Traits::degree_size_type in_edge_index,
           const Graph& graph);

使用具有非包装尺寸的3d 4x4x4网格进行演示(此示例表示边缘上的节点程度较低):

<强> Live On Coliru

#include <boost/graph/grid_graph.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <iostream>

using Grid = boost::grid_graph<3>;
using Traits = boost::graph_traits<Grid>;
using vertex_descriptor = Grid::vertex_descriptor;
using edge_descriptor = Grid::edge_descriptor;

static inline std::ostream& operator<<(std::ostream& os, vertex_descriptor const& vd) {
    return os << "(" << vd[0] << ", " << vd[1] << ", " << vd[2] << ")"; 
}

void print_in_edges(vertex_descriptor vd, Grid const& grid) {
    for (Traits::degree_size_type ei = 0; ei < in_degree(vd, grid); ++ei) {
        auto ed_left = in_edge_at(vd, ei, grid);
        std::cout << "Detected in edge: " << ed_left.first << " -> " << ed_left.second << "\n";
    }
}

void print_out_edges(vertex_descriptor vd, Grid const& grid) {
    for (Traits::degree_size_type ei = 0; ei < out_degree(vd, grid); ++ei) {
        auto ed_left = out_edge_at(vd, ei, grid);
        std::cout << "Detected out edge: " << ed_left.first << " -> " << ed_left.second << "\n";
    }
}

int main() {
    Grid grid({ { 4, 4, 4 } }, false);

    print_in_edges({{ 2, 2, 2 } }, grid);
    print_out_edges({{ 2, 2, 2 } }, grid);
    std::cout << "----\n";
    print_in_edges({{ 0, 0, 0 } }, grid);
    print_out_edges({{ 0, 0, 0 } }, grid);
}

打印:

Detected in edge: (1, 2, 2) -> (2, 2, 2)
Detected in edge: (3, 2, 2) -> (2, 2, 2)
Detected in edge: (2, 1, 2) -> (2, 2, 2)
Detected in edge: (2, 3, 2) -> (2, 2, 2)
Detected in edge: (2, 2, 1) -> (2, 2, 2)
Detected in edge: (2, 2, 3) -> (2, 2, 2)
Detected out edge: (2, 2, 2) -> (1, 2, 2)
Detected out edge: (2, 2, 2) -> (3, 2, 2)
Detected out edge: (2, 2, 2) -> (2, 1, 2)
Detected out edge: (2, 2, 2) -> (2, 3, 2)
Detected out edge: (2, 2, 2) -> (2, 2, 1)
Detected out edge: (2, 2, 2) -> (2, 2, 3)
----
Detected in edge: (1, 0, 0) -> (0, 0, 0)
Detected in edge: (0, 1, 0) -> (0, 0, 0)
Detected in edge: (0, 0, 1) -> (0, 0, 0)
Detected out edge: (0, 0, 0) -> (1, 0, 0)
Detected out edge: (0, 0, 0) -> (0, 1, 0)
Detected out edge: (0, 0, 0) -> (0, 0, 1)