1 //=======================================================================
2 // Copyright 2007 Aaron Windsor
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
9 #include <boost/graph/adjacency_list.hpp>
10 #include <boost/graph/properties.hpp>
11 #include <boost/graph/boyer_myrvold_planar_test.hpp>
12 #include <boost/property_map/property_map.hpp>
13 #include <boost/property_map/vector_property_map.hpp>
14 #include <boost/test/minimal.hpp>
17 using namespace boost
;
20 struct VertexIndexUpdater
22 template <typename Graph
>
25 typename property_map
<Graph
, vertex_index_t
>::type index
= get(vertex_index
, g
);
26 typename graph_traits
<Graph
>::vertex_iterator vi
, vi_end
;
27 typename graph_traits
<Graph
>::vertices_size_type cnt
= 0;
28 for(boost::tie(vi
,vi_end
) = vertices(g
); vi
!= vi_end
; ++vi
)
29 put(index
, *vi
, cnt
++);
33 struct NoVertexIndexUpdater
35 template <typename Graph
> void reset(Graph
&) {}
40 template <typename Graph
, typename VertexIndexUpdater
>
41 void test_K_5(VertexIndexUpdater vertex_index
)
43 typedef typename graph_traits
<Graph
>::vertex_descriptor vertex_t
;
46 vertex_t v1
= add_vertex(g
);
47 vertex_t v2
= add_vertex(g
);
48 vertex_t v3
= add_vertex(g
);
49 vertex_t v4
= add_vertex(g
);
50 vertex_t v5
= add_vertex(g
);
51 vertex_index
.reset(g
);
53 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
55 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
57 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
59 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
61 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
63 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
65 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
67 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
69 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
71 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
73 //This edge should make the graph non-planar
75 BOOST_CHECK(!boyer_myrvold_planarity_test(g
));
83 template <typename Graph
, typename VertexIndexUpdater
>
84 void test_K_3_3(VertexIndexUpdater vertex_index
)
86 typedef typename graph_traits
<Graph
>::vertex_descriptor vertex_t
;
89 vertex_t v1
= add_vertex(g
);
90 vertex_t v2
= add_vertex(g
);
91 vertex_t v3
= add_vertex(g
);
92 vertex_t v4
= add_vertex(g
);
93 vertex_t v5
= add_vertex(g
);
94 vertex_t v6
= add_vertex(g
);
95 vertex_index
.reset(g
);
97 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
99 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
101 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
103 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
105 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
107 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
109 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
111 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
113 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
115 //This edge should make the graph non-planar
117 BOOST_CHECK(!boyer_myrvold_planarity_test(g
));
125 // This test creates a maximal planar graph on num_vertices vertices,
126 // then, if num_vertices is at least 5, adds an additional edge to
127 // create a non-planar graph.
129 template <typename Graph
, typename VertexIndexUpdater
>
130 void test_maximal_planar(VertexIndexUpdater vertex_index
, std::size_t num_vertices
)
132 typedef typename graph_traits
<Graph
>::vertex_descriptor vertex_t
;
135 std::vector
<vertex_t
> vmap
;
136 for(std::size_t i
= 0; i
< num_vertices
; ++i
)
137 vmap
.push_back(add_vertex(g
));
139 vertex_index
.reset(g
);
141 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
143 for(std::size_t i
= 0; i
< num_vertices
; ++i
)
145 add_edge(vmap
[i
], vmap
[(i
+1) % num_vertices
], g
);
146 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
149 //triangulate the interior of the cycle.
150 for(std::size_t i
= 2; i
< num_vertices
- 1; ++i
)
152 add_edge(vmap
[0], vmap
[i
], g
);
153 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
156 //triangulate the exterior of the cycle.
157 for(std::size_t i
= 3; i
< num_vertices
; ++i
)
159 add_edge(vmap
[1], vmap
[i
], g
);
160 BOOST_CHECK(boyer_myrvold_planarity_test(g
));
163 //Now add an additional edge, forcing the graph to be non-planar.
164 if (num_vertices
> 4)
166 add_edge(vmap
[2], vmap
[4], g
);
167 BOOST_CHECK(!boyer_myrvold_planarity_test(g
));
176 int test_main(int, char* [])
178 typedef adjacency_list
182 property
<vertex_index_t
, int>
186 typedef adjacency_list
190 property
<vertex_index_t
, int>
194 typedef adjacency_list
198 property
<vertex_index_t
, int>
202 typedef adjacency_list
206 property
<vertex_index_t
, int>
210 typedef adjacency_list
214 property
<vertex_index_t
, int>
218 test_K_5
<VVgraph_t
>(NoVertexIndexUpdater());
219 test_K_3_3
<VVgraph_t
>(NoVertexIndexUpdater());
220 test_maximal_planar
<VVgraph_t
>(NoVertexIndexUpdater(), 3);
221 test_maximal_planar
<VVgraph_t
>(NoVertexIndexUpdater(), 6);
222 test_maximal_planar
<VVgraph_t
>(NoVertexIndexUpdater(), 10);
223 test_maximal_planar
<VVgraph_t
>(NoVertexIndexUpdater(), 20);
224 test_maximal_planar
<VVgraph_t
>(NoVertexIndexUpdater(), 50);
226 test_K_5
<VLgraph_t
>(VertexIndexUpdater());
227 test_K_3_3
<VLgraph_t
>(VertexIndexUpdater());
228 test_maximal_planar
<VLgraph_t
>(VertexIndexUpdater(), 3);
229 test_maximal_planar
<VLgraph_t
>(VertexIndexUpdater(), 6);
230 test_maximal_planar
<VLgraph_t
>(VertexIndexUpdater(), 10);
231 test_maximal_planar
<VLgraph_t
>(VertexIndexUpdater(), 20);
232 test_maximal_planar
<VLgraph_t
>(VertexIndexUpdater(), 50);
234 test_K_5
<LVgraph_t
>(NoVertexIndexUpdater());
235 test_K_3_3
<LVgraph_t
>(NoVertexIndexUpdater());
236 test_maximal_planar
<LVgraph_t
>(NoVertexIndexUpdater(), 3);
237 test_maximal_planar
<LVgraph_t
>(NoVertexIndexUpdater(), 6);
238 test_maximal_planar
<LVgraph_t
>(NoVertexIndexUpdater(), 10);
239 test_maximal_planar
<LVgraph_t
>(NoVertexIndexUpdater(), 20);
240 test_maximal_planar
<LVgraph_t
>(NoVertexIndexUpdater(), 50);
242 test_K_5
<LLgraph_t
>(VertexIndexUpdater());
243 test_K_3_3
<LLgraph_t
>(VertexIndexUpdater());
244 test_maximal_planar
<LLgraph_t
>(VertexIndexUpdater(), 3);
245 test_maximal_planar
<LLgraph_t
>(VertexIndexUpdater(), 6);
246 test_maximal_planar
<LLgraph_t
>(VertexIndexUpdater(), 10);
247 test_maximal_planar
<LLgraph_t
>(VertexIndexUpdater(), 20);
248 test_maximal_planar
<LLgraph_t
>(VertexIndexUpdater(), 50);
250 test_K_5
<SSgraph_t
>(VertexIndexUpdater());
251 test_K_3_3
<SSgraph_t
>(VertexIndexUpdater());
252 test_maximal_planar
<SSgraph_t
>(VertexIndexUpdater(), 3);
253 test_maximal_planar
<SSgraph_t
>(VertexIndexUpdater(), 6);
254 test_maximal_planar
<SSgraph_t
>(VertexIndexUpdater(), 10);
255 test_maximal_planar
<SSgraph_t
>(VertexIndexUpdater(), 20);
256 test_maximal_planar
<SSgraph_t
>(VertexIndexUpdater(), 50);