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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
//! Implementations of graph traversal methods.
//!
//! All traversal implementations in this module are **iterative**, that is,
//! they don't use recursion. This means that
//!
//! * 👍 visitor is lazy and can be stopped without tricks,
//! * 👍 visitor state is independent on the graph itself, allowing
//!   mutations during traversal,
//! * 👍 traversal is not limited by the size of the program stack,
//! * 👎 there is sometimes extra cost if the [traversal semantics ought
//!   to be
//!   respected](https://11011110.github.io/blog/2013/12/17/stack-based-graph-traversal.html).
//!
//! The order in which the neighbors of a vertex are discovered is not specified
//! and should not be relied upon.

pub mod bfs;
pub mod dfs;

pub(crate) mod raw;
mod visit_set;

#[doc(inline)]
pub use self::{
    bfs::Bfs,
    dfs::{Dfs, DfsEvents, DfsNoBacktrack, DfsPostOrder},
    visit_set::{TypedBitSet, VisitSet},
};

use std::collections::VecDeque;

use rustc_hash::FxHashSet;

use raw::*;

use crate::core::{
    GraphBase, Neighbors, VertexSet,
    id::{IdType, UseVertexId},
};

/// Trait for a specific graph traversal approach.
#[doc(alias = "Walker")]
pub trait Visitor<G> {
    /// The type of the elements being visited.
    type Item;

    /// Advances the visitor and returns the next visited element in given
    /// graph.
    ///
    /// The difference from the [`Iterator::next`] is that the visitor doesn't
    /// hold a reference to the graph and thus allows modifications to the graph
    /// between individual visitor steps or passing the visitor around without
    /// lifetime problems.
    ///
    /// # Examples
    ///
    /// ```
    /// use gryf::{
    ///     algo::is_cyclic,
    ///     visit::{DfsEvent, DfsEvents, Visitor},
    ///     Graph,
    /// };
    ///
    /// let mut graph = Graph::<_, (), _>::new_directed();
    ///
    /// graph.extend_with_vertices(["a", "b", "c", "d", "e", "f"]);
    /// graph.extend_with_edges([(0, 1), (1, 2), (2, 3), (3, 0), (3, 4), (4, 5)]);
    ///
    /// // There is a cycle.
    /// assert!(is_cyclic(&graph));
    /// assert_eq!(graph.edge_count(), 6);
    ///
    /// let root = graph.find_vertex("a").unwrap();
    ///
    /// let mut dfs = DfsEvents::new(&graph);
    /// let mut visitor = dfs.start(root);
    ///
    /// while let Some(event) = visitor.visit_next(&graph) {
    ///     if let DfsEvent::BackEdge { edge, .. } = event {
    ///         // Remove edge that would otherwise create a cycle in the graph.
    ///         graph.remove_edge(edge);
    ///     }
    /// }
    ///
    /// // There is no cycle anymore.
    /// assert!(!is_cyclic(&graph));
    /// assert_eq!(graph.edge_count(), 5);
    /// ```
    fn visit_next(&mut self, graph: &G) -> Option<Self::Item>;

    /// Returns an [iterator](Iterator) that uses the visitor to iterate over
    /// the elements in given graph.
    fn iter<'a>(&'a mut self, graph: &'a G) -> Iter<'a, Self, G>
    where
        Self: Sized,
    {
        Iter {
            visitor: self,
            graph,
        }
    }

    /// Converts the visitor into an [iterator](Iterator) to visit the elements
    /// in given graph.
    fn into_iter(self, graph: &G) -> IntoIter<'_, Self, G>
    where
        Self: Sized,
    {
        IntoIter {
            visitor: self,
            graph,
        }
    }
}

/// Visitor iterator returned from [`Visitor::iter`].
pub struct Iter<'a, V, G> {
    visitor: &'a mut V,
    graph: &'a G,
}

impl<'a, V, G> Iterator for Iter<'a, V, G>
where
    V: Visitor<G>,
{
    type Item = V::Item;

    fn next(&mut self) -> Option<Self::Item> {
        self.visitor.visit_next(self.graph)
    }
}

/// Visitor iterator returned from [`Visitor::into_iter`].
pub struct IntoIter<'a, V, G> {
    visitor: V,
    graph: &'a G,
}

