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
use std::{collections::HashSet, marker::PhantomData};
use anyhow::Result;
use crate::{
algo::Error,
graph::{Edge, EdgeDir},
provide::{Edges, Graph, IdMap, Vertices},
};
/// Finds Eulerian trail and circuit.
///
/// # Examples
/// ```
/// use prepona::prelude::*;
/// use prepona::algo::Eulerian;
/// use prepona::storage::Mat;
/// use prepona::graph::MatGraph;
///
/// // Given:
/// //
/// // a --- b --.
/// // / \ |
/// // e \ |
/// // \ \ |
/// // c -- d |
/// // |_________'
/// //
/// let mut graph = MatGraph::init(Mat::<usize>::init());
/// let a = graph.add_vertex();
/// let b = graph.add_vertex();
/// let c = graph.add_vertex();
/// let d = graph.add_vertex();
/// let e = graph.add_vertex();
/// graph.add_edge_unchecked(a, b, 1.into());
/// graph.add_edge_unchecked(a, e, 1.into());
/// graph.add_edge_unchecked(a, d, 1.into());
/// graph.add_edge_unchecked(b, c, 1.into());
/// graph.add_edge_unchecked(c, d, 1.into());
/// graph.add_edge_unchecked(c, e, 1.into());
///
/// // When: Performing Eulerian trail detection algorithm.
/// let trail = Eulerian::init(&graph).find_trail(&graph).unwrap();
///
/// // Then:
/// assert_eq!(trail.len(), 7);
/// assert_eq!(trail, vec![a, b, c, d, a, e, c]);
/// ```
pub struct Eulerian<W, E: Edge<W>, Ty: EdgeDir, G: Graph<W, E, Ty>> {
unused_edges: HashSet<usize>,
out_deg: Vec<u32>,
in_deg: Vec<u32>,
diff_deg: Vec<i32>,
id_map: IdMap,
trail: Vec<usize>,
phantom_w: PhantomData<W>,
phantom_e: PhantomData<E>,
phantom_ty: PhantomData<Ty>,
phantom_g: PhantomData<G>,
}
impl<W, E, Ty, G> Eulerian<W, E, Ty, G>
where
E: Edge<W>,
Ty: EdgeDir,
G: Graph<W, E, Ty> + Vertices + Edges<W, E>,
{
/// Initializes the structure.
///
/// # Arguments
/// `graph`: The graph to find the Eulerian trail or circuit in.
pub fn init(graph: &G) -> Self {
let id_map = graph.continuos_id_map();
let vertex_count = graph.vertex_count();
let mut out_deg = vec![0; vertex_count];
let mut in_deg = vec![0; vertex_count];
let mut unused_edges = HashSet::new();
for (src_id, dst_id, edge) in graph.edges() {
let src_virt_id = id_map.virt_id_of(src_id);
let dst_virt_id = id_map.virt_id_of(dst_id);
unused_edges.insert(edge.get_id());
out_deg[src_virt_id] += 1;
in_deg[dst_virt_id] += 1;
if Ty::is_undirected() {
in_deg[src_virt_id] += 1;
out_deg[dst_virt_id] += 1;
}
}
let mut diff_deg = vec![0; vertex_count];
for v_id in 0..vertex_count {
diff_deg[v_id] = (out_deg[v_id] as i32) - (in_deg[v_id] as i32);
}
Eulerian {
unused_edges,
out_deg,
in_deg,
diff_deg,
id_map,
trail: vec![],
phantom_w: PhantomData,
phantom_e: PhantomData,
phantom_ty: PhantomData,
phantom_g: PhantomData,
}
}
/// # Returns
/// * `Some`: Containing the id of the vertex that is suitable for starting the Eulerian algorithm from.
/// * `None`: If there is no suitable vertex.
fn find_start_virt_id(&self) -> Option<usize> {
// If any of the bellow searches result in a vertex id, then there is a unique vertex that is suitable for starting the search from.
let unique_start = if Ty::is_undirected() {
// If graph is undirected the vertex with an odd out degree is suitable to start the search from.
self.out_deg.iter().position(|out_deg| (*out_deg % 2) != 0)
} else {
// If graph is directed the vertex with out_degree - in_degree = 1 is suitable to start the search from.
self.diff_deg.iter().position(|diff| *diff == 1)
};
// If there is no unique suitable vertex to start the algorithm from, any vertex with an outgoing edge is suitable.
unique_start.or(self.out_deg.iter().position(|out_deg| *out_deg > 0))
}
/// Finds id of the vertex to start the Eulerian trail from.
///
/// # Returns
/// * `Some`: Containing the id of the starting vertex of Eulerian trail.
/// * `None`: If can not find a starting vertex for Eulerian trail.
pub fn start_of_eulerian_trail(&self) -> Option<usize> {
let has_trail = if Ty::is_undirected() {
// An undirected graph has an Eulerian trail if and only if exactly zero or two vertices have odd degree.
let num_of_odd_degrees = self
.in_deg
.iter()
.filter(|in_deg| (**in_deg % 2) != 0)
.count();
num_of_odd_degrees == 0 || num_of_odd_degrees == 2
} else {
// A directed graph has an Eulerian trail if and only if at most one vertex has (out-degree) − (in-degree) = 1
let pos_diff = self.diff_deg.iter().filter(|diff| **diff == 1).count();
pos_diff == 1 || pos_diff == 0
};
has_trail.then(|| self.find_start_virt_id()).flatten()
}
/// Finds id of the vertex to start the Eulerian circuit from.
///
/// # Returns
/// * `Some`: Containing the id of the starting vertex.
/// * `None`: If can not find a starting vertex for Eulerian circuit.
pub fn start_of_eulerian_circuit(&self) -> Option<usize> {
let has_circuit = if Ty::is_undirected() {
// An undirected graph has an Eulerian cycle if and only if every vertex has even degree.
self.in_deg.iter().all(|in_deg| (*in_deg % 2) == 0)
} else {
// A directed graph has an Eulerian cycle if and only if every vertex has equal in degree and out degree(in other words out-degree - in-degree = 0).
self.diff_deg.iter().all(|diff| *diff == 0)
};
has_circuit.then(|| self.find_start_virt_id()).flatten()
}
/// Finds the Eulerian trail if there is one.
///
/// # Arguments
/// `graph`: Graph to search the Eulerian trail in it.
///
/// # Returns
/// * `Ok`: Containing list of vector ids that will get visited during the Eulerian trail.
/// * `Err`: If graph does not have Eulerian trail.
pub fn find_trail(mut self, graph: &G) -> Result<Vec<usize>> {
// If graph has only one vertex, that single vertex is an Eulerian trail.
if self.out_deg.len() <= 1 {
return Ok(self.trail);
}
let trail_start_id = self.start_of_eulerian_trail();
let circuit_start_id = self.start_of_eulerian_circuit();
// If there is no suitable id to start the search from, graph does not have Eulerian trail.
if trail_start_id.is_none() {
Err(Error::new_etnf())?
}
// Make sure graph has at least one edge to traverse.
if !self.unused_edges.is_empty() {
self.rec_execute(graph, trail_start_id.unwrap());
}
// Trail actually has the visited vertices in the backward order. So first visited vertex is the last item in the `trail` structure.
// So for trail to make sense to the user, reverse it before returning it so that first visited vertex is also the first item in the structure.
self.trail.reverse();
// IMPORTANT: Note that recursive algorithm that finds the trail actually finds the circuit if the graph also has Eulerian circuit.
// So if graph also has an Eulerian circuit, pop the last item in the circuit for it to become a trail. This will convert (v1, v2, v3, v1) to (v1, v2, v3)
if circuit_start_id.is_some() {
self.trail.pop();
}
Ok(self.trail)
}
/// Finds the Eulerian circuit if there is one.
///
/// # Arguments
/// `graph`: Graph to search the eulerian circuit in it.
///
/// # Returns
/// * `Ok`: Containing list of vector ids that will get visited during the eulerian circuit.
/// * `Err`: If graph does not have Eulerian circuit.
pub fn find_circuit(mut self, graph: &G) -> Result<Vec<usize>> {
// If graph has only one vertex, that single vertex is an Eulerian circuit.
if self.out_deg.len() <= 1 {
return Ok(self.trail);
}
let circuit_start_id = self.start_of_eulerian_circuit();
// If there is no suitable id to start the search from, graph does not have Eulerian circuit.
if circuit_start_id.is_none() {
Err(Error::new_ecnf())?
}
// Make sure graph has at least one edge to traverse.
if !self.unused_edges.is_empty() {
self.rec_execute(graph, circuit_start_id.unwrap());
}
// Trail actually has the visited vertices in the backward order. So first visited vertex is the last item in the `trail` structure.
// So for trail to make sense to the user, reverse it before returning it so that first visited vertex is also the first item in the structure.
self.trail.reverse();
Ok(self.trail)
}
// Recursively find the next vertex id in the Eulerian trail/circuit.
fn rec_execute(&mut self, graph: &G, v_virt_id: usize) {
let v_real_id = self.id_map.real_id_of(v_virt_id);
if self.out_deg[v_virt_id] == 0 {
self.trail.push(v_real_id)
} else {
for (dst_real_id, edge) in graph.edges_from_unchecked(v_real_id) {
let dst_virt_id = self.id_map.virt_id_of(dst_real_id);
if self.unused_edges.contains(&edge.get_id()) {
self.unused_edges.remove(&edge.get_id());
self.out_deg[v_virt_id] -= 1;
if Ty::is_undirected() {
self.out_deg[dst_virt_id] -= 1;
}
self.rec_execute(graph, dst_virt_id);
}
}
self.trail.push(v_real_id);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{graph::MatGraph, storage::DiMat, storage::Mat};
#[test]
fn one_vertex_directed_graph_trail() {
// Given: Graph
//
// a
//
let mut graph = MatGraph::init(DiMat::<usize>::init());
graph.add_vertex();
// When: Performing Eulerian trail detection algorithm.
let trail = Eulerian::init(&graph).find_trail(&graph).unwrap();
// Then:
assert_eq!(trail.len(), 0);
}
#[test]
fn one_vertex_directed_graph_circuit() {
// Given: Graph
//
// a
//
let mut graph = MatGraph::init(DiMat::<usize>::init());
graph.add_vertex();
// When: Performing Eulerian circuit detection algorithm.
let circuit = Eulerian::init(&graph).find_circuit(&graph).unwrap();
// Then:
assert_eq!(circuit.len(), 0);
}
#[test]
fn one_vertex_undirected_graph_trail() {
// Given: Graph
//
// a
//
let mut graph = MatGraph::init(Mat::<usize>::init());
graph.add_vertex();
// When: Performing Eulerian trail detection algorithm.
let trail = Eulerian::init(&graph).find_trail(&graph).unwrap();
// Then:
assert_eq!(trail.len(), 0);
}
#[test]
fn one_vertex_undirected_graph_circuit() {
// Given: Graph
//
// a
//
let mut graph = MatGraph::init(Mat::<usize>::init());
graph.add_vertex();
// When: Performing Eulerian circuit detection algorithm.
let circuit = Eulerian::init(&graph).find_circuit(&graph).unwrap();
// Then:
assert_eq!(circuit.len(), 0);
}
#[test]
fn trivial_directed_graph_with_eulerian_trail() {
// Given:
//
// a --> b --> c
//
let mut graph = MatGraph::init(DiMat::<usize>::init());
let a = graph.add_vertex();
let b = graph.add_vertex();
let c = graph.add_vertex();
graph.add_edge_unchecked(a, b, 1.into());
graph.add_edge_unchecked(b, c, 1.into());
// When: Performing Eulerian trail detection algorithm.
let trail = Eulerian::init(&graph).find_trail(&graph).unwrap();
// Then:
assert_eq!(trail.len(), 3);
assert_eq!(trail, vec![a, b, c]);
}
#[test]
fn trivial_undirected_graph_with_eulerian_trail() {
// Given:
//
// a --- b --- c
//
let mut graph = MatGraph::init(Mat::<usize>::init());
let a = graph.add_vertex();
let b = graph.add_vertex();
let c = graph.add_vertex();
graph.add_edge_unchecked(a, b, 1.into());
graph.add_edge_unchecked(b, c, 1.into());
// When: Performing Eulerian trail detection algorithm.
let trail = Eulerian::init(&graph).find_trail(&graph).unwrap();
// Then:
assert_eq!(trail.len(), 3);
assert_eq!(trail, vec![a, b, c]);
}
#[test]
fn trivial_directed_graph_with_eulerian_circuit() {
// Given: Graph
//
// a --> b
// ^ |
// | v
// '---- c
//
let mut graph = MatGraph::init(DiMat::<usize>::init());
let a = graph.add_vertex();
let b = graph.add_vertex();
let c = graph.add_vertex();
graph.add_edge_unchecked(a, b, 1.into());
graph.add_edge_unchecked(b, c, 1.into());
graph.add_edge_unchecked(c, a, 1.into());
// When: Performing Eulerian trail detection algorithm.
let trail = Eulerian::init(&graph).find_trail(&graph).unwrap();
// Then:
assert_eq!(trail.len(), 3);
assert_eq!(trail, vec![a, b, c,]);
}
#[test]
fn trivial_directed_graph_with_eulerian_circuit2() {
// Given: Graph
//
// a --> b
// ^ |
// | v
// '---- c
//
let mut graph = MatGraph::init(DiMat::<usize>::init());
let a = graph.add_vertex();
let b = graph.add_vertex();
let c = graph.add_vertex();
graph.add_edge_unchecked(a, b, 1.into());
graph.add_edge_unchecked(b, c, 1.into());
graph.add_edge_unchecked(c, a, 1.into());
// When: Performing Eulerian circuit detection algorithm.
let circuit = Eulerian::init(&graph).find_circuit(&graph).unwrap();
// Then:
assert_eq!(circuit.len(), 4);
assert_eq!(circuit, vec![a, b, c, a]);
}
#[test]
fn trivial_undirected_graph_with_eulerian_circuit() {
// Given: Graph
//
// a --- b
// | |
// | |
// '---- c
//
let mut graph = MatGraph::init(Mat::<usize>::init());
let a = graph.add_vertex();
let b = graph.add_vertex();
let c = graph.add_vertex();
graph.add_edge_unchecked(a, b, 1.into());
graph.add_edge_unchecked(b, c, 1.into());
graph.add_edge_unchecked(c, a, 1.into());
// When: Performing Eulerian trail detection algorithm.
let trail = Eulerian::init(&graph).find_trail(&graph).unwrap();
// Then:
assert_eq!(trail.len(), 3);
assert_eq!(trail, vec![a, b, c]);
}
#[test]
fn trivial_undirected_graph_with_eulerian_circuit2() {
// Given: Graph
//
// a --- b
// | |
// | |
// '---- c
//
let mut graph = MatGraph::init(Mat::<usize>::init());
let a = graph.add_vertex();
let b = graph.add_vertex();
let c = graph.add_vertex();
graph.add_edge_unchecked(a, b, 1.into());
graph.add_edge_unchecked(b, c, 1.into());
graph.add_edge_unchecked(c, a, 1.into());
// When: Performing Eulerian circuit detection algorithm.
let circuit = Eulerian::init(&graph).find_circuit(&graph).unwrap();
// Then:
assert_eq!(circuit.len(), 4);
assert_eq!(circuit, vec![a, b, c, a]);
}
#[test]
fn complex_undirected_graph_with_eulerian_trail() {
// Given:
//
// a --- b --.
// / \ |
// e \ |
// \ \ |
// c -- d |
// |_________'
//
let mut graph = MatGraph::init(Mat::<usize>::init());
let a = graph.add_vertex();
let b = graph.add_vertex();
let c = graph.add_vertex();
let d = graph.add_vertex();
let e = graph.add_vertex();
graph.add_edge_unchecked(a, b, 1.into());
graph.add_edge_unchecked(a, e, 1.into());
graph.add_edge_unchecked(a, d, 1.into());
graph.add_edge_unchecked(b, c, 1.into());
graph.add_edge_unchecked(c, d, 1.into());
graph.add_edge_unchecked(c, e, 1.into());
// When: Performing Eulerian trail detection algorithm.
let trail = Eulerian::init(&graph).find_trail(&graph).unwrap();
// Then:
assert_eq!(trail.len(), 7);
assert_eq!(trail, vec![a, b, c, d, a, e, c]);
}
#[test]
fn complex_directed_graph_with_eulerian_trail() {
// Given:
//
// |`````| .-------.
// v | | |
// a -. f <-- e <--. |
// | | ^ | |
// | V | | |
// | g -------' | |
// v | |
// b --> c --> d ---' |
// ^ |
// |_____________'
//
let mut graph = MatGraph::init(DiMat::<usize>::init());
let a = graph.add_vertex();
let b = graph.add_vertex();
let c = graph.add_vertex();
let d = graph.add_vertex();
let e = graph.add_vertex();
let f = graph.add_vertex();
let g = graph.add_vertex();
graph.add_edge_unchecked(a, b, 1.into());
graph.add_edge_unchecked(b, c, 1.into());
graph.add_edge_unchecked(c, d, 1.into());
graph.add_edge_unchecked(d, e, 1.into());
graph.add_edge_unchecked(e, f, 1.into());
graph.add_edge_unchecked(f, a, 1.into());
graph.add_edge_unchecked(a, g, 1.into());
graph.add_edge_unchecked(g, e, 1.into());
graph.add_edge_unchecked(e, c, 1.into());
// When: Performing Eulerian trail detection algorithm.
let trail = Eulerian::init(&graph).find_trail(&graph).unwrap();
// Then:
assert_eq!(trail.len(), 10);
assert_eq!(trail, vec![a, b, c, d, e, f, a, g, e, c]);
}
#[test]
fn complex_undirected_graph_with_eulerian_circuit() {
// Given:
//
// a --- b --.
// /| \ |
// e | \ |
// \| \ |
// c -- d |
// |_________'
//
let mut graph = MatGraph::init(Mat::<usize>::init());
let a = graph.add_vertex();
let b = graph.add_vertex();
let c = graph.add_vertex();
let d = graph.add_vertex();
let e = graph.add_vertex();
graph.add_edge_unchecked(a, b, 1.into());
graph.add_edge_unchecked(a, e, 1.into());
graph.add_edge_unchecked(a, d, 1.into());
graph.add_edge_unchecked(a, c, 1.into());
graph.add_edge_unchecked(b, c, 1.into());
graph.add_edge_unchecked(c, d, 1.into());
graph.add_edge_unchecked(c, e, 1.into());
// When: Performing Eulerian trail detection algorithm.
let trail = Eulerian::init(&graph).find_circuit(&graph).unwrap();
// Then:
assert_eq!(trail.len(), 8);
assert_eq!(trail, vec![a, b, c, a, d, c, e, a]);
}
#[test]
fn complex_directed_graph_with_eulerian_circuit() {
// Given:
//
// |`````| .-------.
// v | | |
// .--> a -. f <-- e <--. |
// | | | ^ | |
// | | V | | |
// | | g -------' | |
// | v | |
// | b --> c --> d ---' |
// | /^ |
// .________/ |_____________'
//
let mut graph = MatGraph::init(DiMat::<usize>::init());
let a = graph.add_vertex();
let b = graph.add_vertex();
let c = graph.add_vertex();
let d = graph.add_vertex();
let e = graph.add_vertex();
let f = graph.add_vertex();
let g = graph.add_vertex();
graph.add_edge_unchecked(a, b, 1.into());
graph.add_edge_unchecked(b, c, 1.into());
graph.add_edge_unchecked(c, d, 1.into());
graph.add_edge_unchecked(d, e, 1.into());
graph.add_edge_unchecked(e, f, 1.into());
graph.add_edge_unchecked(f, a, 1.into());
graph.add_edge_unchecked(a, g, 1.into());
graph.add_edge_unchecked(g, e, 1.into());
graph.add_edge_unchecked(e, c, 1.into());
graph.add_edge_unchecked(c, a, 1.into());
// When: Performing Eulerian trail detection algorithm.
let trail = Eulerian::init(&graph).find_circuit(&graph).unwrap();
// Then:
assert_eq!(trail.len(), 11);
assert_eq!(trail, vec![a, b, c, a, g, e, c, d, e, f, a]);
}
#[test]
fn directed_graph_with_start_other_than_0() {
// Given:
//
// d --- a ----.
// | |
// c --- b
let mut graph = MatGraph::init(Mat::<usize>::init());
let b = graph.add_vertex();
let d = graph.add_vertex();
let c = graph.add_vertex();
let a = graph.add_vertex();
graph.add_edge_unchecked(a, b, 1.into());
graph.add_edge_unchecked(b, c, 1.into());
graph.add_edge_unchecked(c, a, 1.into());
graph.add_edge_unchecked(a, d, 1.into());
// When: Performing Eulerian trail detection algorithm.
let trail = Eulerian::init(&graph).find_trail(&graph).unwrap();
// Then: It should start from a or d and not b.
assert_eq!(trail.len(), 5);
assert_eq!(trail, vec![1, 3, 0, 2, 3]);
}
#[test]
fn undirected_graph_with_start_other_than_0() {
// Given:
//
// d <-- a <---.
// | |
// v |
// c --> b
let mut graph = MatGraph::init(DiMat::<usize>::init());
let b = graph.add_vertex();
let d = graph.add_vertex();
let c = graph.add_vertex();
let a = graph.add_vertex();
graph.add_edge_unchecked(a, c, 1.into());
graph.add_edge_unchecked(c, b, 1.into());
graph.add_edge_unchecked(b, a, 1.into());
graph.add_edge_unchecked(a, d, 1.into());
// When: Performing Eulerian trail detection algorithm.
let trail = Eulerian::init(&graph).find_trail(&graph).unwrap();
// Then: It should start from a or d and not b.
assert_eq!(trail.len(), 5);
assert_eq!(trail, vec![3, 2, 0, 3, 1]);
}
#[test]
fn calling_trail_on_undirected_graph_with_circuit() {
// Given:
//
// a --- b --.
// /| \ |
// e | \ |
// \| \ |
// c -- d |
// |_________'
//
let mut graph = MatGraph::init(Mat::<usize>::init());
let a = graph.add_vertex();
let b = graph.add_vertex();
let c = graph.add_vertex();
let d = graph.add_vertex();
let e = graph.add_vertex();
graph.add_edge_unchecked(a, b, 1.into());
graph.add_edge_unchecked(a, e, 1.into());
graph.add_edge_unchecked(a, d, 1.into());
graph.add_edge_unchecked(a, c, 1.into());
graph.add_edge_unchecked(b, c, 1.into());
graph.add_edge_unchecked(c, d, 1.into());
graph.add_edge_unchecked(c, e, 1.into());
// When: Performing Eulerian trail detection algorithm.
let trail = Eulerian::init(&graph).find_trail(&graph).unwrap();
// Then:
assert_eq!(trail.len(), 7);
assert_eq!(trail, vec![a, b, c, a, d, c, e]);
}
#[test]
fn calling_trail_on_directed_graph_with_circuit() {
// Given:
//
// |`````| .-------.
// v | | |
// .--> a -. f <-- e <--. |
// | | | ^ | |
// | | V | | |
// | | g -------' | |
// | v | |
// | b --> c --> d ---' |
// | /^ |
// .________/ |_____________'
//
let mut graph = MatGraph::init(DiMat::<usize>::init());
let a = graph.add_vertex();
let b = graph.add_vertex();
let c = graph.add_vertex();
let d = graph.add_vertex();
let e = graph.add_vertex();
let f = graph.add_vertex();
let g = graph.add_vertex();
graph.add_edge_unchecked(a, b, 1.into());
graph.add_edge_unchecked(b, c, 1.into());
graph.add_edge_unchecked(c, d, 1.into());
graph.add_edge_unchecked(d, e, 1.into());
graph.add_edge_unchecked(e, f, 1.into());
graph.add_edge_unchecked(f, a, 1.into());
graph.add_edge_unchecked(a, g, 1.into());
graph.add_edge_unchecked(g, e, 1.into());
graph.add_edge_unchecked(e, c, 1.into());
graph.add_edge_unchecked(c, a, 1.into());
// When: Performing Eulerian trail detection algorithm.
let trail = Eulerian::init(&graph).find_trail(&graph).unwrap();
// Then:
assert_eq!(trail.len(), 10);
assert_eq!(trail, vec![a, b, c, a, g, e, c, d, e, f]);
}
// TODO: Add test for graphs that does not have Eulerian circuit or trail and check that algorithm returns error indeed.
}