3

我在使用提升图布局算法时遇到问题。提升版本 1_41_0 mingw g++ 4.4.0。

所以我遇到了一些问题你能建议我吗?

  1. 函数 fruchterman_reingold_force_directed_layout 未编译。
  2. kamada_kawai_spring_layout 编译但程序崩溃。
  3. 布局算法的 Boost 文档是错误的,未编译 fruchterman_reingold_force_directed_layout 的示例。

这是我的例子。要使用功能,只需取消注释一个。字符串 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;
}
4

1 回答 1

0

您不能仅通过以这种方式命名来将 vertexIdPropertyMap 用作 ID(如果您想提供自己的 ID,您应该安装一个顶点 id 属性,但这很少需要)。

将您的图表声明为

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

除非您有充分的理由不这样做,否则可以确保 vertex_id 在 [0,n) 中。

如果您从 vertexId 索引中删除虚构的“+2”,您的顶点 id 现在应该与顶点的真实顶点 id 匹配(因此您的程序不应再出现段错误)。

这应该适用于 kamada_kaway,但恕我直言,您的代码可以通过多种方式改进,只需仔细查看 BGL 示例代码。

编辑:修正错字 s/setS/vecS/

于 2010-03-27T21:11:35.087 回答