104 absl::Span<const int> num_nodes_with_color) {
105 FILE* f = fopen(filename.c_str(),
"w");
107 return absl::Status(absl::StatusCode::kInvalidArgument,
108 "Could not open file: '" + filename +
"'");
112 int num_self_arcs = 0;
116 if (
graph.Head(
arc) == node) ++num_self_arcs;
119 if ((
graph.num_arcs() - num_self_arcs) % 2 != 0) {
121 return absl::Status(absl::StatusCode::kInvalidArgument,
122 "WriteGraphToFile() called with directed=false"
123 " and with a graph with an odd number of (non-self)"
128 f,
"%d %d",
static_cast<int64_t
>(
graph.num_nodes()),
129 static_cast<int64_t
>(directed ?
graph.num_arcs()
130 : (
graph.num_arcs() + num_self_arcs) / 2));
131 if (!num_nodes_with_color.empty()) {
132 if (std::accumulate(num_nodes_with_color.begin(),
133 num_nodes_with_color.end(), 0) !=
graph.num_nodes() ||
134 *std::min_element(num_nodes_with_color.begin(),
135 num_nodes_with_color.end()) <= 0) {
136 return absl::Status(absl::StatusCode::kInvalidArgument,
137 "WriteGraphToFile() called with invalid coloring.");
139 absl::FPrintF(f,
" %d", num_nodes_with_color.size());
140 for (
int i = 0; i < num_nodes_with_color.size() - 1; ++i) {
141 absl::FPrintF(f,
" %d",
static_cast<int64_t
>(num_nodes_with_color[i]));
144 absl::FPrintF(f,
"\n");
149 if (directed ||
head >= node) {
150 absl::FPrintF(f,
"%d %d\n",
static_cast<int64_t
>(node),
151 static_cast<uint64_t
>(
head));
156 if (fclose(f) != 0) {
157 return absl::Status(absl::StatusCode::kInternal,
158 "Could not close file '" + filename +
"'");
161 return ::absl::OkStatus();