symbolica 0.15.0

A blazing fast computer algebra system
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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
//! Graphs and related algorithms.
//!
//! # Examples
//!
//! ```
//! use symbolica::graph::Graph;
//! let mut g = Graph::new();
//! let n0 = g.add_node(0);
//! let n1 = g.add_node(1);
//! let n2 = g.add_node(2);
//!
//! g.add_edge(n2, n1, true, 0).unwrap();
//! g.add_edge(n0, n0, false, 0).unwrap();
//! g.add_edge(n0, n1, true, 0).unwrap();
//! g.add_edge(n1, n0, false, 2).unwrap();
//!
//! g.canonize_edges();
//!
//! assert_eq!(g.node(0).edges, [0, 1, 2]);
//! ```

use ahash::{HashMap, HashSet};
use dyn_clone::DynClone;
use std::{
    cmp::Ordering,
    fmt::{Debug, Display},
    hash::Hash,
};

use crate::{
    combinatorics::{unique_permutations, CombinationIterator},
    domains::integer::Integer,
};

/// A node in a graph, with arbitrary data.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Node<NodeData = Empty> {
    /// Arbitrary data associated with the node.
    pub data: NodeData,
    /// Indices of the edges connected to the node.
    pub edges: Vec<usize>,
}

/// An edge in a graph, with arbitrary data.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Edge<EdgeData = Empty> {
    /// Indices of the vertices connected by the edge.
    pub vertices: (usize, usize),
    /// If `true`, the edge is directed from `vertices.0` to `vertices.1`.
    pub directed: bool,
    /// Arbitrary data associated with the edge.
    pub data: EdgeData,
}

/// Empty data type.
#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Empty;

impl Display for Empty {
    fn fmt(&self, _f: &mut std::fmt::Formatter) -> std::fmt::Result {
        Ok(())
    }
}

/// Data that has a public part and a private part. The private
/// part is not used for equality or hashing.
#[derive(Clone)]
pub struct HiddenData<T, U> {
    pub data: T,
    pub hidden: U,
}

impl<T, U> HiddenData<T, U> {
    pub fn new(data: T, hidden: U) -> Self {
        HiddenData { data, hidden }
    }
}

impl<T: PartialEq, U> PartialEq for HiddenData<T, U> {
    fn eq(&self, other: &Self) -> bool {
        self.data == other.data
    }
}

impl<T: Eq, U> Eq for HiddenData<T, U> {}

impl<T: Hash, U> Hash for HiddenData<T, U> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.data.hash(state);
    }
}

impl<T: Display, U: Display> Display for HiddenData<T, U> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{} ({})", self.data, self.hidden)
    }
}

impl<T: PartialOrd, U> PartialOrd for HiddenData<T, U> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.data.partial_cmp(&other.data)
    }
}

impl<T: Ord, U> Ord for HiddenData<T, U> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.data.cmp(&other.data)
    }
}

/// A multigraph with support for arbitrary node and edge data.
///
/// Use [HiddenData] to hide parts of the data from all equality and hashing.
///
/// # Example
///
/// ```
/// use symbolica::graph::Graph;
/// let mut g = Graph::new();
/// let n0 = g.add_node(0);
/// let n1 = g.add_node(1);
/// let n2 = g.add_node(2);
///
/// g.add_edge(n2, n1, true, 0).unwrap();
/// g.add_edge(n0, n0, false, 0).unwrap();
/// g.add_edge(n0, n1, true, 0).unwrap();
/// g.add_edge(n1, n0, false, 2).unwrap();
///
/// g.canonize_edges();
///
/// assert_eq!(g.node(0).edges, [0, 1, 2]);
/// ```
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub struct Graph<NodeData = Empty, EdgeData = Empty> {
    nodes: Vec<Node<NodeData>>,
    edges: Vec<Edge<EdgeData>>,
}

impl<N: Display, E: Display> std::fmt::Display for Graph<N, E> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        for (i, x) in self.nodes.iter().enumerate() {
            writeln!(f, "N{}: {}", i, x.data)?;
        }

        for x in &self.edges {
            if x.directed {
                writeln!(f, "{} {}->{}", x.data, x.vertices.0, x.vertices.1)?;
            } else {
                writeln!(f, "{} {}--{}", x.data, x.vertices.0, x.vertices.1)?;
            }
        }
        Ok(())
    }
}

impl<N: Display, E: Display> Graph<N, E> {
    pub fn to_dot(&self) -> String {
        let mut out = String::new();
        out.push_str("digraph G {\n");

        for (i, x) in self.nodes.iter().enumerate() {
            out.push_str(&format!("  {} [label=\"{}\"];\n", i, x.data));
        }

        for x in &self.edges {
            if x.directed {
                out.push_str(&format!(
                    "  {} -> {} [label=\"{}\"];\n",
                    x.vertices.0, x.vertices.1, x.data
                ));
            } else {
                out.push_str(&format!(
                    "  {} -> {} [dir=none,label=\"{}\"];\n",
                    x.vertices.0, x.vertices.1, x.data
                ));
            }
        }

        out.push_str("}\n");
        out
    }

    pub fn to_mermaid(&self) -> String {
        let mut out = String::new();
        out.push_str("graph TD;\n");

        for (i, x) in self.nodes.iter().enumerate() {
            let d = format!("{}", x.data);
            if d.is_empty() {
                out.push_str(&format!("  {};\n", i));
            } else {
                out.push_str(&format!("  {}[\"{}\"];\n", i, d));
            }
        }

        for x in &self.edges {
            let d = format!("{}", x.data);

            if d.is_empty() {
                if x.directed {
                    out.push_str(&format!("  {} --> {};\n", x.vertices.0, x.vertices.1,));
                } else {
                    out.push_str(&format!("  {} --- {};\n", x.vertices.0, x.vertices.1,));
                }
            } else {
                if x.directed {
                    out.push_str(&format!(
                        "  {} -->|\"{}\"| {};\n",
                        x.vertices.0, x.data, x.vertices.1,
                    ));
                } else {
                    out.push_str(&format!(
                        "  {} ---|\"{}\"| {};\n",
                        x.vertices.0, x.data, x.vertices.1,
                    ));
                }
            }
        }

        out
    }
}

/// Information about a node in a spanning tree.
#[derive(Clone, Debug)]
pub struct NodeInfo {
    pub position: Option<usize>,
    pub parent: usize,
    pub chain_id: Option<usize>,
    pub external: bool,
    pub back_edges: Vec<usize>, // back edges starting from this node
}

/// A spanning tree representation of a graph.
/// Parts of the graph may not be in the tree.
#[derive(Clone, Debug)]
pub struct SpanningTree {
    pub nodes: Vec<NodeInfo>,
    pub order: Vec<usize>,
}

