lemon/nauty_reader.h
changeset 784 1a7fe3bef514
parent 359 0eec1736ff1d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/nauty_reader.h	Thu Nov 05 15:50:01 2009 +0100
     1.3 @@ -0,0 +1,113 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2009
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#ifndef LEMON_NAUTY_READER_H
    1.23 +#define LEMON_NAUTY_READER_H
    1.24 +
    1.25 +#include <vector>
    1.26 +#include <iostream>
    1.27 +#include <string>
    1.28 +
    1.29 +/// \ingroup nauty_group
    1.30 +/// \file
    1.31 +/// \brief Nauty file reader.
    1.32 +
    1.33 +namespace lemon {
    1.34 +
    1.35 +  /// \ingroup nauty_group
    1.36 +  ///
    1.37 +  /// \brief Nauty file reader
    1.38 +  ///
    1.39 +  /// The \e geng program is in the \e gtools suite of the nauty
    1.40 +  /// package. This tool can generate all non-isomorphic undirected
    1.41 +  /// graphs of several classes with given node number (e.g.
    1.42 +  /// general, connected, biconnected, triangle-free, 4-cycle-free,
    1.43 +  /// bipartite and graphs with given edge number and degree
    1.44 +  /// constraints). This function reads a \e nauty \e graph6 \e format
    1.45 +  /// line from the given stream and builds it in the given graph.
    1.46 +  ///
    1.47 +  /// The site of nauty package: http://cs.anu.edu.au/~bdm/nauty/
    1.48 +  ///
    1.49 +  /// For example, the number of all non-isomorphic planar graphs
    1.50 +  /// can be computed with the following code.
    1.51 +  ///\code
    1.52 +  /// int num = 0;
    1.53 +  /// SmartGraph graph;
    1.54 +  /// while (readNautyGraph(graph, std::cin)) {
    1.55 +  ///   PlanarityChecking<SmartGraph> pc(graph);
    1.56 +  ///   if (pc.run()) ++num;
    1.57 +  /// }
    1.58 +  /// std::cout << "Number of planar graphs: " << num << std::endl;
    1.59 +  ///\endcode
    1.60 +  ///
    1.61 +  /// The nauty files are quite huge, therefore instead of the direct
    1.62 +  /// file generation pipelining is recommended. For example,
    1.63 +  ///\code
    1.64 +  /// ./geng -c 10 | ./num_of_planar_graphs
    1.65 +  ///\endcode
    1.66 +  template <typename Graph>
    1.67 +  std::istream& readNautyGraph(Graph& graph, std::istream& is = std::cin) {
    1.68 +    graph.clear();
    1.69 +
    1.70 +    std::string line;
    1.71 +    if (getline(is, line)) {
    1.72 +      int index = 0;
    1.73 +
    1.74 +      int n;
    1.75 +
    1.76 +      if (line[index] == '>') {
    1.77 +        index += 10;
    1.78 +      }
    1.79 +
    1.80 +      char c = line[index++]; c -= 63;
    1.81 +      if (c != 63) {
    1.82 +        n = int(c);
    1.83 +      } else {
    1.84 +        c = line[index++]; c -= 63;
    1.85 +        n = (int(c) << 12);
    1.86 +        c = line[index++]; c -= 63;
    1.87 +        n |= (int(c) << 6);
    1.88 +        c = line[index++]; c -= 63;
    1.89 +        n |= int(c);
    1.90 +      }
    1.91 +
    1.92 +      std::vector<typename Graph::Node> nodes;
    1.93 +      for (int i = 0; i < n; ++i) {
    1.94 +        nodes.push_back(graph.addNode());
    1.95 +      }
    1.96 +
    1.97 +      int bit = -1;
    1.98 +      for (int j = 0; j < n; ++j) {
    1.99 +        for (int i = 0; i < j; ++i) {
   1.100 +          if (bit == -1) {
   1.101 +            c = line[index++]; c -= 63;
   1.102 +            bit = 5;
   1.103 +          }
   1.104 +          bool b = (c & (1 << (bit--))) != 0;
   1.105 +
   1.106 +          if (b) {
   1.107 +            graph.addEdge(nodes[i], nodes[j]);
   1.108 +          }
   1.109 +        }
   1.110 +      }
   1.111 +    }
   1.112 +    return is;
   1.113 +  }
   1.114 +}
   1.115 +
   1.116 +#endif