gravatar
deba@inf.elte.hu
deba@inf.elte.hu
Planarity checking function instead of class (#62)
0 2 0
default
2 files changed with 27 insertions and 24 deletions:
↑ Collapse diff ↑
Show white space 24 line context
... ...
@@ -128,34 +128,24 @@
128 128
    template <typename Graph>
129 129
    struct ChildListNode {
130 130
      typedef typename Graph::Node Node;
131 131
      Node first;
132 132
      Node prev, next;
133 133
    };
134 134

	
135 135
    template <typename Graph>
136 136
    struct ArcListNode {
137 137
      typename Graph::Arc prev, next;
138 138
    };
139 139

	
140
  }
141

	
142
  /// \ingroup planar
143
  ///
144
  /// \brief Planarity checking of an undirected simple graph
145
  ///
146
  /// This class implements the Boyer-Myrvold algorithm for planarity
147
  /// checking of an undirected graph. This class is a simplified
148
  /// version of the PlanarEmbedding algorithm class because neither
149
  /// the embedding nor the kuratowski subdivisons are not computed.
150 140
  template <typename Graph>
151 141
  class PlanarityChecking {
152 142
  private:
153 143

	
154 144
    TEMPLATE_GRAPH_TYPEDEFS(Graph);
155 145

	
156 146
    const Graph& _graph;
157 147

	
158 148
  private:
159 149

	
160 150
    typedef typename Graph::template NodeMap<Arc> PredMap;
161 151

	
... ...
@@ -170,34 +160,26 @@
170 160
    typedef _planarity_bits::NodeDataNode<Graph> NodeDataNode;
171 161
    typedef std::vector<NodeDataNode> NodeData;
172 162

	
173 163
    typedef _planarity_bits::ChildListNode<Graph> ChildListNode;
174 164
    typedef typename Graph::template NodeMap<ChildListNode> ChildLists;
175 165

	
176 166
    typedef typename Graph::template NodeMap<std::list<int> > MergeRoots;
177 167

	
178 168
    typedef typename Graph::template NodeMap<bool> EmbedArc;
179 169

	
180 170
  public:
181 171

	
182
    /// \brief Constructor
183
    ///
184
    /// \note The graph should be simple, i.e. parallel and loop arc
185
    /// free.
186 172
    PlanarityChecking(const Graph& graph) : _graph(graph) {}
187 173

	
188
    /// \brief Runs the algorithm.
189
    ///
190
    /// Runs the algorithm.
191
    /// \return %True when the graph is planar.
192 174
    bool run() {
193 175
      typedef _planarity_bits::PlanarityVisitor<Graph> Visitor;
194 176

	
195 177
      PredMap pred_map(_graph, INVALID);
196 178
      TreeMap tree_map(_graph, false);
197 179

	
198 180
      OrderMap order_map(_graph, -1);
199 181
      OrderList order_list;
200 182

	
201 183
      AncestorMap ancestor_map(_graph, -1);
202 184
      LowMap low_map(_graph, -1);
203 185

	
... ...
@@ -230,25 +212,26 @@
230 212

	
231 213
        for (OutArcIt e(_graph, node); e != INVALID; ++e) {
232 214
          Node target = _graph.target(e);
233 215

	
234 216
          if (order_map[source] < order_map[target] && !tree_map[e]) {
235 217
            embed_arc[target] = true;
236 218
            walkUp(target, source, i, pred_map, low_map,
237 219
                   order_map, order_list, node_data, merge_roots);
238 220
          }
239 221
        }
240 222

	
241 223
        for (typename MergeRoots::Value::iterator it =
242
               merge_roots[node].begin(); it != merge_roots[node].end(); ++it) {
224
                 merge_roots[node].begin(); 
225
               it != merge_roots[node].end(); ++it) {
243 226
          int rn = *it;
244 227
          walkDown(rn, i, node_data, order_list, child_lists,
245 228
                   ancestor_map, low_map, embed_arc, merge_roots);
246 229
        }
247 230
        merge_roots[node].clear();
248 231

	
249 232
        for (OutArcIt e(_graph, node); e != INVALID; ++e) {
250 233
          Node target = _graph.target(e);
251 234

	
252 235
          if (order_map[source] < order_map[target] && !tree_map[e]) {
253 236
            if (embed_arc[target]) {
254 237
              return false;
... ...
@@ -440,25 +423,26 @@
440 423

	
441 424
            merge_stack.push_back(std::make_pair(n, d));
442 425

	
443 426
            int rn = merge_roots[node].front();
444 427

	
445 428
            int xn = node_data[rn].next;
446 429
            Node xnode = order_list[xn];
447 430

	
448 431
            int yn = node_data[rn].prev;
449 432
            Node ynode = order_list[yn];
450 433

	
451 434
            bool rd;
452
            if (!external(xnode, rorder, child_lists, ancestor_map, low_map)) {
435
              if (!external(xnode, rorder, child_lists, 
436
                            ancestor_map, low_map)) {
453 437
              rd = true;
454 438
            } else if (!external(ynode, rorder, child_lists,
455 439
                                 ancestor_map, low_map)) {
456 440
              rd = false;
457 441
            } else if (pertinent(xnode, embed_arc, merge_roots)) {
458 442
              rd = true;
459 443
            } else {
460 444
              rd = false;
461 445
            }
462 446

	
463 447
            merge_stack.push_back(std::make_pair(rn, rd));
464 448

	
... ...
@@ -518,24 +502,40 @@
518 502
      if (ancestor_map[node] < rorder) return true;
519 503

	
520 504
      return false;
521 505
    }
522 506

	
523 507
    bool pertinent(const Node& node, const EmbedArc& embed_arc,
524 508
                   const MergeRoots& merge_roots) {
525 509
      return !merge_roots[node].empty() || embed_arc[node];
526 510
    }
527 511

	
528 512
  };
529 513

	
514
  }
515

	
516
  /// \ingroup planar
517
  ///
518
  /// \brief Planarity checking of an undirected simple graph
519
  ///
520
  /// This function implements the Boyer-Myrvold algorithm for
521
  /// planarity checking of an undirected graph. It is a simplified
522
  /// version of the PlanarEmbedding algorithm class because neither
523
  /// the embedding nor the kuratowski subdivisons are not computed.
524
  template <typename GR>
525
  bool checkPlanarity(const GR& graph) {
526
    _planarity_bits::PlanarityChecking<GR> pc(graph);
527
    return pc.run();
528
  }
529

	
530 530
  /// \ingroup planar
531 531
  ///
532 532
  /// \brief Planar embedding of an undirected simple graph
533 533
  ///
534 534
  /// This class implements the Boyer-Myrvold algorithm for planar
535 535
  /// embedding of an undirected graph. The planar embedding is an
536 536
  /// ordering of the outgoing edges of the nodes, which is a possible
537 537
  /// configuration to draw the graph in the plane. If there is not
538 538
  /// such ordering then the graph contains a \f$ K_5 \f$ (full graph
539 539
  /// with 5 nodes) or a \f$ K_{3,3} \f$ (complete bipartite graph on
540 540
  /// 3 ANode and 3 BNode) subdivision.
541 541
  ///
... ...
@@ -703,25 +703,25 @@
703 703
    ///
704 704
    /// Gives back the successor of an arc. This function makes
705 705
    /// possible to query the cyclic order of the outgoing arcs from
706 706
    /// a node.
707 707
    Arc next(const Arc& arc) const {
708 708
      return _embedding[arc];
709 709
    }
710 710

	
711 711
    /// \brief Gives back the calculated embedding map
712 712
    ///
713 713
    /// The returned map contains the successor of each arc in the
714 714
    /// graph.
715
    const EmbeddingMap& embedding() const {
715
    const EmbeddingMap& embeddingMap() const {
716 716
      return _embedding;
717 717
    }
718 718

	
719 719
    /// \brief Gives back true if the undirected arc is in the
720 720
    /// kuratowski subdivision
721 721
    ///
722 722
    /// Gives back true if the undirected arc is in the kuratowski
723 723
    /// subdivision
724 724
    /// \note The \c run() had to be called with true value.
725 725
    bool kuratowski(const Edge& edge) {
726 726
      return _kuratowski[edge];
727 727
    }
Show white space 24 line context
... ...
@@ -230,30 +230,33 @@
230 230

	
231 231
int main() {
232 232

	
233 233
  for (int i = 0; i < lgfn; ++i) {
234 234
    std::istringstream lgfs(lgf[i]);
235 235

	
236 236
    SmartGraph graph;
237 237
    graphReader(graph, lgfs).run();
238 238

	
239 239
    check(simpleGraph(graph), "Test graphs must be simple");
240 240

	
241 241
    PE pe(graph);
242
    if (pe.run()) {
242
    bool planar = pe.run();
243
    check(checkPlanarity(graph) == planar, "Planarity checking failed");
244

	
245
    if (planar) {
243 246
      checkEmbedding(graph, pe);
244 247

	
245 248
      PlanarDrawing<Graph> pd(graph);
246
      pd.run(pe.embedding());
249
      pd.run(pe.embeddingMap());
247 250
      checkDrawing(graph, pd);
248 251

	
249 252
      PlanarColoring<Graph> pc(graph);
250
      pc.runFiveColoring(pe.embedding());
253
      pc.runFiveColoring(pe.embeddingMap());
251 254
      checkColoring(graph, pc, 5);
252 255

	
253 256
    } else {
254 257
      checkKuratowski(graph, pe);
255 258
    }
256 259
  }
257 260

	
258 261
  return 0;
259 262
}
0 comments (0 inline)