在C++ Boost中,是否有不使用邻接表或邻接矩阵创建图结构的方法?(例如使用指向其相邻顶点的指针的顶点结构)
在C++ Boost中,是否有不使用邻接表或邻接矩阵创建图结构的方法?(例如使用指向其相邻顶点的指针的顶点结构)
#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);
and
std::pair<MyGraph::out_edge_iterator,
MyGraph::out_edge_iterator>
out_edges(MyGraph::::vertex_descriptor vd, const MyGraph & g )
在BGL图形概念中,预定义了大约数十个这样的遍历函数。您必须至少提供那些与上面声明的traversal_category
相关的函数。
如果一切都做得正确,您可以直接使用BGL算法处理数据,而不使用预定义的BGL图形之一。
BGL章节如何将现有图形转换为BGL对此进行了很好的解释。