graph6_rs/
conversion.rs

1/// Conversion trait for graphs into various text graph formats
2pub trait GraphConversion {
3    /// Returns the bitvector representation of the graph
4    fn bit_vec(&self) -> &[usize];
5
6    /// Returns the number of vertices in the graph
7    fn size(&self) -> usize;
8
9    /// Returns true if the graph is directed
10    fn is_directed(&self) -> bool;
11
12    /// Returns the graph in the DOT format
13    fn to_dot(&self, id: Option<usize>) -> String {
14        let n = self.size();
15        let bit_vec = self.bit_vec();
16
17        let mut dot = String::new();
18
19        // include graph type
20        if self.is_directed() {
21            dot.push_str("digraph ");
22        } else {
23            dot.push_str("graph ");
24        }
25
26        // include graph id
27        if let Some(id) = id {
28            dot.push_str(&format!("graph_{} {{", id));
29        } else {
30            dot.push('{');
31        }
32
33        // include edges
34        if self.is_directed() {
35            self.to_directed_dot(&mut dot, bit_vec, n);
36        } else {
37            self.to_undirected_dot(&mut dot, bit_vec, n);
38        }
39
40        // close graph
41        dot.push_str("\n}");
42
43        dot
44    }
45
46    fn to_undirected_dot(&self, dot: &mut String, bit_vec: &[usize], n: usize) {
47        for i in 0..n {
48            for j in i..n {
49                if bit_vec[i * n + j] == 1 {
50                    dot.push_str(&format!("\n{} -- {};", i, j));
51                }
52            }
53        }
54    }
55
56    fn to_directed_dot(&self, dot: &mut String, bit_vec: &[usize], n: usize) {
57        for i in 0..n {
58            for j in 0..n {
59                if bit_vec[i * n + j] == 1 {
60                    dot.push_str(&format!("\n{} -> {};", i, j));
61                }
62            }
63        }
64    }
65
66    /// Returns the graph as an adjacency matrix
67    fn to_adjmat(&self) -> String {
68        let n = self.size();
69        let bit_vec = self.bit_vec();
70
71        let mut adj = String::new();
72        for i in 0..n {
73            for j in 0..n {
74                adj.push_str(&format!("{}", bit_vec[i * n + j]));
75                if j < n - 1 {
76                    adj.push(' ');
77                }
78            }
79            adj.push('\n');
80        }
81        adj
82    }
83
84    /// Returns the graph in a flat adjacency matrix
85    fn to_flat(&self) -> String {
86        let n = self.size();
87        let bit_vec = self.bit_vec();
88
89        let mut flat = String::new();
90        for i in 0..n {
91            for j in 0..n {
92                flat.push_str(&format!("{}", bit_vec[i * n + j]));
93            }
94        }
95        flat
96    }
97
98    /// Returns the graph in the Pajek NET format
99    fn to_net(&self) -> String {
100        let n = self.size();
101        let bit_vec = self.bit_vec();
102
103        let mut net = String::new();
104        net.push_str(&format!("*Vertices {}\n", n));
105        for i in 0..n {
106            net.push_str(&format!("{} \"{}\"\n", i + 1, i));
107        }
108        net.push_str("*Arcs\n");
109        for i in 0..n {
110            for j in 0..n {
111                if bit_vec[i * n + j] == 1 {
112                    net.push_str(&format!("{} {}\n", i + 1, j + 1));
113                }
114            }
115        }
116        net
117    }
118}