The easiest way to convert adjacency_list to adjacency_matrix is to use boost::copy_graph
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); }
Now, to use the adjacency matrix with ublas or the like, you can write a simple access class to make the syntax more compatible with ublas. Continuing the previous fragment, we get:
template <class Graph> class MatrixAccessor { public: typedef typename Graph::Matrix Matrix;
If your adjacency_matrix has some edge related properties, you may need to modify the () operator in MatrixAccessor.
Depending on how much UBLAS you use, you may want to check out MatrixAccessor. For example, out_edge_iterator for a given vertex MatrixGraph is actually an iterator over the matrix column; vertex_iterator can be thought of as an iterator over matrix rows, etc.
Of course, the graph matrix is unchanged and as such should be used with caution.
source share