problemreductions/topology/
small_graphs.rs

1//! Small graph collection for testing and benchmarking.
2//!
3//! This module provides a collection of well-known small graphs commonly used
4//! in graph theory. The graphs are equivalent to those in Graphs.jl's smallgraph
5//! function.
6//!
7//! All edges are 0-indexed (converted from Julia's 1-indexed representation).
8
9/// Returns the edges of the Bull graph.
10/// 5 vertices, 5 edges.
11/// The bull graph is a triangle with two pendant edges.
12pub fn bull() -> (usize, Vec<(usize, usize)>) {
13    (5, vec![(0, 1), (0, 2), (1, 2), (1, 3), (2, 4)])
14}
15
16/// Returns the edges of the Chvátal graph.
17/// 12 vertices, 24 edges.
18/// The Chvátal graph is the smallest triangle-free graph that is 4-chromatic and 4-regular.
19pub fn chvatal() -> (usize, Vec<(usize, usize)>) {
20    (
21        12,
22        vec![
23            (0, 1),
24            (0, 4),
25            (0, 6),
26            (0, 9),
27            (1, 2),
28            (1, 5),
29            (1, 7),
30            (2, 3),
31            (2, 6),
32            (2, 8),
33            (3, 4),
34            (3, 7),
35            (3, 9),
36            (4, 5),
37            (4, 8),
38            (5, 10),
39            (5, 11),
40            (6, 10),
41            (6, 11),
42            (7, 8),
43            (7, 11),
44            (8, 10),
45            (9, 10),
46            (9, 11),
47        ],
48    )
49}
50
51/// Returns the edges of the Cubical graph (3-cube, Q3).
52/// 8 vertices, 12 edges.
53pub fn cubical() -> (usize, Vec<(usize, usize)>) {
54    (
55        8,
56        vec![
57            (0, 1),
58            (0, 3),
59            (0, 4),
60            (1, 2),
61            (1, 7),
62            (2, 3),
63            (2, 6),
64            (3, 5),
65            (4, 5),
66            (4, 7),
67            (5, 6),
68            (6, 7),
69        ],
70    )
71}
72
73/// Returns the edges of the Desargues graph.
74/// 20 vertices, 30 edges.
75pub fn desargues() -> (usize, Vec<(usize, usize)>) {
76    (
77        20,
78        vec![
79            (0, 1),
80            (0, 5),
81            (0, 19),
82            (1, 2),
83            (1, 16),
84            (2, 3),
85            (2, 11),
86            (3, 4),
87            (3, 14),
88            (4, 5),
89            (4, 9),
90            (5, 6),
91            (6, 7),
92            (6, 15),
93            (7, 8),
94            (7, 18),
95            (8, 9),
96            (8, 13),
97            (9, 10),
98            (10, 11),
99            (10, 19),
100            (11, 12),
101            (12, 13),
102            (12, 17),
103            (13, 14),
104            (14, 15),
105            (15, 16),
106            (16, 17),
107            (17, 18),
108            (18, 19),
109        ],
110    )
111}
112
113/// Returns the edges of the Diamond graph.
114/// 4 vertices, 5 edges.
115/// The diamond graph is K4 minus one edge.
116pub fn diamond() -> (usize, Vec<(usize, usize)>) {
117    (4, vec![(0, 1), (0, 2), (1, 2), (1, 3), (2, 3)])
118}
119
120/// Returns the edges of the Dodecahedral graph.
121/// 20 vertices, 30 edges.
122pub fn dodecahedral() -> (usize, Vec<(usize, usize)>) {
123    (
124        20,
125        vec![
126            (0, 1),
127            (0, 10),
128            (0, 19),
129            (1, 2),
130            (1, 8),
131            (2, 3),
132            (2, 6),
133            (3, 4),
134            (3, 19),
135            (4, 5),
136            (4, 17),
137            (5, 6),
138            (5, 15),
139            (6, 7),
140            (7, 8),
141            (7, 14),
142            (8, 9),
143            (9, 10),
144            (9, 13),
145            (10, 11),
146            (11, 12),
147            (11, 18),
148            (12, 13),
149            (12, 16),
150            (13, 14),
151            (14, 15),
152            (15, 16),
153            (16, 17),
154            (17, 18),
155            (18, 19),
156        ],
157    )
158}
159
160/// Returns the edges of the Frucht graph.
161/// 12 vertices, 18 edges.
162/// The Frucht graph is the smallest cubic graph with no non-trivial automorphisms.
163pub fn frucht() -> (usize, Vec<(usize, usize)>) {
164    (
165        12,
166        vec![
167            (0, 1),
168            (0, 6),
169            (0, 7),
170            (1, 2),
171            (1, 7),
172            (2, 3),
173            (2, 8),
174            (3, 4),
175            (3, 9),
176            (4, 5),
177            (4, 9),
178            (5, 6),
179            (5, 10),
180            (6, 10),
181            (7, 11),
182            (8, 9),
183            (8, 11),
184            (10, 11),
185        ],
186    )
187}
188
189/// Returns the edges of the Heawood graph.
190/// 14 vertices, 21 edges.
191/// The Heawood graph is a cage and the incidence graph of the Fano plane.
192pub fn heawood() -> (usize, Vec<(usize, usize)>) {
193    (
194        14,
195        vec![
196            (0, 1),
197            (0, 5),
198            (0, 13),
199            (1, 2),
200            (1, 10),
201            (2, 3),
202            (2, 7),
203            (3, 4),
204            (3, 12),
205            (4, 5),
206            (4, 9),
207            (5, 6),
208            (6, 7),
209            (6, 11),
210            (7, 8),
211            (8, 9),
212            (8, 13),
213            (9, 10),
214            (10, 11),
215            (11, 12),
216            (12, 13),
217        ],
218    )
219}
220
221/// Returns the edges of the House graph.
222/// 5 vertices, 6 edges.
223/// The house graph is a square with a triangle on top.
224pub fn house() -> (usize, Vec<(usize, usize)>) {
225    (5, vec![(0, 1), (0, 2), (1, 3), (2, 3), (2, 4), (3, 4)])
226}
227
228/// Returns the edges of the House X graph.
229/// 5 vertices, 8 edges.
230/// The house graph with both diagonals of the square.
231pub fn housex() -> (usize, Vec<(usize, usize)>) {
232    (
233        5,
234        vec![
235            (0, 1),
236            (0, 2),
237            (0, 3),
238            (1, 2),
239            (1, 3),
240            (2, 3),
241            (2, 4),
242            (3, 4),
243        ],
244    )
245}
246
247/// Returns the edges of the Icosahedral graph.
248/// 12 vertices, 30 edges.
249pub fn icosahedral() -> (usize, Vec<(usize, usize)>) {
250    (
251        12,
252        vec![
253            (0, 1),
254            (0, 5),
255            (0, 7),
256            (0, 8),
257            (0, 11),
258            (1, 2),
259            (1, 5),
260            (1, 6),
261            (1, 8),
262            (2, 3),
263            (2, 6),
264            (2, 8),
265            (2, 9),
266            (3, 4),
267            (3, 6),
268            (3, 9),
269            (3, 10),
270            (4, 5),
271            (4, 6),
272            (4, 10),
273            (4, 11),
274            (5, 6),
275            (5, 11),
276            (7, 8),
277            (7, 9),
278            (7, 10),
279            (7, 11),
280            (8, 9),
281            (9, 10),
282            (10, 11),
283        ],
284    )
285}
286
287/// Returns the edges of Zachary's Karate Club graph.
288/// 34 vertices, 78 edges.
289/// A social network of a karate club.
290pub fn karate() -> (usize, Vec<(usize, usize)>) {
291    (
292        34,
293        vec![
294            (0, 1),
295            (0, 2),
296            (0, 3),
297            (0, 4),
298            (0, 5),
299            (0, 6),
300            (0, 7),
301            (0, 8),
302            (0, 10),
303            (0, 11),
304            (0, 12),
305            (0, 13),
306            (0, 17),
307            (0, 19),
308            (0, 21),
309            (0, 31),
310            (1, 2),
311            (1, 3),
312            (1, 7),
313            (1, 13),
314            (1, 17),
315            (1, 19),
316            (1, 21),
317            (1, 30),
318            (2, 3),
319            (2, 7),
320            (2, 8),
321            (2, 9),
322            (2, 13),
323            (2, 27),
324            (2, 28),
325            (2, 32),
326            (3, 7),
327            (3, 12),
328            (3, 13),
329            (4, 6),
330            (4, 10),
331            (5, 6),
332            (5, 10),
333            (5, 16),
334            (6, 16),
335            (8, 30),
336            (8, 32),
337            (8, 33),
338            (9, 33),
339            (13, 33),
340            (14, 32),
341            (14, 33),
342            (15, 32),
343            (15, 33),
344            (18, 32),
345            (18, 33),
346            (19, 33),
347            (20, 32),
348            (20, 33),
349            (22, 32),
350            (22, 33),
351            (23, 25),
352            (23, 27),
353            (23, 29),
354            (23, 32),
355            (23, 33),
356            (24, 25),
357            (24, 27),
358            (24, 31),
359            (25, 31),
360            (26, 29),
361            (26, 33),
362            (27, 33),
363            (28, 31),
364            (28, 33),
365            (29, 32),
366            (29, 33),
367            (30, 32),
368            (30, 33),
369            (31, 32),
370            (31, 33),
371            (32, 33),
372        ],
373    )
374}
375
376/// Returns the edges of the Krackhardt Kite graph.
377/// 10 vertices, 18 edges.
378pub fn krackhardtkite() -> (usize, Vec<(usize, usize)>) {
379    (
380        10,
381        vec![
382            (0, 1),
383            (0, 2),
384            (0, 3),
385            (0, 5),
386            (1, 3),
387            (1, 4),
388            (1, 6),
389            (2, 3),
390            (2, 5),
391            (3, 4),
392            (3, 5),
393            (3, 6),
394            (4, 6),
395            (5, 6),
396            (5, 7),
397            (6, 7),
398            (7, 8),
399            (8, 9),
400        ],
401    )
402}
403
404/// Returns the edges of the Möbius-Kantor graph.
405/// 16 vertices, 24 edges.
406pub fn moebiuskantor() -> (usize, Vec<(usize, usize)>) {
407    (
408        16,
409        vec![
410            (0, 1),
411            (0, 5),
412            (0, 15),
413            (1, 2),
414            (1, 12),
415            (2, 3),
416            (2, 7),
417            (3, 4),
418            (3, 14),
419            (4, 5),
420            (4, 9),
421            (5, 6),
422            (6, 7),
423            (6, 11),
424            (7, 8),
425            (8, 9),
426            (8, 13),
427            (9, 10),
428            (10, 11),
429            (10, 15),
430            (11, 12),
431            (12, 13),
432            (13, 14),
433            (14, 15),
434        ],
435    )
436}
437
438/// Returns the edges of the Octahedral graph.
439/// 6 vertices, 12 edges.
440pub fn octahedral() -> (usize, Vec<(usize, usize)>) {
441    (
442        6,
443        vec![
444            (0, 1),
445            (0, 2),
446            (0, 3),
447            (0, 4),
448            (1, 2),
449            (1, 3),
450            (1, 5),
451            (2, 4),
452            (2, 5),
453            (3, 4),
454            (3, 5),
455            (4, 5),
456        ],
457    )
458}
459
460/// Returns the edges of the Pappus graph.
461/// 18 vertices, 27 edges.
462pub fn pappus() -> (usize, Vec<(usize, usize)>) {
463    (
464        18,
465        vec![
466            (0, 1),
467            (0, 5),
468            (0, 17),
469            (1, 2),
470            (1, 8),
471            (2, 3),
472            (2, 13),
473            (3, 4),
474            (3, 10),
475            (4, 5),
476            (4, 15),
477            (5, 6),
478            (6, 7),
479            (6, 11),
480            (7, 8),
481            (7, 14),
482            (8, 9),
483            (9, 10),
484            (9, 16),
485            (10, 11),
486            (11, 12),
487            (12, 13),
488            (12, 17),
489            (13, 14),
490            (14, 15),
491            (15, 16),
492            (16, 17),
493        ],
494    )
495}
496
497/// Returns the edges of the Petersen graph.
498/// 10 vertices, 15 edges.
499/// A well-known graph that is 3-regular and has many interesting properties.
500pub fn petersen() -> (usize, Vec<(usize, usize)>) {
501    (
502        10,
503        vec![
504            (0, 1),
505            (0, 4),
506            (0, 5),
507            (1, 2),
508            (1, 6),
509            (2, 3),
510            (2, 7),
511            (3, 4),
512            (3, 8),
513            (4, 9),
514            (5, 7),
515            (5, 8),
516            (6, 8),
517            (6, 9),
518            (7, 9),
519        ],
520    )
521}
522
523/// Returns the edges of the Sedgewick Maze graph.
524/// 8 vertices, 10 edges.
525pub fn sedgewickmaze() -> (usize, Vec<(usize, usize)>) {
526    (
527        8,
528        vec![
529            (0, 2),
530            (0, 5),
531            (0, 7),
532            (1, 7),
533            (2, 6),
534            (3, 4),
535            (3, 5),
536            (4, 5),
537            (4, 6),
538            (4, 7),
539        ],
540    )
541}
542
543/// Returns the edges of the Tetrahedral graph (K4).
544/// 4 vertices, 6 edges.
545pub fn tetrahedral() -> (usize, Vec<(usize, usize)>) {
546    (4, vec![(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)])
547}
548
549/// Returns the edges of the Truncated Cube graph.
550/// 24 vertices, 36 edges.
551pub fn truncatedcube() -> (usize, Vec<(usize, usize)>) {
552    // Edges from Julia's Graphs.jl (converted to 0-indexed)
553    (
554        24,
555        vec![
556            (0, 1),
557            (0, 2),
558            (0, 4),
559            (1, 11),
560            (1, 14),
561            (2, 3),
562            (2, 4),
563            (3, 6),
564            (3, 8),
565            (4, 5),
566            (5, 16),
567            (5, 18),
568            (6, 7),
569            (6, 8),
570            (7, 10),
571            (7, 12),
572            (8, 9),
573            (9, 17),
574            (9, 20),
575            (10, 11),
576            (10, 12),
577            (11, 14),
578            (12, 13),
579            (13, 21),
580            (13, 22),
581            (14, 15),
582            (15, 19),
583            (15, 23),
584            (16, 17),
585            (16, 18),
586            (17, 20),
587            (18, 19),
588            (19, 23),
589            (20, 21),
590            (21, 22),
591            (22, 23),
592        ],
593    )
594}
595
596/// Returns the edges of the Truncated Tetrahedron graph.
597/// 12 vertices, 18 edges.
598pub fn truncatedtetrahedron() -> (usize, Vec<(usize, usize)>) {
599    (
600        12,
601        vec![
602            (0, 1),
603            (0, 2),
604            (0, 9),
605            (1, 2),
606            (1, 6),
607            (2, 3),
608            (3, 4),
609            (3, 11),
610            (4, 5),
611            (4, 11),
612            (5, 6),
613            (5, 7),
614            (6, 7),
615            (7, 8),
616            (8, 9),
617            (8, 10),
618            (9, 10),
619            (10, 11),
620        ],
621    )
622}
623
624/// Returns the edges of the Tutte graph.
625/// 46 vertices, 69 edges.
626/// A 3-regular graph that is not Hamiltonian.
627pub fn tutte() -> (usize, Vec<(usize, usize)>) {
628    (
629        46,
630        vec![
631            (0, 1),
632            (0, 2),
633            (0, 3),
634            (1, 4),
635            (1, 26),
636            (2, 10),
637            (2, 11),
638            (3, 18),
639            (3, 19),
640            (4, 5),
641            (4, 33),
642            (5, 6),
643            (5, 29),
644            (6, 7),
645            (6, 27),
646            (7, 8),
647            (7, 14),
648            (8, 9),
649            (8, 38),
650            (9, 10),
651            (9, 37),
652            (10, 39),
653            (11, 12),
654            (11, 39),
655            (12, 13),
656            (12, 35),
657            (13, 14),
658            (13, 15),
659            (14, 34),
660            (15, 16),
661            (15, 22),
662            (16, 17),
663            (16, 44),
664            (17, 18),
665            (17, 43),
666            (18, 45),
667            (19, 20),
668            (19, 45),
669            (20, 21),
670            (20, 41),
671            (21, 22),
672            (21, 23),
673            (22, 40),
674            (23, 24),
675            (23, 27),
676            (24, 25),
677            (24, 32),
678            (25, 26),
679            (25, 31),
680            (26, 33),
681            (27, 28),
682            (28, 29),
683            (28, 32),
684            (29, 30),
685            (30, 31),
686            (30, 33),
687            (31, 32),
688            (34, 35),
689            (34, 38),
690            (35, 36),
691            (36, 37),
692            (36, 39),
693            (37, 38),
694            (40, 41),
695            (40, 44),
696            (41, 42),
697            (42, 43),
698            (42, 45),
699            (43, 44),
700        ],
701    )
702}
703
704/// Get a small graph by name.
705///
706/// Returns `Some((num_vertices, edges))` if the graph exists, `None` otherwise.
707///
708/// Available graphs: bull, chvatal, cubical, desargues, diamond, dodecahedral,
709/// frucht, heawood, house, housex, icosahedral, karate, krackhardtkite,
710/// moebiuskantor, octahedral, pappus, petersen, sedgewickmaze, tetrahedral,
711/// truncatedcube, truncatedtetrahedron, tutte
712pub fn smallgraph(name: &str) -> Option<(usize, Vec<(usize, usize)>)> {
713    match name {
714        "bull" => Some(bull()),
715        "chvatal" => Some(chvatal()),
716        "cubical" => Some(cubical()),
717        "desargues" => Some(desargues()),
718        "diamond" => Some(diamond()),
719        "dodecahedral" => Some(dodecahedral()),
720        "frucht" => Some(frucht()),
721        "heawood" => Some(heawood()),
722        "house" => Some(house()),
723        "housex" => Some(housex()),
724        "icosahedral" => Some(icosahedral()),
725        "karate" => Some(karate()),
726        "krackhardtkite" => Some(krackhardtkite()),
727        "moebiuskantor" => Some(moebiuskantor()),
728        "octahedral" => Some(octahedral()),
729        "pappus" => Some(pappus()),
730        "petersen" => Some(petersen()),
731        "sedgewickmaze" => Some(sedgewickmaze()),
732        "tetrahedral" => Some(tetrahedral()),
733        "truncatedcube" => Some(truncatedcube()),
734        "truncatedtetrahedron" => Some(truncatedtetrahedron()),
735        "tutte" => Some(tutte()),
736        _ => None,
737    }
738}
739
740/// List all available small graph names.
741pub fn available_graphs() -> Vec<&'static str> {
742    vec![
743        "bull",
744        "chvatal",
745        "cubical",
746        "desargues",
747        "diamond",
748        "dodecahedral",
749        "frucht",
750        "heawood",
751        "house",
752        "housex",
753        "icosahedral",
754        "karate",
755        "krackhardtkite",
756        "moebiuskantor",
757        "octahedral",
758        "pappus",
759        "petersen",
760        "sedgewickmaze",
761        "tetrahedral",
762        "truncatedcube",
763        "truncatedtetrahedron",
764        "tutte",
765    ]
766}
767
768#[cfg(test)]
769#[path = "../unit_tests/topology/small_graphs.rs"]
770mod tests;