【问题标题】:How to use Boost 1.41.0 graph layout algorithmes如何使用 Boost 1.41.0 图形布局算法
【发布时间】:2009-12-22 10:15:34
【问题描述】:

我在使用 boost 图形布局算法时遇到问题。 提升版本 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;
}

【问题讨论】:

    标签: algorithm boost graph-layout


    【解决方案1】:

    您不能仅通过将 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/

    【讨论】:

    • 感谢您的回答。我只是在研究boost图库。您能否为我提供使用 boost 的图形库布局算法的示例。我认为 boost 的图表样本和文档已经过时了。丹尼尔
    猜你喜欢
    • 2015-04-21
    • 1970-01-01
    • 2016-12-07
    • 2011-06-29
    • 2015-12-12
    • 1970-01-01
    • 2023-03-21
    • 2013-04-21
    • 1970-01-01
    相关资源
    最近更新 更多