traitgraph_dimacs_io/
lib.rs

1#![warn(missing_docs)]
2//! This crate offers functions to read and write graphs in TSPLIB format.
3
4use std::io::Write;
5use traitgraph::index::GraphIndex;
6use traitgraph::interface::StaticGraph;
7
8/// Write the graph in the following format, ignoring node and edge data.
9///
10/// ```text
11/// <node count> <edge count>
12/// <from node> <to node>
13/// ```
14///
15/// The second line is repeated for each edge.
16pub fn write_topology<Graph: StaticGraph, Writer: Write>(graph: &Graph, writer: &mut Writer) {
17    writeln!(writer, "{} {}", graph.node_count(), graph.edge_count()).unwrap();
18    for node in graph.node_indices() {
19        for out_neighbor in graph.out_neighbors(node) {
20            writeln!(
21                writer,
22                "{} {}",
23                node.as_usize(),
24                out_neighbor.node_id.as_usize()
25            )
26            .unwrap();
27        }
28    }
29}