【问题标题】:Boost graph segmentation faultBoost 图形分割错误
【发布时间】:2017-01-05 05:29:33
【问题描述】:

我正在尝试使用 Boost 使用 Chrobak-Payne 算法嵌入平面图。 我能够成功运行example,但是当我尝试修改它并使用不同的图表时,它无法正常工作。我正在尝试嵌入second platonic graph,但它不起作用,并且我的代码因“分段错误:11”而崩溃。我认为这是因为我需要使用 make_connected、make_biconnected_planar 和 make_maximal_planar,但添加它们并不能解决问题。

这是使用第二个柏拉图图和三个辅助函数的修改源示例:

//=======================================================================
// Copyright 2007 Aaron Windsor
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/properties.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/property_map/property_map.hpp>
#include <vector>

#include <boost/graph/planar_canonical_ordering.hpp>
#include <boost/graph/is_straight_line_drawing.hpp>
#include <boost/graph/chrobak_payne_drawing.hpp>
#include <boost/graph/boyer_myrvold_planar_test.hpp>



using namespace boost;

//a class to hold the coordinates of the straight line embedding
struct coord_t
{
  std::size_t x;
  std::size_t y;
};


int main(int argc, char** argv)
{
typedef adjacency_list
    < vecS,
      vecS,
      undirectedS,
      property<vertex_index_t, int>,
      property<edge_index_t, int>
    > 
    graph;

  graph g(7);
  add_edge(0,1,g);
  add_edge(1,2,g);
  add_edge(2,3,g);
  add_edge(3,0,g);
  add_edge(0,4,g);
  add_edge(1,5,g);
  add_edge(2,6,g);
  add_edge(3,7,g);
  add_edge(4,5,g);
  add_edge(5,6,g);
  add_edge(6,7,g);
  add_edge(7,4,g);

  make_connected(g); //Make connected (1/3)

  //Compute the planar embedding as a side-effect
  typedef std::vector< graph_traits<graph>::edge_descriptor > vec_t;
  std::vector<vec_t> embedding(num_vertices(g));
  boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g,
                                   boyer_myrvold_params::embedding = 
                                     &embedding[0]
                                   );


  make_biconnected_planar(g, &embedding[0]); //Make biconnected planar (2/3)

  make_maximal_planar(g, &embedding[0]); //Make maximal planar (3/3)

  //Find a canonical ordering
  std::vector<graph_traits<graph>::vertex_descriptor> ordering;
  planar_canonical_ordering(g, &embedding[0], std::back_inserter(ordering));

  //Set up a property map to hold the mapping from vertices to coord_t's
  typedef std::vector< coord_t > straight_line_drawing_storage_t;
  typedef boost::iterator_property_map
    < straight_line_drawing_storage_t::iterator, 
      property_map<graph, vertex_index_t>::type 
    >
    straight_line_drawing_t;

  straight_line_drawing_storage_t straight_line_drawing_storage
    (num_vertices(g));
  straight_line_drawing_t straight_line_drawing
    (straight_line_drawing_storage.begin(), 
     get(vertex_index,g)
     );

  // Compute the straight line drawing
  chrobak_payne_straight_line_drawing(g, 
                                      embedding, 
                                      ordering.begin(),
                                      ordering.end(),
                                      straight_line_drawing
                                      );



  std::cout << "The straight line drawing is: " << std::endl;
  graph_traits<graph>::vertex_iterator vi, vi_end;
  for(boost::tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi)
    {
      coord_t coord(get(straight_line_drawing,*vi));
      std::cout << *vi << " -> (" << coord.x << ", " << coord.y << ")" 
                << std::endl;
    }

  // Verify that the drawing is actually a plane drawing
  if (is_straight_line_drawing(g, straight_line_drawing))
    std::cout << "Is a plane drawing." << std::endl;
  else
    std::cout << "Is not a plane drawing." << std::endl;

  return 0;
}

但由于某种原因,我仍然遇到分段错误。我知道它在通话中:

chrobak_payne_straight_line_drawing(g, 
                                  embedding, 
                                  ordering.begin(),
                                  ordering.end(),
                                  straight_line_drawing
                                  );

因为没有它它运行良好(但不计算嵌入)。导致此分段错误的内存问题在哪里?我嵌入的图比示例小。

【问题讨论】:

    标签: c++ boost graph segmentation-fault


    【解决方案1】:

    来自must be a maximal planar graph with at least 3 vertices,要求k > 2才能成功。您对Planar Canonical Ordering 的调用返回了两个顶点。 Catch is chrobak_payne_straight_line_drawing 不会为您检查,它会在 std 中的向量迭代器测试中断言。

    添加:

    assert( ordering.size( ) > 2 );
    

    在调用之前或条件之前,取决于你在做什么。

    一个优势:

    add_edge(1,4,g);
    

    它会奏效的。

    【讨论】:

    • 感谢您的回答。但是,我不明白为什么当原始图有 7 时排序只有两个顶点。如果它已经是一个最大平面图,我需要在规范排序之前调用什么函数来允许它返回两个以上的顶点(假设我正确地将其设为最大平面)?
    • 我没有正确调用最大平面。问题是,我不会在图中存储新顶点。 Here is an example of using all the helper functions correctly.
    • 谢谢@Ben Sisson,我有很多关于图表的知识要学。我会检查你的代码并学习。
    猜你喜欢
    • 2018-01-15
    • 2021-03-08
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多