How to use Boost 1.41.0 graph layout algorithmes
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?
- The function fruchterman_reingold_force_directed_layout isn't compiled.
- The kamada_kawai_spring_layout compiled but program crashed.
- Boost documentation to layout algori开发者_如何转开发thms 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;
}
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/
精彩评论