gryf 0.2.1

Graph data structure library with focus on convenience, versatility, correctness and performance.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Find (strongly) [connected] components in a graph.
//!
//! See available parameters [here](ConnectedComponentsBuilder#implementations)
//! or [here](StronglyConnectedComponentsBuilder#implementations).
//!
//! # Examples
//!
//! ```
//! use gryf::{Graph, algo::{is_connected, is_strongly_connected}};
//!
//! let mut graph = Graph::new_directed();
//!
//! let a = graph.add_vertex("a");
//! let b = graph.add_vertex("b");
//! let c = graph.add_vertex("c");
//! let d = graph.add_vertex("d");
//!
//! graph.add_edge(a, b, ());
//! graph.add_edge(b, c, ());
//! graph.add_edge(c, d, ());
//!
//! // Weak connectivity is checked on directed graphs by default.
//! assert!(is_connected(&graph));
//! assert!(!is_strongly_connected(&graph));
//!
//! graph.add_edge(d, a, ());
//! assert!(is_strongly_connected(&graph));
//! ```
//!
//! [connected]: https://en.wikipedia.org/wiki/Connectivity_(graph_theory)

use crate::core::GraphBase;

mod builder;
mod dfs;
mod kosaraju;

pub use builder::{ConnectedComponentsBuilder, StronglyConnectedComponentsBuilder};

/// Connected components of a graph.
///
/// See [module](self) documentation for more details and example.
#[derive(Debug)]
pub struct ConnectedComponents<G: GraphBase> {
    components: Vec<Vec<G::VertexId>>,
}

/// Strongly connected components of a graph.
///
/// See [module](self) documentation for more details and example.
#[derive(Debug)]
pub struct StronglyConnectedComponents<G: GraphBase> {
    inner: ConnectedComponents<G>,
}

impl<G> ConnectedComponents<G>
where
    G: GraphBase,
{
    /// Returns the number of components.
    #[allow(clippy::len_without_is_empty)]
    pub fn len(&self) -> usize {
        self.components.len()
    }

    /// Returns an iterator of the components.
    pub fn iter(&self) -> Iter<'_, G> {
        Iter {
            inner: self.components.iter(),
        }
    }
}

impl<G> StronglyConnectedComponents<G>
where
    G: GraphBase,
{
    /// Returns the number of components.
    #[allow(clippy::len_without_is_empty)]
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Returns an iterator of the components.
    pub fn iter(&self) -> Iter<'_, G> {
        self.inner.iter()
    }
}

pub struct Iter<'a, G: GraphBase> {
    inner: std::slice::Iter<'a, Vec<G::VertexId>>,
}

impl<'a, G> Iterator for Iter<'a, G>
where
    G: GraphBase,
{
    type Item = &'a [G::VertexId];

    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next().map(|component| component.as_slice())
    }
}

/// Algorithm for [`ConnectedComponents`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum AlgoWeak {
    /// Standard DFS algorithm for traversing the graph.
    ///
    /// # Use cases
    ///
    /// * Finding groups where elements relate to each other.
    Dfs,
}

/// Algorithm for [`StronglyConnectedComponents`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum AlgoStrong {
    /// [Kosaraju's
    /// algorithm](https://en.wikipedia.org/wiki/Kosaraju%27s_algorithm).
    ///
    /// Kosaraju's algorithm is an iterative algorithm with two passes, one DFS
    /// pass on the graph collecting the sequence of closed vertices and one DFS
    /// pass on the transposed graph traversing the graph in the reversed order
    /// of vertices collected in the first pass.
    ///
    /// # Use cases
    ///
    /// * Finding groups where elements transitively depend on each other.
    Kosaraju,
}

mod algo {
    use super::{AlgoStrong, AlgoWeak};

    #[derive(Debug)]
    pub struct AnyAlgo;

    #[derive(Debug)]
    pub struct SpecificAlgoWeak(pub Option<AlgoWeak>);

