... | ... |
@@ -10,110 +10,110 @@ |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_NAUTY_READER_H |
20 | 20 |
#define LEMON_NAUTY_READER_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <iostream> |
24 | 24 |
#include <string> |
25 | 25 |
|
26 | 26 |
/// \ingroup io_group |
27 | 27 |
/// |
28 | 28 |
/// @defgroup nauty_group NAUTY format |
29 | 29 |
/// |
30 | 30 |
/// \brief Read \e Nauty format |
31 | 31 |
/// |
32 | 32 |
/// Tool to read graphs from \e Nauty format data |
33 | 33 |
|
34 | 34 |
/// \ingroup nauty_group |
35 | 35 |
/// \file |
36 | 36 |
/// \brief Nauty file reader. |
37 | 37 |
namespace lemon { |
38 | 38 |
|
39 | 39 |
/// \ingroup nauty_group |
40 | 40 |
/// |
41 | 41 |
/// \brief Nauty file reader |
42 | 42 |
/// |
43 | 43 |
/// The \e geng program is in the \e gtools suite of the nauty |
44 | 44 |
/// package. This tool can generate all non-isomorphic undirected |
45 | 45 |
/// graphs with given node number from several classes (for example, |
46 | 46 |
/// general, connected, biconnected, triangle-free, 4-cycle-free, |
47 | 47 |
/// bipartite and graphs with given edge number and degree |
48 | 48 |
/// constraints). This function reads a \e nauty \e graph6 \e format |
49 | 49 |
/// line from the given stream and builds it in the given graph. |
50 | 50 |
/// |
51 | 51 |
/// The site of nauty package: http://cs.anu.edu.au/~bdm/nauty/ |
52 | 52 |
/// |
53 | 53 |
/// For example, the number of all non-isomorphic connected graphs |
54 | 54 |
/// can be computed with following code. |
55 | 55 |
///\code |
56 | 56 |
/// int num = 0; |
57 | 57 |
/// SmartGraph graph; |
58 |
/// while(readNauty(std::cin, graph)) { |
|
59 |
/// PlanarityChecking<SmartUGraph> pc(graph); |
|
58 |
/// while (readNauty(graph, std::cin)) { |
|
59 |
/// PlanarityChecking<SmartGraph> pc(graph); |
|
60 | 60 |
/// if (pc.run()) ++num; |
61 | 61 |
/// } |
62 | 62 |
/// std::cout << "Number of planar graphs: " << num << std::endl; |
63 | 63 |
///\endcode |
64 | 64 |
/// |
65 | 65 |
/// The nauty files are quite huge, therefore instead of the direct |
66 | 66 |
/// file generation the pipelining is recommended. |
67 | 67 |
///\code |
68 | 68 |
/// ./geng -c 10 | ./num_of_pg |
69 | 69 |
///\endcode |
70 | 70 |
template <typename Graph> |
71 |
std::istream& readNauty(std::istream& is |
|
71 |
std::istream& readNauty(Graph& graph, std::istream& is) { |
|
72 | 72 |
graph.clear(); |
73 | 73 |
|
74 | 74 |
std::string line; |
75 | 75 |
if (getline(is, line)) { |
76 | 76 |
int index = 0; |
77 | 77 |
|
78 | 78 |
int n; |
79 | 79 |
|
80 | 80 |
if (line[index] == '>') { |
81 | 81 |
index += 10; |
82 | 82 |
} |
83 | 83 |
|
84 | 84 |
char c = line[index++]; c -= 63; |
85 | 85 |
if (c != 63) { |
86 | 86 |
n = int(c); |
87 | 87 |
} else { |
88 | 88 |
c = line[index++]; c -= 63; |
89 | 89 |
n = (int(c) << 12); |
90 | 90 |
c = line[index++]; c -= 63; |
91 | 91 |
n |= (int(c) << 6); |
92 | 92 |
c = line[index++]; c -= 63; |
93 | 93 |
n |= int(c); |
94 | 94 |
} |
95 | 95 |
|
96 | 96 |
std::vector<typename Graph::Node> nodes; |
97 | 97 |
for (int i = 0; i < n; ++i) { |
98 | 98 |
nodes.push_back(graph.addNode()); |
99 | 99 |
} |
100 | 100 |
|
101 | 101 |
int bit = -1; |
102 | 102 |
for (int j = 0; j < n; ++j) { |
103 | 103 |
for (int i = 0; i < j; ++i) { |
104 | 104 |
if (bit == -1) { |
105 | 105 |
c = line[index++]; c -= 63; |
106 | 106 |
bit = 5; |
107 | 107 |
} |
108 | 108 |
bool b = (c & (1 << (bit--))) != 0; |
109 | 109 |
|
110 | 110 |
if (b) { |
111 | 111 |
graph.addEdge(nodes[i], nodes[j]); |
112 | 112 |
} |
113 | 113 |
} |
114 | 114 |
} |
115 | 115 |
} |
116 | 116 |
return is; |
117 | 117 |
} |
118 | 118 |
} |
119 | 119 |
|
0 comments (0 inline)