impl<'a, V, G> Iterator for IntoIter<'a, V, G>
where
    V: Visitor<G>,
{
    type Item = V::Item;

    fn next(&mut self) -> Option<Self::Item> {
        self.visitor.visit_next(self.graph)
    }
}

/// A collection of starting vertices for a graph traversal.
///
/// This trait is implemented for any [`Iterator`].
pub trait VisitRoots<I: IdType> {
    /// Returns next ID to start the traversal from.
    ///
    /// Note that the returned ID might have already been visited. It is the
    /// responsibility of the visitor to ignore such elements.
    fn next_root(&mut self) -> Option<I>;

    /// Returns `true` if the collection can determine that all remaining roots
    /// have already been visited based on the currently visited set.
    ///
    /// This is an optimization for early return without the need of iterating
    /// over all roots in cases where this can be determined to be unnecessary.
    /// An example is [`VisitAll`] which checks for [`VisitSet::visited_count`]
    /// being equal to the number of vertices in the original graph.
    ///
    /// By default, `false` is returned which effectively delegates the
    /// indication of being done for [`VisitRoots::next_root`] by returning
    /// `None`.
    fn is_done(&mut self, _visited: &impl VisitSet<I>) -> bool {
        false
    }
}

impl<I: IdType, T> VisitRoots<I> for T
where
    T: Iterator<Item = I>,
{
    fn next_root(&mut self) -> Option<I> {
        self.next()
    }
}

/// A [`VisitRoots`] collection for visiting all vertices in a graph.
pub struct VisitAll<'a, G>
where
    G: VertexSet,
{
    graph: &'a G,
    ids: G::VerticesByIdIter<'a>,
}

impl<'a, G> VisitAll<'a, G>
where
    G: VertexSet,
{
    /// Creates the collection from the given graph.
    pub fn new(graph: &'a G) -> Self {
        Self {
            graph,
            ids: graph.vertices_by_id(),
        }
    }
}

impl<G> VisitRoots<G::VertexId> for VisitAll<'_, G>
where
    G: VertexSet,
{
    fn next_root(&mut self) -> Option<G::VertexId> {
        self.ids.next()
    }

    // Optimize the early return for cases when the whole graph was traversed
    // yet the `self.ids` iterator (containing all vertices in the graph at the
    // beginning) has not been exhausted.
    fn is_done(&mut self, visited: &impl VisitSet<G::VertexId>) -> bool {
        // Since we are holding a shared reference to the graph, it could not
        // have been mutated during traversal. In particular, the number of
        // vertices didn't change.
        visited.visited_count() == self.graph.vertex_count()
    }
}

/// Strictly monotonically increasing numbering of graph traversal events.
///
/// This is useful to some algorithms that base their decision on the event time
/// comparison.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Time(pub usize);

impl Time {
    /// The largest possible value of time.
    pub const MAX: Time = Time(usize::MAX);
}

/// Depth-first search visitor event.
///
/// Use [`DfsEvents`] visitor to traverse a graph by reporting DFS events.
///
/// # Examples
///
/// See [`DfsEvents`] for examples.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DfsEvent<G>
where
    G: GraphBase,
{
    /// A new vertex was discovered.
    Open {
        /// Discovered vertex.
        vertex: G::VertexId,

        /// Discovering time.
        time: Time,
    },

    /// An edge of the tree formed by the traversal.
    TreeEdge {
        /// Source endpoint of the edge.
        from: G::VertexId,

        /// Target endpoint of the edge.
        to: G::VertexId,

        /// Edge ID.
        edge: G::EdgeId,
    },

    /// An edge to an already [closed](DfsEvent::Close) vertex.
    ///
    /// Presence of a back edge indicates a cycle in the graph.
    BackEdge {
        /// Source endpoint of the edge.
        from: G::VertexId,

        /// Target (closed) endpoint of the edge.
        to: G::VertexId,

        /// Edge ID.
        edge: G::EdgeId,
    },

    /// An edge to an already [discovered](DfsEvent::Open) but not yet
    /// [closed](DfsEvent::Close) vertex.
    ///
    /// Cross edge is an edge between vertices in different "branches" of the
    /// traversal tree. Forward edge is an edge between vertices in the same
    /// "branch" of the traversal tree. When the [discover
    /// time](DfsEvent::Open::time) of the `from` vertex is higher than `to`
    /// vertex, it's cross edge, otherwise it's forward edge.
    ///
    /// In undirected graphs there is no concept of cross or forward edges.
    CrossForwardEdge {
        /// Source endpoint of the edge.
        from: G::VertexId,

        /// Target (closed) endpoint of the edge.
        to: G::VertexId,

        /// Edge ID.
        edge: G::EdgeId,
    },

    /// All edges from the vertex have been reported.
    Close {
        /// Closed vertex.
        vertex: G::VertexId,

        /// Closing time.
        time: Time,
    },
}

