我的任务是在矩阵中找到从一点到另一点的最短路线。只能在这样的方向上移动(上、下、左、右)。
0 0 0 0 1 0 0 0
1 0 0 0 0 0 0 0
0 0 0 1 0 1 F 0
0 1 0 1 0 0 0 0
0 0 0 1 0 0 0 0
0 S 0 1 0 0 1 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0
S - 起点
F - 目的地(Finish)
0 - 空闲单元格(我们可以穿过它们)
1 - “墙”(我们不能穿过它们)
很明显,广度优先搜索以最佳方式解决了这个问题。 我知道 Boost 库提供了这个算法,但我以前没有使用过 Boost。
如何使用 Boost 在我的案例中进行广度优先搜索?
据我了解,Boost 的广度优先搜索算法仅适用于图形。
我想将矩阵转换为具有 m*n
顶点和 m*(n -1) + (m-1)*n
的图形不是一个好主意边缘。
我可以将广度优先搜索算法应用于矩阵(不将其转换为图形),还是实现我自己的广度优先搜索函数更好?
最佳答案
(提前为这个答案的长度道歉。自从我使用 BGL 以来已经有一段时间了,我认为这将是一个很好的复习。完整代码是 here。)
Boost Graph Library(以及一般的泛型编程)的美妙之处在于,您无需使用任何特定的数据结构即可利用给定的算法。您提供的矩阵以及遍历它的规则已经定义了一个图。所需要做的就是将这些规则编码到可用于利用 BGL 算法的特征类中。
具体来说,我们要做的是定义boost::graph_traits<T>
的特化。为你的图表。假设您的矩阵是 int
的单个数组是行优先格式。可惜专攻graph_traits
对于 int[N]
将是不够的,因为它不提供有关矩阵维度的任何信息。因此,让我们按如下方式定义您的图形:
namespace matrix
{
typedef int cell;
static const int FREE = 0;
static const int WALL = 1;
template< size_t ROWS, size_t COLS >
struct graph
{
cell cells[ROWS*COLS];
};
}
我在这里对单元格数据使用了组合,但如果要在外部进行管理,您也可以轻松地使用指针。现在我们有了一个用矩阵维度编码的类型,可以用来特化 graph_traits
.但首先让我们定义一些我们需要的函数和类型。
顶点类型和辅助函数:
namespace matrix
{
typedef size_t vertex_descriptor;
template< size_t ROWS, size_t COLS >
size_t get_row(
vertex_descriptor vertex,
graph< ROWS, COLS > const & )
{
return vertex / COLS;
}
template< size_t ROWS, size_t COLS >
size_t get_col(
vertex_descriptor vertex,
graph< ROWS, COLS > const & )
{
return vertex % COLS;
}
template< size_t ROWS, size_t COLS >
vertex_descriptor make_vertex(
size_t row,
size_t col,
graph< ROWS, COLS > const & )
{
return row * COLS + col;
}
}
遍历顶点的类型和函数:
namespace matrix
{
typedef const cell * vertex_iterator;
template< size_t ROWS, size_t COLS >
std::pair< vertex_iterator, vertex_iterator >
vertices( graph< ROWS, COLS > const & g )
{
return std::make_pair( g.cells, g.cells + ROWS*COLS );
}
typedef size_t vertices_size_type;
template< size_t ROWS, size_t COLS >
vertices_size_type
num_vertices( graph< ROWS, COLS > const & g )
{
return ROWS*COLS;
}
}
边缘类型:
namespace matrix
{
typedef std::pair< vertex_descriptor, vertex_descriptor > edge_descriptor;
bool operator==(
edge_descriptor const & lhs,
edge_descriptor const & rhs )
{
return
lhs.first == rhs.first && lhs.second == rhs.second ||
lhs.first == rhs.second && lhs.second == rhs.first;
}
bool operator!=(
edge_descriptor const & lhs,
edge_descriptor const & rhs )
{
return !(lhs == rhs);
}
}
最后,迭代器和函数帮助我们遍历顶点和边之间存在的关联关系:
namespace matrix
{
template< size_t ROWS, size_t COLS >
vertex_descriptor
source(
edge_descriptor const & edge,
graph< ROWS, COLS > const & )
{
return edge.first;
}
template< size_t ROWS, size_t COLS >
vertex_descriptor
target(
edge_descriptor const & edge,
graph< ROWS, COLS > const & )
{
return edge.second;
}
typedef boost::shared_container_iterator< std::vector< edge_descriptor > > out_edge_iterator;
template< size_t ROWS, size_t COLS >
std::pair< out_edge_iterator, out_edge_iterator >
out_edges(
vertex_descriptor vertex,
graph< ROWS, COLS > const & g )
{
boost::shared_ptr< std::vector< edge_descriptor > > edges( new std::vector< edge_descriptor >() );
if( g.cells[vertex] == FREE )
{
size_t
row = get_row( vertex, g ),
col = get_col( vertex, g );
if( row != 0 )
{
vertex_descriptor up = make_vertex( row - 1, col, g );
if( g.cells[up] == FREE )
edges->push_back( edge_descriptor( vertex, up ) );
}
if( row != ROWS-1 )
{
vertex_descriptor down = make_vertex( row + 1, col, g );
if( g.cells[down] == FREE )
edges->push_back( edge_descriptor( vertex, down ) );
}
if( col != 0 )
{
vertex_descriptor left = make_vertex( row, col - 1, g );
if( g.cells[left] == FREE )
edges->push_back( edge_descriptor( vertex, left ) );
}
if( col != COLS-1 )
{
vertex_descriptor right = make_vertex( row, col + 1, g );
if( g.cells[right] == FREE )
edges->push_back( edge_descriptor( vertex, right ) );
}
}
return boost::make_shared_container_range( edges );
}
typedef size_t degree_size_type;
template< size_t ROWS, size_t COLS >
degree_size_type
out_degree(
vertex_descriptor vertex,
graph< ROWS, COLS > const & g )
{
std::pair< out_edge_iterator, out_edge_iterator > edges = out_edges( vertex, g );
return std::distance( edges.first, edges.second );
}
}
现在我们准备好定义我们的特化 boost::graph_traits
:
namespace boost
{
template< size_t ROWS, size_t COLS >
struct graph_traits< matrix::graph< ROWS, COLS > >
{
typedef matrix::vertex_descriptor vertex_descriptor;
typedef matrix::edge_descriptor edge_descriptor;
typedef matrix::out_edge_iterator out_edge_iterator;
typedef matrix::vertex_iterator vertex_iterator;
typedef boost::undirected_tag directed_category;
typedef boost::disallow_parallel_edge_tag edge_parallel_category;
struct traversal_category :
virtual boost::vertex_list_graph_tag,
virtual boost::incidence_graph_tag {};
typedef matrix::vertices_size_type vertices_size_type;
typedef matrix::degree_size_type degree_size_type;
static vertex_descriptor null_vertex() { return ROWS*COLS; }
};
}
下面是如何执行广度优先搜索并找到最短路径:
int main()
{
const size_t rows = 8, cols = 8;
using namespace matrix;
typedef graph< rows, cols > my_graph;
my_graph g =
{
FREE, FREE, FREE, FREE, WALL, FREE, FREE, FREE,
WALL, FREE, FREE, FREE, FREE, FREE, FREE, FREE,
FREE, FREE, FREE, WALL, FREE, WALL, FREE, FREE,
FREE, WALL, FREE, WALL, FREE, FREE, FREE, FREE,
FREE, FREE, FREE, WALL, FREE, FREE, FREE, FREE,
FREE, FREE, FREE, WALL, FREE, FREE, WALL, FREE,
FREE, FREE, FREE, FREE, FREE, FREE, WALL, FREE,
FREE, FREE, FREE, FREE, FREE, FREE, WALL, FREE,
};
const vertex_descriptor
start_vertex = make_vertex( 5, 1, g ),
finish_vertex = make_vertex( 2, 6, g );
vertex_descriptor predecessors[rows*cols] = { 0 };
using namespace boost;
breadth_first_search(
g,
start_vertex,
visitor( make_bfs_visitor( record_predecessors( predecessors, on_tree_edge() ) ) ).
vertex_index_map( identity_property_map() ) );
typedef std::list< vertex_descriptor > path;
path p;
for( vertex_descriptor vertex = finish_vertex; vertex != start_vertex; vertex = predecessors[vertex] )
p.push_front( vertex );
p.push_front( start_vertex );
for( path::const_iterator cell = p.begin(); cell != p.end(); ++cell )
std::cout << "[" << get_row( *cell, g ) << ", " << get_col( *cell, g ) << "]\n" ;
return 0;
}
从开始到结束沿最短路径输出单元格:
[5, 1]
[4, 1]
[4, 2]
[3, 2]
[2, 2]
[1, 2]
[1, 3]
[1, 4]
[1, 5]
[1, 6]
[2, 6]
关于c++ - 是否可以将boost库的广度优先搜索算法应用于矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8950188/