views:

305

answers:

1

Hi I have problem using boost graph layout algorithmes. boost verision 1_41_0 mingw g++ 4.4.0.

So there are issues I have encountered Can you suggest me with them?

  1. The function fruchterman_reingold_force_directed_layout isn't compiled.
  2. The kamada_kawai_spring_layout compiled but program crashed.
  3. Boost documentation to layout algorithms is wrong, sample to fruchterman_reingold_force_directed_layout isn't compiled.

This is my example. To use function just uncomment one. String 60, 61, 63.

#include <boost/config.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/graph/simple_point.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/circle_layout.hpp>
#include <boost/graph/fruchterman_reingold.hpp>
#include <boost/graph/kamada_kawai_spring_layout.hpp>
#include <iostream>

//typedef boost::square_topology<>::point_difference_type Point;
typedef boost::square_topology<>::point_type Point;

struct VertexProperties
{
    std::size_t index;
    Point point;
};

struct EdgeProperty
{
    EdgeProperty(const std::size_t &w):weight(w) {}
    double weight;
};


typedef boost::adjacency_list<boost::listS,
            boost::listS, boost::undirectedS,
            VertexProperties, EdgeProperty > Graph;

typedef boost::property_map<Graph, std::size_t VertexProperties::*>::type VertexIndexPropertyMap;
typedef boost::property_map<Graph, Point VertexProperties::*>::type PositionMap;
typedef boost::property_map<Graph, double EdgeProperty::*>::type WeightPropertyMap;

typedef boost::graph_traits<Graph>::vertex_descriptor VirtexDescriptor;

int main()
{
    Graph graph;

    VertexIndexPropertyMap vertexIdPropertyMap = boost::get(&VertexProperties::index, graph);

    for (int i = 0; i < 3; ++i) {
        VirtexDescriptor vd = boost::add_vertex(graph);
        vertexIdPropertyMap[vd] = i + 2;
    }

    boost::add_edge(boost::vertex(1, graph), boost::vertex(0, graph), EdgeProperty(5), graph);
    boost::add_edge(boost::vertex(2, graph), boost::vertex(0, graph), EdgeProperty(5), graph);

    std::cout << "Vertices\n";
    boost::print_vertices(graph, vertexIdPropertyMap);

    std::cout << "Edges\n";
    boost::print_edges(graph, vertexIdPropertyMap);

    PositionMap positionMap = boost::get(&VertexProperties::point, graph);
    WeightPropertyMap weightPropertyMap = boost::get(&EdgeProperty::weight, graph);

    boost::circle_graph_layout(graph, positionMap, 100);
   // boost::fruchterman_reingold_force_directed_layout(graph, positionMap, boost::square_topology<>());

    boost::kamada_kawai_spring_layout(graph, positionMap, weightPropertyMap,
        boost::square_topology<>(), boost::side_length<double>(10), boost::layout_tolerance<>(),
        1, vertexIdPropertyMap);

    std::cout << "Coordinates\n";
    boost::graph_traits<Graph>::vertex_iterator i, end;
    for (boost::tie(i, end) = boost::vertices(graph); i != end; ++i) {
        std::cout << "ID: (" << vertexIdPropertyMap[*i] << ") x: " << positionMap[*i][0] << " y: " << positionMap[*i][1] << "\n";
    }

    return 0;
}
A: 

You cannot use the vertexIdPropertyMap as an ID only by naming it that way (if you want to provide your own ID you should install a vertex id property, but this is rarely needed).

Declare your graph as

typedef boost::adjacency_list<boost::vecS,
            boost::vecS, boost::undirectedS,
            VertexProperties, EdgeProperty > Graph;

unless you have a good reason to do otherwise, this way the vertex_id is assured to be in [0,n).

If you remove the fictitious "+2" from your vertexId indices your vertex id should now match real the vertex id of the vertices (and so your program should not segfault anymore).

This should do for kamada_kaway, but IMHO your code can be improved in many ways just looking carefully at BGL sample code.

EDIT: fixed typo s/setS/vecS/

baol
Thank for you answer. I am just study the boost graph library. Can you provide me with sample using boost's graph library layout algorithms. I think the boost's graph samples and documentation are out of date.Daniil
Try here: http://www.boost.org/doc/libs/1_42_0/libs/graph/test/layout_test.cpp
baol