impl SpanningTree {
    pub fn is_connected(&self) -> bool {
        self.nodes.iter().all(|x| x.position.is_some())
    }

    pub fn chain_decomposition(&mut self) {
        // now build the chains, starting from the DFS root
        for &n in &self.order {
            let mut back_edge_index = 0;

            while back_edge_index < self.nodes[n].back_edges.len() {
                let node = self.nodes[n].back_edges[back_edge_index];
                back_edge_index += 1;

                if node == n {
                    // self-loop
                    continue;
                }

                // set blocker
                if self.nodes[n].chain_id.is_none() {
                    self.nodes[n].chain_id = Some(n);
                }

                let mut target = node;
                while self.nodes[target].chain_id.is_none() {
                    let nn = &mut self.nodes[target];
                    nn.chain_id = Some(n);
                    target = nn.parent;
                }

                // the start node is always excluded from the chain,
                // as we define the chain to contain the edges
                // that connect the node to its parent
                if self.nodes[n].chain_id == Some(n) {
                    self.nodes[n].chain_id = None;
                }
            }
        }
    }

    /// Count non-external bridge nodes. Make sure to call [Self::chain_decomposition] first.
    pub fn count_bridges(&self) -> usize {
        self.nodes
            .iter()
            .enumerate()
            .filter(|(n, x)| {
                x.chain_id.is_none()
                    && !self.nodes[x.parent].external
                    && !x.external
                    && x.parent != *n // exclude the root
                    && !self.nodes[x.parent].back_edges.iter().any(|end| n == end)
            })
            .count()
    }
}

impl<N, E> Graph<N, E> {
    /// Create an empty graph.
    pub fn new() -> Self {
        Graph {
            nodes: Vec::new(),
            edges: Vec::new(),
        }
    }

    /// Add a node to the graph, with arbitrary data, and return its index.
    pub fn add_node(&mut self, data: N) -> usize {
        let index = self.nodes.len();
        self.nodes.push(Node {
            edges: Vec::new(),
            data,
        });
        index
    }

    /// Add an edge between vertex indices `source` and `target` to the graph, with arbitrary data.
    /// If `directed` is true, the edge is directed from `source` to `target`.
    pub fn add_edge(
        &mut self,
        source: usize,
        target: usize,
        directed: bool,
        data: E,
    ) -> Result<usize, &'static str> {
        if source >= self.nodes.len() || target >= self.nodes.len() {
            return Err("Invalid node index");
        }

        let index = self.edges.len();
        self.edges.push(Edge {
            vertices: if !directed && source > target {
                (target, source)
            } else {
                (source, target)
            },
            directed,
            data,
        });
        self.nodes[source].edges.push(index);

        if source != target {
            self.nodes[target].edges.push(index);
        }
        Ok(index)
    }

    /// Set the data of the node at index `index`, returning the old data.
    pub fn set_node_data(&mut self, index: usize, data: N) -> N {
        std::mem::replace(&mut self.nodes[index].data, data)
    }

    /// Set the data of the edge at index `index`, returning the old data.
    pub fn set_edge_data(&mut self, index: usize, data: E) -> E {
        std::mem::replace(&mut self.edges[index].data, data)
    }

    /// Set the directed status of the edge at index `index`, returning the old value.
    pub fn set_directed(&mut self, index: usize, directed: bool) -> bool {
        std::mem::replace(&mut self.edges[index].directed, directed)
    }

    /// Delete the last added edge. This operation is O(1).
    pub fn delete_last_edge(&mut self) -> Option<Edge<E>> {
        if let Some(edge) = self.edges.pop() {
            self.nodes[edge.vertices.0].edges.pop();
            if edge.vertices.0 != edge.vertices.1 {
                self.nodes[edge.vertices.1].edges.pop();
            }
            Some(edge)
        } else {
            None
        }
    }

    /// Remove the last added empty node. This operation is O(1).
    pub fn delete_last_empty_node(&mut self) -> Option<Node<N>> {
        if let Some(node) = self.nodes.last() {
            if node.edges.is_empty() {
                return self.nodes.pop();
            }
        }
        None
    }

    /// Get the node with index `index`.
    #[inline(always)]
    pub fn node(&self, index: usize) -> &Node<N> {
        &self.nodes[index]
    }

    /// Get the edge with index `index`.
    #[inline(always)]
    pub fn edge(&self, index: usize) -> &Edge<E> {
        &self.edges[index]
    }

    /// Get all nodes of the graph.
    #[inline(always)]
    pub fn nodes(&self) -> &[Node<N>] {
        &self.nodes
    }

    /// Get all edges of the graph.
    #[inline(always)]
    pub fn edges(&self) -> &[Edge<E>] {
        &self.edges
    }

    // Get the number of loop in the graph, using E - V + 1
    pub fn num_loops(&self) -> usize {
        self.edges.len() + 1 - self.nodes.len()
    }

    /// Generate a spanning tree of the graph, starting at `start_vertex`.
    pub fn get_spanning_tree(&self, start_vertex: usize) -> SpanningTree {
        let mut nodes_to_visit = vec![(start_vertex, start_vertex)];
        let mut tree_nodes: Vec<_> = self
            .nodes
            .iter()
            .map(|n| NodeInfo {
                position: None,
                parent: 0,
                chain_id: None,
                external: n.edges.len() == 1,
                back_edges: vec![],
            })
            .collect();
        let mut order = vec![0; self.nodes.len()];

        let mut index = 0;
        while let Some((n, parent)) = nodes_to_visit.pop() {
            if let Some(p) = tree_nodes[n].position {
                let par = &mut tree_nodes[parent];
                if par.position.unwrap() < p {
                    par.back_edges.push(n);
                }
                continue;
            }

            tree_nodes[n].position = Some(index);
            tree_nodes[n].parent = parent;
            order[index] = n;
            index += 1;

            for e in &self.node(n).edges {
                let edge = self.edge(*e);
                let target = if edge.vertices.0 == n {
                    edge.vertices.1
                } else {
                    edge.vertices.0
                };

                if n == target {
                    tree_nodes[n].back_edges.push(n);
                }

                if tree_nodes[target].position.is_none() {
                    nodes_to_visit.push((target, n));
                }
            }
        }

        SpanningTree {
            nodes: tree_nodes,
            order,
        }
    }

    /// Check if the graph is connected.
    pub fn is_connected(&self) -> bool {
        if self.nodes.is_empty() {
            return true;
        }

        self.get_spanning_tree(0)
            .nodes
            .iter()
            .all(|x| x.position.is_some())
    }
}

