【发布时间】:2013-12-06 07:49:58
【问题描述】:
有没有什么方法可以在不使用 C++ boost 中的邻接列表或邻接矩阵的情况下创建图形结构? (使用指向其相邻顶点的指针的顶点结构)
【问题讨论】:
标签: c++ boost boost-graph
有没有什么方法可以在不使用 C++ boost 中的邻接列表或邻接矩阵的情况下创建图形结构? (使用指向其相邻顶点的指针的顶点结构)
【问题讨论】:
标签: c++ boost boost-graph
这是可能的,当然,前提是您的数据具有理论图的“特征”,这意味着您基本上处理的是“顶点”和“边”,即使在您的代码中它们被称为“节点”和“链接”。
这种结构称为“BGL 图形适配器”。不过,这可能是一个有点挑战性的 C++ 练习。总体思路是让 BGL 了解有关您的数据的很多细节:
所以你定义了一个类,比如 MyGraph,它通常是一个非常轻量级的类,并且只保留很少的指向数据的指针。然后通过提供 BGL graph_traits 的 模板特化 来定义它的特征:
#include <boost/graph/graph_traits.hpp>
namespace boost {
template <>
struct graph_traits<MyGraph>
{
typedef ... vertex_descriptor; //what plays a role of vertex in your data
typedef ... edge_descriptor; //what plays a role of edge in your data
//other typedefs from graph_traits like edge_iterator, out_edge_iterator, etc.
//plus, you specify "categories" of your graph explaining what types of traversal are
//available (more the better)
struct traversal_category
: public virtual boost::vertex_list_graph_tag
, public virtual boost::adjacency_graph_tag
, public virtual boost::bidirectional_graph_tag //means we provide access to in_edges
//and to out_edges of a given vertex
{
};
};
}
之后,您实现全局函数,这些函数提供对图形结构的访问和迭代器,例如:
MyGraph::vertex_descriptor
source(MyGraph::edge_descriptor e, const MyGraph & g);
和
std::pair<MyGraph::out_edge_iterator,
MyGraph::out_edge_iterator>
out_edges(MyGraph::::vertex_descriptor vd, const MyGraph & g )
在BGL graph concepts 中预定义了大约几十个这样的遍历函数。您必须至少提供与上面声明的traversal_category 相关的内容。
如果一切正常,您可以直接将数据与 BGL 算法一起使用,而无需使用任何预定义的 BGL 图。
BGL 章节How to Convert Existing Graphs to BGL 中给出了关于该主题的一个很好的解释
【讨论】: