causal_hub/models/graphs/
mod.rs

1mod directed;
2pub use directed::*;
3
4mod undirected;
5use ndarray::prelude::*;
6pub use undirected::*;
7
8use crate::types::{Labels, Set};
9
10/// A trait for graphs.
11pub trait Graph {
12    /// Creates an empty graph with the given labels.
13    ///
14    /// # Arguments
15    ///
16    /// * `labels` - The labels of the vertices in the graph.
17    ///
18    /// # Notes
19    ///
20    /// * Labels will be sorted in alphabetical order.
21    ///
22    /// # Panics
23    ///
24    /// * If the labels are not unique.
25    ///
26    /// # Returns
27    ///
28    /// A new graph instance.
29    ///
30    fn empty<I, V>(labels: I) -> Self
31    where
32        I: IntoIterator<Item = V>,
33        V: AsRef<str>;
34
35    /// Creates a complete graph with the given labels.
36    ///
37    /// # Arguments
38    ///
39    /// * `labels` - The labels of the vertices in the graph.
40    ///
41    /// # Notes
42    ///
43    /// * Labels will be sorted in alphabetical order.
44    /// * No self-loops are created.
45    ///
46    /// # Panics
47    ///
48    /// * If the labels are not unique.
49    ///
50    /// # Returns
51    ///
52    /// A new graph instance.
53    ///
54    fn complete<I, V>(labels: I) -> Self
55    where
56        I: IntoIterator<Item = V>,
57        V: AsRef<str>;
58
59    /// Returns the iterator of vertices in the graph.
60    ///
61    /// # Returns
62    ///
63    /// A set representing the vertices in the graph.
64    ///
65    fn vertices(&self) -> Set<usize>;
66
67    /// Checks if a vertex exists in the graph.
68    ///
69    /// # Arguments
70    ///
71    /// * `x` - The index of the vertex.
72    ///
73    /// # Returns
74    ///
75    /// `true` if the vertex exists, `false` otherwise.
76    ///
77    fn has_vertex(&self, x: usize) -> bool;
78
79    /// Returns the iterator of edges in the graph.
80    ///
81    /// # Returns
82    ///
83    /// A set of tuples representing the edges in the graph.
84    ///
85    fn edges(&self) -> Set<(usize, usize)>;
86
87    /// Checks if there is an edge between vertices `x` and `y`.
88    ///
89    /// # Arguments
90    ///
91    /// * `x` - The first vertex.
92    /// * `y` - The second vertex.
93    ///
94    /// # Panics
95    ///
96    /// * If any of the vertices are out of bounds.
97    ///
98    /// # Returns
99    ///
100    /// `true` if there is an edge between `x` and `y`, `false` otherwise.
101    ///
102    fn has_edge(&self, x: usize, y: usize) -> bool;
103
104    /// Adds an edge between vertices `x` and `y`.
105    ///
106    /// # Arguments
107    ///
108    /// * `x` - The first vertex.
109    /// * `y` - The second vertex.
110    ///
111    /// # Panics
112    ///
113    /// * If any of the vertices are out of bounds.
114    ///
115    /// # Returns
116    ///
117    /// `true` if the edge was added, `false` if it already existed.
118    ///
119    fn add_edge(&mut self, x: usize, y: usize) -> bool;
120
121    /// Deletes the edge between vertices `x` and `y`.
122    ///
123    /// # Arguments
124    ///
125    /// * `x` - The first vertex.
126    /// * `y` - The second vertex.
127    ///
128    /// # Panics
129    ///
130    /// * If any of the vertices are out of bounds.
131    ///
132    /// # Returns
133    ///
134    /// `true` if the edge was deleted, `false` if it did not exist.
135    ///
136    fn del_edge(&mut self, x: usize, y: usize) -> bool;
137
138    /// Creates a graph from an adjacency matrix and labels.
139    ///
140    /// # Arguments
141    ///
142    /// * `labels` - An iterator over the labels of the vertices.
143    /// * `adjacency_matrix` - A reference to a 2D array representing the adjacency matrix.
144    ///
145    /// # Returns
146    ///
147    /// A new graph instance.
148    ///
149    fn from_adjacency_matrix(labels: Labels, adjacency_matrix: Array2<bool>) -> Self;
150
151    /// Converts the graph to an adjacency matrix.
152    ///
153    /// # Returns
154    ///
155    /// A 2D array representing the adjacency matrix of the graph.
156    ///
157    fn to_adjacency_matrix(&self) -> Array2<bool>;
158}