#[cfg(test)]
mod tests {
    use crate::{
        core::{
            GraphAdd, GraphFull,
            id::DefaultId,
            marker::{Directed, Undirected},
        },
        storage::{AdjList, Stable},
    };

    use super::*;

    macro_rules! dfs_event {
        (open, $v:expr, $t:expr) => {
            DfsEvent::Open {
                vertex: $v,
                time: Time($t),
            }
        };
        (tree, $e:expr, ($u:expr, $v:expr)) => {
            DfsEvent::TreeEdge {
                from: $u,
                to: $v,
                edge: $e,
            }
        };
        (back, $e:expr, ($u:expr, $v:expr)) => {
            DfsEvent::BackEdge {
                from: $u,
                to: $v,
                edge: $e,
            }
        };
        (cross_forward, $e:expr, ($u:expr, $v:expr)) => {
            DfsEvent::CrossForwardEdge {
                from: $u,
                to: $v,
                edge: $e,
            }
        };
        (close, $v:expr, $t:expr) => {
            DfsEvent::Close {
                vertex: $v,
                time: Time($t),
            }
        };
    }

    #[test]
    fn bfs_connected() {
        let mut graph = AdjList::<_, _, Undirected, DefaultId>::new();

        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(());
        let v5 = graph.add_vertex(());

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

        let vertices = Bfs::new(&graph).start(v0).iter(&graph).collect::<Vec<_>>();

        assert_eq!(vertices, vec![v0, v1, v2, v3, v4, v5]);
    }

    #[test]
    fn dfs_connected() {
        let mut graph = AdjList::<_, _, Undirected, DefaultId>::new();

        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(());
        let v5 = graph.add_vertex(());

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

        let vertices = Dfs::new(&graph).start(v0).iter(&graph).collect::<Vec<_>>();

        assert_eq!(vertices, vec![v0, v1, v4, v5, v2, v3]);
    }

    #[test]
    fn bfs_disconnected() {
        let mut graph = Stable::new(AdjList::<_, _, Undirected, DefaultId>::new());

        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(());
        let v5 = graph.add_vertex(());

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

        graph.remove_vertex(&v1);

        let vertices = Bfs::new(&graph).start(v2).iter(&graph).collect::<Vec<_>>();

        assert_eq!(vertices, vec![v2, v5, v4]);
    }

    #[test]
    fn dfs_disconnected() {
        let mut graph = Stable::new(AdjList::<_, _, Undirected, DefaultId>::new());

        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(());
        let v5 = graph.add_vertex(());

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

        graph.remove_vertex(&v1);

        let vertices = Dfs::new(&graph).start(v2).iter(&graph).collect::<Vec<_>>();

        assert_eq!(vertices, vec![v2, v5, v4]);
    }

    #[test]
    fn bfs_disconnected_all() {
        let mut graph = Stable::new(AdjList::<_, _, Undirected, DefaultId>::new());

        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(());
        let v5 = graph.add_vertex(());

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

        graph.remove_vertex(&v1);

        let vertices = Bfs::new(&graph)
            .start_all(&graph)
            .iter(&graph)
            .collect::<Vec<_>>();

        assert_eq!(vertices, vec![v0, v2, v5, v4, v3]);
    }

    #[test]
    fn dfs_disconnected_all() {
        let mut graph = Stable::new(AdjList::<_, _, Undirected, DefaultId>::new());

        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(());
        let v5 = graph.add_vertex(());

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

        graph.remove_vertex(&v1);

        let vertices = Dfs::new(&graph)
            .start_all(&graph)
            .iter(&graph)
            .collect::<Vec<_>>();

        assert_eq!(vertices, vec![v0, v2, v5, v4, v3]);
    }

