简体   繁体   English

使用 Boost 图的大小变化图

[英]Size changing graph using Boost graph

Hi I am pretty new to the Boost libraries.嗨,我对 Boost 库很陌生。 I want to build a graph from a square two dimensional map that will be used for a star algorithm (the map is an array with 1s and 0s for both wall and normal terrain).我想从将用于星形算法的方形二维地图构建一个图形(该地图是一个数组,对于墙壁和正常地形,都有 1 和 0)。

The graph should be undirected and change with the size of the map.该图应该是无向的,并随着地图的大小而变化。 Each node has 8 edges (except for sides of the map).每个节点有 8 条边(地图的边除外)。

I've gone through a few examples but I don't understand the procedure for building graphs of this size since most examples look like this (look bellow) in the boost graph library documentation.我已经浏览了一些示例,但我不明白构建这种大小的图形的过程,因为在 boost 图形库文档中,大多数示例看起来像这样(如下所示)。

Any help or ideas will be really appreciated任何帮助或想法将不胜感激

    #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>

  using namespace boost;

  int main(int,char*[])
  {
    // create a typedef for the Graph type
    typedef adjacency_list<vecS, vecS, bidirectionalS> Graph;

    // Make convenient labels for the vertices
    enum { A, B, C, D, E, N };
    const int num_vertices = N;
    const char* name = "ABCDE";

    // writing out the edges in the graph
    typedef std::pair<int, int> Edge;
    Edge edge_array[] =
    { Edge(A,B), Edge(A,D), Edge(C,A), Edge(D,C),
      Edge(C,E), Edge(B,D), Edge(D,E) };
    const int num_edges = sizeof(edge_array)/sizeof(edge_array[0]);

    // declare a graph object
    Graph g(num_vertices);

    // add the edges to the graph object
    for (int i = 0; i < num_edges; ++i){
      add_edge(edge_array[i].first, edge_array[i].second, g);
    }
    return 0;
  }

On second reading of the question it seems like your question is simply how to add nodes and edges.在第二次阅读问题时,您的问题似乎只是如何添加节点和边。

Here's a start that queries for the number of rows/columns and creates the square "grid".这是查询行/列数并创建方形“网格”的开始。 I use a nodes matrix on the side to have easy lookup from (x,y) in the grid to the vertex descriptor in the graph.我在侧面使用了一个nodes矩阵,以便从网格中的 (x,y) 轻松查找到图中的顶点描述符。

Live On Coliru 住在 Coliru

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

using namespace boost;

struct Point {
    int x, y; 
    friend std::ostream& operator<<(std::ostream& os, Point p) {
        return os << "[" << p.x << "," << p.y << "]";
    }
};

int main() {
    using std::vector;
    using Graph             = adjacency_list<setS, vecS, undirectedS, Point>;
    using vertex_descriptor = Graph::vertex_descriptor;

    Graph lattuce;

    int num_rows;
    if (!(std::cin >> num_rows && num_rows > 0))
        return 255;

    vector<vector<vertex_descriptor> > nodes(num_rows, vector<vertex_descriptor>(num_rows));

    for (auto i = 0; i < num_rows; ++i)
        for (auto j = 0; j < num_rows; ++j)
            nodes[i][j] = add_vertex(Point{i,j}, lattuce);

    auto is_valid = [num_rows](Point p) { return (p.x >= 0 && p.x < num_rows) && 
                                                 (p.y >= 0 && p.y < num_rows); };

    for (auto vd : make_iterator_range(vertices(lattuce))) {
        auto p = lattuce[vd];

        for (Point neighbour : {
                Point { p.x - 1, p.y - 1 }, Point { p.x - 1, p.y + 0 }, Point { p.x - 1, p.y + 1 },
                Point { p.x + 0, p.y - 1 }, Point { p.x + 0, p.y + 1 },
                Point { p.x + 1, p.y - 1 }, Point { p.x + 1, p.y + 0 }, Point { p.x + 1, p.y + 1 },
            })
        {
            if (is_valid(neighbour))
                add_edge(nodes[neighbour.x][neighbour.y], vd, lattuce);
        };
    }

    print_graph(lattuce, get(vertex_bundle, lattuce));
}

Prints, eg for input 3 :打印,例如输入3

[0,0] <--> [0,1] [1,0] [1,1] 
[0,1] <--> [0,0] [0,2] [1,0] [1,1] [1,2] 
[0,2] <--> [0,1] [1,1] [1,2] 
[1,0] <--> [0,0] [0,1] [1,1] [2,0] [2,1] 
[1,1] <--> [0,0] [0,1] [0,2] [1,0] [1,2] [2,0] [2,1] [2,2] 
[1,2] <--> [0,1] [0,2] [1,1] [2,1] [2,2] 
[2,0] <--> [1,0] [1,1] [2,1] 
[2,1] <--> [1,0] [1,1] [1,2] [2,0] [2,2] 
[2,2] <--> [1,1] [1,2] [2,1] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM