2009-12-22 74 views
3

我有使用boost圖形佈局算法的問題。 boost verision 1_41_0 mingw g ++ 4.4.0。如何使用Boost 1.41.0圖形佈局算法

所以有我遇到的問題你可以建議我與他們?

  1. 未編譯函數fruchterman_reingold_force_directed_layout。
  2. 已編譯kamada_kawai_spring_layout但程序崩潰。
  3. 將佈局算法升級到佈局算法是錯誤的,未對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; 
} 

回答

0

,如果你想提供自己的ID,你應該安裝一個頂點id屬性,您不僅可以通過命名這種方式使用vertexIdPropertyMap作爲ID(,但這是很少需要)。

聲明你的圖作爲

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

,除非你有一個很好的理由不這樣做,這樣的vertex_id是有保證的是在[0,N)。

如果你從你的vertexId索引中刪除虛構的「+2」,你的頂點id現在應該與頂點的頂點id匹配(因此你的程序不應該再seg段了)。

這應該爲kamada_kaway,但恕我直言,你的代碼可以在很多方面改善只是仔細查看BGL示例代碼。

編輯:固定錯字s/setS/vecS/

+0

謝謝你的回答。我只是研究boost圖庫。你能否使用boost的圖形庫佈局算法爲我提供示例。我認爲boost的圖表示例和文檔已經過時。 Daniil – Daniil 2010-03-29 08:43:24

+0

試試這裏:http://www.boost.org/doc/libs/1_42_0/libs/graph/test/layout_test.cpp – baol 2010-03-30 18:42:48