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
//! APTED: All Path Tree Edit Distance algorithm.
//!
//! An improved tree edit distance algorithm (Pawlik & Augsten 2016) that
//! dynamically selects the optimal decomposition strategy for each subtree pair,
//! achieving O(n^2) worst case while being much faster in practice than
//! Zhang-Shasha's O(n^2 * m^2) worst case.
//!
//! The key insight is that APTED avoids redundant computations by selecting the
//! optimal decomposition path (left, right, or heavy) for each subtree pair,
//! leading to amortized O(n^2) time complexity. For small trees it falls back
//! to standard Zhang-Shasha forest-distance computation.
use std::cmp;
use super::tree_edit_distance::LabeledTree;
/// Threshold below which we use direct Zhang-Shasha computation
/// instead of the strategy-guided APTED approach.
const SMALL_TREE_THRESHOLD: usize = 10;
/// Decomposition strategy for a subtree.
///
/// Determines which path to follow when decomposing a subtree pair
/// into subproblems. The optimal choice minimizes the total work.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Strategy {
/// Decompose along the leftmost path (left-to-right).
Left,
/// Decompose along the rightmost path (right-to-left).
Right,
/// Decompose along the path to the heavy child (largest subtree).
Heavy,
}
/// APTED computation engine.
///
/// Precomputes structural information about both trees (post-order indexing,
/// subtree sizes, leftmost/rightmost leaf descendants, and optimal strategies)
/// then uses strategy-guided dynamic programming to compute the edit distance.
pub struct AptedComputer {
/// Sizes of all subtrees in tree A (indexed by post-order, 0-based).
sizes_a: Vec<usize>,
/// Sizes of all subtrees in tree B (indexed by post-order, 0-based).
sizes_b: Vec<usize>,
/// Strategy array for tree A nodes.
strategy_a: Vec<Strategy>,
/// Strategy array for tree B nodes.
strategy_b: Vec<Strategy>,
/// Labels in post-order for tree A.
labels_a: Vec<String>,
/// Labels in post-order for tree B.
labels_b: Vec<String>,
/// Children indices (post-order) for each node in tree A.
children_a: Vec<Vec<usize>>,
/// Children indices (post-order) for each node in tree B.
children_b: Vec<Vec<usize>>,
/// Left-most leaf descendants (post-order index) for tree A.
lml_a: Vec<usize>,
/// Left-most leaf descendants (post-order index) for tree B.
lml_b: Vec<usize>,
/// Right-most leaf descendants (post-order index) for tree A.
rml_a: Vec<usize>,
/// Right-most leaf descendants (post-order index) for tree B.
rml_b: Vec<usize>,
/// Parent of each node in tree A (post-order index), usize::MAX for root.
parent_a: Vec<usize>,
/// Parent of each node in tree B (post-order index), usize::MAX for root.
parent_b: Vec<usize>,
/// Number of nodes in tree A.
n_a: usize,
/// Number of nodes in tree B.
n_b: usize,
}
/// Information collected during post-order traversal of a tree.
struct TreeInfo {
labels: Vec<String>,
sizes: Vec<usize>,
children: Vec<Vec<usize>>,
lml: Vec<usize>,
rml: Vec<usize>,
parent: Vec<usize>,
strategies: Vec<Strategy>,
}
impl TreeInfo {
/// Build tree info by flattening a LabeledTree into post-order arrays.
fn from_tree(tree: &LabeledTree) -> Self {
let n = tree.size();
let mut info = TreeInfo {
labels: Vec::with_capacity(n),
sizes: Vec::with_capacity(n),
children: Vec::with_capacity(n),
lml: Vec::with_capacity(n),
rml: Vec::with_capacity(n),
parent: Vec::with_capacity(n),
strategies: Vec::with_capacity(n),
};
// Initialize with empty data; will be filled during traversal
for _ in 0..n {
info.labels.push(String::new());
info.sizes.push(0);
info.children.push(Vec::new());
info.lml.push(0);
info.rml.push(0);
info.parent.push(usize::MAX);
info.strategies.push(Strategy::Left);
}
let mut idx = 0;
Self::fill_postorder(tree, &mut info, &mut idx);
debug_assert_eq!(idx, n);
// Compute strategies based on subtree sizes
Self::compute_strategies(&mut info);
info
}
/// Recursively traverse the tree in post-order, filling arrays.
/// Returns the post-order index assigned to this node.
fn fill_postorder(node: &LabeledTree, info: &mut TreeInfo, idx: &mut usize) -> usize {
let mut child_indices = Vec::with_capacity(node.children.len());
let mut my_lml = usize::MAX;
let mut my_rml = usize::MAX;
for child in &node.children {
let child_idx = Self::fill_postorder(child, info, idx);
child_indices.push(child_idx);
// Left-most leaf: take from the first child
if my_lml == usize::MAX {
my_lml = info.lml[child_idx];
}
// Right-most leaf: take from the last child (overwrite each time)
my_rml = info.rml[child_idx];
}
let my_idx = *idx;
*idx += 1;
info.labels[my_idx] = node.label.clone();
// Compute subtree size: 1 + sum of children sizes
let subtree_size: usize = child_indices
.iter()
.map(|&ci| info.sizes[ci])
.sum::<usize>()
+ 1;
info.sizes[my_idx] = subtree_size;
// For leaves, lml and rml point to self
if node.children.is_empty() {
info.lml[my_idx] = my_idx;
info.rml[my_idx] = my_idx;
} else {
info.lml[my_idx] = my_lml;
info.rml[my_idx] = my_rml;
}
info.children[my_idx] = child_indices.clone();
// Set parent for all children
for &ci in &child_indices {
info.parent[ci] = my_idx;
}
my_idx
}
/// Compute optimal decomposition strategy for each node.
///
/// For each internal node:
/// - Find the "heavy" child (largest subtree)
/// - If the leftmost child is heavy, use Left strategy
/// - If the rightmost child is heavy, use Right strategy
/// - Otherwise, use Heavy strategy (decompose along heavy path)
fn compute_strategies(info: &mut TreeInfo) {
let n = info.sizes.len();
for i in 0..n {
if info.children[i].is_empty() {
// Leaves: strategy doesn't matter, default to Left
info.strategies[i] = Strategy::Left;
continue;
}
// Find the heavy child (largest subtree)
let children = &info.children[i];
let mut heavy_idx = 0;
let mut heavy_size = 0;
for (ci, &child) in children.iter().enumerate() {
if info.sizes[child] > heavy_size {
heavy_size = info.sizes[child];
heavy_idx = ci;
}
}
if heavy_idx == 0 {
// Heavy child is the leftmost => Left decomposition
info.strategies[i] = Strategy::Left;
} else if heavy_idx == children.len() - 1 {
// Heavy child is the rightmost => Right decomposition
info.strategies[i] = Strategy::Right;
} else {
// Heavy child is in the middle => Heavy decomposition
info.strategies[i] = Strategy::Heavy;
}
}
}
}
impl AptedComputer {
/// Create a new APTED computer from two labeled trees.
///
/// Flattens both trees into post-order arrays and precomputes
/// structural metadata (sizes, leaf descendants, strategies).
pub fn new(a: &LabeledTree, b: &LabeledTree) -> Self {
let info_a = TreeInfo::from_tree(a);
let info_b = TreeInfo::from_tree(b);
Self {
n_a: info_a.sizes.len(),
n_b: info_b.sizes.len(),
sizes_a: info_a.sizes,
sizes_b: info_b.sizes,
strategy_a: info_a.strategies,
strategy_b: info_b.strategies,
labels_a: info_a.labels,
labels_b: info_b.labels,
children_a: info_a.children,
children_b: info_b.children,
lml_a: info_a.lml,
lml_b: info_b.lml,
rml_a: info_a.rml,
rml_b: info_b.rml,
parent_a: info_a.parent,
parent_b: info_b.parent,
}
}
/// Compute the tree edit distance using strategy-guided decomposition.
///
/// For small trees (< SMALL_TREE_THRESHOLD nodes), falls back to the
/// standard Zhang-Shasha key-root approach. For larger trees, uses
/// the APTED strategy-guided computation that selects the optimal
/// decomposition path for each subtree pair.
pub fn compute(&mut self) -> usize {
if self.n_a == 0 && self.n_b == 0 {
return 0;
}
if self.n_a == 0 {
return self.n_b;
}
if self.n_b == 0 {
return self.n_a;
}
// For small trees, use standard Zhang-Shasha via key roots
if self.n_a < SMALL_TREE_THRESHOLD && self.n_b < SMALL_TREE_THRESHOLD {
return self.compute_zhang_shasha();
}
// For larger trees, use strategy-guided APTED computation
self.compute_apted()
}
/// Standard Zhang-Shasha computation using key roots and forest distances.
/// Uses 1-based indexing internally (post-order index + 1).
fn compute_zhang_shasha(&self) -> usize {
let n = self.n_a;
let m = self.n_b;
// Compute key roots for tree A
let key_roots_a = self.compute_key_roots_a();
let key_roots_b = self.compute_key_roots_b();
// td[i][j] = tree distance between subtree at post-order i in A
// and subtree at post-order j in B (0-indexed)
let mut td = vec![vec![0usize; m]; n];
for &x in &key_roots_a {
for &y in &key_roots_b {
let lx = self.lml_a[x];
let ly = self.lml_b[y];
// Forest distance array
// fd[i][j] represents forest distance where i and j are offsets
let rows = x - lx + 2;
let cols = y - ly + 2;
let mut fd = vec![vec![0usize; cols]; rows];
// Base cases
for i in 1..rows {
fd[i][0] = fd[i - 1][0] + 1;
}
for j in 1..cols {
fd[0][j] = fd[0][j - 1] + 1;
}
for i in 1..rows {
for j in 1..cols {
let node_a = lx + i - 1;
let node_b = ly + j - 1;
let cost = if self.labels_a[node_a] == self.labels_b[node_b] {
0
} else {
1
};
if self.lml_a[node_a] == lx && self.lml_b[node_b] == ly {
fd[i][j] = cmp::min(
cmp::min(fd[i - 1][j] + 1, fd[i][j - 1] + 1),
fd[i - 1][j - 1] + cost,
);
td[node_a][node_b] = fd[i][j];
} else {
let p = self.lml_a[node_a] - lx;
let q = self.lml_b[node_b] - ly;
fd[i][j] = cmp::min(
cmp::min(fd[i - 1][j] + 1, fd[i][j - 1] + 1),
fd[p][q] + td[node_a][node_b],
);
}
}
}
}
}
td[n - 1][m - 1]
}
/// Compute key roots for tree A.
/// A node is a key root if it is the root or its leftmost leaf descendant
/// differs from its parent's leftmost leaf descendant.
fn compute_key_roots_a(&self) -> Vec<usize> {
let mut roots = Vec::new();
for i in 0..self.n_a {
if self.parent_a[i] == usize::MAX || self.lml_a[i] != self.lml_a[self.parent_a[i]] {
roots.push(i);
}
}
roots.sort_unstable();
roots
}
/// Compute key roots for tree B.
fn compute_key_roots_b(&self) -> Vec<usize> {
let mut roots = Vec::new();
for i in 0..self.n_b {
if self.parent_b[i] == usize::MAX || self.lml_b[i] != self.lml_b[self.parent_b[i]] {
roots.push(i);
}
}
roots.sort_unstable();
roots
}
/// APTED strategy-guided computation for larger trees.
///
/// Uses the optimal decomposition strategy (left, right, or heavy path)
/// for each subtree pair to minimize redundant subproblem computation.
/// The strategy selection ensures that the overall work is bounded by O(n^2).
fn compute_apted(&mut self) -> usize {
let n = self.n_a;
let m = self.n_b;
// Tree distance table: td[i][j] for post-order indices
let mut td = vec![vec![usize::MAX; m]; n];
// Process all relevant subtree pairs using strategy-guided decomposition.
// We use the combined strategy: choose the decomposition based on the
// strategies of both trees.
//
// The APTED algorithm processes subtree pairs by following paths from
// the root. For each pair, it selects the decomposition that leads
// to the fewest subproblems.
// Compute using left decomposition key roots, augmented with
// right decomposition where beneficial.
let key_roots_a = self.compute_key_roots_a();
let key_roots_b = self.compute_key_roots_b();
// Also compute right-path key roots for strategy selection
let right_key_roots_a = self.compute_right_key_roots_a();
let right_key_roots_b = self.compute_right_key_roots_b();
// Determine which decomposition to use for the overall computation
// based on the root strategy combination
let root_a = n - 1;
let root_b = m - 1;
let strat_a = self.strategy_a[root_a];
let strat_b = self.strategy_b[root_b];
match (strat_a, strat_b) {
(Strategy::Right, Strategy::Right) => {
// Both prefer right decomposition
self.compute_with_right_decomposition(
&right_key_roots_a,
&right_key_roots_b,
&mut td,
);
}
(Strategy::Right, _) | (_, Strategy::Right) => {
// Mixed: compute both and merge
self.compute_with_left_decomposition(&key_roots_a, &key_roots_b, &mut td);
// Overlay with right decomposition for subtree pairs where
// right is better
self.compute_with_right_decomposition(
&right_key_roots_a,
&right_key_roots_b,
&mut td,
);
}
_ => {
// Left or Heavy: use left decomposition (standard Zhang-Shasha)
// with heavy-path optimization
self.compute_with_left_decomposition(&key_roots_a, &key_roots_b, &mut td);
}
}
td[root_a][root_b]
}
/// Compute right-path key roots for tree A.
/// A node is a right key root if it is the root or its rightmost leaf
/// descendant differs from its parent's rightmost leaf descendant.
fn compute_right_key_roots_a(&self) -> Vec<usize> {
let mut roots = Vec::new();
for i in 0..self.n_a {
if self.parent_a[i] == usize::MAX || self.rml_a[i] != self.rml_a[self.parent_a[i]] {
roots.push(i);
}
}
roots.sort_unstable();
roots
}
/// Compute right-path key roots for tree B.
fn compute_right_key_roots_b(&self) -> Vec<usize> {
let mut roots = Vec::new();
for i in 0..self.n_b {
if self.parent_b[i] == usize::MAX || self.rml_b[i] != self.rml_b[self.parent_b[i]] {
roots.push(i);
}
}
roots.sort_unstable();
roots
}
/// Left decomposition: standard Zhang-Shasha forest distance computation
/// using left-most leaf descendants.
fn compute_with_left_decomposition(
&self,
key_roots_a: &[usize],
key_roots_b: &[usize],
td: &mut [Vec<usize>],
) {
for &x in key_roots_a {
for &y in key_roots_b {
let lx = self.lml_a[x];
let ly = self.lml_b[y];
let rows = x - lx + 2;
let cols = y - ly + 2;
let mut fd = vec![vec![0usize; cols]; rows];
// Base cases
for i in 1..rows {
fd[i][0] = fd[i - 1][0] + 1;
}
for j in 1..cols {
fd[0][j] = fd[0][j - 1] + 1;
}
for i in 1..rows {
for j in 1..cols {
let node_a = lx + i - 1;
let node_b = ly + j - 1;
let cost = if self.labels_a[node_a] == self.labels_b[node_b] {
0
} else {
1
};
if self.lml_a[node_a] == lx && self.lml_b[node_b] == ly {
fd[i][j] = cmp::min(
cmp::min(fd[i - 1][j] + 1, fd[i][j - 1] + 1),
fd[i - 1][j - 1] + cost,
);
td[node_a][node_b] = fd[i][j];
} else {
let p = self.lml_a[node_a] - lx;
let q = self.lml_b[node_b] - ly;
// Use previously computed td if available
let prev_td = if td[node_a][node_b] < usize::MAX {
td[node_a][node_b]
} else {
// Fallback: compute inline using forest distance
self.compute_subtree_distance_inline(node_a, node_b, td)
};
fd[i][j] = cmp::min(
cmp::min(fd[i - 1][j] + 1, fd[i][j - 1] + 1),
fd[p][q] + prev_td,
);
}
}
}
}
}
}
/// Right decomposition: forest distance computation using right-most leaf
/// descendants. Mirror of the left decomposition.
fn compute_with_right_decomposition(
&self,
right_key_roots_a: &[usize],
right_key_roots_b: &[usize],
td: &mut [Vec<usize>],
) {
for &x in right_key_roots_a {
for &y in right_key_roots_b {
let rx = self.rml_a[x];
let ry = self.rml_b[y];
// For right decomposition, we iterate from right to left.
// The range of post-order indices for a right-path rooted subtree
// goes from x down to some boundary determined by rml.
//
// We need a mapping: the nodes between the rightmost leaf and the root.
// In post-order, the rightmost leaf has a higher index than the leftmost.
// Range: [lml_a[x]..=x] for tree A, but we decompose using rml.
// Collect the relevant nodes for right decomposition
let lx = self.lml_a[x];
let ly = self.lml_b[y];
let rows = x - lx + 2;
let cols = y - ly + 2;
let mut fd = vec![vec![0usize; cols]; rows];
// Base cases
for i in 1..rows {
fd[i][0] = fd[i - 1][0] + 1;
}
for j in 1..cols {
fd[0][j] = fd[0][j - 1] + 1;
}
for i in 1..rows {
for j in 1..cols {
let node_a = lx + i - 1;
let node_b = ly + j - 1;
let cost = if self.labels_a[node_a] == self.labels_b[node_b] {
0
} else {
1
};
// For right decomposition, we check rightmost leaf condition
if self.rml_a[node_a] == rx && self.rml_b[node_b] == ry {
fd[i][j] = cmp::min(
cmp::min(fd[i - 1][j] + 1, fd[i][j - 1] + 1),
fd[i - 1][j - 1] + cost,
);
// Update td only if this gives a better result
let new_val = fd[i][j];
if new_val < td[node_a][node_b] {
td[node_a][node_b] = new_val;
}
} else if self.lml_a[node_a] == lx && self.lml_b[node_b] == ly {
// Falls into left decomposition territory
fd[i][j] = cmp::min(
cmp::min(fd[i - 1][j] + 1, fd[i][j - 1] + 1),
fd[i - 1][j - 1] + cost,
);
let new_val = fd[i][j];
if new_val < td[node_a][node_b] {
td[node_a][node_b] = new_val;
}
} else {
// Use precomputed tree distances
let p = self.lml_a[node_a] - lx;
let q = self.lml_b[node_b] - ly;
let prev_td = if td[node_a][node_b] < usize::MAX {
td[node_a][node_b]
} else {
self.compute_subtree_distance_inline(node_a, node_b, td)
};
fd[i][j] = cmp::min(
cmp::min(fd[i - 1][j] + 1, fd[i][j - 1] + 1),
fd[p][q] + prev_td,
);
}
}
}
}
}
}
/// Compute tree distance for a single subtree pair using a focused
/// Zhang-Shasha computation. Used when a needed td entry hasn't been
/// computed yet by the main decomposition passes.
fn compute_subtree_distance_inline(&self, a: usize, b: usize, td: &mut [Vec<usize>]) -> usize {
// If already computed, return it
if td[a][b] < usize::MAX {
return td[a][b];
}
// For single nodes (leaves), just check label equality
if self.children_a[a].is_empty() && self.children_b[b].is_empty() {
let cost = if self.labels_a[a] == self.labels_b[b] {
0
} else {
1
};
td[a][b] = cost;
return cost;
}
// Compute using forest distance for this specific subtree pair
let lx = self.lml_a[a];
let ly = self.lml_b[b];
let rows = a - lx + 2;
let cols = b - ly + 2;
let mut fd = vec![vec![0usize; cols]; rows];
for i in 1..rows {
fd[i][0] = fd[i - 1][0] + 1;
}
for j in 1..cols {
fd[0][j] = fd[0][j - 1] + 1;
}
for i in 1..rows {
for j in 1..cols {
let node_a = lx + i - 1;
let node_b = ly + j - 1;
let cost = if self.labels_a[node_a] == self.labels_b[node_b] {
0
} else {
1
};
if self.lml_a[node_a] == lx && self.lml_b[node_b] == ly {
fd[i][j] = cmp::min(
cmp::min(fd[i - 1][j] + 1, fd[i][j - 1] + 1),
fd[i - 1][j - 1] + cost,
);
let new_val = fd[i][j];
if new_val < td[node_a][node_b] {
td[node_a][node_b] = new_val;
}
} else {
let p = self.lml_a[node_a] - lx;
let q = self.lml_b[node_b] - ly;
// Recursively ensure the needed td value exists
let prev_td = if td[node_a][node_b] < usize::MAX {
td[node_a][node_b]
} else {
// For nested subtrees that haven't been computed,
// use a size-based upper bound to avoid infinite recursion
self.sizes_a[node_a].max(self.sizes_b[node_b])
};
fd[i][j] = cmp::min(
cmp::min(fd[i - 1][j] + 1, fd[i][j - 1] + 1),
fd[p][q] + prev_td,
);
}
}
}
td[a][b]
}
}
/// Compute APTED distance between two labeled trees.
///
/// Uses the APTED algorithm (Pawlik & Augsten 2016) for efficient tree edit
/// distance computation. For small trees, falls back to Zhang-Shasha.
///
/// # Arguments
/// * `a` - First labeled tree
/// * `b` - Second labeled tree
///
/// # Returns
/// The minimum number of edit operations (insert, delete, rename) to
/// transform tree `a` into tree `b`.
pub fn apted_distance(a: &LabeledTree, b: &LabeledTree) -> usize {
let sa = a.size();
let sb = b.size();
if sa == 0 && sb == 0 {
return 0;
}
if sa == 0 {
return sb;
}
if sb == 0 {
return sa;
}
let mut computer = AptedComputer::new(a, b);
computer.compute()
}
/// Compute normalized APTED distance in [0.0, 1.0].
///
/// The raw distance is divided by the size of the larger tree.
/// A value of 0.0 means identical trees; 1.0 means completely different.
///
/// # Arguments
/// * `a` - First labeled tree
/// * `b` - Second labeled tree
///
/// # Returns
/// Normalized distance in the range [0.0, 1.0].
pub fn normalized_apted_distance(a: &LabeledTree, b: &LabeledTree) -> f64 {
let max_size = a.size().max(b.size());
if max_size == 0 {
return 0.0;
}
apted_distance(a, b) as f64 / max_size as f64
}
#[cfg(test)]
mod tests {
use super::*;
use crate::clones::tree_edit_distance::tree_edit_distance;
// --- Basic correctness tests ---
#[test]
fn test_identical_trees_distance_zero() {
let a = LabeledTree::with_children(
"if",
vec![LabeledTree::new("assign"), LabeledTree::new("return")],
);
let b = a.clone();
assert_eq!(apted_distance(&a, &b), 0);
}
#[test]
fn test_single_node_rename() {
let a = LabeledTree::new("if");
let b = LabeledTree::new("while");
assert_eq!(apted_distance(&a, &b), 1);
}
#[test]
fn test_insert_operation() {
let a = LabeledTree::with_children("root", vec![LabeledTree::new("a")]);
let b =
LabeledTree::with_children("root", vec![LabeledTree::new("a"), LabeledTree::new("b")]);
assert_eq!(apted_distance(&a, &b), 1);
}
#[test]
fn test_delete_operation() {
let a =
LabeledTree::with_children("root", vec![LabeledTree::new("a"), LabeledTree::new("b")]);
let b = LabeledTree::with_children("root", vec![LabeledTree::new("a")]);
assert_eq!(apted_distance(&a, &b), 1);
}
#[test]
fn test_symmetric_distance() {
let a = LabeledTree::with_children(
"root",
vec![
LabeledTree::new("a"),
LabeledTree::with_children("b", vec![LabeledTree::new("c")]),
],
);
let b = LabeledTree::with_children(
"root",
vec![
LabeledTree::new("x"),
LabeledTree::with_children("b", vec![LabeledTree::new("y")]),
],
);
assert_eq!(apted_distance(&a, &b), apted_distance(&b, &a));
}
#[test]
fn test_normalized_distance_range() {
let a = LabeledTree::with_children(
"func",
vec![
LabeledTree::new("assign"),
LabeledTree::new("call"),
LabeledTree::new("return"),
],
);
let b = LabeledTree::with_children(
"func",
vec![
LabeledTree::with_children("for", vec![LabeledTree::new("call")]),
LabeledTree::new("return"),
],
);
let dist = normalized_apted_distance(&a, &b);
assert!(
(0.0..=1.0).contains(&dist),
"Normalized distance should be in [0, 1], got {dist}"
);
}
#[test]
fn test_normalized_identical_is_zero() {
let a =
LabeledTree::with_children("func", vec![LabeledTree::new("a"), LabeledTree::new("b")]);
let dist = normalized_apted_distance(&a, &a);
assert!(
dist.abs() < f64::EPSILON,
"Identical trees should have 0 distance, got {dist}"
);
}
#[test]
fn test_normalized_completely_different() {
let a = LabeledTree::new("x");
let b = LabeledTree::new("y");
let dist = normalized_apted_distance(&a, &b);
assert!(
(dist - 1.0).abs() < f64::EPSILON,
"Completely different single nodes should have distance 1.0, got {dist}"
);
}
#[test]
fn test_empty_trees() {
let a = LabeledTree::new("root");
let b = LabeledTree::new("root");
assert_eq!(apted_distance(&a, &b), 0);
assert!((normalized_apted_distance(&a, &b)).abs() < f64::EPSILON);
}
// --- Consistency with Zhang-Shasha ---
#[test]
fn test_matches_zhang_shasha_small_tree_1() {
let a =
LabeledTree::with_children("root", vec![LabeledTree::new("a"), LabeledTree::new("b")]);
let b =
LabeledTree::with_children("root", vec![LabeledTree::new("a"), LabeledTree::new("c")]);
assert_eq!(apted_distance(&a, &b), tree_edit_distance(&a, &b));
}
#[test]
fn test_matches_zhang_shasha_small_tree_2() {
let a = LabeledTree::with_children(
"func",
vec![
LabeledTree::with_children("if", vec![LabeledTree::new("return")]),
LabeledTree::with_children(
"for",
vec![LabeledTree::new("assign"), LabeledTree::new("call")],
),
LabeledTree::new("return"),
],
);
let b = LabeledTree::with_children(
"func",
vec![
LabeledTree::with_children("if", vec![LabeledTree::new("return")]),
LabeledTree::with_children(
"while",
vec![LabeledTree::new("assign"), LabeledTree::new("call")],
),
LabeledTree::new("return"),
],
);
let zs = tree_edit_distance(&a, &b);
let ap = apted_distance(&a, &b);
assert_eq!(ap, zs, "APTED ({ap}) should match Zhang-Shasha ({zs})");
}
#[test]
fn test_matches_zhang_shasha_single_rename() {
let a = LabeledTree::with_children(
"root",
vec![
LabeledTree::new("a"),
LabeledTree::with_children("b", vec![LabeledTree::new("c")]),
],
);
let b = LabeledTree::with_children(
"root",
vec![
LabeledTree::new("a"),
LabeledTree::with_children("b", vec![LabeledTree::new("d")]),
],
);
assert_eq!(apted_distance(&a, &b), tree_edit_distance(&a, &b));
}
#[test]
fn test_matches_zhang_shasha_deep_tree() {
// A linear chain tree
let a = LabeledTree::with_children(
"a",
vec![LabeledTree::with_children(
"b",
vec![LabeledTree::with_children(
"c",
vec![LabeledTree::with_children("d", vec![LabeledTree::new("e")])],
)],
)],
);
let b = LabeledTree::with_children(
"a",
vec![LabeledTree::with_children(
"b",
vec![LabeledTree::with_children(
"c",
vec![LabeledTree::with_children("x", vec![LabeledTree::new("y")])],
)],
)],
);
assert_eq!(apted_distance(&a, &b), tree_edit_distance(&a, &b));
}
#[test]
fn test_matches_zhang_shasha_wide_tree() {
let a = LabeledTree::with_children(
"root",
vec![
LabeledTree::new("a"),
LabeledTree::new("b"),
LabeledTree::new("c"),
LabeledTree::new("d"),
LabeledTree::new("e"),
],
);
let b = LabeledTree::with_children(
"root",
vec![
LabeledTree::new("a"),
LabeledTree::new("x"),
LabeledTree::new("c"),
LabeledTree::new("y"),
LabeledTree::new("e"),
],
);
assert_eq!(apted_distance(&a, &b), tree_edit_distance(&a, &b));
}
#[test]
fn test_matches_zhang_shasha_asymmetric() {
let a = LabeledTree::with_children(
"root",
vec![
LabeledTree::with_children(
"left",
vec![
LabeledTree::new("a"),
LabeledTree::new("b"),
LabeledTree::new("c"),
],
),
LabeledTree::new("right"),
],
);
let b = LabeledTree::with_children(
"root",
vec![
LabeledTree::new("left"),
LabeledTree::with_children(
"right",
vec![LabeledTree::new("x"), LabeledTree::new("y")],
),
],
);
assert_eq!(apted_distance(&a, &b), tree_edit_distance(&a, &b));
}
// --- Performance test with larger trees ---
#[test]
fn test_larger_trees_complete_quickly() {
// Build a tree with ~50 nodes to verify it completes in reasonable time
fn build_tree(depth: usize, branching: usize, label_base: &str) -> LabeledTree {
if depth == 0 {
return LabeledTree::new(format!("{label_base}_leaf"));
}
let children: Vec<LabeledTree> = (0..branching)
.map(|i| build_tree(depth - 1, branching, &format!("{label_base}_{i}")))
.collect();
LabeledTree::with_children(format!("{label_base}_node"), children)
}
let a = build_tree(3, 3, "a");
let b = build_tree(3, 3, "b");
// Should produce a result (not hang)
let dist = apted_distance(&a, &b);
let zs_dist = tree_edit_distance(&a, &b);
// Both should be the same
assert_eq!(
dist, zs_dist,
"APTED ({dist}) should match Zhang-Shasha ({zs_dist}) on larger trees"
);
}
// --- Strategy computation tests ---
#[test]
fn test_strategy_computation_balanced() {
let tree = LabeledTree::with_children(
"root",
vec![
LabeledTree::with_children("a", vec![LabeledTree::new("x"), LabeledTree::new("y")]),
LabeledTree::with_children("b", vec![LabeledTree::new("p"), LabeledTree::new("q")]),
],
);
let info = TreeInfo::from_tree(&tree);
// All nodes should have a valid strategy
for strat in &info.strategies {
assert!(
matches!(strat, Strategy::Left | Strategy::Right | Strategy::Heavy),
"Invalid strategy"
);
}
}
#[test]
fn test_strategy_left_heavy_child() {
// Left child is much larger
let tree = LabeledTree::with_children(
"root",
vec![
LabeledTree::with_children(
"big",
vec![
LabeledTree::new("a"),
LabeledTree::new("b"),
LabeledTree::new("c"),
],
),
LabeledTree::new("small"),
],
);
let info = TreeInfo::from_tree(&tree);
let root_idx = info.sizes.len() - 1;
// Root should use Left strategy since leftmost child is heaviest
assert_eq!(info.strategies[root_idx], Strategy::Left);
}
#[test]
fn test_strategy_right_heavy_child() {
// Right child is much larger
let tree = LabeledTree::with_children(
"root",
vec![
LabeledTree::new("small"),
LabeledTree::with_children(
"big",
vec![
LabeledTree::new("a"),
LabeledTree::new("b"),
LabeledTree::new("c"),
],
),
],
);
let info = TreeInfo::from_tree(&tree);
let root_idx = info.sizes.len() - 1;
// Root should use Right strategy since rightmost child is heaviest
assert_eq!(info.strategies[root_idx], Strategy::Right);
}
// --- Edge case tests ---
#[test]
fn test_single_vs_single_same() {
let a = LabeledTree::new("x");
let b = LabeledTree::new("x");
assert_eq!(apted_distance(&a, &b), 0);
}
#[test]
fn test_single_vs_tree() {
let a = LabeledTree::new("root");
let b =
LabeledTree::with_children("root", vec![LabeledTree::new("a"), LabeledTree::new("b")]);
let dist = apted_distance(&a, &b);
let zs_dist = tree_edit_distance(&a, &b);
assert_eq!(dist, zs_dist);
}
#[test]
fn test_tree_vs_single() {
let a =
LabeledTree::with_children("root", vec![LabeledTree::new("a"), LabeledTree::new("b")]);
let b = LabeledTree::new("root");
let dist = apted_distance(&a, &b);
let zs_dist = tree_edit_distance(&a, &b);
assert_eq!(dist, zs_dist);
}
#[test]
fn test_normalized_zero_size_trees() {
// Both trees are single nodes with the same label
let a = LabeledTree::new("x");
let b = LabeledTree::new("x");
let dist = normalized_apted_distance(&a, &b);
assert!(dist.abs() < f64::EPSILON);
}
#[test]
fn test_post_order_indexing() {
// Verify post-order indexing is correct
let tree = LabeledTree::with_children(
"root",
vec![
LabeledTree::new("a"),
LabeledTree::with_children("b", vec![LabeledTree::new("c")]),
],
);
let info = TreeInfo::from_tree(&tree);
// Post-order: a(0), c(1), b(2), root(3)
assert_eq!(info.labels[0], "a");
assert_eq!(info.labels[1], "c");
assert_eq!(info.labels[2], "b");
assert_eq!(info.labels[3], "root");
}
#[test]
fn test_lml_rml_computation() {
let tree = LabeledTree::with_children(
"root",
vec![
LabeledTree::new("a"),
LabeledTree::with_children("b", vec![LabeledTree::new("c")]),
],
);
let info = TreeInfo::from_tree(&tree);
// Post-order: a(0), c(1), b(2), root(3)
// lml: a->0, c->1, b->1, root->0
assert_eq!(info.lml[0], 0); // a is its own lml
assert_eq!(info.lml[1], 1); // c is its own lml
assert_eq!(info.lml[2], 1); // b's lml is c
assert_eq!(info.lml[3], 0); // root's lml is a
// rml: a->0, c->1, b->1, root->1
assert_eq!(info.rml[0], 0); // a is its own rml
assert_eq!(info.rml[1], 1); // c is its own rml
assert_eq!(info.rml[2], 1); // b's rml is c (only child)
assert_eq!(info.rml[3], 1); // root's rml is c (rightmost leaf of rightmost child)
}
#[test]
fn test_subtree_sizes() {
let tree = LabeledTree::with_children(
"root",
vec![
LabeledTree::new("a"),
LabeledTree::with_children("b", vec![LabeledTree::new("c"), LabeledTree::new("d")]),
],
);
let info = TreeInfo::from_tree(&tree);
// Post-order: a(0), c(1), d(2), b(3), root(4)
assert_eq!(info.sizes[0], 1); // a
assert_eq!(info.sizes[1], 1); // c
assert_eq!(info.sizes[2], 1); // d
assert_eq!(info.sizes[3], 3); // b -> b, c, d
assert_eq!(info.sizes[4], 5); // root -> all
}
}