impl<N, E: Eq + Ord + Hash> Graph<N, E> {
    /// Get the number of different ways to permute the multi-edges, leading
    /// to the same graph, while keeping the vertices fixed.
    ///
    /// Every self-loop yields an additional factor two.
    pub fn get_edge_automorphism_group_size(&self) -> Integer {
        let mut count = Integer::one();
        let mut h = HashMap::default();

        for e in &self.edges {
            if e.vertices.0 == e.vertices.1 {
                count *= 2;
            }

            h.entry(e.vertices)
                .or_insert(vec![])
                .push((e.directed, &e.data));
        }

        for (_, mut v) in h.into_iter() {
            v.sort();

            let mut counter = 1;
            let mut last = &v[0];
            for d in v.iter().skip(1) {
                if d == last {
                    counter += 1;
                } else {
                    count *= Integer::factorial(counter);
                    counter = 1;
                    last = d;
                }
            }
            count *= Integer::factorial(counter);
        }

        count
    }

    /// Sort and relabel the edges of the graph, keeping the vertices fixed.
    pub fn canonize_edges(&mut self) {
        for n in &mut self.nodes {
            n.edges.clear();
        }

        self.edges.sort();

        for (i, e) in self.edges.iter().enumerate() {
            self.nodes[e.vertices.0].edges.push(i);
            if e.vertices.0 != e.vertices.1 {
                self.nodes[e.vertices.1].edges.push(i);
            }
        }
    }
}

struct GenerationSettingsAndInput<'a, N, E> {
    vertex_signatures: &'a [Vec<(Option<bool>, E)>],
    allowed_structures: &'a HashSet<Vec<(Option<bool>, E)>>,
    min_degree: usize,
    max_degree: usize,
    settings: &'a GenerationSettings<N, E>,
}

pub trait FilterFn<N, E>: Fn(&Graph<N, E>, usize) -> bool + DynClone + Send + Sync {}
dyn_clone::clone_trait_object!(<N, E> FilterFn<N,E>);
impl<N, E, T: Clone + Send + Sync + Fn(&Graph<N, E>, usize) -> bool> FilterFn<N, E> for T {}

pub trait AbortCheck: Fn() -> bool + DynClone + Send + Sync {}
dyn_clone::clone_trait_object!(AbortCheck);
impl<T: Clone + Send + Sync + Fn() -> bool> AbortCheck for T {}

#[derive(Clone)]
pub struct GenerationSettings<N, E> {
    filter_fn: Option<Box<dyn FilterFn<N, E>>>,
    abort_check: Option<Box<dyn AbortCheck>>,
    max_vertices: Option<usize>,
    max_loops: Option<usize>,
    max_bridges: Option<usize>,
    allow_self_loops: bool,
}

impl<N, E> Default for GenerationSettings<N, E> {
    fn default() -> Self {
        Self::new()
    }
}

impl<N, E> GenerationSettings<N, E> {
    /// Create a new settings object with default values.
    /// These are:
    /// ```text
    /// filter_fn: None
    /// abort_check: None
    /// max_vertices: None
    /// max_loops: None
    /// max_bridges: None
    /// allow_self_loops: false
    /// ```
    pub const fn new() -> Self {
        Self {
            filter_fn: None,
            abort_check: None,
            max_vertices: None,
            max_loops: None,
            max_bridges: None,
            allow_self_loops: false,
        }
    }

    /// Set the maximum number of vertices in the generated graphs.
    pub fn max_vertices(mut self, max_vertices: usize) -> Self {
        self.max_vertices = Some(max_vertices);
        self
    }

    /// Set the maximum number of loops in the generated graphs.
    pub fn max_loops(mut self, max_loops: usize) -> Self {
        self.max_loops = Some(max_loops);
        self
    }

    /// Set the maximum number of bridges in the generated graphs.
    pub fn max_bridges(mut self, max_bridges: usize) -> Self {
        self.max_bridges = Some(max_bridges);
        self
    }

    /// Allow self-loops in the generated graphs.
    pub fn allow_self_loops(mut self, allow_self_loops: bool) -> Self {
        self.allow_self_loops = allow_self_loops;
        self
    }

    /// Set a filter function that is called during the graph generation.
    /// The first argument is the graph `g` and the second argument the vertex count `n`
    /// that specifies that the first `n` vertices are completed (no new edges will) be
    /// assigned to them. The filter function should return `true` if the current
    /// incomplete graph is allowed, else it should return `false` and the graph is discarded.
    pub fn filter_fn(mut self, filter_fn: Box<dyn FilterFn<N, E>>) -> Self {
        self.filter_fn = Some(filter_fn);
        self
    }

    /// A function that is called during graph generation to see if the graph
    /// generation should be aborted. The graphs that were generated at this stage
    /// are returned.
    pub fn abort_check(mut self, abort_check: Box<dyn AbortCheck>) -> Self {
        self.abort_check = Some(abort_check);
        self
    }
}

impl<
        N: Default + Clone + Eq + Hash + Ord + Display,
        E: Clone + Ord + Eq + Hash + Debug + Display,
    > Graph<N, E>
{
    /// Generate all connected graphs with `external_edges` half-edges and the given allowed list
    /// of vertex connections.
    ///
    /// Returns the canonical form of the graph and the size of its automorphism group (including edge permutations).
    /// If the graph generation was aborted, all currently generated graphs are returned in the `Err` argument.
    pub fn generate(
        external_edges: &[(N, (Option<bool>, E))],
        vertex_signatures: &[Vec<(Option<bool>, E)>],
        settings: &GenerationSettings<N, E>,
    ) -> Result<HashMap<Graph<N, E>, Integer>, HashMap<Graph<N, E>, Integer>> {
        if settings.max_vertices.is_none() && settings.max_loops.is_none() {
            panic!("At least one of max_vertices or max_loops must be set");
        }

        let vertex_sorted: Vec<_> = vertex_signatures
            .iter()
            .map(|x| {
                let mut x = x.clone();
                x.sort();
                x
            })
            .collect();

        let mut edge_signatures = vec![];
        let mut g = Self::new();
        for (n, _) in external_edges {
            edge_signatures.push(vec![]);
            g.add_node(n.clone());
        }

        if external_edges.len() == 0 {
            edge_signatures.push(vec![]);
            g.add_node(N::default());
        }

        let mut allowed_structures = HashSet::default();
        for e in &vertex_sorted {
            for k in 0..=e.len() {
                let mut it = CombinationIterator::new(e.len(), k);
                while let Some(c) = it.next() {
                    for p in unique_permutations(c).1 {
                        allowed_structures.insert(
                            p.iter()
                                .map(|&x| (e[x].0, e[x].1.clone()))
                                .collect::<Vec<_>>(),
                        );
                    }
                }
            }
        }

        let settings = GenerationSettingsAndInput {
            vertex_signatures: &vertex_sorted,
            allowed_structures: &allowed_structures,
            min_degree: vertex_sorted.iter().map(|x| x.len()).min().unwrap_or(0),
            max_degree: vertex_sorted.iter().map(|x| x.len()).max().unwrap_or(0),
            settings,
        };

        let mut out = HashMap::default();
        match g.generate_impl(external_edges, 0, &settings, &mut edge_signatures, &mut out) {
            Ok(()) => Ok(out),
            Err(()) => Err(out),
        }
    }

    fn generate_impl(
        &mut self,
        external_edges: &[(N, (Option<bool>, E))],
        cur_vertex: usize,
        settings: &GenerationSettingsAndInput<N, E>,
        edge_signatures: &mut Vec<Vec<(Option<bool>, E)>>,
        out: &mut HashMap<Graph<N, E>, Integer>,
    ) -> Result<(), ()> {
        if let Some(max_vertices) = settings.settings.max_vertices {
            if self.nodes.len() > max_vertices {
                return Ok(());
            }
        }

        if let Some(max_loops) = settings.settings.max_loops {
            // filter based on an underestimate of the loop count
            // determine the minimal number of additional edges
            // and assume that we create one connected component
            let mut extra_edges = self
                .nodes
                .iter()
                .enumerate()
                .skip(cur_vertex)
                .map(|(i, x)| {
                    if i < external_edges.len() && x.edges.is_empty() {
                        1
                    } else if i >= external_edges.len() && x.edges.len() < settings.min_degree {
                        settings.min_degree - x.edges.len()
                    } else {
                        0
                    }
                })
                .sum::<usize>();
            extra_edges = (extra_edges + 1) / 2;

            let e = self.edges.len() + extra_edges + 1;
            if e > max_loops + self.nodes.len() {
                return Ok(());
            }
        }

        if let Some(f) = &settings.settings.filter_fn {
            if !f(self, cur_vertex) {
                return Ok(());
            }
        }

        if cur_vertex == self.nodes.len() {
            let mut spanning_tree = self.get_spanning_tree(0);

            if !spanning_tree.is_connected() {
                return Ok(());
            }

            if let Some(abort_check) = &settings.settings.abort_check {
                if abort_check() {
                    return Err(());
                }
            }

            if let Some(max_bridges) = settings.settings.max_bridges {
                spanning_tree.chain_decomposition();
                if spanning_tree.count_bridges() > max_bridges {
                    return Ok(());
                }
            }

            let c = self.canonize();
            out.insert(c.graph, c.automorphism_group_size);
            return Ok(());
        }

        // find completions for the current vertex
        if cur_vertex < external_edges.len() {
            // generate a single connection with the external edge
            let n = self.node(cur_vertex).edges.len();
            if n == 0 {
                let mut edges_left: Vec<(_, usize)> =
                    vec![(external_edges[cur_vertex].1.clone(), 1)];

                self.distribute_edges(
                    cur_vertex,
                    external_edges.len(), // do not allow connections to other external edges
                    edge_signatures,
                    external_edges,
                    &mut edges_left,
                    0,
                    settings,
                    out,
                )?;
            } else if n == 1 {
                self.generate_impl(
                    external_edges,
                    cur_vertex + 1,
                    settings,
                    edge_signatures,
                    out,
                )?;
            }

            return Ok(());
        }

        let mut cur_edges: Vec<_> = self
            .node(cur_vertex)
            .edges
            .iter()
            .map(|e| {
                (
                    if self.edges[*e].directed {
                        Some(self.edges[*e].vertices.0 == cur_vertex)
                    } else {
                        None
                    },
                    self.edges[*e].data.clone(),
                )
            })
            .collect();
        cur_edges.sort();

        let mut edges_left: Vec<((Option<bool>, E), usize)> = vec![];
        'next_signature: for d in settings.vertex_signatures {
            // check if the current state is compatible
            if d.len() < cur_edges.len() {
                continue;
            }

            if *d == cur_edges {
                self.generate_impl(
                    external_edges,
                    cur_vertex + 1,
                    settings,
                    edge_signatures,
                    out,
                )?;
                continue;
            }

            edges_left.clear();
            let mut edge_pos = 0;
            for e in d {
                if edge_pos < cur_edges.len() {
                    if cur_edges[edge_pos] == *e {
                        edge_pos += 1;
                        continue;
                    } else if cur_edges[edge_pos] < *e {
                        // incompatible
                        continue 'next_signature;
                    }
                }

                if let Some(last) = edges_left.last_mut() {
                    if last.0 == *e {
                        last.1 += 1;
                        continue;
                    }
                }

                edges_left.push((e.clone(), 1));
            }

            if edge_pos < cur_edges.len() {
                // incompatible
                continue;
            }

            self.distribute_edges(
                cur_vertex,
                cur_vertex,
                edge_signatures,
                external_edges,
                &mut edges_left,
                0,
                settings,
                out,
            )?;
        }

        Ok(())
    }

    fn distribute_edges<'a>(
        &mut self,
        source: usize,
        cur_target: usize,
        edge_signatures: &mut Vec<Vec<(Option<bool>, E)>>,
        external_edges: &[(N, (Option<bool>, E))],
        edge_count: &mut [((Option<bool>, E), usize)],
        cur_edge_count_group_index: usize,
        settings: &'a GenerationSettingsAndInput<N, E>,
        out: &mut HashMap<Graph<N, E>, Integer>,
    ) -> Result<(), ()> {
        if edge_count.iter().all(|x| x.1 == 0) {
            return self.generate_impl(external_edges, source + 1, settings, edge_signatures, out);
        }

        let mut grown = false;
        if cur_target == self.nodes.len() {
            grown = true;
            edge_signatures.push(vec![]);
            self.add_node(N::default());
        } else {
            self.distribute_edges(
                source,
                cur_target + 1,
                edge_signatures,
                external_edges,
                edge_count,
                0,
                settings,
                out,
            )?;
        }

        if source == cur_target {
            if !settings.settings.allow_self_loops {
                return Ok(());
            }

            for p1 in cur_edge_count_group_index..edge_count.len() {
                if let Some(dir) = edge_count[p1].0 .0 {
                    if edge_count[p1].1 == 0 {
                        continue;
                    }

                    // find the edge signature going in the other direction
                    for p2 in cur_edge_count_group_index + 1..edge_count.len() {
                        if edge_count[p2].0 .0 == Some(!dir)
                            && edge_count[p1].0 .1 == edge_count[p2].0 .1
                        {
                            if edge_count[p2].1 == 0 {
                                break;
                            }

                            edge_count[p1].1 -= 1;
                            edge_count[p2].1 -= 1;

                            self.add_edge(source, source, true, edge_count[p1].0 .1.clone())
                                .unwrap();

                            self.distribute_edges(
                                source,
                                source,
                                edge_signatures,
                                external_edges,
                                edge_count,
                                p1,
                                settings,
                                out,
                            )?;

                            self.delete_last_edge();

                            edge_count[p1].1 += 1;
                            edge_count[p2].1 += 1;
                            break;
                        }
                    }
                } else {
                    let (e, count) = &mut edge_count[p1];
                    if *count < 2 {
                        continue;
                    }

                    *count -= 2;
                    self.add_edge(source, source, false, e.1.clone()).unwrap();

                    self.distribute_edges(
                        source,
                        source,
                        edge_signatures,
                        external_edges,
                        edge_count,
                        p1,
                        settings,
                        out,
                    )?;

                    self.delete_last_edge();

                    edge_count[p1].1 += 2;
                }
            }

            return Ok(());
        }

        let max_degree = if cur_target < external_edges.len() {
            1
        } else {
            settings.max_degree
        };

        if self.node(cur_target).edges.len() + 1 > max_degree {
            return Ok(());
        }

        for p in cur_edge_count_group_index..edge_count.len() {
            let (e, count) = &mut edge_count[p];

            if *count == 0 {
                continue;
            }

            if cur_target < external_edges.len() && e != &external_edges[cur_target].1 {
                continue;
            }

            // check if the target edge signature is allowed
            if let Some(dir) = e.0 {
                edge_signatures[cur_target].push((Some(!dir), e.1.clone()));
            } else {
                edge_signatures[cur_target].push((None, e.1.clone()));
            }

            if !settings
                .allowed_structures
                .contains(&edge_signatures[cur_target])
            {
                edge_signatures[cur_target].pop();
                continue;
            }

            *count -= 1;

            if let Some(dir) = e.0 {
                if dir {
                    self.add_edge(source, cur_target, true, e.1.clone())
                        .unwrap();
                } else {
                    self.add_edge(cur_target, source, true, e.1.clone())
                        .unwrap();
                }
            } else {
                self.add_edge(source, cur_target, false, e.1.clone())
                    .unwrap();
            }

            self.distribute_edges(
                source,
                cur_target,
                edge_signatures,
                external_edges,
                edge_count,
                p,
                settings,
                out,
            )?;

            edge_signatures[cur_target].pop();
            self.delete_last_edge(); // TODO: cache edge data

            edge_count[p].1 += 1;
        }

        if grown {
            edge_signatures.pop();
            self.delete_last_empty_node().unwrap();
        }

        Ok(())
    }
}

