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