How to access edge_descriptor with given vertex_descriptor in boost::grid_graph

前端 未结 1 640
死守一世寂寞
死守一世寂寞 2021-01-14 23:41

I\'m trying to change the weight of edges in my grid_graph but fail to access the edge_descriptor with:

std::pair ed_right = boo         


        
相关标签:
1条回答
  • 2021-01-15 00:20

    You can get the in/out edges for any particular node:

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

    Demo using a 3d 4x4x4 grid with non-wrapping dimensions (this show-cases that the degree of nodes on the edges is lower):

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

    Prints:

    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)
    
    0 讨论(0)
提交回复
热议问题