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
//! Implementations of [depth-first search] (DFS) algorithm.
//!
//! [depth-first search]: https://en.wikipedia.org/wiki/Depth-first_search

use super::*;

/// Standard DFS traversal over the vertices of a graph.
pub struct Dfs<G>
where
    G: GraphBase,
{
    raw: RawVisit<G, UseVertexId, RawDfs>,
}

/// [`Dfs`] rooted in a single vertex.
pub struct DfsRooted<'a, G>
where
    G: GraphBase,
{
    raw: &'a mut RawVisit<G, UseVertexId, RawDfs>,
}

/// [`Dfs`] with possibly multiple roots.
pub struct DfsMulti<'a, G, S>
where
    G: GraphBase,
    S: VisitRoots<G::VertexId>,
{
    raw: &'a mut RawVisit<G, UseVertexId, RawDfs>,
    multi: RawVisitMulti<G, UseVertexId, RawDfs, S>,
}

impl<G> Dfs<G>
where
    G: GraphBase,
{
    #[doc = include_str!("../../docs/include/visit.new.md")]
    pub fn new(graph: &G) -> Self
    where
        G: GraphBase,
    {
        Self {
            raw: RawVisit::new(graph.vertex_count_hint()),
        }
    }

    #[doc = include_str!("../../docs/include/visit.start.md")]
    pub fn start(&mut self, root: G::VertexId) -> DfsRooted<'_, G> {
        self.raw.start(root);
        DfsRooted { raw: &mut self.raw }
    }

    #[doc = include_str!("../../docs/include/visit.start_all.md")]
    pub fn start_all<'a>(&'a mut self, graph: &'a G) -> DfsMulti<'a, G, VisitAll<'a, G>>
    where
        G: VertexSet,
    {
        DfsMulti {
            raw: &mut self.raw,
            multi: RawVisitMulti::new(VisitAll::new(graph)),
        }
    }

    #[doc = include_str!("../../docs/include/visit.start_multi.md")]
    pub fn start_multi<S>(&mut self, roots: S) -> DfsMulti<'_, G, S>
    where
        S: VisitRoots<G::VertexId>,
    {
        DfsMulti {
            raw: &mut self.raw,
            multi: RawVisitMulti::new(roots),
        }
    }

    #[doc = include_str!("../../docs/include/visit.reset.md")]
    pub fn reset(&mut self) {
        self.raw.reset();
    }

    #[doc = include_str!("../../docs/include/visit.visited.md")]
    pub fn visited(&self) -> &impl VisitSet<G::VertexId> {
        &self.raw.visited
    }

    #[doc = include_str!("../../docs/include/visit.visited_mut.md")]
    pub fn visited_mut(&mut self) -> &mut impl VisitSet<G::VertexId> {
        &mut self.raw.visited
    }
}

impl<'a, G> Visitor<G> for DfsRooted<'a, G>
where
    G: Neighbors,
{
    type Item = G::VertexId;

    fn visit_next(&mut self, graph: &G) -> Option<Self::Item> {
        self.raw.next(graph, |_, _| true)
    }
}

impl<'a, S, G> Visitor<G> for DfsMulti<'a, G, S>
where
    S: VisitRoots<G::VertexId>,
    G: Neighbors + VertexSet,
{
    type Item = G::VertexId;

    fn visit_next(&mut self, graph: &G) -> Option<Self::Item> {
        self.multi.next_multi(
            self.raw,
            |raw| raw.next(graph, |_, _| true),
            |vertex| graph.contains_vertex(vertex),
        )
    }
}

/// Standard DFS traversal that produces [`DfsEvent`]s.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeSet;
///
/// use gryf::{
///     adapt::Subgraph,
///     algo::{is_connected, is_cyclic},
///     visit::{DfsEvent, DfsEvents, Visitor},
///     Graph,
/// };
///
/// let mut graph = Graph::<_, (), _>::new_directed();
///
/// graph.extend_with_vertices(["a", "b", "c", "d", "e", "f", "g", "h"]);
/// graph.extend_with_edges([
///     (0, 1),
///     (0, 2),
///     (0, 7),
///     (1, 3),
///     (2, 4),
///     (3, 5),
///     (4, 3),
///     (4, 6),
///     (4, 7),
///     (5, 1),
/// ]);
///
/// // There is a cycle.
/// assert!(is_cyclic(&graph));
///
/// let root = graph.find_vertex("a").unwrap();
///
/// let spanning_tree_edges = DfsEvents::new(&graph)
///     .start(root)
///     .into_iter(&graph)
///     .filter_map(|event| {
///         if let DfsEvent::TreeEdge { edge, .. } = event {
///             Some(edge)
///         } else {
///             None
///         }
///     })
///     .collect::<BTreeSet<_>>();
///
/// let spanning_tree = Subgraph::with_state(graph, spanning_tree_edges)
///     .filter_edge(|edge, _, state| state.contains(edge));
///
/// // The subgraph is a spanning tree.
/// assert!(!is_cyclic(&spanning_tree));
/// assert!(is_connected(&spanning_tree));
/// ```
pub struct DfsEvents<G>
where
    G: GraphBase,
{
    raw: RawVisit<G, UseVertexId, RawDfsExtra>,
    closed: FxHashSet<G::VertexId>,
    is_directed: bool,
}

/// [`DfsEvents`] rooted in a single vertex.
pub struct DfsEventsRooted<'a, G>
where
    G: GraphBase,
{
    raw: &'a mut RawVisit<G, UseVertexId, RawDfsExtra>,
    closed: &'a mut FxHashSet<G::VertexId>,
    queue: VecDeque<DfsEvent<G>>,
    time: usize,
    is_directed: bool,
}

/// [`DfsEvents`] with possibly multiple roots.
pub struct DfsEventsMulti<'a, G, S>
where
    G: GraphBase,
    S: VisitRoots<G::VertexId>,
{
    raw: &'a mut RawVisit<G, UseVertexId, RawDfsExtra>,
    multi: RawVisitMulti<G, UseVertexId, RawDfsExtra, S>,
    closed: &'a mut FxHashSet<G::VertexId>,
    queue: VecDeque<DfsEvent<G>>,
    time: usize,
    is_directed: bool,
}

impl<G> DfsEvents<G>
where
    G: GraphBase,
{
    #[doc = include_str!("../../docs/include/visit.new.md")]
    pub fn new(graph: &G) -> Self
    where
        G: GraphBase,
    {
        let is_directed = graph.is_directed();

        let raw = RawVisit::new(graph.vertex_count_hint());
        let closed = if is_directed {
            FxHashSet::with_capacity_and_hasher(raw.visited.capacity(), rustc_hash::FxBuildHasher)
        } else {
            // Discovered set (in raw) is used instead of closed set in
            // undirected graph because it is the only information needed for
            // back edge determination. See back edge signalling below for more
            // details.
            FxHashSet::default()
        };

        Self {
            raw,
            closed,
            is_directed,
        }
    }

    #[doc = include_str!("../../docs/include/visit.start.md")]
    pub fn start(&mut self, root: G::VertexId) -> DfsEventsRooted<'_, G> {
        self.raw.start(RawDfsExtraItem::start(root));
        DfsEventsRooted {
            raw: &mut self.raw,
            closed: &mut self.closed,
            queue: VecDeque::new(),
            time: 0,
            is_directed: self.is_directed,
        }
    }

    #[doc = include_str!("../../docs/include/visit.start_all.md")]
    pub fn start_all<'a>(&'a mut self, graph: &'a G) -> DfsEventsMulti<'a, G, VisitAll<'a, G>>
    where
        G: VertexSet,
    {
        DfsEventsMulti {
            raw: &mut self.raw,
            multi: RawVisitMulti::new(VisitAll::new(graph)),
            closed: &mut self.closed,
            queue: VecDeque::new(),
            time: 0,
            is_directed: self.is_directed,
        }
    }

    #[doc = include_str!("../../docs/include/visit.start_multi.md")]
    pub fn start_multi<S>(&mut self, roots: S) -> DfsEventsMulti<'_, G, S>
    where
        S: VisitRoots<G::VertexId>,
    {
        DfsEventsMulti {
            raw: &mut self.raw,
            multi: RawVisitMulti::new(roots),
            closed: &mut self.closed,
            queue: VecDeque::new(),
            time: 0,
            is_directed: self.is_directed,
        }
    }

    #[doc = include_str!("../../docs/include/visit.reset.md")]
    pub fn reset(&mut self) {
        self.raw.reset();
    }

    #[doc = include_str!("../../docs/include/visit.visited.md")]
    pub fn visited(&self) -> &impl VisitSet<G::VertexId> {
        &self.raw.visited
    }

    #[doc = include_str!("../../docs/include/visit.visited_mut.md")]
    pub fn visited_mut(&mut self) -> &mut impl VisitSet<G::VertexId> {
        &mut self.raw.visited
    }

    fn process_next_callback(
        raw: &RawVisit<G, UseVertexId, RawDfsExtra>,
        raw_event: RawEvent<G>,
        closed: &mut FxHashSet<G::VertexId>,
        queue: &mut VecDeque<DfsEvent<G>>,
        is_directed: bool,
    ) -> bool {
        match raw_event {
            RawEvent::Popped { .. } => {}
            RawEvent::Push { vertex, from } => {
                queue.push_back(DfsEvent::TreeEdge {
                    from: from.0,
                    to: vertex,
                    edge: from.1,
                });
            }
            RawEvent::Skip { vertex, from } => {
                let from = from.expect("edge tail vertex always available");

                if is_directed {
                    if !closed.contains(&vertex) {
                        queue.push_back(DfsEvent::BackEdge {
                            from: from.0,
                            to: vertex,
                            edge: from.1,
                        });
                    } else {
                        queue.push_back(DfsEvent::CrossForwardEdge {
                            from: from.0,
                            to: vertex,
                            edge: from.1,
                        });
                    }
                } else {
                    // `RawDfsExtra` traverses all neighbors of the vertex.
                    // However, this means that the edge back to the parent
                    // is also reported (as skipped, since the parent is
                    // already opened). We need to detect that and avoid
                    // signalling this edge as back edge. Due to the
                    // implementation details of `RawDfsExtra`, in the time
                    // of signalling `Skip` event, the item on top of the
                    // stack is the parent of the skipped vertex.
                    if !closed.contains(&vertex) {
                        let parent = raw.collection.0.last().map(RawDfsExtra::id);

                        if parent.as_ref() != Some(&vertex) {
                            queue.push_back(DfsEvent::BackEdge {
                                from: from.0,
                                to: vertex,
                                edge: from.1,
                            });
                        }
                    }

                    // Cross-forward edges do not make sense in undirected
                    // graphs.
                }
            }
        }

        true
    }

    fn process_next_event(
        raw_extra_event: RawDfsExtraEvent<G>,
        closed: &mut FxHashSet<G::VertexId>,
        queue: &mut VecDeque<DfsEvent<G>>,
        time: &mut usize,
    ) {
        if let RawDfsExtraEvent::Close(ref vertex) = raw_extra_event {
            closed.insert(vertex.clone());
        }

        let event = match raw_extra_event {
            RawDfsExtraEvent::Open(vertex) => DfsEvent::Open {
                vertex,
                time: Time(*time),
            },
            RawDfsExtraEvent::Close(vertex) => DfsEvent::Close {
                vertex,
                time: Time(*time),
            },
        };

        *time += 1;
        queue.push_back(event);
    }
}

impl<'a, G> Visitor<G> for DfsEventsRooted<'a, G>
where
    G: Neighbors,
{
    type Item = DfsEvent<G>;

    fn visit_next(&mut self, graph: &G) -> Option<Self::Item> {
        if let Some(event) = self.queue.pop_front() {
            return Some(event);
        }

        if let Some(raw_extra_event) = self.raw.next(graph, |raw, raw_event| {
            DfsEvents::process_next_callback(
                raw,
                raw_event,
                self.closed,
                &mut self.queue,
                self.is_directed,
            )
        }) {
            DfsEvents::process_next_event(
                raw_extra_event,
                self.closed,
                &mut self.queue,
                &mut self.time,
            );
        };

        self.queue.pop_front()
    }
}

impl<'a, S, G> Visitor<G> for DfsEventsMulti<'a, G, S>
where
    G: Neighbors + VertexSet,
    S: VisitRoots<G::VertexId>,
{
    type Item = DfsEvent<G>;

    fn visit_next(&mut self, graph: &G) -> Option<Self::Item> {
        if let Some(event) = self.queue.pop_front() {
            return Some(event);
        }

        if let Some(raw_extra_event) = self.multi.next_multi(
            self.raw,
            |raw| {
                raw.next(graph, |raw, raw_event| {
                    DfsEvents::process_next_callback(
                        raw,
                        raw_event,
                        self.closed,
                        &mut self.queue,
                        self.is_directed,
                    )
                })
            },
            |vertex| graph.contains_vertex(vertex),
        ) {
            DfsEvents::process_next_event(
                raw_extra_event,
                self.closed,
                &mut self.queue,
                &mut self.time,
            );
        }

        self.queue.pop_front()
    }
}

/// [Post-order DFS] traversal over vertices of a graph.
///
/// [Post-order DFS]: https://en.wikipedia.org/wiki/Tree_traversal#Post-order,_LRN
///
/// # Examples
///
/// ```
/// use gryf::{
///     visit::{DfsPostOrder, Visitor},
///     Graph,
/// };
///
/// let mut graph = Graph::<_, (), _>::new_directed();
///
/// graph.extend_with_vertices(["a", "b", "c"]);
/// graph.extend_with_edges([(0, 1), (0, 2)]);
///
/// let root = graph.find_vertex("a").unwrap();
///
/// let post_order_last = DfsPostOrder::new(&graph)
///     .start(root)
///     .into_iter(&graph)
///     .last()
///     .unwrap();
///
/// assert_eq!(post_order_last, root);
/// ```
pub struct DfsPostOrder<G>
where
    G: GraphBase,
{
    raw: RawVisit<G, UseVertexId, RawDfsExtra>,
}

/// [`DfsPostOrder`] rooted in a single vertex.
pub struct DfsPostOrderRooted<'a, G>
where
    G: GraphBase,
{
    raw: &'a mut RawVisit<G, UseVertexId, RawDfsExtra>,
}

/// [`DfsPostOrder`] with possibly multiple roots.
pub struct DfsPostOrderMulti<'a, G, S>
where
    G: GraphBase,
    S: VisitRoots<G::VertexId>,
{
    raw: &'a mut RawVisit<G, UseVertexId, RawDfsExtra>,
    multi: RawVisitMulti<G, UseVertexId, RawDfsExtra, S>,
}

impl<G> DfsPostOrder<G>
where
    G: GraphBase,
{
    #[doc = include_str!("../../docs/include/visit.new.md")]
    pub fn new(graph: &G) -> Self
    where
        G: GraphBase,
    {
        Self {
            raw: RawVisit::new(graph.vertex_count_hint()),
        }
    }

    #[doc = include_str!("../../docs/include/visit.start.md")]
    pub fn start(&mut self, root: G::VertexId) -> DfsPostOrderRooted<'_, G> {
        self.raw.start(RawDfsExtraItem::start(root));
        DfsPostOrderRooted { raw: &mut self.raw }
    }

    #[doc = include_str!("../../docs/include/visit.start_all.md")]
    pub fn start_all<'a>(&'a mut self, graph: &'a G) -> DfsPostOrderMulti<'a, G, VisitAll<'a, G>>
    where
        G: VertexSet,
    {
        DfsPostOrderMulti {
            raw: &mut self.raw,
            multi: RawVisitMulti::new(VisitAll::new(graph)),
        }
    }

    #[doc = include_str!("../../docs/include/visit.start_multi.md")]
    pub fn start_multi<S>(&mut self, roots: S) -> DfsPostOrderMulti<'_, G, S>
    where
        S: VisitRoots<G::VertexId>,
    {
        DfsPostOrderMulti {
            raw: &mut self.raw,
            multi: RawVisitMulti::new(roots),
        }
    }

    #[doc = include_str!("../../docs/include/visit.reset.md")]
    pub fn reset(&mut self) {
        self.raw.reset();
    }

    #[doc = include_str!("../../docs/include/visit.visited.md")]
    pub fn visited(&self) -> &impl VisitSet<G::VertexId> {
        &self.raw.visited
    }

    #[doc = include_str!("../../docs/include/visit.visited_mut.md")]
    pub fn visited_mut(&mut self) -> &mut impl VisitSet<G::VertexId> {
        &mut self.raw.visited
    }
}

impl<'a, G> Visitor<G> for DfsPostOrderRooted<'a, G>
where
    G: Neighbors,
{
    type Item = G::VertexId;

    fn visit_next(&mut self, graph: &G) -> Option<Self::Item> {
        loop {
            if let RawDfsExtraEvent::Close(vertex) = self.raw.next(graph, |_, _| true)? {
                return Some(vertex);
            }
        }
    }
}

impl<'a, S, G> Visitor<G> for DfsPostOrderMulti<'a, G, S>
where
    G: Neighbors + VertexSet,
    S: VisitRoots<G::VertexId>,
{
    type Item = G::VertexId;

    fn visit_next(&mut self, graph: &G) -> Option<Self::Item> {
        self.multi
            .next_multi(
                self.raw,
                |raw| loop {
                    if let RawDfsExtraEvent::Close(vertex) = raw.next(graph, |_, _| true)? {
                        return Some(RawDfsExtraItem::<G>::closed(vertex));
                    }
                },
                |vertex| graph.contains_vertex(vertex),
            )
            .as_ref()
            .map(RawDfsExtra::id)
    }
}

/// DFS traversal that does not backtrack to explore other branches of the
/// traversal tree after the first one is ended.
///
/// In other words, this traversal implementation always visits only a single
/// neighbor of a vertex, ignoring the other neighbors. This means that visiting
/// all vertices in the graph is **not guaranteed**.
///
/// This traversal algorithm is useful in a greedy initialization of some
/// algorithms before switching to the proper yet costly procedure.
pub struct DfsNoBacktrack<G>
where
    G: GraphBase,
{
    raw: RawVisit<G, UseVertexId, RawDfsNoBacktrack>,
}

/// [`DfsNoBacktrack`] rooted in a single vertex.
pub struct DfsNoBacktrackRooted<'a, G>
where
    G: GraphBase,
{
    raw: &'a mut RawVisit<G, UseVertexId, RawDfsNoBacktrack>,
}

/// [`DfsNoBacktrack`] with possibly multiple roots.
pub struct DfsNoBacktrackMulti<'a, G, S>
where
    G: GraphBase,
    S: VisitRoots<G::VertexId>,
{
    raw: &'a mut RawVisit<G, UseVertexId, RawDfsNoBacktrack>,
    multi: RawVisitMulti<G, UseVertexId, RawDfsNoBacktrack, S>,
}

impl<G> DfsNoBacktrack<G>
where
    G: GraphBase,
{
    #[doc = include_str!("../../docs/include/visit.new.md")]
    pub fn new(graph: &G) -> Self
    where
        G: GraphBase,
    {
        Self {
            raw: RawVisit::new(graph.vertex_count_hint()),
        }
    }

    #[doc = include_str!("../../docs/include/visit.start.md")]
    pub fn start(&mut self, root: G::VertexId) -> DfsNoBacktrackRooted<'_, G> {
        self.raw.start(root);
        DfsNoBacktrackRooted { raw: &mut self.raw }
    }

    #[doc = include_str!("../../docs/include/visit.start_all.md")]
    pub fn start_all<'a>(&'a mut self, graph: &'a G) -> DfsNoBacktrackMulti<'a, G, VisitAll<'a, G>>
    where
        G: VertexSet,
    {
        DfsNoBacktrackMulti {
            raw: &mut self.raw,
            multi: RawVisitMulti::new(VisitAll::new(graph)),
        }
    }

    #[doc = include_str!("../../docs/include/visit.start_multi.md")]
    pub fn start_multi<S>(&mut self, roots: S) -> DfsNoBacktrackMulti<'_, G, S>
    where
        S: VisitRoots<G::VertexId>,
    {
        DfsNoBacktrackMulti {
            raw: &mut self.raw,
            multi: RawVisitMulti::new(roots),
        }
    }

    #[doc = include_str!("../../docs/include/visit.reset.md")]
    pub fn reset(&mut self) {
        self.raw.reset();
    }

    #[doc = include_str!("../../docs/include/visit.visited.md")]
    pub fn visited(&self) -> &impl VisitSet<G::VertexId> {
        &self.raw.visited
    }

    #[doc = include_str!("../../docs/include/visit.visited_mut.md")]
    pub fn visited_mut(&mut self) -> &mut impl VisitSet<G::VertexId> {
        &mut self.raw.visited
    }
}

impl<'a, G> Visitor<G> for DfsNoBacktrackRooted<'a, G>
where
    G: Neighbors,
{
    type Item = G::VertexId;

    fn visit_next(&mut self, graph: &G) -> Option<Self::Item> {
        self.raw.next(graph, |_, _| true)
    }
}

impl<'a, S, G> Visitor<G> for DfsNoBacktrackMulti<'a, G, S>
where
    G: Neighbors + VertexSet,
    S: VisitRoots<G::VertexId>,
{
    type Item = G::VertexId;

    fn visit_next(&mut self, graph: &G) -> Option<Self::Item> {
        self.multi.next_multi(
            self.raw,
            |raw| raw.next(graph, |_, _| true),
            |vertex| graph.contains_vertex(vertex),
        )
    }
}