BGL 的 algorithm(十二)

这是关于 BGL 算法最后一部分。之后我们将深入了解一些 graph 相关的算法(如 BGL 涉及的算法或者近似算法系列里面谈到的一些近似算法),并给出基于 BGL 的实现。

这里主要是对平面图这种特殊 graph 的相关算法,我们知道所谓平面图,等价的是其任意子图一定不是 5 个顶点的完全图,或者是 3-3 的完全二分图。对应平面图还有著名的 Euler 公式,它是 topology 的结论,表明一个平面图顶点、边、面和联通分支之间的数值关系。判断一个图是否为平面图可以用 Boyer-Myrvold Planarity Testing,这是一个 O(|V|) 代价的算法,它或者能给出 planar embedding 或者判断该图为非平面图。函数 boyer_myrvold_planarity_test 实现了这个算法。

对 planar graph 进行绘制一般会考虑将给顶无向图转换成 connected、biconncted(指不仅 connected 且去掉一个顶点仍然 connected)且 maximal planar graph(不能再加非自环边而仍然保持 planarity 了)。BGL 提供了这样三个函数(make_connected、make_biconnected 与 make_maximal_planar)用于将给定的 graph 转成符合对应条件的。当我们将 graph 转换为 maximal planar 后,可以使用 chrobak_payne_straight_line_drawing 进行绘制。

为了方便检查 boyer_myrvold_planarity_test 的结果,BGL 提供了 is_kuratowski_subgraph 检查是否含有前面两种导致不是 planar graph 的 subgraph。is_straight_line_drawing 可以验证作图结果的确没有相交的边。下面是 BGL 提供的例程。

#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>
    > graph;

  //Define the storage type for the planar embedding
  typedef std::vector< std::vector< graph_traits<graph>::edge_descriptor > >
    embedding_storage_t;
  typedef boost::iterator_property_map
    < embedding_storage_t::iterator,
      property_map<graph, vertex_index_t>::type
    >
    embedding_t;

  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(3,4,g);
  add_edge(4,5,g);
  add_edge(5,6,g);
  add_edge(6,3,g);
  add_edge(0,4,g);
  add_edge(1,3,g);
  add_edge(3,5,g);
  add_edge(2,6,g);
  add_edge(1,4,g);
  add_edge(1,5,g);
  add_edge(1,6,g);

  // Create the planar embedding
  embedding_storage_t embedding_storage(num_vertices(g));
  embedding_t embedding(embedding_storage.begin(), get(vertex_index,g));
  boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g,
                               boyer_myrvold_params::embedding = embedding
                               );

  // Find a canonical ordering
  std::vector<graph_traits<graph>::vertex_descriptor> ordering;
  planar_canonical_ordering(g, embedding, 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(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;
}

运行结果如下

The straight line drawing is: 
0 -> (0, 0)
1 -> (10, 0)
2 -> (5, 4)
3 -> (5, 5)
4 -> (2, 1)
5 -> (3, 2)
6 -> (4, 3)
Is a plane drawing.

——————
And Hagar bore Abram a son: and Abram called his son’s name, which Hagar bore, Ishmael.

Advertisements
BGL 的 algorithm(十二)

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s