    #[derive(Debug)]
    pub struct SpecificAlgoStrong(pub Option<AlgoStrong>);

    #[derive(Debug)]
    pub struct Dfs;

    #[derive(Debug)]
    pub struct Kosaraju;
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeSet;

    use proptest::prelude::*;

    use crate::{
        adapt::Subgraph,
        algo::{is_connected, is_strongly_connected},
        core::{
            GraphAdd, Neighbors, VertexSet,
            id::DefaultId,
            marker::{Directed, EdgeType, Undirected},
        },
        infra::proptest::{graph_directed, graph_undirected},
        storage::AdjList,
    };

    use super::*;

    fn assert_valid<G>(connected_components: ConnectedComponents<G>, graph: &G)
    where
        G: GraphBase<EdgeType = Undirected> + Neighbors + VertexSet,
    {
        assert_eq!(
            connected_components
                .iter()
                .flat_map(|component| component.iter().cloned())
                .collect::<BTreeSet<_>>(),
            graph.vertices_by_id().collect::<BTreeSet<_>>(),
            "vertices connected components are not corresponding to graph vertices"
        );

        let n_components = connected_components.len();
        if n_components == 1 {
            assert!(
                is_connected(graph),
                "only one connected component but graph not connected"
            );
        } else if n_components > 1 {
            assert!(
                !is_connected(graph),
                "multiple connected components but graph connected"
            );

            for component in connected_components.iter() {
                assert_ne!(component.len(), 0, "component is empty");

                let subgraph = Subgraph::with_state(graph, component)
                    .filter_vertex(|vertex, _, state| state.contains(vertex));

                assert!(
                    is_connected(&subgraph),
                    "component is actually not connected"
                );
            }
        } else {
            assert_eq!(
                graph.vertex_count(),
                0,
                "zero connected components but graph not empty"
            );
        }
    }

    fn assert_valid_strongly<G>(connected_components: ConnectedComponents<G>, graph: &G)
    where
        G: GraphBase<EdgeType = Directed> + Neighbors + VertexSet,
    {
        assert_eq!(
            connected_components
                .iter()
                .flat_map(|component| component.iter().cloned())
                .collect::<BTreeSet<_>>(),
            graph.vertices_by_id().collect::<BTreeSet<_>>(),
            "vertices connected components are not corresponding to graph vertices"
        );

        let n_components = connected_components.len();
        if n_components == 1 {
            assert!(
                is_strongly_connected(graph),
                "only one connected component but graph not connected"
            );
        } else if n_components > 1 {
            assert!(
                !is_strongly_connected(graph),
                "multiple connected components but graph connected"
            );

            for component in connected_components.iter() {
                assert_ne!(component.len(), 0, "component is empty");

                let subgraph = Subgraph::with_state(graph, component)
                    .filter_vertex(|vertex, _, state| state.contains(vertex));

                assert!(
                    is_strongly_connected(&subgraph),
                    "component is actually not connected"
                );
            }
        } else {
            assert_eq!(
                graph.vertex_count(),
                0,
                "zero connected components but graph not empty"
            );
        }
    }

    fn create_empty_graph<Ty: EdgeType>() -> AdjList<(), (), Ty, DefaultId> {
        AdjList::default()
    }

    fn create_connected_graph() -> AdjList<(), (), Undirected, DefaultId> {
        let mut graph = AdjList::default();

        let v0 = graph.add_vertex(());
        let v1 = graph.add_vertex(());
        let v2 = graph.add_vertex(());

        graph.add_edge(&v0, &v1, ());
        graph.add_edge(&v1, &v2, ());

        graph
    }

    fn create_disconnected_graph() -> AdjList<(), (), Undirected, DefaultId> {
        let mut graph = AdjList::default();

        let v0 = graph.add_vertex(());
        let v1 = graph.add_vertex(());
        let v2 = graph.add_vertex(());
        let v3 = graph.add_vertex(());
        graph.add_vertex(());

        graph.add_edge(&v0, &v1, ());
        graph.add_edge(&v2, &v3, ());

        graph
    }

