117 absl::Span<const int> num_nodes_with_color) {
118 FILE* f = fopen(filename.c_str(),
"w");
120 return absl::Status(absl::StatusCode::kInvalidArgument,
121 "Could not open file: '" + filename +
"'");
125 int num_self_arcs = 0;
129 if (
graph.Head(arc) == node) ++num_self_arcs;
132 if ((
graph.num_arcs() - num_self_arcs) % 2 != 0) {
134 return absl::Status(absl::StatusCode::kInvalidArgument,
135 "WriteGraphToFile() called with directed=false"
136 " and with a graph with an odd number of (non-self)"
141 f,
"%d %d",
static_cast<int64_t
>(
graph.num_nodes()),
142 static_cast<int64_t
>(directed ?
graph.num_arcs()
143 : (
graph.num_arcs() + num_self_arcs) / 2));
144 if (!num_nodes_with_color.empty()) {
145 if (std::accumulate(num_nodes_with_color.begin(),
146 num_nodes_with_color.end(), 0) !=
graph.num_nodes() ||
147 *std::min_element(num_nodes_with_color.begin(),
148 num_nodes_with_color.end()) <= 0) {
149 return absl::Status(absl::StatusCode::kInvalidArgument,
150 "WriteGraphToFile() called with invalid coloring.");
152 absl::FPrintF(f,
" %d", num_nodes_with_color.size());
153 for (
int i = 0; i < num_nodes_with_color.size() - 1; ++i) {
154 absl::FPrintF(f,
" %d",
static_cast<int64_t
>(num_nodes_with_color[i]));
157 absl::FPrintF(f,
"\n");
162 if (directed || head >= node) {
163 absl::FPrintF(f,
"%d %d\n",
static_cast<int64_t
>(node),
164 static_cast<uint64_t
>(head));
169 if (fclose(f) != 0) {
170 return absl::Status(absl::StatusCode::kInternal,
171 "Could not close file '" + filename +
"'");
174 return ::absl::OkStatus();