Boost logo

Boost Users :

Subject: Re: [Boost-users] about convert adjacency_matrix into common boost matrix
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-12-24 12:12:07


AMDG

Peng Wang wrote:
> I am a newbie of boost.
> Could I ask one question that how to convert BGL adjacency_matrix object
> into common matrix
> (http://www.boost.org/doc/libs/1_37_0/libs/numeric/ublas/doc/matrix.htm)?
> I am sorry that if the question is too simple. I have looked around but did
> not
> find a answer.
>

I presume that you want a 1 where there is an edge an a 0 otherwise?

#include <boost/graph/adjacency_matrix.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/foreach.hpp>

boost::numeric::ublas::matrix<double> to_ublas_matrix(const
boost::adjacency_matrix<>& m) {
    std::size_t size = num_vertices(m);
    boost::numeric::ublas::matrix<double> result(size, size);
    BOOST_FOREACH(std::size_t row, vertices(m)) {
        BOOST_FOREACH(std::size_t column, vertices(m)) {
            result(row, column) = edge(row, column, m).second;
        }
    }
    return(result);
}

int main() {
    boost::adjacency_matrix<> m(5);
    add_edge(0, 2, m);
    boost::numeric::ublas::matrix<double> u(to_ublas_matrix(m));
}

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net