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 に答える