1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_NAUTY_READER_H
20 #define LEMON_NAUTY_READER_H
28 /// @defgroup nauty_group NAUTY format
30 /// \brief Read \e Nauty format
32 /// Tool to read graphs from \e Nauty format data
34 /// \ingroup nauty_group
36 /// \brief Nauty file reader.
39 /// \ingroup nauty_group
41 /// \brief Nauty file reader
43 /// The \e geng program is in the \e gtools suite of the nauty
44 /// package. This tool can generate all non-isomorphic undirected
45 /// graphs with given node number from several classes (for example,
46 /// general, connected, biconnected, triangle-free, 4-cycle-free,
47 /// bipartite and graphs with given edge number and degree
48 /// constraints). This function reads a \e nauty \e graph6 \e format
49 /// line from the given stream and builds it in the given graph.
51 /// The site of nauty package: http://cs.anu.edu.au/~bdm/nauty/
53 /// For example, the number of all non-isomorphic connected graphs
54 /// can be computed with following code.
58 /// while(readNauty(std::cin, graph)) {
59 /// PlanarityChecking<SmartUGraph> pc(graph);
60 /// if (pc.run()) ++num;
62 /// std::cout << "Number of planar graphs: " << num << std::endl;
65 /// The nauty files are quite huge, therefore instead of the direct
66 /// file generation the pipelining is recommended.
68 /// ./geng -c 10 | ./num_of_pg
70 template <typename Graph>
71 std::istream& readNauty(std::istream& is, Graph& graph) {
75 if (getline(is, line)) {
80 if (line[index] == '>') {
84 char c = line[index++]; c -= 63;
88 c = line[index++]; c -= 63;
90 c = line[index++]; c -= 63;
92 c = line[index++]; c -= 63;
96 std::vector<typename Graph::Node> nodes;
97 for (int i = 0; i < n; ++i) {
98 nodes.push_back(graph.addNode());
102 for (int j = 0; j < n; ++j) {
103 for (int i = 0; i < j; ++i) {
105 c = line[index++]; c -= 63;
108 bool b = (c & (1 << (bit--))) != 0;
111 graph.addEdge(nodes[i], nodes[j]);