从BGL的图提取邻接矩阵 [英] Extract the adjacency matrix from a BGL graph
问题描述
使用的 Boost图库我要寻找一种方式为从底层图重新$ P $由要么psented提取邻接矩阵 提高::的adjacency_list
或的boost :: adjacency_matrix
。我想,要使用这个矩阵提高::数字:: uBLAS库
求解线性方程组的系统。
Using the Boost Graph Library I am looking for a way to extract the adjacency matrix from an underlying graph represented by either boost::adjacency_list
or boost::adjacency_matrix
. I'd like to use this matrix in conjunction with boost::numeric::ublas
to solve a system of simultaneous linear equations.
下面是一个最小的例子,让你去:
Here is a minimal example to get you going:
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/adjacency_matrix.hpp>
using namespace boost;
typedef boost::adjacency_list< listS, vecS, directedS > ListGraph;
typedef boost::adjacency_matrix< directedS > MatrixGraph;
int main(){
ListGraph lg;
add_edge (0, 1, lg);
add_edge (0, 3, lg);
add_edge (1, 2, lg);
add_edge (2, 3, lg);
//How do I get the adjacency matrix underlying lg?
MatrixGraph mg(3);
add_edge (0, 1, mg);
add_edge (0, 3, mg);
add_edge (1, 2, mg);
add_edge (2, 3, mg);
//How do I get the adjacency matrix underlying mg?
}
如果任何人都可以想出一个有效的方式来获得邻接矩阵,我将非常感激。理想的是,溶液与uBLAS库兼容。我不知道是否有避免在整个图形迭代的方式。
If anyone could come up with an efficient way to obtain the adjacency matrix I would be much obliged. Ideally the solution is compatible with uBLAS. I wonder if there is a way to avoid iteration through the entire graph.
推荐答案
要的adjacency_list转换成adjacency_matrix最简单的方法是使用的boost :: copy_graph
The easiest way to convert adjacency_list into adjacency_matrix is to use boost::copy_graph
您code为 MatrixGraph毫克
应作如下修改
Your code for MatrixGraph mg
should be modified as follows
#include <boost/graph/copy.hpp>
#include <cassert>
using namespace boost;
typedef boost::adjacency_list< listS, vecS, directedS > ListGraph;
typedef boost::adjacency_matrix< directedS > MatrixGraph;
int main(){
ListGraph lg;
add_edge(0, 1, lg);
add_edge(0, 3, lg);
add_edge(1, 2, lg);
add_edge(2, 3, lg);
//How do I get the adjacency matrix underlying lg?
//How do I get the adjacency matrix underlying mg?
MatrixGraph mg( num_vertices(lg));
boost::copy_graph(lg, mg);
}
现在,使用具有的uBLAS或类似的邻接矩阵,你可以写一个简单的访问类,使语法的差别的更柔顺。继续previous片段,我们得到:
Now, to use adjacency matrix with ublas or similar, you can write a simple "access" class to make syntax more compliant with ublas. Continuing previous snippet we get:
template <class Graph>
class MatrixAccessor
{
public:
typedef typename Graph::Matrix Matrix; //actually a vector<
typedef typename Matrix::const_reference const_reference;
MatrixAccessor(const Graph* g)
: m_g(g)
{
static_assert(boost::is_same<size_t, typename Graph::vertex_descriptor>::value, "Vertex descriptor should be of integer type");
}
const_reference operator()(size_t u, size_t v) const
{
return m_g->get_edge(u, v);
}
const Graph* m_g;
};
void use_matrix(const MatrixGraph & mg)
{
MatrixAccessor<MatrixGraph> matr(&mg);
assert(matr(0, 1) == 1);
assert(matr(0, 2) == 0);
}
在情况下,你adjacency_matrix有一些边缘捆绑的属性,您可能需要MatrixAccessor修改运算符()。
In case your adjacency_matrix has some edge-bundled properties, you might need to modify the operator() in MatrixAccessor.
根据你多少的差别的使用,可以进一步细化MatrixAccessor。例如, out_edge_iterator
的MatrixGraph的定顶点实际上是在矩阵列的迭代器; vertex_iterator可以被视为迭代以上矩阵的行,等等。
Depending on how much uBLAS you use, you can refine MatrixAccessor further. For example, out_edge_iterator
for a given vertex of a MatrixGraph is actually an iterator over matrix column; vertex_iterator can be treated as iterator over matrix rows, etc.
当然,图矩阵是不可改变的,因此应谨慎使用。
Of course, graph matrix is immutable and as such should be used with care.
这篇关于从BGL的图提取邻接矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!