    fn create_strongly_connected_graph() -> AdjList<(), (), Directed, DefaultId> {
        let mut graph = AdjList::default();

        let v0 = graph.add_vertex(());
        let v1 = graph.add_vertex(());
        let v2 = graph.add_vertex(());

        graph.add_edge(&v0, &v1, ());
        graph.add_edge(&v1, &v2, ());
        graph.add_edge(&v2, &v0, ());

        graph
    }

    fn create_strongly_disconnected_graph() -> AdjList<(), (), Directed, DefaultId> {
        let mut graph = AdjList::default();

        let v0 = graph.add_vertex(());
        let v1 = graph.add_vertex(());
        let v2 = graph.add_vertex(());
        let v3 = graph.add_vertex(());
        let v4 = graph.add_vertex(());
        graph.add_vertex(());

        graph.add_edge(&v0, &v1, ());
        graph.add_edge(&v1, &v2, ());
        graph.add_edge(&v2, &v0, ());
        graph.add_edge(&v2, &v3, ());
        graph.add_edge(&v3, &v4, ());
        graph.add_edge(&v4, &v3, ());

        graph
    }

    #[test]
    fn undirected_dfs_empty() {
        let graph = create_empty_graph::<Undirected>();
        let connected_components = ConnectedComponents::on(&graph).using(AlgoWeak::Dfs).run();

        assert_valid(connected_components, &graph);
    }

    #[test]
    fn undirected_dfs_connected() {
        let graph = create_connected_graph();
        let connected_components = ConnectedComponents::on(&graph).using(AlgoWeak::Dfs).run();

        assert_valid(connected_components, &graph);
    }

    #[test]
    fn undirected_dfs_disconnected() {
        let graph = create_disconnected_graph();
        let connected_components = ConnectedComponents::on(&graph).using(AlgoWeak::Dfs).run();

        assert_valid(connected_components, &graph);
    }

    #[test]
    fn directed_kosaraju_empty() {
        let graph = create_empty_graph::<Directed>();
        let connected_components = StronglyConnectedComponents::on(&graph)
            .using(AlgoStrong::Kosaraju)
            .run();

        assert_valid_strongly(connected_components.inner, &graph);
    }

    #[test]
    fn directed_kosaraju_connected() {
        let graph = create_strongly_connected_graph();
        let connected_components = StronglyConnectedComponents::on(&graph)
            .using(AlgoStrong::Kosaraju)
            .run();

        dbg!(&connected_components);

        assert_valid_strongly(connected_components.inner, &graph);
    }

    #[test]
    fn directed_kosaraju_disconnected() {
        let graph = create_strongly_disconnected_graph();
        let connected_components = StronglyConnectedComponents::on(&graph)
            .using(AlgoStrong::Kosaraju)
            .run();

        dbg!(&connected_components);

        assert_valid_strongly(connected_components.inner, &graph);
    }

    proptest! {
        #[test]
        #[ignore = "run property-based tests with `cargo test proptest_ -- --ignored`"]
        fn proptest_connected_components_undirected_dfs(graph in graph_undirected(any::<()>(), any::<()>())) {
            let connected_components = ConnectedComponents::on(&graph)
                .using(AlgoWeak::Dfs)
                .run();

            assert_valid(connected_components, &graph);
        }

        #[test]
        #[ignore = "run property-based tests with `cargo test proptest_ -- --ignored`"]
        fn proptest_connected_components_directed_kosaraju(graph in graph_directed(any::<()>(), any::<()>())) {
            let connected_components = StronglyConnectedComponents::on(&graph)
                .using(AlgoStrong::Kosaraju)
                .run();

            assert_valid_strongly(connected_components.inner, &graph);
        }
    }
}