使用Boost图的大小更改图
Size changing graph using Boost graph
嗨,我对Boost库很陌生。我想从一个正方形的二维地图中构建一个图形,该地图将用于星形算法(该地图是一个1和0的数组,用于墙壁和正常地形)。
图形应该是无向的,并随着地图的大小而变化。每个节点都有8条边(贴图的边除外)。
我已经看过一些例子,但我不理解构建这种大小的图的过程,因为在boost图库文档中,大多数例子都是这样的(如下所示)。
任何帮助或想法都将非常感谢
#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;
}
在对问题进行二读时,您的问题似乎只是如何添加节点和边。
这里有一个开始,它查询行/列的数量并创建正方形的"网格"。我在边上使用nodes
矩阵,以便从网格中的(x,y)轻松查找到图中的顶点描述符。
在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));
}
打印,例如输入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]
相关文章:
- boost::asio::steady_timer()与sleep()我应该使用哪一个
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 使用Boost Interprocess创建托管共享内存需要很长时间
- 如果C++对象的类在另一个boost模块中声明,如何使用boost将指向该对象的指针返回到python
- 如何在 Boost.Asio 中使用 Zero-copy sendmsg/receive
- 使用 boost 进行标记化会给出相同的输出
- 如何使用 Boost Asio 在 Android 上获取我的本地 udp IP 地址?
- 在成员dynamic_bitset上使用 boost::from_block_range 时出错,但在本地dynamic
- 将 boost::odeint 与向量类一起使用,而无需调整向量的大小
- Growing Boost.使用单个写入器的进程间内存映射文件
- 使用Boost使用自定义环境创建子过程
- Boost ::使用与多个水槽的同一后端使用相同的后端安全性
- boost::使用成员函数绑定回调的帮助
- Boost使用指向对象的指针向量(C++-linux)
- Boost::使用字符数组格式化
- 是否可以使用Boost.使用预分配的缓冲区格式化
- 为什么 Boost 使用全局函数覆盖来实现自定义验证程序"Program Options"
- 链接错误Boost.使用stlport作为标准库的Python
- 通过Python BOOST使用派生类(c++)