-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathAPTED.java
More file actions
1820 lines (1721 loc) · 78 KB
/
APTED.java
File metadata and controls
1820 lines (1721 loc) · 78 KB
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
/* MIT License
*
* Copyright (c) 2017 Mateusz Pawlik
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package at.unisalzburg.apted.distance;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Stack;
import at.unisalzburg.apted.node.Node;
import at.unisalzburg.apted.node.NodeIndexer;
import at.unisalzburg.apted.costmodel.CostModel;
/**
* Implements APTED algorithm [1,2].
*
* <ul>
* <li>Optimal strategy with all paths.
* <li>Single-node single path function supports currently only unit cost.
* <li>Two-node single path function not included.
* <li>\Delta^L and \Delta^R based on Zhang and Shasha's algorithm for executing
* left and right paths (as in [3]). If only left and right paths are used
* in the strategy, the memory usage is reduced by one quadratic array.
* <li>For any other path \Delta^A from [1] is used.
* </ul>
*
* References:
* <ul>
* <li>[1] M. Pawlik and N. Augsten. Efficient Computation of the Tree Edit
* Distance. ACM Transactions on Database Systems (TODS) 40(1). 2015.
* <li>[2] M. Pawlik and N. Augsten. Tree edit distance: Robust and memory-
* efficient. Information Systems 56. 2016.
* <li>[3] M. Pawlik and N. Augsten. RTED: A Robust Algorithm for the Tree Edit
* Distance. PVLDB 5(4). 2011.
* </ul>
*
* @param <C> type of cost model.
* @param <D> type of node data.
*/
public class APTED<C extends CostModel, D> {
/**
* Identifier of left path type = {@value LEFT};
*/
private static final byte LEFT = 0;
/**
* Identifier of right path type = {@value RIGHT};
*/
private static final byte RIGHT = 1;
/**
* Identifier of inner path type = {@value INNER};
*/
private static final byte INNER = 2;
/**
* Indexer of the source tree.
*
* @see at.unisalzburg.apted.node.NodeIndexer
*/
private NodeIndexer it1;
/**
* Indexer of the destination tree.
*
* @see at.unisalzburg.apted.node.NodeIndexer
*/
private NodeIndexer it2;
/**
* The size of the source input tree.
*/
private int size1;
/**
* The size of the destination tree.
*/
private int size2;
/**
* The distance matrix [1, Sections 3.4,8.2,8.3]. Used to store intermediate
* distances between pairs of subtrees.
*/
private float delta[][];
/**
* One of distance arrays to store intermediate distances in spfA.
*/
// TODO: Verify if other spf-local arrays are initialised within spf. If yes,
// move q to spf to - then, an offset has to be used to access it.
private float q[];
/**
* Array used in the algorithm before [1]. Using it does not change the
* complexity.
*
* <p>TODO: Do not use it [1, Section 8.4].
*/
private int fn[];
/**
* Array used in the algorithm before [1]. Using it does not change the
* complexity.
*
* <p>TODO: Do not use it [1, Section 8.4].
*/
private int ft[];
/**
* Stores the number of subproblems encountered while computing the distance
* [1, Section 10].
*/
private long counter;
/**
* Cost model to be used for calculating costs of edit operations.
*/
private C costModel;
/**
* Constructs the APTED algorithm object with the specified cost model.
*
* @param costModel cost model for edit operations.
*/
public APTED(C costModel) {
this.costModel = costModel;
}
/**
* Compute tree edit distance between source and destination trees using
* APTED algorithm [1,2].
*
* @param t1 source tree.
* @param t2 destination tree.
* @return tree edit distance.
*/
public float computeEditDistance(Node<D> t1, Node<D> t2) {
// Index the nodes of both input trees.
init(t1, t2);
// Determine the optimal strategy for the distance computation.
// Use the heuristic from [2, Section 5.3].
if (it1.lchl < it1.rchl) {
delta = computeOptStrategy_postL(it1, it2);
} else {
delta = computeOptStrategy_postR(it1, it2);
}
// Initialise structures for distance computation.
tedInit();
// Compute the distance.
return gted(it1, it2);
}
/**
* This method is only for testing purspose. It computes TED with a fixed
* path type in the strategy to trigger execution of a specific single-path
* function.
*
* @param t1 source tree.
* @param t2 destination tree.
* @param spfType single-path function to trigger (LEFT or RIGHT).
* @return tree edit distance.
*/
public float computeEditDistance_spfTest(Node<D> t1, Node<D> t2, int spfType) {
// Index the nodes of both input trees.
init(t1, t2);
// Initialise delta array.
delta = new float[size1][size2];
// Fix a path type to trigger specific spf.
for (int i = 0; i < delta.length; i++) {
for (int j = 0; j < delta[i].length; j++) {
// Fix path type.
if (spfType == LEFT) {
delta[i][j] = it1.preL_to_lld(i) + 1;
} else if (spfType == RIGHT) {
delta[i][j] = it1.preL_to_rld(i) + 1;
}
}
}
// Initialise structures for distance computation.
tedInit();
// Compute the distance.
return gted(it1, it2);
}
/**
* Initialises node indexers and stores input tree sizes.
*
* @param t1 source input tree.
* @param t2 destination input tree.
*/
public void init(Node<D> t1, Node<D> t2) {
it1 = new NodeIndexer(t1, costModel);
it2 = new NodeIndexer(t2, costModel);
size1 = it1.getSize();
size2 = it2.getSize();
}
/**
* After the optimal strategy is computed, initialises distances of deleting
* and inserting subtrees without their root nodes.
*/
private void tedInit() {
// Reset the subproblems counter.
counter = 0L;
// Initialize arrays.
int maxSize = Math.max(size1, size2) + 1;
// TODO: Move q initialisation to spfA.
q = new float[maxSize];
// TODO: Do not use fn and ft arrays [1, Section 8.4].
fn = new int[maxSize + 1];
ft = new int[maxSize + 1];
// Compute subtree distances without the root nodes when one of subtrees
// is a single node.
int sizeX = -1;
int sizeY = -1;
int parentX = -1;
int parentY = -1;
// Loop over the nodes in reversed left-to-right preorder.
for(int x = 0; x < size1; x++) {
sizeX = it1.sizes[x];
parentX = it1.parents[x];
for(int y = 0; y < size2; y++) {
sizeY = it2.sizes[y];
parentY = it2.parents[y];
// Set values in delta based on the sums of deletion and insertion
// costs. Substract the costs for root nodes.
// In this method we don't have to verify the order of the input trees
// because it is equal to the original.
if (sizeX == 1 && sizeY == 1) {
delta[x][y] = 0.0f;
} else if (sizeX == 1) {
delta[x][y] = it2.preL_to_sumInsCost[y] - costModel.ins(it2.preL_to_node[y]); // USE COST MODEL.
} else if (sizeY == 1) {
delta[x][y] = it1.preL_to_sumDelCost[x] - costModel.del(it1.preL_to_node[x]); // USE COST MODEL.
}
}
}
}
/**
* Compute the optimal strategy using left-to-right postorder traversal of
* the nodes [2, Algorithm 1].
*
* @param it1 node indexer of the source input tree.
* @param it2 node indexer of the destination input tree.
* @return array with the optimal strategy.
*/
// TODO: Document the internals. Point to lines of the lagorithm.
public float[][] computeOptStrategy_postL(NodeIndexer it1, NodeIndexer it2) {
int size1 = it1.getSize();
int size2 = it2.getSize();
float strategy[][] = new float[size1][size2];
float cost1_L[][] = new float[size1][];
float cost1_R[][] = new float[size1][];
float cost1_I[][] = new float[size1][];
float cost2_L[] = new float[size2];
float cost2_R[] = new float[size2];
float cost2_I[] = new float[size2];
int cost2_path[] = new int[size2];
float leafRow[] = new float[size2];
int pathIDOffset = size1;
float minCost = 0x7fffffffffffffffL;
int strategyPath = -1;
int[] pre2size1 = it1.sizes;
int[] pre2size2 = it2.sizes;
int[] pre2descSum1 = it1.preL_to_desc_sum;
int[] pre2descSum2 = it2.preL_to_desc_sum;
int[] pre2krSum1 = it1.preL_to_kr_sum;
int[] pre2krSum2 = it2.preL_to_kr_sum;
int[] pre2revkrSum1 = it1.preL_to_rev_kr_sum;
int[] pre2revkrSum2 = it2.preL_to_rev_kr_sum;
int[] preL_to_preR_1 = it1.preL_to_preR;
int[] preL_to_preR_2 = it2.preL_to_preR;
int[] preR_to_preL_1 = it1.preR_to_preL;
int[] preR_to_preL_2 = it2.preR_to_preL;
int[] pre2parent1 = it1.parents;
int[] pre2parent2 = it2.parents;
boolean[] nodeType_L_1 = it1.nodeType_L;
boolean[] nodeType_L_2 = it2.nodeType_L;
boolean[] nodeType_R_1 = it1.nodeType_R;
boolean[] nodeType_R_2 = it2.nodeType_R;
int[] preL_to_postL_1 = it1.preL_to_postL;
int[] preL_to_postL_2 = it2.preL_to_postL;
int[] postL_to_preL_1 = it1.postL_to_preL;
int[] postL_to_preL_2 = it2.postL_to_preL;
int size_v, parent_v_preL, parent_w_preL, parent_w_postL = -1, size_w, parent_v_postL = -1;
int leftPath_v, rightPath_v;
float[] cost_Lpointer_v, cost_Rpointer_v, cost_Ipointer_v;
float[] strategypointer_v;
float[] cost_Lpointer_parent_v = null, cost_Rpointer_parent_v = null, cost_Ipointer_parent_v = null;
float[] strategypointer_parent_v = null;
int krSum_v, revkrSum_v, descSum_v;
boolean is_v_leaf;
int v_in_preL;
int w_in_preL;
Stack<float[]> rowsToReuse_L = new Stack<float[]>();
Stack<float[]> rowsToReuse_R = new Stack<float[]>();
Stack<float[]> rowsToReuse_I = new Stack<float[]>();
for(int v = 0; v < size1; v++) {
v_in_preL = postL_to_preL_1[v];
is_v_leaf = it1.isLeaf(v_in_preL);
parent_v_preL = pre2parent1[v_in_preL];
if (parent_v_preL != -1) {
parent_v_postL = preL_to_postL_1[parent_v_preL];
}
strategypointer_v = strategy[v_in_preL];
size_v = pre2size1[v_in_preL];
leftPath_v = -(preR_to_preL_1[preL_to_preR_1[v_in_preL] + size_v - 1] + 1);// this is the left path's ID which is the leftmost leaf node: l-r_preorder(r-l_preorder(v) + |Fv| - 1)
rightPath_v = v_in_preL + size_v - 1 + 1; // this is the right path's ID which is the rightmost leaf node: l-r_preorder(v) + |Fv| - 1
krSum_v = pre2krSum1[v_in_preL];
revkrSum_v = pre2revkrSum1[v_in_preL];
descSum_v = pre2descSum1[v_in_preL];
if(is_v_leaf) {
cost1_L[v] = leafRow;
cost1_R[v] = leafRow;
cost1_I[v] = leafRow;
for(int i = 0; i < size2; i++) {
strategypointer_v[postL_to_preL_2[i]] = v_in_preL;
}
}
cost_Lpointer_v = cost1_L[v];
cost_Rpointer_v = cost1_R[v];
cost_Ipointer_v = cost1_I[v];
if(parent_v_preL != -1 && cost1_L[parent_v_postL] == null) {
if (rowsToReuse_L.isEmpty()) {
cost1_L[parent_v_postL] = new float[size2];
cost1_R[parent_v_postL] = new float[size2];
cost1_I[parent_v_postL] = new float[size2];
} else {
cost1_L[parent_v_postL] = rowsToReuse_L.pop();
cost1_R[parent_v_postL] = rowsToReuse_R.pop();
cost1_I[parent_v_postL] = rowsToReuse_I.pop();
}
}
if (parent_v_preL != -1) {
cost_Lpointer_parent_v = cost1_L[parent_v_postL];
cost_Rpointer_parent_v = cost1_R[parent_v_postL];
cost_Ipointer_parent_v = cost1_I[parent_v_postL];
strategypointer_parent_v = strategy[parent_v_preL];
}
Arrays.fill(cost2_L, 0L);
Arrays.fill(cost2_R, 0L);
Arrays.fill(cost2_I, 0L);
Arrays.fill(cost2_path, 0);
for(int w = 0; w < size2; w++) {
w_in_preL = postL_to_preL_2[w];
parent_w_preL = pre2parent2[w_in_preL];
if (parent_w_preL != -1) {
parent_w_postL = preL_to_postL_2[parent_w_preL];
}
size_w = pre2size2[w_in_preL];
if (it2.isLeaf(w_in_preL)) {
cost2_L[w] = 0L;
cost2_R[w] = 0L;
cost2_I[w] = 0L;
cost2_path[w] = w_in_preL;
}
minCost = 0x7fffffffffffffffL;
strategyPath = -1;
float tmpCost = 0x7fffffffffffffffL;
if (size_v <= 1 || size_w <= 1) { // USE NEW SINGLE_PATH FUNCTIONS FOR SMALL SUBTREES
minCost = Math.max(size_v, size_w);
} else {
tmpCost = (float) size_v * (float) pre2krSum2[w_in_preL] + cost_Lpointer_v[w];
if (tmpCost < minCost) {
minCost = tmpCost;
strategyPath = leftPath_v;
}
tmpCost = (float) size_v * (float) pre2revkrSum2[w_in_preL] + cost_Rpointer_v[w];
if (tmpCost < minCost) {
minCost = tmpCost;
strategyPath = rightPath_v;
}
tmpCost = (float) size_v * (float) pre2descSum2[w_in_preL] + cost_Ipointer_v[w];
if (tmpCost < minCost) {
minCost = tmpCost;
strategyPath = (int)strategypointer_v[w_in_preL] + 1;
}
tmpCost = (float) size_w * (float) krSum_v + cost2_L[w];
if (tmpCost < minCost) {
minCost = tmpCost;
strategyPath = -(preR_to_preL_2[preL_to_preR_2[w_in_preL] + size_w - 1] + pathIDOffset + 1);
}
tmpCost = (float) size_w * (float) revkrSum_v + cost2_R[w];
if (tmpCost < minCost) {
minCost = tmpCost;
strategyPath = w_in_preL + size_w - 1 + pathIDOffset + 1;
}
tmpCost = (float) size_w * (float) descSum_v + cost2_I[w];
if (tmpCost < minCost) {
minCost = tmpCost;
strategyPath = cost2_path[w] + pathIDOffset + 1;
}
}
if (parent_v_preL != -1) {
cost_Rpointer_parent_v[w] += minCost;
tmpCost = -minCost + cost1_I[v][w];
if (tmpCost < cost1_I[parent_v_postL][w]) {
cost_Ipointer_parent_v[w] = tmpCost;
strategypointer_parent_v[w_in_preL] = strategypointer_v[w_in_preL];
}
if (nodeType_R_1[v_in_preL]) {
cost_Ipointer_parent_v[w] += cost_Rpointer_parent_v[w];
cost_Rpointer_parent_v[w] += cost_Rpointer_v[w] - minCost;
}
if (nodeType_L_1[v_in_preL]) {
cost_Lpointer_parent_v[w] += cost_Lpointer_v[w];
} else {
cost_Lpointer_parent_v[w] += minCost;
}
}
if (parent_w_preL != -1) {
cost2_R[parent_w_postL] += minCost;
tmpCost = -minCost + cost2_I[w];
if (tmpCost < cost2_I[parent_w_postL]) {
cost2_I[parent_w_postL] = tmpCost;
cost2_path[parent_w_postL] = cost2_path[w];
}
if (nodeType_R_2[w_in_preL]) {
cost2_I[parent_w_postL] += cost2_R[parent_w_postL];
cost2_R[parent_w_postL] += cost2_R[w] - minCost;
}
if (nodeType_L_2[w_in_preL]) {
cost2_L[parent_w_postL] += cost2_L[w];
} else {
cost2_L[parent_w_postL] += minCost;
}
}
strategypointer_v[w_in_preL] = strategyPath;
}
if (!it1.isLeaf(v_in_preL)) {
Arrays.fill(cost1_L[v], 0);
Arrays.fill(cost1_R[v], 0);
Arrays.fill(cost1_I[v], 0);
rowsToReuse_L.push(cost1_L[v]);
rowsToReuse_R.push(cost1_R[v]);
rowsToReuse_I.push(cost1_I[v]);
}
}
return strategy;
}
/**
* Compute the optimal strategy using right-to-left postorder traversal of
* the nodes [2, Algorithm 1].
*
* @param it1 node indexer of the source input tree.
* @param it2 node indexer of the destination input tree.
* @return array with the optimal strategy.
*/
// QUESTION: Is it possible to merge it with the other strategy computation?
// TODO: Document the internals. Point to lines of the lagorithm.
public float[][] computeOptStrategy_postR(NodeIndexer it1, NodeIndexer it2) {
int size1 = it1.getSize();
int size2 = it2.getSize();
float strategy[][] = new float[size1][size2];
float cost1_L[][] = new float[size1][];
float cost1_R[][] = new float[size1][];
float cost1_I[][] = new float[size1][];
float cost2_L[] = new float[size2];
float cost2_R[] = new float[size2];
float cost2_I[] = new float[size2];
int cost2_path[] = new int[size2];
float leafRow[] = new float[size2];
int pathIDOffset = size1;
float minCost = 0x7fffffffffffffffL;
int strategyPath = -1;
int[] pre2size1 = it1.sizes;
int[] pre2size2 = it2.sizes;
int[] pre2descSum1 = it1.preL_to_desc_sum;
int[] pre2descSum2 = it2.preL_to_desc_sum;
int[] pre2krSum1 = it1.preL_to_kr_sum;
int[] pre2krSum2 = it2.preL_to_kr_sum;
int[] pre2revkrSum1 = it1.preL_to_rev_kr_sum;
int[] pre2revkrSum2 = it2.preL_to_rev_kr_sum;
int[] preL_to_preR_1 = it1.preL_to_preR;
int[] preL_to_preR_2 = it2.preL_to_preR;
int[] preR_to_preL_1 = it1.preR_to_preL;
int[] preR_to_preL_2 = it2.preR_to_preL;
int[] pre2parent1 = it1.parents;
int[] pre2parent2 = it2.parents;
boolean[] nodeType_L_1 = it1.nodeType_L;
boolean[] nodeType_L_2 = it2.nodeType_L;
boolean[] nodeType_R_1 = it1.nodeType_R;
boolean[] nodeType_R_2 = it2.nodeType_R;
int size_v, parent_v, parent_w, size_w;
int leftPath_v, rightPath_v;
float[] cost_Lpointer_v, cost_Rpointer_v, cost_Ipointer_v;
float[] strategypointer_v;
float[] cost_Lpointer_parent_v = null, cost_Rpointer_parent_v = null, cost_Ipointer_parent_v = null;
float[] strategypointer_parent_v = null;
int krSum_v, revkrSum_v, descSum_v;
boolean is_v_leaf;
Stack<float[]> rowsToReuse_L = new Stack<float[]>();
Stack<float[]> rowsToReuse_R = new Stack<float[]>();
Stack<float[]> rowsToReuse_I = new Stack<float[]>();
for(int v = size1 - 1; v >= 0; v--) {
is_v_leaf = it1.isLeaf(v);
parent_v = pre2parent1[v];
strategypointer_v = strategy[v];
size_v = pre2size1[v];
leftPath_v = -(preR_to_preL_1[preL_to_preR_1[v] + pre2size1[v] - 1] + 1);// this is the left path's ID which is the leftmost leaf node: l-r_preorder(r-l_preorder(v) + |Fv| - 1)
rightPath_v = v + pre2size1[v] - 1 + 1; // this is the right path's ID which is the rightmost leaf node: l-r_preorder(v) + |Fv| - 1
krSum_v = pre2krSum1[v];
revkrSum_v = pre2revkrSum1[v];
descSum_v = pre2descSum1[v];
if (is_v_leaf) {
cost1_L[v] = leafRow;
cost1_R[v] = leafRow;
cost1_I[v] = leafRow;
for (int i = 0; i < size2; i++) {
strategypointer_v[i] = v;
}
}
cost_Lpointer_v = cost1_L[v];
cost_Rpointer_v = cost1_R[v];
cost_Ipointer_v = cost1_I[v];
if (parent_v != -1 && cost1_L[parent_v] == null) {
if (rowsToReuse_L.isEmpty()) {
cost1_L[parent_v] = new float[size2];
cost1_R[parent_v] = new float[size2];
cost1_I[parent_v] = new float[size2];
} else {
cost1_L[parent_v] = rowsToReuse_L.pop();
cost1_R[parent_v] = rowsToReuse_R.pop();
cost1_I[parent_v] = rowsToReuse_I.pop();
}
}
if (parent_v != -1) {
cost_Lpointer_parent_v = cost1_L[parent_v];
cost_Rpointer_parent_v = cost1_R[parent_v];
cost_Ipointer_parent_v = cost1_I[parent_v];
strategypointer_parent_v = strategy[parent_v];
}
Arrays.fill(cost2_L, 0L);
Arrays.fill(cost2_R, 0L);
Arrays.fill(cost2_I, 0L);
Arrays.fill(cost2_path, 0);
for (int w = size2 - 1; w >= 0; w--) {
size_w = pre2size2[w];
if (it2.isLeaf(w)) {
cost2_L[w] = 0L;
cost2_R[w] = 0L;
cost2_I[w] = 0L;
cost2_path[w] = w;
}
minCost = 0x7fffffffffffffffL;
strategyPath = -1;
float tmpCost = 0x7fffffffffffffffL;
if (size_v <= 1 || size_w <= 1) { // USE NEW SINGLE_PATH FUNCTIONS FOR SMALL SUBTREES
minCost = Math.max(size_v, size_w);
} else {
tmpCost = (float) size_v * (float) pre2krSum2[w] + cost_Lpointer_v[w];
if (tmpCost < minCost) {
minCost = tmpCost;
strategyPath = leftPath_v;
}
tmpCost = (float) size_v * (float) pre2revkrSum2[w] + cost_Rpointer_v[w];
if (tmpCost < minCost){
minCost = tmpCost;
strategyPath = rightPath_v;
}
tmpCost = (float) size_v * (float) pre2descSum2[w] + cost_Ipointer_v[w];
if (tmpCost < minCost) {
minCost = tmpCost;
strategyPath = (int)strategypointer_v[w] + 1;
}
tmpCost = (float) size_w * (float) krSum_v + cost2_L[w];
if (tmpCost < minCost) {
minCost = tmpCost;
strategyPath = -(preR_to_preL_2[preL_to_preR_2[w] + size_w - 1] + pathIDOffset + 1);
}
tmpCost = (float) size_w * (float) revkrSum_v + cost2_R[w];
if (tmpCost < minCost) {
minCost = tmpCost;
strategyPath = w + size_w - 1 + pathIDOffset + 1;
}
tmpCost = (float) size_w * (float) descSum_v + cost2_I[w];
if (tmpCost < minCost) {
minCost = tmpCost;
strategyPath = cost2_path[w] + pathIDOffset + 1;
}
}
if (parent_v != -1) {
cost_Lpointer_parent_v[w] += minCost;
tmpCost = -minCost + cost1_I[v][w];
if (tmpCost < cost1_I[parent_v][w]) {
cost_Ipointer_parent_v[w] = tmpCost;
strategypointer_parent_v[w] = strategypointer_v[w];
}
if (nodeType_L_1[v]) {
cost_Ipointer_parent_v[w] += cost_Lpointer_parent_v[w];
cost_Lpointer_parent_v[w] += cost_Lpointer_v[w] - minCost;
}
if (nodeType_R_1[v]) {
cost_Rpointer_parent_v[w] += cost_Rpointer_v[w];
} else {
cost_Rpointer_parent_v[w] += minCost;
}
}
parent_w = pre2parent2[w];
if (parent_w != -1) {
cost2_L[parent_w] += minCost;
tmpCost = -minCost + cost2_I[w];
if (tmpCost < cost2_I[parent_w]) {
cost2_I[parent_w] = tmpCost;
cost2_path[parent_w] = cost2_path[w];
}
if (nodeType_L_2[w]) {
cost2_I[parent_w] += cost2_L[parent_w];
cost2_L[parent_w] += cost2_L[w] - minCost;
}
if (nodeType_R_2[w]) {
cost2_R[parent_w] += cost2_R[w];
} else {
cost2_R[parent_w] += minCost;
}
}
strategypointer_v[w] = strategyPath;
}
if (!it1.isLeaf(v)) {
Arrays.fill(cost1_L[v], 0);
Arrays.fill(cost1_R[v], 0);
Arrays.fill(cost1_I[v], 0);
rowsToReuse_L.push(cost1_L[v]);
rowsToReuse_R.push(cost1_R[v]);
rowsToReuse_I.push(cost1_I[v]);
}
}
return strategy;
}
/**
* Implements spf1 single path function for the case when one of the subtrees
* is a single node [2, Section 6.1, Algorithm 2].
*
* <p>We allow an arbitrary cost model which in principle may allow renames to
* have a lower cost than the respective deletion plus insertion. Thus,
* Formula 4 in [2] has to be modified to account for that case.
*
* <p>In this method we don't have to verify if input subtrees have been
* swapped because they're always passed in the original input order.
*
* @param ni1 node indexer for the source input subtree.
* @param ni2 node indexer for the destination input subtree.
* @param subtreeRootNode1 root node of a subtree in the source input tree.
* @param subtreeRootNode2 root node of a subtree in the destination input tree.
* @return the tree edit distance between two subtrees of the source and destination input subtrees.
*/
// TODO: Merge the initialisation loop in tedInit with this method.
// Currently, spf1 doesn't have to store distances in delta, because
// all of them have been stored in tedInit.
private float spf1 (NodeIndexer ni1, int subtreeRootNode1, NodeIndexer ni2, int subtreeRootNode2) {
int subtreeSize1 = ni1.sizes[subtreeRootNode1];
int subtreeSize2 = ni2.sizes[subtreeRootNode2];
if (subtreeSize1 == 1 && subtreeSize2 == 1) {
Node<D> n1 = ni1.preL_to_node[subtreeRootNode1];
Node<D> n2 = ni2.preL_to_node[subtreeRootNode2];
float maxCost = costModel.del(n1) + costModel.ins(n2);
float renCost = costModel.ren(n1, n2);
return renCost < maxCost ? renCost : maxCost;
}
if (subtreeSize1 == 1) {
Node<D> n1 = ni1.preL_to_node[subtreeRootNode1];
Node<D> n2 = null;
float cost = ni2.preL_to_sumInsCost[subtreeRootNode2];
float maxCost = cost + costModel.del(n1);
float minRenMinusIns = cost;
float nodeRenMinusIns = 0;
for (int i = subtreeRootNode2; i < subtreeRootNode2 + subtreeSize2; i++) {
n2 = ni2.preL_to_node[i];
nodeRenMinusIns = costModel.ren(n1, n2) - costModel.ins(n2);
if (nodeRenMinusIns < minRenMinusIns) {
minRenMinusIns = nodeRenMinusIns;
}
}
cost += minRenMinusIns;
return cost < maxCost ? cost : maxCost;
}
if (subtreeSize2 == 1) {
Node<D> n1 = null;
Node<D> n2 = ni2.preL_to_node[subtreeRootNode2];
float cost = ni1.preL_to_sumDelCost[subtreeRootNode1];
float maxCost = cost + costModel.ins(n2);
float minRenMinusDel = cost;
float nodeRenMinusDel = 0;
for (int i = subtreeRootNode1; i < subtreeRootNode1 + subtreeSize1; i++) {
n1 = ni1.preL_to_node[i];
nodeRenMinusDel = costModel.ren(n1, n2) - costModel.del(n1);
if (nodeRenMinusDel < minRenMinusDel) {
minRenMinusDel = nodeRenMinusDel;
}
}
cost += minRenMinusDel;
return cost < maxCost ? cost : maxCost;
}
return -1;
}
/**
* Implements GTED algorithm [1, Section 3.4].
*
* @param it1 node indexer for the source input tree.
* @param it2 node indexer for the destination input tree.
* @return the tree edit distance between the source and destination trees.
*/
// TODO: Document the internals. Point to lines of the algorithm.
private float gted(NodeIndexer it1, NodeIndexer it2) {
int currentSubtree1 = it1.getCurrentNode();
int currentSubtree2 = it2.getCurrentNode();
int subtreeSize1 = it1.sizes[currentSubtree1];
int subtreeSize2 = it2.sizes[currentSubtree2];
// Use spf1.
if ((subtreeSize1 == 1 || subtreeSize2 == 1)) {
return spf1(it1, currentSubtree1, it2, currentSubtree2);
}
int strategyPathID = (int)delta[currentSubtree1][currentSubtree2];
byte strategyPathType = -1;
int currentPathNode = Math.abs(strategyPathID) - 1;
int pathIDOffset = it1.getSize();
int parent = -1;
if(currentPathNode < pathIDOffset) {
strategyPathType = getStrategyPathType(strategyPathID, pathIDOffset, it1, currentSubtree1, subtreeSize1);
while((parent = it1.parents[currentPathNode]) >= currentSubtree1) {
int ai[];
int k = (ai = it1.children[parent]).length;
for(int i = 0; i < k; i++) {
int child = ai[i];
if(child != currentPathNode) {
it1.setCurrentNode(child);
gted(it1, it2);
}
}
currentPathNode = parent;
}
// TODO: Move this property away from node indexer and pass directly to spfs.
it1.setCurrentNode(currentSubtree1);
// Pass to spfs a boolean that says says if the order of input subtrees
// has been swapped compared to the order of the initial input trees.
// Used for accessing delta array and deciding on the edit operation
// [1, Section 3.4].
if (strategyPathType == 0) {
return spfL(it1, it2, false);
}
if (strategyPathType == 1) {
return spfR(it1, it2, false);
}
return spfA(it1, it2, Math.abs(strategyPathID) - 1, strategyPathType, false);
}
currentPathNode -= pathIDOffset;
strategyPathType = getStrategyPathType(strategyPathID, pathIDOffset, it2, currentSubtree2, subtreeSize2);
while((parent = it2.parents[currentPathNode]) >= currentSubtree2) {
int ai1[];
int l = (ai1 = it2.children[parent]).length;
for(int j = 0; j < l; j++) {
int child = ai1[j];
if(child != currentPathNode) {
it2.setCurrentNode(child);
gted(it1, it2);
}
}
currentPathNode = parent;
}
// TODO: Move this property away from node indexer and pass directly to spfs.
it2.setCurrentNode(currentSubtree2);
// Pass to spfs a boolean that says says if the order of input subtrees
// has been swapped compared to the order of the initial input trees. Used
// for accessing delta array and deciding on the edit operation
// [1, Section 3.4].
if (strategyPathType == 0) {
return spfL(it2, it1, true);
}
if (strategyPathType == 1) {
return spfR(it2, it1, true);
}
return spfA(it2, it1, Math.abs(strategyPathID) - pathIDOffset - 1, strategyPathType, true);
}
/**
* Implements the single-path function spfA. Here, we use it strictly for
* inner paths (spfL and spfR have better performance for leaft and right
* paths, respectively) [1, Sections 7 and 8]. However, in this stage it
* also executes correctly for left and right paths.
*
* @param it1 node indexer of the left-hand input subtree.
* @param it2 node indexer of the right-hand input subtree.
* @param pathID the left-to-right preorder id of the strategy path's leaf node.
* @param pathType type of the strategy path (LEFT, RIGHT, INNER).
* @param treesSwapped says if the order of input subtrees has been swapped
* compared to the order of the initial input trees. Used
* for accessing delta array and deciding on the edit
* operation.
* @return tree edit distance between left-hand and right-hand input subtrees.
*/
// TODO: Document the internals. Point to lines of the algorithm.
// The implementation has been micro-tuned: variables initialised once,
// pointers to arrays precomputed and fixed for entire lower-level loops,
// parts of lower-level loops that don't change moved to upper-level loops.
private float spfA(NodeIndexer it1, NodeIndexer it2, int pathID, byte pathType, boolean treesSwapped) {
Node<D>[] it2nodes = it2.preL_to_node;
Node<D> lFNode;
int[] it1sizes = it1.sizes;
int[] it2sizes = it2.sizes;
int[] it1parents = it1.parents;
int[] it2parents = it2.parents;
int[] it1preL_to_preR = it1.preL_to_preR;
int[] it2preL_to_preR = it2.preL_to_preR;
int[] it1preR_to_preL = it1.preR_to_preL;
int[] it2preR_to_preL = it2.preR_to_preL;
int currentSubtreePreL1 = it1.getCurrentNode();
int currentSubtreePreL2 = it2.getCurrentNode();
// Variables to incrementally sum up the forest sizes.
int currentForestSize1 = 0;
int currentForestSize2 = 0;
int tmpForestSize1 = 0;
// Variables to incrementally sum up the forest cost.
float currentForestCost1 = 0;
float currentForestCost2 = 0;
float tmpForestCost1 = 0;
int subtreeSize2 = it2.sizes[currentSubtreePreL2];
int subtreeSize1 = it1.sizes[currentSubtreePreL1];
float[][] t = new float[subtreeSize2+1][subtreeSize2+1];
float[][] s = new float[subtreeSize1+1][subtreeSize2+1];
float minCost = -1;
// sp1, sp2 and sp3 correspond to three elements of the minimum in the
// recursive formula [1, Figure 12].
float sp1 = 0;
float sp2 = 0;
float sp3 = 0;
int startPathNode = -1;
int endPathNode = pathID;
int it1PreLoff = endPathNode;
int it2PreLoff = currentSubtreePreL2;
int it1PreRoff = it1preL_to_preR[endPathNode];
int it2PreRoff = it2preL_to_preR[it2PreLoff];
// variable declarations which were inside the loops
int rFlast,lFlast,endPathNode_in_preR,startPathNode_in_preR,parent_of_endPathNode,parent_of_endPathNode_in_preR,
lFfirst,rFfirst,rGlast,rGfirst,lGfirst,rG_in_preL,rGminus1_in_preL,parent_of_rG_in_preL,lGlast,lF_in_preR,lFSubtreeSize,
lGminus1_in_preR,parent_of_lG,parent_of_lG_in_preR,rF_in_preL,rFSubtreeSize,
rGfirst_in_preL;
boolean leftPart,rightPart,fForestIsTree,lFIsConsecutiveNodeOfCurrentPathNode,lFIsLeftSiblingOfCurrentPathNode,
rFIsConsecutiveNodeOfCurrentPathNode,rFIsRightSiblingOfCurrentPathNode;
float[] sp1spointer,sp2spointer,sp3spointer,sp3deltapointer,swritepointer,sp1tpointer,sp3tpointer;
// These variables store the id of the source (which array) of looking up
// elements of the minimum in the recursive formula [1, Figures 12,13].
byte sp1source,sp3source;
// Loop A [1, Algorithm 3] - walk up the path.
while (endPathNode >= currentSubtreePreL1) {
it1PreLoff = endPathNode;
it1PreRoff = it1preL_to_preR[endPathNode];
rFlast = -1;
lFlast = -1;
endPathNode_in_preR = it1preL_to_preR[endPathNode];
startPathNode_in_preR = startPathNode == -1 ? 0x7fffffff : it1preL_to_preR[startPathNode];
parent_of_endPathNode = it1parents[endPathNode];
parent_of_endPathNode_in_preR = parent_of_endPathNode == -1 ? 0x7fffffff : it1preL_to_preR[parent_of_endPathNode];
if (startPathNode - endPathNode > 1) {
leftPart = true;
} else {
leftPart = false;
}
if (startPathNode >= 0 && startPathNode_in_preR - endPathNode_in_preR > 1) {
rightPart = true;
} else {
rightPart = false;
}
// Deal with nodes to the left of the path.
if (pathType == 1 || pathType == 2 && leftPart) {
if (startPathNode == -1) {
rFfirst = endPathNode_in_preR;
lFfirst = endPathNode;
} else {
rFfirst = startPathNode_in_preR;
lFfirst = startPathNode - 1;
}
if (!rightPart) {
rFlast = endPathNode_in_preR;
}
rGlast = it2preL_to_preR[currentSubtreePreL2];
rGfirst = (rGlast + subtreeSize2) - 1;
lFlast = rightPart ? endPathNode + 1 : endPathNode;
fn[fn.length - 1] = -1;
for (int i = currentSubtreePreL2; i < currentSubtreePreL2 + subtreeSize2; i++) {
fn[i] = -1;
ft[i] = -1;
}
// Store the current size and cost of forest in F.
tmpForestSize1 = currentForestSize1;
tmpForestCost1 = currentForestCost1;
// Loop B [1, Algoritm 3] - for all nodes in G (right-hand input tree).
for (int rG = rGfirst; rG >= rGlast; rG--) {
lGfirst = it2preR_to_preL[rG];
rG_in_preL = it2preR_to_preL[rG];
rGminus1_in_preL = rG <= it2preL_to_preR[currentSubtreePreL2] ? 0x7fffffff : it2preR_to_preL[rG - 1];
parent_of_rG_in_preL = it2parents[rG_in_preL];
// This if statement decides on the last lG node for Loop D [1, Algorithm 3];
if (pathType == 1){
if (lGfirst == currentSubtreePreL2 || rGminus1_in_preL != parent_of_rG_in_preL) {
lGlast = lGfirst;
} else {
lGlast = it2parents[lGfirst]+1;
}
} else {
lGlast = lGfirst == currentSubtreePreL2 ? lGfirst : currentSubtreePreL2+1;
}
updateFnArray(it2.preL_to_ln[lGfirst], lGfirst, currentSubtreePreL2);
updateFtArray(it2.preL_to_ln[lGfirst], lGfirst);
int rF = rFfirst;
// Reset size and cost of the forest in F.
currentForestSize1 = tmpForestSize1;
currentForestCost1 = tmpForestCost1;
// Loop C [1, Algorithm 3] - for all nodes to the left of the path node.
for (int lF = lFfirst; lF >= lFlast; lF--) {
// This if statement fixes rF node.
if (lF == lFlast && !rightPart) {
rF = rFlast;
}
lFNode = it1.preL_to_node[lF];
// Increment size and cost of F forest by node lF.
currentForestSize1++;
currentForestCost1 += (treesSwapped ? costModel.ins(lFNode) : costModel.del(lFNode)); // USE COST MODEL - sum up deletion cost of a forest.
// Reset size and cost of forest in G to subtree G_lGfirst.
currentForestSize2 = it2sizes[lGfirst];
currentForestCost2 = (treesSwapped ? it2.preL_to_sumDelCost[lGfirst] : it2.preL_to_sumInsCost[lGfirst]); // USE COST MODEL - reset to subtree insertion cost.
lF_in_preR = it1preL_to_preR[lF];
fForestIsTree = lF_in_preR == rF;
lFSubtreeSize = it1sizes[lF];
lFIsConsecutiveNodeOfCurrentPathNode = startPathNode - lF == 1;
lFIsLeftSiblingOfCurrentPathNode = lF + lFSubtreeSize == startPathNode;
sp1spointer = s[(lF + 1) - it1PreLoff];
sp2spointer = s[lF - it1PreLoff];
sp3spointer = s[0];
sp3deltapointer = treesSwapped ? null : delta[lF];