graph_canon/
dense.rs

1use nauty_Traces_sys::{empty_graph, ADDONEARC, SETWORDSNEEDED};
2use petgraph::{visit::GetAdjacencyMatrix, EdgeType, Graph};
3use std::ffi::c_int;
4
5#[derive(Debug, Eq, PartialEq, Hash)]
6pub struct DenseGraph {
7    pub g: Vec<u64>,
8    pub n: usize,
9    pub e: usize,
10    pub m: usize,
11    pub nodes: Nodes,
12}
13impl DenseGraph {
14    pub fn from_petgraph<N, E, Ty>(graph: &Graph<N, E, Ty>) -> Self
15    where
16        Ty: EdgeType,
17    {
18        let n = graph.node_count();
19        let e = graph.edge_count();
20        let m = SETWORDSNEEDED(n);
21        let nodes = Nodes::new(n);
22        let mut g = empty_graph(m, n);
23        let adj = graph.adjacency_matrix();
24        for idx in 0..n {
25            for jdx in 0..n {
26                if adj.contains(idx * n + jdx) {
27                    ADDONEARC(&mut g, idx, jdx, m)
28                }
29            }
30        }
31        Self { g, n, e, m, nodes }
32    }
33
34    pub fn orbits(&self) -> &[i32] {
35        &self.nodes.orbits
36    }
37}
38
39#[derive(Debug, Eq, PartialEq, Hash)]
40pub struct Nodes {
41    pub lab: Vec<c_int>,
42    pub ptn: Vec<c_int>,
43    pub orbits: Vec<c_int>,
44}
45impl Nodes {
46    pub fn new(n: usize) -> Self {
47        Self {
48            lab: (0..n as i32).collect(),
49            ptn: vec![0; n],
50            orbits: vec![0; n],
51        }
52    }
53}