    #[test]
    fn bfs_disconnected_multi() {
        let mut graph = Stable::new(AdjList::<_, _, Undirected, DefaultId>::new());

        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(());
        let v5 = graph.add_vertex(());

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

        graph.remove_vertex(&v1);

        let vertices = Bfs::new(&graph)
            .start_multi([v0, v2].into_iter())
            .iter(&graph)
            .collect::<Vec<_>>();

        assert_eq!(vertices, vec![v0, v2, v5, v4]);
    }

    #[test]
    fn bfs_disconnected_multi_mutation() {
        let mut graph = Stable::new(AdjList::<_, _, Undirected, DefaultId>::new());

        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(());
        let v5 = graph.add_vertex(());

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

        graph.remove_vertex(&v1);

        let mut visit = Bfs::new(&graph);
        let mut visit = visit.start_multi([v0, v2].into_iter());

        // Remove one of the starts.
        graph.remove_vertex(&v0);

        let vertices = visit.iter(&graph).collect::<Vec<_>>();

        // Removing one of the start causes incomplete visit, but not a crash.
        assert_eq!(vertices, vec![v2, v5, v4]);
    }

    #[test]
    fn dfs_events() {
        // The graph with edge types (assuming implementation details of
        // `AdjList`).
        //
        //                          +----------------- T -----+
        //                          |                         |
        //               +-- B --- (2) -- B --- (3) -- T --- (4)
        //               |                       |
        // (0) -- T --- (1)                      T
        //               |                       |
        //               +-- T --- (5) -- B --- (6) -- T --- (7)
        //                          |                         |
        //                          +---- T ------------------+

        let mut graph = AdjList::<_, _, Undirected, DefaultId>::new();

        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(());
        let v5 = graph.add_vertex(());
        let v6 = graph.add_vertex(());
        let v7 = graph.add_vertex(());

        let e0 = graph.add_edge(&v0, &v1, ());
        let e1 = graph.add_edge(&v1, &v2, ());
        let e2 = graph.add_edge(&v2, &v3, ());
        let e3 = graph.add_edge(&v3, &v4, ());
        let e4 = graph.add_edge(&v4, &v2, ());
        let e5 = graph.add_edge(&v1, &v5, ());
        let e6 = graph.add_edge(&v5, &v6, ());
        let e7 = graph.add_edge(&v6, &v7, ());
        let e8 = graph.add_edge(&v5, &v7, ());
        let e9 = graph.add_edge(&v3, &v6, ());

        let events = DfsEvents::new(&graph)
            .start(v0)
            .iter(&graph)
            .collect::<Vec<_>>();

        let expected = vec![
            dfs_event!(open, v0, 0),
            dfs_event!(tree, e0, (v0, v1)),
            dfs_event!(open, v1, 1),
            dfs_event!(tree, e5, (v1, v5)),
            dfs_event!(open, v5, 2),
            dfs_event!(tree, e8, (v5, v7)),
            dfs_event!(open, v7, 3),
            dfs_event!(tree, e7, (v7, v6)),
            dfs_event!(open, v6, 4),
            dfs_event!(tree, e9, (v6, v3)),
            dfs_event!(open, v3, 5),
            dfs_event!(tree, e3, (v3, v4)),
            dfs_event!(open, v4, 6),
            dfs_event!(tree, e4, (v4, v2)),
            dfs_event!(open, v2, 7),
            dfs_event!(back, e2, (v2, v3)),
            dfs_event!(back, e1, (v2, v1)),
            dfs_event!(close, v2, 8),
            dfs_event!(close, v4, 9),
            dfs_event!(close, v3, 10),
            dfs_event!(back, e6, (v6, v5)),
            dfs_event!(close, v6, 11),
            dfs_event!(close, v7, 12),
            dfs_event!(close, v5, 13),
            dfs_event!(close, v1, 14),
            dfs_event!(close, v0, 15),
        ];

        assert_eq!(events, expected);
    }