/// Information about the graph and its canonical form.
pub struct CanonicalForm<N, E> {
    /// Mapping of the vertices from the input graph to the canonical graph.
    pub vertex_map: Vec<usize>,
    /// Generators of the automorphism group of the graph.
    pub orbit_generators: Vec<Vec<Vec<usize>>>,
    /// An orbit of the graph.
    pub orbit: Vec<usize>,
    /// The size of the automorphism group of the graph, including
    /// the permutations stemming from identical edges.
    pub automorphism_group_size: Integer,
    pub graph: Graph<N, E>,
}

impl<N: Clone + PartialOrd + Ord + Eq + Hash, E: Clone + PartialOrd + Ord + Eq + Hash> Graph<N, E> {
    /// Canonize the graph using McKay's canonical graph labeling algorithm,
    /// returning the vertex mapping and the canonical form.
    pub fn canonize(&self) -> CanonicalForm<N, E> {
        if self.nodes.is_empty() {
            return CanonicalForm {
                vertex_map: vec![],
                orbit_generators: vec![],
                orbit: vec![],
                automorphism_group_size: Integer::one(),
                graph: self.clone(),
            };
        }

        if self.nodes.len() <= u16::MAX as usize {
            self.canonize_impl::<u16>(false)
        } else if self.nodes.len() <= u32::MAX as usize {
            self.canonize_impl::<u32>(false)
        } else {
            self.canonize_impl::<usize>(false)
        }
    }

    fn canonize_impl<I: NodeIndex>(&self, verbose: bool) -> CanonicalForm<N, E> {
        let mut stack = vec![SearchTreeNode::<I>::new(self)];
        let mut automorphisms = vec![];
        let mut minimal_representatives_per_generator = vec![];
        let mut leaf_nodes: HashMap<_, (Vec<_>, Vec<_>)> = HashMap::default(); // TODO: limit growth
        let mut current_best: Option<(Graph<&N, &E>, Vec<I>, Vec<Invariant<I>>)> = None;

        let mut node_buffer = vec![];

        let mut automorphism_group_len = Integer::one();
        let mut orbit = (0..self.nodes.len()).collect::<Vec<_>>();

        while let Some(mut node) = stack.pop() {
            if node.selected_vertex.is_none() {
                node.refine(self);
            }

            if let Some((_, _, best_invariant)) = &current_best {
                // the canonical form is defined as the maximal isomorph, prepended with the node invariants of the path
                // at each tree level, the node invariant must therefore be at least as good as the best
                // to be a potential canonical form
                match node
                    .invariant
                    .cmp(&best_invariant[stack.len().min(best_invariant.len() - 1)])
                {
                    Ordering::Less => {
                        node_buffer.push(node);
                        continue;
                    }
                    Ordering::Greater => {
                        // we will find a better isomorph on this path
                        current_best = None;
                    }
                    Ordering::Equal => {}
                }
            }

            if node.partition.iter().all(|x| x.len() == 1) {
                let partition: Vec<_> = node.partition.iter().map(|x| x[0]).collect();

                let mut g = Graph::new();
                for i in &partition {
                    g.add_node(&self.node(i.to_usize()).data);
                }
                let mut edges = vec![];
                for e in &self.edges {
                    let a = partition
                        .iter()
                        .position(|&x| x.to_usize() == e.vertices.0)
                        .unwrap();
                    let b = partition
                        .iter()
                        .position(|&x| x.to_usize() == e.vertices.1)
                        .unwrap();

                    if e.directed || a <= b {
                        edges.push((a, b, e.directed, &e.data));
                    } else {
                        edges.push((b, a, e.directed, &e.data));
                    }
                }
                edges.sort();
                for (v1, v2, dir, d) in edges {
                    g.add_edge(v1, v2, dir, d).unwrap();
                }

                let path: Vec<_> = stack.iter().map(|x| x.selected_vertex.unwrap()).collect();

                if let Some((old_partition, old_path)) = leaf_nodes.get(&g) {
                    // construct the automorphism transformation
                    let mut seen = vec![false; partition.len()];

                    let mut fixed = vec![];
                    let mut minimal_representatives = vec![];
                    let mut orbits = vec![];
                    for x in &partition {
                        let mut cur = *x;
                        if seen[cur.to_usize()] {
                            continue;
                        }

                        let mut orbit = vec![cur];

                        let parts = [old_partition, &partition];
                        let mut part_i = 0;
                        loop {
                            cur = parts[(part_i + 1) % 2]
                                [parts[part_i].iter().position(|y| y == &cur).unwrap()];

                            if cur == *x {
                                break;
                            } else {
                                seen[cur.to_usize()] = true;
                                orbit.push(cur);
                                part_i = (part_i + 1) % 2;
                            }
                        }

                        orbit.sort();

                        if orbit.len() == 1 {
                            fixed.push(orbit[0]);
                        } else {
                            // only store the minimal representative per orbit
                            minimal_representatives.push(orbit[0]);
                            orbits.push(orbit);
                        }
                    }

                    minimal_representatives_per_generator.push((fixed, minimal_representatives));
                    automorphisms.push(orbits);

                    // fall back to common ancestor in the search tree
                    let mut i = 0;
                    for (p1, p2) in old_path.iter().zip(path.iter()) {
                        if p1 == p2 {
                            i += 1;
                        } else {
                            break;
                        }
                    }

                    // we will pop an extra node at the start of the next loop, hence + 1
                    node_buffer.extend(stack.drain(i + 1..));
                    continue;
                }

                if let Some((best, _, _)) = &current_best {
                    debug_assert!(g != *best);
                    if g < *best {
                        // isomorph does not improve the best
                        // add it to the list of terminal nodes anyway to discover new
                        // automorphisms
                        leaf_nodes.insert(g, (partition, path));
                        node_buffer.push(node);
                        continue;
                    }
                }

                let mut best_invariant: Vec<_> =
                    stack.iter().map(|x| x.invariant.clone()).collect();
                best_invariant.push(node.invariant.clone());
                current_best = Some((g.clone(), partition.clone(), best_invariant));
                leaf_nodes.insert(g, (partition, path));
                node_buffer.push(node);
            } else {
                let (x, p) = if let Some(p) = node.selected_part {
                    // upon a repeat visit, filter the list of possible children with
                    // the automorphism group, taking the smallest out of every orbit
                    // for non-left node children, we apply a simpler filter that does
                    // not combine orbits and only takes the smallest vertex per orbit

                    for (i, o) in orbit.iter_mut().enumerate() {
                        *o = i;
                    }

                    if node.left_node {
                        // TODO: we can update the orbit globally, as there is only one left path
                        for s_orbits in automorphisms.iter() {
                            // filter orbits that do not fix the vertices that are fixed by the selected path
                            if stack
                                .iter()
                                .map(|x| x.selected_vertex.unwrap())
                                .any(|x| s_orbits.iter().any(|o| o.contains(&x)))
                            {
                                continue;
                            }

                            for s_orbits in s_orbits {
                                // find minimal representatives for each orbit
                                let min =
                                    s_orbits.iter().map(|x| orbit[x.to_usize()]).min().unwrap();
                                for a in s_orbits {
                                    let old_val = orbit[a.to_usize()];

                                    if old_val == min {
                                        continue;
                                    }

                                    for o in &mut orbit {
                                        if *o == old_val {
                                            *o = min;
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        for (fixed, reps) in &minimal_representatives_per_generator {
                            if stack
                                .iter()
                                .all(|x| fixed.contains(&x.selected_vertex.unwrap()))
                            {
                                node.children_to_visit
                                    .retain(|x| reps.contains(&x) || fixed.contains(&x));
                            }
                        }
                    }

                    let orig = node.orig_selected_vertex.unwrap().to_usize();

                    // check if the last vertex we tried turned out to be in the same orbit as the first one
                    // due to newly found automorphisms
                    if node.left_node {
                        if orbit[node.selected_vertex.unwrap().to_usize()] == orig {
                            node.children_visited_equal_to_first += 1;
                        }
                    }

                    node.selected_vertex = None;
                    while !node.children_to_visit.is_empty() {
                        // individualize x
                        let x = node.children_to_visit.remove(0);
                        if node.left_node && orbit[x.to_usize()] == orig {
                            node.children_visited_equal_to_first += 1;
                        }

                        if orbit[x.to_usize()] == x.to_usize() {
                            node.selected_vertex = Some(x);
                            break;
                        }
                    }

                    if node.children_to_visit.is_empty() && node.selected_vertex.is_none() {
                        // use the left-most path to determine the automorphism group size,
                        // by applying the orbit-stabilizer theorem; we determined the number of child
                        // vertices that are in the same orbit as the first child vertex
                        // multiplying this number at every level gives the automorphism group size, since
                        // at every level we stabilize the first vertex
                        if node.left_node {
                            if verbose {
                                println!(
                                    "Level={}, group size={}, orbit num={}",
                                    stack.len(),
                                    node.children_visited_equal_to_first,
                                    orbit.iter().enumerate().filter(|(i, x)| *i == **x).count()
                                );
                            }
                            automorphism_group_len *= node.children_visited_equal_to_first as u64;
                        }

                        node_buffer.push(node);
                        continue;
                    }

                    (node.selected_vertex.unwrap(), p.to_usize())
                } else {
                    // find the first minimal length non-trivial part to individualize
                    let smallest = node
                        .partition
                        .iter()
                        .map(|x| x.len())
                        .filter(|x| *x > 1)
                        .min()
                        .unwrap();

                    let (p, part) = node
                        .partition
                        .iter()
                        .enumerate()
                        .find(|x| x.1.len() == smallest)
                        .unwrap();

                    node.children_to_visit = part.clone();
                    node.selected_part = Some(I::from_usize(p));

                    // individualize x
                    let x = node.children_to_visit.remove(0);
                    node.selected_vertex = Some(x);

                    (x, p)
                };

                let mut new_node = match node_buffer.pop() {
                    Some(mut n) => {
                        n.partition.clear();
                        n.children_to_visit.clear();
                        n.children_visited_equal_to_first = 0;
                        n.orig_selected_vertex = None;
                        n.selected_part = None;
                        n.selected_vertex = None;
                        n
                    }
                    None => SearchTreeNode::default(),
                };

                new_node.left_node = node.left_node && node.orig_selected_vertex.is_none();

                if node.orig_selected_vertex.is_none() {
                    node.orig_selected_vertex = Some(x);
                }

                new_node
                    .partition
                    .extend(node.partition.iter().take(p).cloned());
                let part = &node.partition[p.to_usize()];
                new_node.partition.push(vec![x]);
                new_node
                    .partition
                    .push(part.iter().filter(|&y| *y != x).cloned().collect());
                new_node
                    .partition
                    .extend(node.partition.iter().skip(p + 1).cloned());

                stack.push(node);
                stack.push(new_node);
            }
        }

        let (best, map, _) = current_best.unwrap();
        let mut g = Graph::new();
        for i in 0..best.nodes.len() {
            g.add_node(best.node(i).data.clone());
        }
        for e in &best.edges {
            g.add_edge(e.vertices.0, e.vertices.1, e.directed, e.data.clone())
                .unwrap();
        }

        let inv_map: Vec<_> = (0..self.nodes.len())
            .map(|x| {
                map.iter()
                    .position(|y| y.to_usize() == x)
                    .unwrap()
                    .to_usize()
            })
            .collect();

        // transform the automorphisms to the new vertex numbering
        let automorphisms: Vec<_> = automorphisms
            .into_iter()
            .map(|x| {
                x.into_iter()
                    .map(|y| {
                        y.into_iter()
                            .map(|z| inv_map[z.to_usize()])
                            .collect::<Vec<_>>()
                    })
                    .collect::<Vec<_>>()
            })
            .collect();

        let orbit = (0..orbit.len())
            .map(|x| inv_map[orbit[map[x].to_usize()]])
            .collect();

        CanonicalForm {
            vertex_map: inv_map,
            orbit_generators: automorphisms,
            automorphism_group_size: automorphism_group_len * g.get_edge_automorphism_group_size(),
            orbit,
            graph: g,
        }
    }

    /// Returns `true` iff the graph is isomorphic to `other`.
    pub fn is_isomorphic(&self, other: &Self) -> bool {
        if self.nodes.len() != other.nodes.len() || self.edges.len() != other.edges.len() {
            return false;
        }

        if self == other {
            return true;
        }

        let mut node = SearchTreeNode::<usize>::new(self);
        let mut other_node = SearchTreeNode::new(other);

        if node.partition.len() != other_node.partition.len() {
            return false;
        }

        // check if the vertex colors are the same
        for (i, j) in node.partition.iter().zip(&other_node.partition) {
            if i.len() != j.len() || self.node(i[0]).data != other.node(j[0]).data {
                return false;
            }
        }

        // check if the first refinement has the same node invariant
        node.refine(self);
        other_node.refine(other);

        if node.invariant != other_node.invariant {
            return false;
        }

        let other_canon = other.canonize().graph;

        if *self == other_canon {
            return true;
        }

        self.canonize().graph == other_canon
    }
}

/// An index type that can be used to index nodes in a graph.
/// Used to save memory for small graphs.
trait NodeIndex: Default + Copy + PartialOrd + Ord + Eq + Hash + Display + Debug {
    fn to_usize(&self) -> usize;
    fn from_usize(x: usize) -> Self;
}

impl NodeIndex for usize {
    #[inline(always)]
    fn to_usize(&self) -> usize {
        *self
    }

    #[inline(always)]
    fn from_usize(x: usize) -> Self {
        x
    }
}

impl NodeIndex for u32 {
    #[inline(always)]
    fn to_usize(&self) -> usize {
        *self as usize
    }

    #[inline(always)]
    fn from_usize(x: usize) -> Self {
        debug_assert!(x <= u32::MAX as usize);
        x as u32
    }
}

impl NodeIndex for u16 {
    #[inline(always)]
    fn to_usize(&self) -> usize {
        *self as usize
    }

    #[inline(always)]
    fn from_usize(x: usize) -> Self {
        debug_assert!(x <= u16::MAX as usize);
        x as u16
    }
}

/// A node invariant.
#[derive(Default, Debug, Clone, PartialOrd, Ord, PartialEq, Eq)]
struct Invariant<N: NodeIndex> {
    partition_lengths: Vec<N>,
}

/// A search tree node used for canonization.
#[derive(Default)]
struct SearchTreeNode<N: NodeIndex> {
    partition: Vec<Vec<N>>,
    selected_part: Option<N>,
    selected_vertex: Option<N>,
    children_to_visit: Vec<N>,
    left_node: bool,
    orig_selected_vertex: Option<N>,
    children_visited_equal_to_first: usize,
    invariant: Invariant<N>,
}

impl<I: NodeIndex> SearchTreeNode<I> {
    /// Create a new search tree node with a partition refined on vertex color.
    fn new<N: PartialOrd + Ord + Hash, E>(g: &Graph<N, E>) -> Self {
        let mut h = HashMap::default();
        for (i, x) in g.nodes.iter().enumerate() {
            h.entry(&x.data)
                .or_insert_with(Vec::new)
                .push(I::from_usize(i));
        }
        let mut partition: Vec<_> = h.into_iter().collect();
        partition.sort_by_key(|x| x.0);

        SearchTreeNode {
            partition: partition.into_iter().map(|x| x.1).collect(),
            selected_part: None,
            selected_vertex: None,
            children_to_visit: vec![],
            orig_selected_vertex: None,
            left_node: true,
            children_visited_equal_to_first: 0,
            invariant: Invariant::default(),
        }
    }

    /// Compute a node invariant.
    fn update_invariant(&mut self) {
        self.invariant.partition_lengths.clear();
        // TODO: improve invariant
        self.invariant
            .partition_lengths
            .extend(self.partition.iter().map(|x| I::from_usize(x.len())));
    }

    /// Apply a label-invariant refinement to the partition.
    /// The current refinement is based on the signature of the neighbors of a vertex.
    /// The signature takes directional edges and edge colors into account.
    fn refine<N, E: PartialOrd + Ord>(&mut self, g: &Graph<N, E>) {
        let largest_partition = self.partition.iter().map(|x| x.len()).max().unwrap();
        let mut degrees = vec![(vec![], I::from_usize(0)); largest_partition];

        let mut last_stable_index = 0; // no splits happened before this index in the last round
        'next: loop {
            for (ii, i) in self.partition.iter().enumerate() {
                if i.len() == 1 {
                    continue;
                }

                for (jj, j) in self.partition.iter().enumerate() {
                    if ii < last_stable_index && jj < last_stable_index {
                        // this part is already tested and stable
                        continue;
                    }

                    // sorted edge colors of edges in i that connect to vertices in j
                    // the length of this vector is the degree of the vertex i in j
                    for ((edge_data, vert), v) in degrees.iter_mut().zip(i) {
                        edge_data.clear();

                        for e in &g.node(v.to_usize()).edges {
                            let e = g.edge(*e);
                            let (k, is_source) = if e.vertices.0 == v.to_usize() {
                                (NodeIndex::from_usize(e.vertices.1), true)
                            } else {
                                (NodeIndex::from_usize(e.vertices.0), false)
                            };
                            if j.contains(&k) {
                                if e.directed {
                                    edge_data.push((&e.data, e.directed, is_source));
                                } else {
                                    edge_data.push((&e.data, false, false));
                                }
                            }
                        }
                        edge_data.sort_unstable();
                        *vert = *v;
                    }

                    if degrees[..i.len()].windows(2).all(|w| w[0].0 == w[1].0) {
                        continue;
                    }

                    degrees[..i.len()].sort_unstable();

                    let mut degs = vec![];
                    let mut cur = vec![degrees[0].1];
                    for x in degrees[..i.len()].windows(2) {
                        if x[0].0 != x[1].0 {
                            degs.push(cur);
                            cur = vec![x[1].1];
                        } else {
                            cur.push(x[1].1);
                        }
                    }
                    degs.push(cur);

                    self.partition.splice(ii..=ii, degs);
                    last_stable_index = ii;
                    continue 'next;
                }
            }

            break;
        }

        self.update_invariant();
    }
}

#[cfg(test)]
mod test {
    use crate::graph::{GenerationSettings, Graph, SearchTreeNode};

    #[test]
    fn directed() {
        let mut g = Graph::new();
        let n0 = g.add_node(0);
        let n1 = g.add_node(0);
        g.add_edge(n0, n1, false, 0).unwrap();
        g.add_edge(n0, n1, false, 1).unwrap();

        let mut node = SearchTreeNode::<usize>::default();
        node.partition = vec![vec![0, 1]];
        node.refine(&g);
        assert_eq!(node.partition.len(), 1); // cannot refine

        let mut g = Graph::new();
        let n0 = g.add_node(0);
        let n1 = g.add_node(0);
        g.add_edge(n0, n1, true, 0).unwrap();
        g.add_edge(n0, n1, false, 1).unwrap();

        let mut node = SearchTreeNode::<usize>::default();
        node.partition = vec![vec![0, 1]];
        node.refine(&g);
        assert_eq!(node.partition.len(), 2); // distinguish two nodes based on directed edge
    }

    #[test]
    fn isomorphic() {
        let mut g = Graph::new();
        let n0 = g.add_node(0);
        g.add_edge(n0, n0, false, 0).unwrap();
        g.add_edge(n0, n0, false, 0).unwrap();

        let mut g1 = Graph::new();
        let n0 = g1.add_node(0);
        g1.add_edge(n0, n0, false, 0).unwrap();

        assert!(!g.is_isomorphic(&g1));

        g1.add_edge(n0, n0, true, 0).unwrap();
        assert!(!g.is_isomorphic(&g1));

        g.add_edge(n0, n0, true, 0).unwrap();
        g1.add_edge(n0, n0, false, 0).unwrap();
        assert!(g.is_isomorphic(&g1));

        let _ = g.add_node(1);
        let _ = g1.add_node(0);
        assert!(!g.is_isomorphic(&g1));

        let _ = g1.add_node(1);
        let _ = g.add_node(0);
        assert!(g.is_isomorphic(&g1));
    }

    #[test]
    fn canonize() {
        let mut g = Graph::new();
        let n0 = g.add_node(1);
        let n1 = g.add_node(0);
        let n2 = g.add_node(1);
        let n3 = g.add_node(0);
        let n4 = g.add_node(2);
        let n5 = g.add_node(0);
        let n6 = g.add_node(1);
        let n7 = g.add_node(0);
        let n8 = g.add_node(1);

        g.add_edge(n0, n1, false, 0).unwrap();
        g.add_edge(n0, n3, false, 0).unwrap();
        g.add_edge(n1, n2, false, 0).unwrap();
        g.add_edge(n1, n3, false, 0).unwrap();
        g.add_edge(n1, n4, false, 0).unwrap();
        g.add_edge(n1, n5, false, 0).unwrap();
        g.add_edge(n2, n5, false, 0).unwrap();
        g.add_edge(n3, n4, false, 0).unwrap();
        g.add_edge(n3, n6, false, 0).unwrap();
        g.add_edge(n3, n7, false, 0).unwrap();
        g.add_edge(n4, n5, false, 0).unwrap();
        g.add_edge(n4, n7, false, 0).unwrap();
        g.add_edge(n5, n7, false, 0).unwrap();
        g.add_edge(n5, n8, false, 0).unwrap();
        g.add_edge(n6, n7, false, 0).unwrap();
        g.add_edge(n7, n8, false, 0).unwrap();

        let c = g.canonize();

        assert_eq!(c.orbit_generators.len(), 2);
        assert_eq!(c.automorphism_group_size, 8);
        assert_eq!(c.graph.edge(0).vertices, (0, 2));
    }

    #[test]
    fn canonize_edges() {
        let mut g = Graph::new();
        let n0 = g.add_node(0);
        let n1 = g.add_node(1);
        let n2 = g.add_node(2);

        g.add_edge(n2, n1, true, 0).unwrap();
        g.add_edge(n0, n0, false, 0).unwrap();
        g.add_edge(n0, n1, true, 0).unwrap();
        g.add_edge(n1, n0, false, 2).unwrap();

        g.canonize_edges();

        assert_eq!(g.node(0).edges, [0, 1, 2]);
    }

    #[test]
    fn generate() {
        let gs = Graph::<_, &str>::generate(
            &[(1, (None, "g")), (2, (None, "g"))],
            &[
                vec![(None, "g"), (None, "g"), (None, "g")],
                vec![(Some(true), "q"), (Some(false), "q"), (None, "g")],
                vec![(None, "g"), (None, "g"), (None, "g"), (None, "g")],
            ],
            &GenerationSettings::new()
                .max_loops(3)
                .max_bridges(0)
                .allow_self_loops(true),
        )
        .unwrap();

        assert_eq!(gs.len(), 210);
    }

    #[test]
    fn generate_tree() {
        let external_edges = vec![
            (1, (None, "g")),
            (2, (None, "g")),
            (3, (None, "g")),
            (4, (None, "g")),
        ];
        let vertex_signatures = vec![
            vec![(None, "g"), (None, "g"), (None, "g")],
            vec![(None, "g"), (None, "g"), (None, "g"), (None, "g")],
        ];

        let graphs = Graph::generate(
            &external_edges,
            &vertex_signatures,
            &GenerationSettings::new()
                .max_loops(2)
                .max_bridges(0)
                .allow_self_loops(true),
        )
        .unwrap();

        assert_eq!(graphs.len(), 278);
    }

    #[test]
    fn generate_with_filter() {
        let graphs = Graph::<_, &str>::generate(
            &[(1, (None, "g")), (2, (None, "g"))],
            &[
                vec![(None, "g"), (None, "g"), (None, "g")],
                vec![(Some(true), "q"), (Some(false), "q"), (None, "g")],
                vec![(None, "g"), (None, "g"), (None, "g"), (None, "g")],
            ],
            &GenerationSettings::new()
                .max_loops(4)
                .max_bridges(0)
                .allow_self_loops(true)
                .filter_fn(Box::new(|g, c| {
                    // each graph must have at most one node of type g-g-g
                    let mut count = 0;
                    for n in 0..c {
                        // loop only through frozen nodes
                        let node = g.node(n);
                        if node.edges.len() == 3
                            && node.edges.iter().all(|e| g.edge(*e).data == "g")
                        {
                            count += 1;
                            if count == 2 {
                                return false;
                            }
                        }
                    }

                    true
                })),
        )
        .unwrap();

        assert_eq!(graphs.len(), 845);
    }
}