16 #ifndef UTIL_GRAPH_IO_H_
17 #define UTIL_GRAPH_IO_H_
25 #include "absl/status/status.h"
26 #include "absl/status/statusor.h"
27 #include "absl/strings/numbers.h"
28 #include "absl/strings/str_cat.h"
29 #include "absl/strings/str_format.h"
30 #include "absl/strings/str_join.h"
31 #include "absl/strings/str_split.h"
49 template <
class Graph>
82 template <
class Graph>
84 const std::string& filename,
bool directed,
85 std::vector<int>* num_nodes_with_color_or_null);
99 template <
class Graph>
102 const std::vector<int>& num_nodes_with_color);
106 template <
class Graph>
109 std::vector<typename Graph::NodeIndex> adj;
113 if (!out.empty()) out +=
'\n';
114 absl::StrAppend(&out, node,
"->", graph.
Head(arc));
119 adj.push_back(graph.
Head(arc));
122 std::sort(adj.begin(), adj.end());
124 if (node != 0) out +=
'\n';
125 absl::StrAppend(&out, node,
": ", absl::StrJoin(adj,
" "));
131 template <
class Graph>
133 const std::string& filename,
bool directed,
134 std::vector<int>* num_nodes_with_color_or_null) {
135 std::unique_ptr<Graph> graph;
136 int64 num_nodes = -1;
137 int64 num_expected_lines = -1;
138 int64 num_lines_read = 0;
139 for (
const std::string& line :
FileLines(filename)) {
141 if (num_lines_read == 1) {
142 std::vector<int64> header_ints;
152 num_nodes = header_ints[0];
153 num_expected_lines = header_ints[1];
154 if (num_nodes_with_color_or_null !=
nullptr) {
155 num_nodes_with_color_or_null->clear();
156 if (header_ints.size() == 2) {
158 num_nodes_with_color_or_null->push_back(num_nodes);
160 const int num_colors = header_ints[2];
161 if (header_ints.size() != num_colors + 2) {
163 absl::StatusCode::kInvalidArgument,
165 "There should be num_colors-1 color cardinalities in the"
167 filename,
"' (where num_colors=", num_colors,
168 "): the last color cardinality should be",
" skipped."));
170 num_nodes_with_color_or_null->reserve(num_colors);
171 int num_nodes_left = num_nodes;
172 for (
int i = 3; i < header_ints.size(); ++i) {
173 num_nodes_with_color_or_null->push_back(header_ints[i]);
174 num_nodes_left -= header_ints[i];
175 if (header_ints[i] <= 0 || num_nodes_left <= 0) {
177 absl::StatusCode::kInvalidArgument,
179 "The color cardinalities in the header of '", filename,
180 " should always be >0 and add up to less than the"
181 " total number of nodes."));
184 num_nodes_with_color_or_null->push_back(num_nodes_left);
187 const int64 num_arcs = (directed ? 1 : 2) * num_expected_lines;
188 graph.reset(
new Graph(num_nodes, num_arcs));
193 if (sscanf(line.c_str(),
"%ld %ld", &node1, &node2) != 2 || node1 < 0 ||
194 node2 < 0 || node1 >= num_nodes || node2 >= num_nodes) {
196 absl::StatusCode::kInvalidArgument,
197 absl::StrCat(
"In '", filename,
"', line ", num_lines_read,
198 ": Expected two",
" integers in the range [0, ",
205 if (num_lines_read > num_expected_lines + 1)
continue;
206 graph->
AddArc(node1, node2);
207 if (!directed && node1 != node2) graph->
AddArc(node2, node1);
209 if (num_lines_read == 0) {
210 return absl::Status(absl::StatusCode::kInvalidArgument,
211 "Unknown or empty file");
213 if (num_lines_read != num_expected_lines + 1) {
214 return absl::Status(absl::StatusCode::kInvalidArgument,
215 absl::StrCat(
"The number of arcs/edges in '", filename,
216 "' (", num_lines_read - 1,
217 " does not match the value announced in",
218 " the header (", num_expected_lines,
")"));
221 return graph.release();
224 template <
class Graph>
227 const std::vector<int>& num_nodes_with_color) {
228 FILE* f = fopen(filename.c_str(),
"w");
230 return absl::Status(absl::StatusCode::kInvalidArgument,
231 "Could not open file: '" + filename +
"'");
235 int num_self_arcs = 0;
239 if (graph.
Head(arc) == node) ++num_self_arcs;
242 if ((graph.
num_arcs() - num_self_arcs) % 2 != 0) {
244 return absl::Status(absl::StatusCode::kInvalidArgument,
245 "WriteGraphToFile() called with directed=false"
246 " and with a graph with an odd number of (non-self)"
253 : (graph.
num_arcs() + num_self_arcs) / 2));
254 if (!num_nodes_with_color.empty()) {
255 if (std::accumulate(num_nodes_with_color.begin(),
256 num_nodes_with_color.end(), 0) != graph.
num_nodes() ||
257 *std::min_element(num_nodes_with_color.begin(),
258 num_nodes_with_color.end()) <= 0) {
259 return absl::Status(absl::StatusCode::kInvalidArgument,
260 "WriteGraphToFile() called with invalid coloring.");
262 fprintf(f,
" %lu", num_nodes_with_color.size());
263 for (
int i = 0; i < num_nodes_with_color.size() - 1; ++i) {
264 absl::FPrintF(f,
" %d",
static_cast<int64>(num_nodes_with_color[i]));
272 if (directed ||
head >= node) {
273 absl::FPrintF(f,
"%d %d\n",
static_cast<int64>(node),
278 if (fclose(f) != 0) {
279 return absl::Status(absl::StatusCode::kInternal,
280 "Could not close file '" + filename +
"'");
282 return ::absl::OkStatus();
287 #endif // UTIL_GRAPH_IO_H_