    #[test]
    fn dfs_events_directed() {
        // The graph with edge types (assuming implementation details of
        // `AdjList`).
        //
        //                          +----------------- B -----+
        //                          v                         |
        //               +-- T --> (2) -- T --> (3) -- T --> (4)
        //               |                       |
        // (0) -- T --> (1)                      C
        //               |                       v
        //               +-- T --> (5) -- T --> (6) -- F --> (7)
        //                          |                         ʌ
        //                          +---- T ------------------+

        let mut graph = AdjList::<_, _, Directed, DefaultId>::new();

        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(());
        let v5 = graph.add_vertex(());
        let v6 = graph.add_vertex(());
        let v7 = graph.add_vertex(());

        let e0 = graph.add_edge(&v0, &v1, ());
        let e1 = graph.add_edge(&v1, &v2, ());
        let e2 = graph.add_edge(&v2, &v3, ());
        let e3 = graph.add_edge(&v3, &v4, ());
        let e4 = graph.add_edge(&v4, &v2, ());
        let e5 = graph.add_edge(&v1, &v5, ());
        let e6 = graph.add_edge(&v5, &v6, ());
        let e7 = graph.add_edge(&v6, &v7, ());
        let e8 = graph.add_edge(&v5, &v7, ());
        let e9 = graph.add_edge(&v3, &v6, ());

        let events = DfsEvents::new(&graph)
            .start(v0)
            .iter(&graph)
            .collect::<Vec<_>>();

        let expected = vec![
            dfs_event!(open, v0, 0),
            dfs_event!(tree, e0, (v0, v1)),
            dfs_event!(open, v1, 1),
            dfs_event!(tree, e5, (v1, v5)),
            dfs_event!(open, v5, 2),
            dfs_event!(tree, e8, (v5, v7)),
            dfs_event!(open, v7, 3),
            dfs_event!(close, v7, 4),
            dfs_event!(tree, e6, (v5, v6)),
            dfs_event!(open, v6, 5),
            dfs_event!(cross_forward, e7, (v6, v7)),
            dfs_event!(close, v6, 6),
            dfs_event!(close, v5, 7),
            dfs_event!(tree, e1, (v1, v2)),
            dfs_event!(open, v2, 8),
            dfs_event!(tree, e2, (v2, v3)),
            dfs_event!(open, v3, 9),
            dfs_event!(cross_forward, e9, (v3, v6)),
            dfs_event!(tree, e3, (v3, v4)),
            dfs_event!(open, v4, 10),
            dfs_event!(back, e4, (v4, v2)),
            dfs_event!(close, v4, 11),
            dfs_event!(close, v3, 12),
            dfs_event!(close, v2, 13),
            dfs_event!(close, v1, 14),
            dfs_event!(close, v0, 15),
        ];

        assert_eq!(events, expected);
    }

    #[test]
    fn dfs_events_isolated() {
        let mut graph = AdjList::<_, (), Undirected, DefaultId>::new();

        let v0 = graph.add_vertex(());

        let events = DfsEvents::new(&graph)
            .start(v0)
            .iter(&graph)
            .collect::<Vec<_>>();

        let expected = vec![dfs_event!(open, v0, 0), dfs_event!(close, v0, 1)];

        assert_eq!(events, expected);
    }

    #[test]
    fn dfs_post_order() {
        let mut graph = AdjList::<_, _, Undirected, DefaultId>::new();

        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(());
        let v5 = graph.add_vertex(());

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

        let vertices = DfsPostOrder::new(&graph)
            .start(v0)
            .iter(&graph)
            .collect::<Vec<_>>();

        assert_eq!(vertices, vec![v2, v5, v4, v3, v1, v0]);
    }

    #[test]
    fn dfs_no_backtrack() {
        let mut graph = AdjList::<_, _, Undirected, DefaultId>::new();

        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(());
        let v5 = graph.add_vertex(());

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

        let vertices = DfsNoBacktrack::new(&graph)
            .start(v0)
            .iter(&graph)
            .collect::<Vec<_>>();

        // v3 is missing because it would be visited only if the algorithm would
        // continue from v1 after traversing the "long" branch.
        assert_eq!(vertices, vec![v0, v1, v2, v5, v4]);
    }
}