-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tex
1609 lines (1425 loc) · 95.5 KB
/
main.tex
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
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{microtype}
\usepackage{framed}
\usepackage{todonotes}
\usepackage{wrapfig}
\usepackage{graphicx}
\usepackage{epstopdf}
\usepackage{amssymb,amsmath}
\usepackage{array}
\usepackage{xspace,enumerate}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{thm-restate}
\usepackage{verbatim}
\usepackage{mathtools}
\usepackage{sidecap}
\usepackage{svg}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage[font=small,labelfont=bf,skip=1pt]{caption}
\usepackage[ruled, noline, noend, linesnumbered,]{algorithm2e}
%\usepackage{hyperref}
%\usepackage{cleveref}
\usepackage{subcaption}
\usepackage{algpseudocode}
\usepackage[margin=1.65in]{geometry}
\usepackage{pgfplots}
\usepackage{listings}
\usepackage{nicefrac}
\lstset{
basicstyle=\ttfamily,
mathescape
}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{trees,automata,positioning}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{decorations.pathmorphing,shapes}
\usetikzlibrary{calc,decorations.pathmorphing,shapes}
\usetikzlibrary{arrows}
\tikzset{
every node/.style={
align=center,
draw=none,
},
-latex,
}
\usepackage[colorlinks]{hyperref}
\usepackage[capitalise]{cleveref}
\newcommand{\cS}{\mathcal{S}}
\newcommand{\cG}{\mathcal{G}}
\newcommand{\cF}{\mathcal{F}}
\newcommand{\cX}{\mathcal{X}}
\newcommand{\cP}{\mathcal{P}}
\newcommand{\cM}{\mathcal{M}}
\newcommand{\cI}{\mathcal{I}}
\newcommand{\cL}{\mathcal{L}}
\newcommand{\cO}{\mathcal{O}}
\newcommand{\cT}{\mathcal{T}}
\newcommand{\cC}{\mathcal{C}}
\newcommand{\Tone}{\ensuremath{T_1}}
\newcommand{\Ttwo}{\ensuremath{T_2}}
\newcommand{\Fone}{F_1}
\newcommand{\Ftwo}{F_2}
\newcommand{\ctO}{\tilde{\mathcal{O}}}
\newcommand{\alg}{\textsc{ALG}}
\newcommand{\opt}{\textsc{OPT}}
\newcommand{\nodelabel}{\ell}
\newcommand{\conserved}{\mathsf{conserved}}
\newcommand{\children}{\mathsf{children}}
\newcommand{\head}{\mathsf{head}}
\newcommand{\level}{\mathsf{level}}
\newcommand{\access}{\mathsf{access}}
\newcommand{\mode}{\mathsf{mode}}
\newcommand{\freq}{\mathsf{freq}}
\newcommand{\rep}{\mathsf{rep}}
\newcommand{\dg}{\mathsf{deg}}
\newcommand{\edge}[3]{\ensuremath{#1 | #2 \rightarrow #3}}
\newtheorem{problem2}{Problem}{\bfseries}{\itshape}
\newtheorem{example2}{Example}{\itshape}{\rmfamily}
\newtheorem{example}{Example}
\newtheorem{lemma}{Lemma}
\newtheorem{theorem}{Theorem}
\newtheorem{definition}{Definition}
\newtheorem{fact}[theorem]{Fact}
\newtheorem{observation}[theorem]{Observation}
\SetKwInput{Algorithm}{Algorithm}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{conjecture}{Conjecture}
\crefname{hypothesis}{Hypothesis}{Hypotheses}
\newtheorem{problem}{Problem}
\newtheorem{remark}{Remark}
\newcommand{\notaestesa}[2]{%
\marginpar{\color{red!75!black}\textbf{\texttimes}}%
{\color{red!75!black}%
[\,\textbullet\,\textsf{\textbf{#1:}} %
\textsf{\footnotesize#2}\,\textbullet\,]}%
}
\newcommand{\gdv}[1]{\notaestesa{GDV}{#1}}
\newcommand{\murray}[1]{\notaestesa{Murray}{#1}}
\newcommand{\giulia}[1]{\notaestesa{Giulia}{#1}}
\title{Rearrangement Distances}
\author{giuliabernardini.morg }
\date{November 2020}
\begin{document}
\maketitle
\section{Introduction}\label{sec:trees_motiv}
Phylogenetic trees represent a plausible evolutionary relationship between the most disparate objects: natural languages in linguistics~\cite{gray2009language,walker2012cultural,nakhleh2005comparison}, ancient manuscripts in archaeology~\cite{buneman1971recovery}, genes and species in biology~\cite{huber2006phylogenetic,huson2006application}.
The leaves of such trees are labelled by the entities they represent, while the internal nodes are unlabelled and stand for unknown or extinct items.
A great wealth of methods to infer phylogenies have been developed over the decades~\cite{felsenstein2004inferring,steel2016phylogeny}, together with various techniques to compare the output of different algorithms, e.g., by building a consensus tree that captures the similarity between a set of conflicting trees~\cite{bryant2003classification,jansson2016improved,jansson2016algorithms,DBLP:conf/icalp/GawrychowskiLSW18} or by defining a metric between two trees~\cite{dobson1975comparing,brodal2013efficient,estabrook1985comparison,dudek2019computing,robinson1979comparison,robinson1981comparison}.
Fully-labelled trees, in opposition to classical phylogenies, may model an evolutionary history where the internal nodes, just like the leaves, correspond to extant entities.
An important phenomenon that fits this model well is cancer progressions~\cite{hajirasouliha2014combinatorial,nowell1976clonal}.
With the increasing amount of data and algorithms becoming available for inferring cancer evolution~\cite{malikic2019phiscs,jiao2014inferring,yuan2015bitphylogeny,bonizzoni2018does,bonizzoni2017beyond}, there is a pressing need of methods to provide a meaningful comparison among the trees produced by different approaches.
Besides the well-studied edit distance for fully-labelled trees~\cite{tai1979tree,zhang1989simple,pawlik2015efficient,mcvicar2016sumoted}, a few recent papers proposed ad-hoc metrics for tumor phylogenies~\cite{Karpov2019,govek2018consensus,DiNardo2019,Bioinfo2020}.
Taking inspiration from the existing literature~\cite{dasgupta1997distances, bordewich2005computational, allen2001subtree,steel2016phylogeny} on phylogeny rearrangement, the study of an operational notion of distance for
rearranging a fully-labelled tree is of great interest, and there are still many unexplored questions to be answered.
In this work, we open the investigation of some notions of the
rearrangement distance for two rooted trees which are fully labelled
by the same set of labels.
Building upon the existing
literature~\cite{semple2003phylogenetics,steel2016phylogeny} on
phylogenetic trees rearrangement, we propose some operations for
rearranging a fully-labelled tree.
The distance between a pair of
trees is then the shortest sequence of these operations that
transforms the first tree into the second tree.
We consider rooted trees on $n$ nodes labelled with distinct labels from $[n]=\{1,2,\ldots,n\}$, and identify nodes with their labels.
The first operation
we introduce is an adaptation of the SPR
operation~\cite{bordewich2005computational} to a fully-labelled tree. We call such an operation \emph{link-and-cut} operation, while the second operation
is a \emph{permutation} of some labels of the tree (notice that such an operation does change the topology of the tree).
When only one kind of operations is allowed, we have the link-and-cut distance and the permutation distance. When both operations are possible, then we have the rearrangement distance.
For computing the permutation distance, in Section~\ref{sec:permutation} we connect the complexity to that of calculating the largest cardinality matching in a sparse bipartite graph. By designing two-way reductions we show that these problems are equivalent, up to polylogarithmic factors.
Due to the recent progress in the area of fine-grained complexity we now know, for many problems that can be solved in polynomial
time, what is essentially the best possible exponent in the running time, conditioned on some plausible but yet unproven
hypothesis~\cite{Williams18}. For max-flow, and more specifically maximum matching, this is not the case yet, although
we do have some understanding of the complexity of the related problem of computing the max-flow between all pairs of
nodes~\cite{AbboudKT20,KrauthgamerT18,AbboudGIKPTUW19}. So, even though our reductions don't tell us what is the best possible
exponent in the running time, they do imply that it is the same as for maximum matching in a sparse bipartite graph.
In particular, by plugging in the asymptotically fastest known algorithm~\cite{liu2020faster}, we obtain an $\ctO(n^{4/3+o(1)})$ time algorithm for computing
the permutation distance between two trees on $n$ nodes. The main technical novelty in our reduction from permutation
distance is that, even though the natural approach would result in multiple instances of weighted maximum bipartite matching,
we manage to keep the graphs unweighted.
In Section~\ref{sec:rearrangement} we show that computing the rearrangement distance between two trees is NP-hard, via a reduction from $3$-dimensional matching~\cite{karp1972reducibility}. In Section~\ref{sec:approxbinary} we consider the special case where one of the two tree is binary and give a simple linear-time $4$-approximation ratio algorithm.
In Section~\ref{sec:approxtree} we design a linear-time constant-factor approximation algorithm that does
not assume that the trees are binary. The algorithm consists of multiple phases, each of them introducing more and
more structure into the currently considered instance, while making sure that we don't pay more than the optimal distance
times some constant. To connect the number of steps used in every phase with the optimal distance, we introduce a new
combinatorial object that can be used to lower bound the latter, inspired by the well-known algorithm
for computing the majority~\cite{Moore91}.
In Section~\ref{sec:fixedpar} we describe a fixed-parameter algorithm for the rearrangement distance.
Finally, in Section~\ref{sec:experiments} we report an experimental analysis that we have run to validate the approximation factor of those algorithms.
\section{Preliminaries}\label{sec:preliminariestree}
Let $[n]=\{1,2,\ldots,n\}$.
Throughout this paper, we consider \emph{fully-labelled} rooted trees and forests, whose nodes are labelled with distinct labels from $[n]$, and identify nodes with their
labels.
All the problems we consider will have as input two fully-labeled trees or forests such that the set of labels is $[n]$ for both trees (resp. forests), where $n$ is the number of nodes of each input object.
We next introduce the two operations on trees that we will study.
\begin{definition}[link-and-cut operation]
\label{def:link-and-cut}
Let $u$, $v$, and $w$ be three vertices of a tree $T$, such that $v$ is a child of $u$ and $w$ is not a descendant of $v$.
Then the link-and-cut operation $v\,|\,u\rightarrow w$ on $T$ consists of a sequence of two operations: first delete from $T$ the edge $(v,u)$, then add the edge $(v,w)$, effectively switching the parent of $v$ from $u$ to $w$.
\end{definition}
%
\begin{definition}[permutation operation]
\label{def:permutation}
Let $T$ be a tree and let $\pi:[n]\mapsto [n]$ be a bijective function.
The permutation operation defined by $\pi$ consists in applying $\pi$ to $T$, thus changing the label of each node of $T$ labeled by $x$ to $\pi(x)$.
\end{definition}
%
Notice that such a function $\pi$ is a permutation of a subset of $[n]$, where only the labels $x$ such that $\pi(x)\neq x$ are involved in the permutation.
The size $|\pi|$ of a permutation is the number of elements $x$ s.t. $\pi(x)\neq x$.
%Recall that two trees $\Tone$ and $\Ttwo$ are isomorphic if and only if we can reorder the children of every node so as to make the
%trees identical after disregarding the labels.
Given a forest $F$, the parent of $u$ in $F$ is denoted $p_{F}(u)$, and we use the convention
that $p_{F}(u)=\bot$ when $u$ is a root in $F$. Moreover, $F|u$ denotes the subtree of $F$ rooted at $u$,
$\children_{F}(u)$ stands for the set of children of a node $u$ in $F$, and $\level_{F}(u)$ is the level of $u$ in $F$
(the roots have level $0$).
% Two trees $\Tone$ and $\Ttwo$ are \emph{topologically isomorphic}, denoted $\Tone \approx \Ttwo$, if and only if there exists a bijection $\mu$ between their nodes
% such that, for every vertex $v$ of $T_1$, $|\children_{T_1}(v)| = |\children_{T_2}(\mu(v))|$.
Two trees $\Tone$ and $\Ttwo$ are isomorphic, denoted $\Tone \equiv \Ttwo$, if and only if there exists a bijection $\mu$ between their nodes
such that, for every vertex $u$ of $T_1$, $\mu(p_{T_{1}}(u))=p_{T_{2}}(\mu(u))$.
Notice that, by this definition, $\mu$ maps the root of $\Tone$ to the root of $\Ttwo$.
% Let $\cI(\Tone,\Ttwo)$ denote the set of all such bijections $\mu$.
% Given two isomorphic trees $\Tone$ and $\Ttwo$, we seek a permutation $\pi$ with the smallest possible size that
% transforms $\Tone$ into $\Ttwo$. This is equivalent to finding $\mu\in\cI(\Tone,\Ttwo)$ that maximises
% the number of conserved nodes $\conserved(\mu)=\{ u : u = \mu(u) \}$,
% as these two values sum up to $n$.
We now give the notions of distance between trees
$T_1$ and $T_2$ labelled by $[n]$ that we will study.
Unless otherwise stated, throughout this paper we will assume that the roots of $\Tone$ and $\Ttwo$ have the same label.
\begin{definition}[Distances]
\label{definition:edge}\label{definition:distances}
Let $T_1$ and $T_2$ be two trees labeled by the set $[n]$.
The \emph{link-and-cut distance} $d_{\ell}(T_1,T_2)$ is the length
of the shortest sequence of link-and-cut operations which transforms
$T_1$ into $T_2$.
\label{definition:rearrangment}
The \emph{rearrangement distance} $d(T_1,T_2)$ is the smallest size of any
sequence of link-and-cut and permutation operations that transforms $T_1$ into $T_2$.
\label{definition:permutation}
If $T_1$ and $T_2$ are isomorphic, then the \emph{permutation distance} $d_{\pi}(T_1, T_2)$ is the size
$|\pi|$ of the smallest permutation $\pi$ that transforms $T_1$ into
$T_2$.
\end{definition}
Clearly, the permutation distance $d_{\pi}(T_1,T_2)$ is defined only
when $T_1$ and $T_2$ are isomorphic, and it is evidently well
posed. The following Lemma ensures that the definition of link-and-cut
distance is well posed too.
\begin{lemma}
\label{lemma:wellposed}
Given trees $T_1$ and $T_2$ each labelled by $[n]$, there always
exists a sequence of link-and-cut operations that transforms $T_1$
into $T_2$.
\end{lemma}
%
\begin{proof}
For any node $v$, $p_{T_1}(v)=u$, such that $p_{T_2}(v)=w$ and $w$ is
a descendant of $v$ in $T_1$ --- and thus the operation
$\edge{v}{u}{w}$ is not directly applicable --- we prove that there
exists a node $z$ on the path from $v$ to $w$ in $T_1$ (including $w$)
such that $p_{T_2}(z)$ is not a descendant of $v$ in $T_2$ nor a
descendant of $z$ in $T_1$. This implies that after applying the
valid operation $\edge{z}{p_{T_1}(z)}{p_{T_2}(z)}$, the operation
$\edge{v}{u}{w}$ becomes valid too. There is always such a node $z$
because, should it not exist, $w$ would be a descendant of $v$ also in
$T_2$, giving rise to the cycle $(w \rightarrow v \rightarrow \cdots
\rightarrow w)$ and thus contradicting the fact that $T_2$ is a tree.
\end{proof}
Since these operations are invertible,
all the above distance measures are symmetric. Moreover, they satisfy by
definition the triangle inequality.
% : consider, e.g., the rearrangement
% distance. Given $T_1$, $T_2$ and $T_3$ labelled by the same set of
% labels, let $\cS_{12}$ be a sequence that transforms $T_1$ into $T_2$
% such that $|\cS_{12}|=d(T_1,T_2)$, $\cS_{23}$ a sequence that
% transforms $T_2$ into $T_3$ with $|\cS_{23}|=d(T_2,T_3)$, $\cS_{13}$ a
% sequence that transforms $T_1$ into $T_3$ and $|\cS_{13}|=d(T_1,T_3)$.
% It is evident that the concatenation $\cS_{12} \cS_{13}$ of $\cS_{12}$
% and $\cS_{23}$ is a sequence that transforms $T_1$ into $T_3$, and by
% Definition~\ref{definition:rearrangment} its size is larger or equal to
% $d(T_1,T_3)$: thus $d(T_1,T_2) + d(T_2,T_3) \ge |\cS_{12} \cS_{23}|\ge
% d(T_1,T_3)$. A similar argument shows that the triangular inequality
% also holds for the link-and-cut distance and the permutation distance.
%We next define two structures that capture two fundamental characteristics of the trees to compare.
% \begin{definition}[active set]
% \label{definition:active}
% Given trees $T_1$ and $T_2$, we call
% \emph{active} the subset $\cX \subseteq [n]$ of labels which have
% different parents in $T_1$ and $T_2$, i.e., $v \in \cX$ iff
% $p_{T_1}(v) \ne p_{T_2}(v)$.
% \end{definition}
In Section~\ref{sec:approxtree}, for ease of presentation, in addition to the link-and-cut operation we will also work with the cut operation, defined as follows:
\begin{definition}
[cut operation] Let $u,v$ be two nodes such that $v$ is a child of $u$. The cut operation
$(v\dagger u)$ removes the edge $(v,u)$, effectively making $v$ a root and $u$ a leaf.
\end{definition}
The size of a sequence of cut and permutation operations is defined similarly as for a sequence
of link-and-cut and permutation operations.
Since a permutation operation is essentially just renaming the nodes, we can assume
that all permutation operations precede all link-and-cut (or cut) operations, or vice versa.
Furthermore, consecutive permutation operations that are not pairwise disjoint can be replaced by a single permutation operation
without increasing the total size.
This leads to the notion of rearrangement distance between two forests $\Fone$ and $\Ftwo$.
We write $\Fone\sim \Ftwo$ to denote that, for every $u\in [n]$, at least one of the following three conditions holds:
(i) $p_{\Fone}(u) = p_{\Ftwo}(u)$,
(ii) $p_{\Fone}(u)=\bot$, or
(iii) $p_{\Ftwo}(u)=\bot$.
The rearrangement distance $\tilde{d}(\Fone,\Ftwo)$ is the
smallest size of any sequence of cut and permutation operations that transforms $\Fone$ into $\Fone'$ such that $\Fone' \sim \Ftwo$.
This is the same as the smallest size of any sequence of cut and permutation operations
that transforms $\Ftwo$ into $\Ftwo'$ such that $\Fone \sim \Ftwo'$, as both sizes are equal
to the minimum over all permutations $\pi$ that fix the original root of the following expression
\[ |\{ u : \pi(u) \neq u\}| + |\{ u : p_{\Fone}(u) \neq p_{\Ftwo}(\pi(u))~\land~p_{\Fone}(u)\neq \bot~\land~p_{\Ftwo}(\pi(u))\neq \bot \}| . \]
Consequently, $\tilde{d}$ defines a metric.
In Section~\ref{sec:approxtree} we connect $d(\Tone,\Ttwo)$ and $\tilde{d}(\Tone,\Ttwo)$, and then work with the latter.
A matching in a bipartite graph is a subset of edges with no two edges meeting at the same vertex. A maximum matching
in an unweighted bipartite graph is a matching of maximum cardinality, whereas a maximum weight matching in a weighted
bipartite graph is a matching in which the sum of weights is maximised.
Given an unweighted bipartite graph with $m$ edges, the well-known algorithm by Hopcroft and Karp~\cite{HopcroftK73} finds
a maximum matching in $\cO(m^{1.5})$ time. This has been recently improved by Liu and Sidford to $\tilde\cO(m^{4/3+o(1)})~$\cite{liu2020faster}.
A \emph{heavy path decomposition} of a tree $T$ is obtained by selecting, for every non-leaf node $u\in T$, its \emph{heavy child} $v$ such
that $T|v$ is the largest: there will be some subtlety in how to resolve a tie in this definition that will be explained in detail later.
This procedure decomposes the nodes of $T$ into node-disjoint paths called \emph{heavy paths}.
Each heavy path $p$ starts at some node, called its \emph{head}, and ends at a leaf: $\head_{T}(u)$ denotes the head of the heavy path containing a node $u$ in $T$. An important property of such a decomposition is
that the number of distinct heavy paths above any leaf (that is, intersecting the path from a leaf to the root) is only logarithmic
in the size of $T$~\cite{DBLP:journals/jcss/SleatorT83}.
\section{Permutation Distance}
\label{sec:permutation}
Our aim is to find an isomorphic map $\mu\in\cI(\Tone,\Ttwo)$ that maximises $\conserved(\mu)$, that is
$\gamma(\Tone,\Ttwo)=\max \{ \conserved(\mu) : \mu \in \cI(\Tone,\Ttwo)\}$. To make the notation less
cluttered, we define $\gamma(x,y)=\gamma(\Tone|x,\Ttwo|y)$.
Let us start by describing a simple polynomial time algorithm which illustrates the basic idea behind the procedure.
We will then show how to improve it to obtain a faster algorithm that uses unweighted bipartite maximum matching.
Finally, we will show a reduction from bipartite maximum matching to computing
the permutation distance, establishing that these two problems are in fact equivalent, up to polylogarithmic factors.
\subsection{Polynomial Time Algorithm }\label{subsec:n2}
The first step of our solution is to run the folklore linear-time algorithm of Aho et al.~\cite{Aho} for determining if two rooted trees are isomorphic.
Recall that this algorithm assigns a number from $\{1,2,\ldots,2n\}$ to every node of $\Tone$ and $\Ttwo$ so that the subtrees
rooted at two nodes are isomorphic if and only if their numbers are equal.
The high-level idea is then to consider a weighted bipartite graph
$G(u,v)$ for each $u,v\in [n]$ such that $\level_{\Tone}(u)=\level_{\Ttwo}(v)$ and $\Tone|u \equiv \Ttwo|v$.
The vertices of $G(u,v)$ are $\children_{\Tone}(u)$ and $\children_{\Ttwo}(v)$, and there is
an edge of weight $\gamma(u',v')$ between $u' \in \children_{\Tone}(u)$ and $v' \in \children_{\Ttwo}(v)$ if and only if
$\Tone|u' \equiv \Ttwo|v'$ and $\gamma(u',v')>0$.
We call such graphs the \emph{distance graphs} for $\Tone$ and $\Ttwo$ and denote them collectively by $\cG(\Tone,\Ttwo)$. See Figure~\ref{fig:graphs}
for an example.
The weights $\gamma(u,v)$ are computed as follows, with $\cM(G(u,v))$ denoting the weight of
a (not necessarily perfect) maximum weight matching in $G(u,v)$, and $\Gamma:[n]\times[n]\rightarrow\{0,1\}$ being a function such that $\Gamma(u,v)=1$ if $u=v$ and $\Gamma(u,v)=0$ otherwise.
\begin{equation}
\label{eq:recursion}
\gamma(u,v) = \left\{
\begin{array}{ll}
\cM(G(u,v))+ \Gamma(u,v) & \textrm{ if } T_1|u \equiv T_2|v,\\
0 & \textrm{ otherwise.}\\
\end{array}
\right.
\end{equation}
Note that the overall number of edges created in all graphs is $\cO(n^2)$. Indeed, for each $u\in [n]$
such that $\level_{\Tone}(u)=\level_{\Ttwo}(u)$ and $\Tone|u \equiv \Ttwo|u$,
and for each pair of ancestors $z$ of $u$ in $\Tone$ and $w$ of $u$ in $\Ttwo$ such that $\level_{\Tone}(z)=\level_{\Ttwo}(w)$ and
$\Tone|z \equiv \Ttwo|w$, we possibly add an edge $(z,w)$ to the graph $G(p_{\Tone}(z),p_{\Ttwo}(w))$.
Since there are up to $n$
pairs of ancestors on the same level for each label, and the labels are $n$, there are $\cO(n^2)$ edges overall.
We then start from the deepest level in both trees, and we move up level by level
towards the roots in both trees simultaneously. For each level $k$, we consider all pairs of isomorphic subtrees rooted at level $k$,
build the corresponding distance graphs, and use Equation (\ref{eq:recursion}) to weigh the edges. After having reached the roots, we
return the value of $\gamma(\Tone,\Ttwo)$. The correctness of the algorithm is given by the following lemmas, the first one stating that the permutation distance is equal to the minimum number of labels that are not conserved by any isomorphic mapping.
\begin{lemma}\label{lem:gamma}
For any two isomorphic trees $\Tone$, $\Ttwo$, each labelled by $[n]$, it holds that $d_{\pi}(\Tone,\Ttwo)=n-\gamma(\Tone,\Ttwo)$.
\end{lemma}
\begin{proof}
Consider an isomorphic mapping $\mu$ from $T_1$ to $T_2$ that has the
minimum number of mismatched labels $\Delta(\mu) = n-\gamma(\Tone,\Ttwo)$ and consider the set of labels of the vertices involved in the
set of mismatching vertices given by
$\mu$.
Clearly, such labels are in a permutation $\pi$ which rearranges the
labels of tree $T_1$ to obtain $T_2$, while, by construction of $\mu$,
all the other labels will not be perturbed by $\pi$. Then we need
to show that such a permutation rearranges the minimum number of
distinct labels, that is, its size $|\pi| = d_{\pi}(T_1, T_2)$ is
the permutation distance. Indeed, assume to the contrary that the
permutation distance $d_{\pi}(T_1,T_2) < |\pi|$. This implies the
existence of a permutation $\pi'$ that rearranges fewer labels than
$\pi$, i.e., $|\pi'| < |\pi|$. Then we show that there exists an
isomorphic mapping $\mu'$ that has mismatch number less than the one
of $\mu$, contradicting the initial assumption.
Indeed, consider the permutation $\pi'$ and define the mapping $\mu'$
from $T_1$ to $T_2$ such that $\mu'(u) = v$ whenever $\pi'(v) = u$ The
mapping $\mu'$ is an isomorphism by the construction of $\pi'$, since
$\mu'(\pi'(u)) = u$ for all nodes $u$ of $\Tone$, and with the application
of $\pi'$, the two trees are congruent, hence congruency of the
labels implies isomorphism of the two trees. This concludes the
proof that $\mu'$ is an isomorphism thus leading to a contradiction.
\end{proof}
\begin{lemma}
Recursion~\ref{eq:recursion} is correct.
\end{lemma}
\begin{proof}
It is essentially a proof by induction. Recall that $\gamma(u,v)$ is the maximum number of labels conserved by any isomorphism between $\Tone|u$ and $\Ttwo|v$. If both $u$ and $v$ are
leaves, then $T_1|u \equiv T_2|v$ is trivially true and
$\gamma(u,v) = \Gamma(u,v)$. When $T_1|u \equiv T_2|v$ does not hold,
then the permutation distance (thus $n-\gamma(u,v)$, by Lemma~\ref{lem:gamma}) is undefined.
Otherwise, if $u$ and $v$ are internal vertices, and
$T_1|u \equiv T_2|v$, then let $\mu$ be a bijective mapping from the
nodes of $T_1|u$ to the nodes of $T_2|v$ maximizing $\conserved(\mu)$.
By the definition of $\gamma$ and the construction of $\mu$, $\gamma(u,v)=\sum_{z\in\children(u)}\gamma(z,\mu(z)) + \Gamma(u,v)$.
By the inductive hypothesis, all values $\gamma(z,\mu(z))$ are correctly computed; since $\mu$ is the bijective mapping maximizing the conserved labels, no other mapping $\mu'$
can achieve a larger value of $\sum_{z\in\children(u)}\gamma(z,\mu(z))$, and so $\mu(z)$ is determined by a maximum weight matching on $G(u,v)$.
\end{proof}
The running time is polynomial if we plug in any polynomial-time maximum weight matching algorithm.
In the next subsection we show how to obtain a better running time by constructing a different version of distance graphs, so that the total weight of their edges will be subquadratic,
and replacing maximum weight matching with maximum matching.
\subsection{Reduction to Bipartite Maximum Matching}
\label{sub:reduction}
We start by finding a heavy path decomposition of $\Tone$ and $\Ttwo$, with some extra care in resolving a tie if there are multiple children with subtrees of the same size, as follows.
Recall that we already know which subtrees of $\Tone$ and $\Ttwo$ are isomorphic, as the algorithm of~\cite{Aho} assigns the same number from $\{1,2,\ldots,2n\}$ to nodes of $\Tone$ and $\Ttwo$ with isomorphic subtrees.
For every $u,v\in [n]$ such that $\Tone|u \equiv \Ttwo|v$, we would like the heavy child $u'$ of $u$ in $\Tone$
and $v'$ of $v$ in $\Ttwo$ to be such that $\Tone|u' \equiv \Ttwo|v'$.
This can be implemented in linear time: it suffices to group the
nodes with isomorphic subtrees together, and then make the choice just once for every such group.
Consider a graph $G(u,v)$ for some $u,v\in [n]$: the edge corresponding to the heavy child $u'$ of $u$ in $\Tone$
and the heavy child $v'$ of $v$ in $\Ttwo$ is called \emph{special} (note that this edge might not exist).
The key observation is that the properties of heavy path decomposition
allow us to bound the total weight of non-special edges by $\cO(n\log n)$.
\begin{figure}[t]
\centering
\includegraphics[width=0.9\linewidth]{Fig/Tone_Ttwo.pdf}
\caption[Example of heavy path decomposition]{$\Tone$ and $\Ttwo$ with a possible heavy path decomposition.}
\label{fig:paths}
\end{figure}
\begin{figure}
\centering
\begin{subfigure}[height=2.5cm]{0.38\textwidth}
\includegraphics[width=\linewidth]{Fig/graphs.pdf}
\end{subfigure}
~~~~~~
\begin{subfigure}[height=2.5cm]{0.13\textwidth}
\includegraphics[width=\linewidth]{Fig/G1aa.pdf}
\end{subfigure}
~~~~~~
\begin{subfigure}[height=2.5cm]{0.13\textwidth}
\includegraphics[width=\linewidth]{Fig/G2aa.pdf}
\end{subfigure}
\caption[Graphs of type 1 and type 3]{$G(a,a)$ (type 1), $G'(a,a)$, $G''(a,a)$ and $G(b,c)$ (type3) for the trees of Figure~\ref{fig:paths}. The special edge in each graph is dashed.}
\label{fig:graphs}
\end{figure}
\begin{lemma}
\label{lem:nonspecial}
The total weight of all non-special edges in $\cG(\Tone,\Ttwo)$ is $\cO(n\log n)$.
\end{lemma}
\begin{proof}
Consider any $u\in [n]$ such that $\level_{\Tone}(u)=\level_{\Ttwo}(u)$ and $\Tone|u \equiv \Ttwo|u$.
For each pair of ancestors $z$ of $u$ in $\Tone$ and $w$ of $u$ in $\Ttwo$ such that $\level_{\Tone}(z)=\level_{\Ttwo}(w)$,
$\Tone|z \equiv \Ttwo|w$ and either $\head_{\Tone}(z)=z$ or $\head_{\Ttwo}(w)=w$, $u$ will contribute 1 to the weight of an edge $(z,w)$ in $G(p_{\Tone}(z),p_{\Ttwo}(w))$.
Because there are at most $\log n$ heavy paths above
any node of $\Tone$ or $\Ttwo$, each label $u\in [n]$ contributes 1 to the weight of at most $2\log n$ non-special edges, making their total weight $\cO(n\log n)$ overall.
\end{proof}
Recall that $\Gamma:[n]\times[n]\rightarrow\{0,1\}$ is a function such that $\Gamma(u,v)=1$ if $u=v$ and $\Gamma(u,v)=0$ otherwise. We divide the graphs in $\cG(\Tone,\Ttwo)$ into three types (see Figure~\ref{fig:graphs}, left, for an example):
\begin{description}
\item[Type 1: ] graphs $G(u,v)$ with at least one non-special edge.
\item[Type 2: ] graphs $G(u,v)$ with no non-special edges, and $\Gamma(u,v)=1$.
\item[Type 3: ] graphs $G(u,v)$ with no non-special edges, and $\Gamma(u,v)=0$.
\end{description}
We will construct only the graphs of type 1 and 2, and extract from them the information that the graphs of type 3 would have captured.
In what follows we show how to construct the graphs of type 1 and 2 in $\cO(n\log^{2} n)$ time.
\paragraph{Constructing the graphs of type 1 and 2.}\label{subsub:constructing}
The first step is to find all pairs of nodes that correspond to graphs of type 1 or 2, and store them
in a dictionary $D$ implemented as a balanced search tree with $\cO(\log n)$ access time.
The second step is to find the non-special edges of these graphs, and store them in a separate dictionary, also implemented as a balanced search tree with $\cO(\log n)$ access time.
Note that the weights will be found at a later stage of the algorithm.
We assume that both trees have been already decomposed into heavy paths, and we already know which subtrees are isomorphic.
This can be preprocessed in $\cO(n)$ time.
\begin{lemma}
All graphs of type 1 and 2 can be identified in $\cO(n\log^2 n)$ time.
\end{lemma}
\begin{proof}
We consider every $u\in [n]$ such that $\level_{\Tone}(u)=\level_{\Ttwo}(u)$
and $\Tone | u \equiv \Ttwo | u$ in two passes. In the first pass,
we need to iterate over every ancestor $z$ of $u$ in $\Tone$
and $w$ of $u$ in $\Ttwo$ such that $\level_{\Tone}(z)=\level_{\Ttwo}(w)$, $T_{1}|z \equiv T_{2}|w$ and either $\head_{\Tone}(z)=z$ or $\head_{\Ttwo}(w)=w$,
and if additionally $T_{1}|p_{T_{1}}(z) \equiv T_{2}|p_{T_{2}}(w)$ then designate $G(p_{T_{1}}(z),p_{T_{2}}(w))$ to be a graph of type 1 and insert it into $D$.
As a non-special edge $(z,w)$ of a graph $G(p_{T_{1}}(z),p_{T_{2}}(w))$ is such that either $z$ or $w$ are not on the same heavy path as their parents, this correctly determines all graphs of type 1.
To efficiently iterate over all such $z$ and $w$ given $u$,
we assume that the nodes of every heavy path of a tree $T$ are stored in an array, so that, given any node $u\in T$, we are able to access the node that belongs to the same heavy path as $u$ and whose level is $\ell$ in constant time, if it exists.
We denote such operation $\access_{T}(u,\ell)$.
Given two nodes $u\in\Tone$ and $v\in\Ttwo$ on the same level, the procedure above shows how to iterate over every ancestor $z$ of $u$ and $w$ of $v$ such that $\level_{\Tone}(z)=\level_{\Ttwo}(w)$ and either $\head_{\Tone}(z)=z$ or $\head_{\Ttwo}(w)=w$, in $\cO(\log n)$ time,
implying that all graphs of type 1 can be identified in $\cO(n\log^{2}n)$ time.
{
\setlength{\interspacetitleruled}{0pt}%
\setlength{\algotitleheightrule}{0pt}%
\begin{algorithm}[tb]
\While{$u \neq \bot$ {\bf and} $v\neq \bot$}{
\uIf{$ \level_{T_{1}}(\head_{T_{1}}(u)) < \level_{T_{2}}(\head_{T_{2}}(v)) $}{
output $\access_{T_{1}}(u,\level_{T_{2}}(\head_{T_{2}}(v)))$ and $\head_{T_{2}}(v)$\;
$v \gets p_{T_{2}}(\head_{T_{2}}(v))$\;
}\eIf{$ \level_{T_{1}}(\head_{T_{1}}(u)) > \level_{T_{2}}(\head_{T_{2}}(v)) $}{
output $\head_{T_{1}}(u)$ and $\access_{T_{2}}(v,\level_{T_{1}}(\head_{T_{1}}(u)))$\;
$u \gets p_{T_{1}}(\head_{T_{1}}(u))$\;
}{
output $\head_{T_{1}}(u)$ and $\head_{T_{2}}(v)$\;
$u \gets p_{T_{1}}(\head_{T_{1}}(u))$\;
$v \gets p_{T_{2}}(\head_{T_{2}}(v))$\;
}
}
\end{algorithm}
}
In the second pass,
for each $u\in [n]$ such that $\level_{\Tone}(u)=\level_{\Ttwo}(u)$
and $\Tone | u \equiv \Ttwo | u$, we designate $G(u,u)$ to be a graph of type 2, unless it has been already designated to be a graph of type 1.
\end{proof}
\begin{lemma}
All graphs of type 1 and 2 can be populated with their edges in $\cO(n\log^2 n)$ time.
\end{lemma}
\begin{proof}
For each such graph $G(u,v)$ such that none of $u,v$
is a leaf, let $u'$ be the unique heavy child of $u$, and $v'$ be the unique heavy child of $v$. We add the special
edge $(u',v')$ to $G(u,v)$. To find the non-special edges, we again consider every $u\in [n]$ such that
$\level_{\Tone}(u)=\level_{\Ttwo}(u)$ and $\Tone|u \equiv \Ttwo|u$: we iterate over the ancestors $z$ of $u$ in $\Tone$
and $w$ of $u$ in $\Ttwo$ such that $\level_{\Tone}(z)=\level_{\Ttwo}(w)$, $T_{1}|z \equiv T_{2}|w$ and either $\head_{\Tone}(z)=z$ or $\head_{\Ttwo}(w)=w$, and if additionally $T_{1}|p_{\Tone}(z) \equiv T_{2}|p_{\Ttwo}(w)$ then add a non-special edge
$(z,w)$ to $G(p_{\Tone}(z),p_{\Ttwo}(w))$ .
This takes $\cO(n\log^{2}n)$ time overall.
\end{proof}
\paragraph{Processing the graphs of type 1 and 2.}\label{subsub:processing}
Having constructed the graphs of type 1 and 2 in $\cO(n\log^{2}n)$ time, we process them level by level.
Consider $G(u,v)$: for each of its edges $(u',v')$ corresponding to $u'\in\children_{T_{1}}(u)$
and $v'\in\children_{T_{2}}(v)$, we need to extract its weight $\gamma(u',v')$. If $G(u',v')$ is of type 1 or 2,
the graph
can be extracted from the dictionary in $\cO(\log n)$ time. Otherwise,
$G(u',v')$ is of type 3 and we need to make up for not having processed such graphs.
To this aim, we associate a sorted list of levels with each pair of heavy paths of $\Tone$ and $\Ttwo$.
The lists are stored in a dictionary indexed by the heads of the heavy paths.
For every $u,v\in[n]$ such that $G(u,v)$ is of type 1 or 2, we append
the levels of $u$ and $v$ to the lists associated with the respective heavy paths.
The lists can be constructed in $\cO(n\log^{2}n)$ time by processing the graphs level by level, and allow us to efficiently use the following lemma.
\begin{lemma}
\label{lem:type3}
Consider $u,v\in [n]$ such that $\level_{\Tone}(u)=\level_{\Ttwo}(v)$ and $\Tone|u\equiv \Ttwo|v$,
but $G(u,v)$ is of type 3. Either both $u$ and $v$ are leaves and $\gamma(u,v)=0$,
or the heavy child of $u$ is $u'$, the heavy child of $v$ is $v'$, and $\gamma(u,v)=\gamma(u',v')$.
\end{lemma}
\begin{proof}
First observe that $u\neq v$, as otherwise $G(u,v)$ would be of type 2.
Becase $\Tone|u\equiv \Ttwo|v$, either both $u$ and $v$ are leaves or none of them is a leaf.
In the former case, $G(u,v)$ is empty and $\gamma(u,v)=0$.
By how we resolve ties in the heavy path decomposition, in the latter case we have $\Tone|u' \equiv \Ttwo|v'$,
where $u'$ is the heavy child of $u$ and $v'$ is the heavy child of $v$.
$G(u,v)$ consists of the unique special edge corresponding to the heavy child $u'$ of $u$
and $v'$ of $v$, so $\cM(G(u,v))$ is equal to the
cost of the special edge, and by Equation~\ref{eq:recursion} we obtain that $\gamma(u,v)=\gamma(u',v')$.
\end{proof}
Given $u,v\in [n]$ such that $\level_{\Tone}(u)=\level_{\Ttwo}(v)=\ell$ and $\Tone|u\equiv \Ttwo|v$,
we extract $\gamma(u,v)$ by accessing the sorted list associated with the heavy paths of $u$ and $v$: we binary search for the smallest level $\ell'\geq \ell$ such that the heavy paths of $u$ and $v$ respectively contain a node
$u'$ and $v'$, both on level $\ell'$, with $G(u',v')$ of type
1 or 2. Then Lemma~\ref{lem:type3}, together with the fact that in our heavy path decomposition the subtrees rooted at
the heavy children of two nodes with isomorphic subtrees are also isomorphic, implies that
$\gamma(u,v)=\gamma(u',v')$.
It remains to describe how to compute $\cM(G(u,v))$ for every graph $G(u,v)$ of type 1 and 2. We could
have used any maximum weight matching algorithm, but this would result in a higher running time.
Our goal is to plug in a maximum unweighted matching algorithm. This seems problematic as $G(u,v)$ is a weighted
bipartite graph, but we will show that maximum weight matching can be reduced to multiple instances
of maximum matching. However, bounding the overall running time will require bounding the total weight
of all edges belonging to graphs of type 1 and 2. By Lemma~\ref{lem:nonspecial} we already know that the total
weight of all non-special edges is $\cO(n\log n)$, but such bound doesn't hold for the special edges.
Therefore, we proceed as follows. Let $u'$ be the heavy child of $u$ and $v'$ be the heavy child of $v$.
We construct $G'(u,v)$ by removing the special edge from $G(u,v)$.
We also construct $G''(u,v)$ from $G(u,v)$ by removing all the edges incident to $u'$ and $v'$ (see Figure~\ref{fig:graphs} for an example).
Equation~\ref{eq:recursion} can then be rewritten as follows:
\begin{equation}
\label{eq:weight}
\gamma(u,v)=\max\{\cM(G'(u,v)), \cM(G''(u,v))+\gamma(u',v') \} + \Gamma(u,v) .
\end{equation}
This is because a maximum weight matching in $G(u,v)$ either includes the special edge $(u',v')$, implying that no other edges incident to $u'$ and $v'$ can be part of the matching and thus $\cM(G(u,v))=\cM(G''(u,v))+\gamma(u',v')$, or it does not include it, thus $\cM(G(u,v))=\cM(G'(u,v))$.
Since the graphs $G'(u,v)$ and $G''(u,v)$ contain only non-special edges,
the overall weight of all edges in the obtained instances of maximum weight matching is $\cO(n\log n)$.
We already know that constructing all the relevant graphs takes $\cO(n\log^{2}n)$ time. It remains to
analyze the time to calculate the maximum weight matching in every $G'(u,v)$ and $G''(u,v)$.
We first present a preliminary lemma that connects the complexity of calculating the maximum weight matching
in a weighted bipartite graph to the complexity of calculating the maximum matching in an unweighted bipartite graph.
\begin{lemma}[\cite{decomposition}]
\label{lem:decomposition}
Let $G$ be a weighted bipartite graph, and let $N$ be the total weight of all the edges of $G$. Calculating the
maximum weight matching in $G$ can be reduced in $\cO(N)$ time to multiple instances of calculating the
maximum matching in an unweighted bipartite graph, in such a way that the total number of edges in all such graphs is at most $N$.
\end{lemma}
\begin{proof}
Using the decomposition theorem of Kao, Lam, Sung, and Ting~\cite{decomposition}, we can reduce computing
the maximum weight matching in a weighted bipartite graph such that the total weight of all edges is $N$ to
multiple instances of calculating the largest cardinality matching in an unweighted bipartite graph. The total
number of edges in all unweighted bipartite graphs is $\sum_{i}m_{i}=N$ and the reduction can be implemented
in $\cO(N)$ time by maintaining a list of edges with weight $w$, for every $w=1,2,\ldots,N$.
\end{proof}
\begin{theorem}
\label{thm:tomatching}
Let $f(m)$ be the complexity of calculating the maximum matching in an unweighted bipartite graph on $m$ edges,
and let $f(m)/m$ be nondecreasing.
The permutation distance can be computed in $\tilde{\cO}(f(n))$ time.
\end{theorem}
\begin{proof}
The total number of edges in all constructed graphs is $\cO(n\log n)$, and the total time to construct the relevant
graphs and extract the costs of their edges is $\cO(n\log^{2}n)$.
Thus, the total running time is $\cO(n\log^{2}n)$ plus the time to compute the maximum weight matching in every graph of type 1
and type 2. Let $N_{i}$ be the total weight of all non-special edges in the $i$-th of these graphs.
By Lemma~\ref{lem:nonspecial}, $\sum_{i} N_{i} = \cO(n\log n)$.
Additionally, $N_{i} \leq n$.
Let $m_{i,j}$ be the number of edges in the $j$-th instance of unweighted bipartite matching for the $i$-th graph.
By Lemma~\ref{lem:decomposition}, the overall time is hence $\sum_{i,j} f(m_{i,j})$, where
$\sum_{i,j} m_{i,j} \leq \sum_{i} N_{i} = \cO(n\log n)$ and $m_{i,j} \leq N_{i} \leq n$.
We upper bound $\sum_{i,j}f(m_{i,j})$ using the assumption that $f(m)/m$ is nondecreasing as follows:
\[
\sum_{i,j} f(m_{i,j}) = \sum_{i,j} m_{i,j} \cdot f(m_{i,j})/m_{i,j} \leq \sum_{i,j} m_{i,j} \cdot f(n)/n = \cO(f(n)\log n). \qedhere
\]
\end{proof}
\begin{corollary}
The permutation distance can be computed in $\tilde{\cO}(n^{4/3+o(1)})$ time.
\end{corollary}
\subsection{Reduction from Bipartite Maximum Matching}
\label{sec:from}
We complement the algorithm described in Subection~\ref{sub:reduction} with a reduction from
bipartite maximum matching to computing the permutation distance: see Figure~\ref{fig:reduction} for an example.
\begin{theorem}
\label{thm:frommatching}
Given an unweighted bipartite graph on $m$ edges, we can construct in $\cO(m)$ time two trees
with permutation distance equal to the cardinality of the maximum matching.
\end{theorem}
\begin{proof}
We first modify the graph so that the degree of every node is at most 3. This can be ensured in $\cO(m)$
time by repeating the following transformation: take a node $u$ with neighbours $v_{1},v_{2},\ldots,v_{k}$, $k\geq 4$.
Replace $u$ with $u'$ and $u''$ both connected to a new node $v$, connect $u'$ to $v_{1},v_{2},\ldots,v_{k-2}$ and
$u''$ to $v_{k-1},v_{k}$. It can be verified that the cardinality of the maximum matching in the new graph
is equal to that in the original graph increased by 1. By storing, for every node, the incident edges in a linked list, we can
implement a single step of this transformation in constant time, and there are at most $m$ steps.
We will now first construct two unlabelled trees and then explicitly assign appropriate labels to their nodes.
Without loss of generality, let the nodes of the graph be $u_{1},u_{2},\ldots,u_{m}$ and $v_{1},v_{2},\ldots,v_{m}$.
%and every edge connects some $u_{i}$ with some $v_{j}$.
In the first tree we create $m$ nodes, labelled with $u_{1},u_{2},\ldots,u_{m}$,
connected to a common unlabelled root. In the second tree we do the same with nodes $v_{1},v_{2},\ldots,v_{m}$. Then, for every edge $(u_{i},v_{j})$ of the graph, we attach a new leaf to $u_{i}$ in the first tree and to $v_{j}$ in the
second tree, and assign the same label to both of them. Finally, we attach enough unlabelled leaves to every $u_{i}$ and $v_{j}$ to make their degrees all equal to 3.
To make both trees fully-labelled on the same set of labels, we further attach $1+m+3m-m=3m+1$ \emph{extra leaves} to the roots of both trees.
For every unlabelled leaf attached to $u_{1},u_{2},\ldots,u_{m}$ of the first tree, we choose an unlabelled extra leaf of the second tree, and assign the same label to both of them.
We then assign the same label to the root of the first tree and an extra leaf of the second tree, and label the last $m$ extra leaves of the second tree with $u_{1},u_{2},\ldots,u_{m}$.
We finally swap the trees and repeat the same procedure: see Figure~\ref{fig:reduction} for an example.
The permutation distance between the two trees is equal
to the cardinality of the maximum matching. Indeed, the trees are clearly isomorphic; moreover, any isomorphism must match extra leaves with extra leaves, and every $u_{i}$ to a $v_{\pi(j)}$, for some permutation $\pi$ on $[m]$. The extra leaves do not
contribute to the number of conserved nodes, while $u_{i}$ and $v_{\pi(j)}$ contribute 1 if and only if $(u_{i},v_{\pi(j)})$ was
an edge in the original graph. Thus, the distance is equal to the maximum over all permutations $\pi$
of the number of edges $(u_{i},v_{\pi(j)})$. This in turn is equal to the cardinality of the maximum matching in the original
graph.
\end{proof}
\begin{figure}
\centering
\begin{subfigure}[b]{0.14\textwidth}
\includegraphics[width=\linewidth]{Fig/reduction_trees.pdf}
\vspace{-3mm}
\end{subfigure}
\begin{subfigure}[b]{0.42\textwidth}
\includegraphics[width=\linewidth]{Fig/reduction1.pdf}
\vspace{-3mm}
\end{subfigure}
\begin{subfigure}[b]{0.42\textwidth}
\includegraphics[width=\linewidth]{Fig/reduction2.pdf}
\vspace{-3mm}
\end{subfigure}
\caption[Reduction from Bipartite Maximum Matching]{The two trees built for the graph on the left, according to Theorem~\ref{thm:frommatching}.}
\label{fig:reduction}
\end{figure}
\section{Link-and-cut Distance}
\label{sec:link-and-cut}
In this section we present a simple linear-time algorithm to compute the link-and-cut distance between two trees.
\begin{comment}
The main technical device is called \emph{family partition}.\giulia{We do not really need to define the family partition in this section, I think. }
Given two trees $T_1$ and $T_2$, we can associate with $v$ the pair
$(p_{T_1}(v),p_{T_2}(v))$ of the parents of $v$ in the two trees.
Given a pair of (not necessarily distinct) nodes $u$ and $w$, we
define $\cP_{(u,w)}$ as the set $\{v ~:~ p_{T_1}(v) = u, ~ p_{T_2}(v)
= w\}$.\giulia{Does it make sense to define it for $u=w$, when in $\mathcal{P}$ we only consider the sets with $u\neq w$? Also, $\mathcal{P}$ was a partition of the active set, but now it does not partition anything. We should at least change the name} Since each vertex different from the root has exactly one
parent in each tree, each vertex $v\in [n]$ belongs to exactly one set $\cP_{(u,w)}$.
This fact leads to the following definition, which is illustrated in
Example~\ref{ex:partition}.
\begin{definition}[family partition]
\label{definition:partition}
Let trees $T_1$ and $T_2$ each be labelled by $[n]$: for each vertex
$v \in [n]$ we denote the set $\cP_{(u,w)} = \{v ~:~ p_{T_1}(v) = u,
~ p_{T_2}(v) = w\}$. Then $\cP$, called \emph{family partition}, is the collection of the nonempty sets $\cP_{(u,w)}$, $u,w \in V$ such that $u\neq w$.
\end{definition}
\begin{example}
\label{ex:partition}
Consider $T_1$ and $T_2$ of Figure~\ref{fig:dist}.
The family partition is composed of the
following sets: $\cP_{(2,3)}=\{4,5,6\}$, $\cP_{(3,2)}=\{10\}$.
\end{example}
\end{comment}
\begin{figure}[t]
\centering
\includegraphics[width=0.95\linewidth]{Fig/rearrangement.pdf}
\caption[Example of rearrangement distance between two trees]{Trees $\Tone$ and $\Ttwo$ labelled by $[10]$. The link-and-cut distance $d_\ell(\Tone,\Ttwo)$ is 4, given, for example, by the sequence $\edge{4}{2}{3}$, $\edge{5}{2}{3}$, $\edge{6}{2}{3}$, $\edge{10}{3}{2}$. The rearrangement distance is 3, given, for example, by the sequence $(2~3),\edge{7}{3}{2}$.}
\label{fig:dist}
\end{figure}
%
%
%The following Lemma explicits the relevance of the notion of family partition.
\begin{lemma}
\label{lemma:algorithm-link-and-cut}
Given two trees $T_1$ and $T_2$ each labelled by $[n]$, let $X$ be the set of nodes $v$ such that $p_{T_1}(v) \neq p_{T_2}(v)$.
Then $d_\ell(T_1, T_2)= |X|$ and a sequence of $|X|$ link-and-cut operations that transforms $T_1$ into $T_2$ can be computed in linear time.
%there is a linear-time algorithm that computes the shortest sequence of link-and-cut operations that transforms $T_1$ into $T_2$, and such a sequence consists of $|X|$ operations.
\end{lemma}
%
\begin{proof}
% In this proof we will exploit the fact that no two nodes of a tree share the same label by using a label to denote a node --- we will make the tree explicit when necessary.\giulia{I removed this because in the preliminaries we already say that we identify the nodes with their labels.}
Let $v$ be a node in $X$. %, that is $v$ belongs to a set $\cP_{(a,b)}$ with $a\neq b$.
Notice that each node in $X$ must be moved with at least a link-and-cut operation, hence $d_\ell(T_1, T_2)\ge |X|$. In the following we will describe how to obtain a sequence of exactly $|X|$ operation transforming $T_1$ into $T_2$.
Let $v$ be a node of $X$ such that no proper descendant of $v$ in $T_1$ belongs to $X$ --- if no such node $v$ exists, then $T_1$ and $T_2$ are isomorphic. \giulia{I do not understand the previous claim. How can such $v$ not exist? Only if $X$ is empty, but then the two trees are equal.}
Without loss of generality, let $p_{T_1}(v)=u$ and $p_{T_2}(v)=w$.
We claim that $w$ is never a descendant of $v$ in $T_1$, and we can therefore apply the $\edge{v}{u}{w}$ operation --- hence removing $v$ from $X$ --- and recurse on the resulting tree.
In fact, assume for a contradiction that $w$ is a descendant of $v$ in $T_1$, and let $<v, x_1, \ldots, x_{q-1}, x_q = w>$ be the path of $T_1$ from $v$ to $w$. Since no proper descendant of $v$ in $T_1$ belongs to $X$, $p_{T_1}(x_i)=p_{T_2}(x_i)$ for each $i\le q$, that is $<v, x_1, \ldots, x_{q-1}, x_q = w>$ is also a path of $T_2$, implying that $v$ is an ancestor of $w$ in $T_2$ as well.
This contradicts the fact that $p_{T_2}(v)=w$, therefore $w$ is not a descendant of $v$ in $T_1$ and the link-and-cut operation $\edge{v}{u}{w}$ is valid.
The time complexity is a consequence of the fact that a visit of the two trees $T_1$ and $T_2$ suffices to determine $p_{T_1}(x)$ and $p_{T_2}(x)$ for all $x\in[n]$ and the order in which the operations can be applied.
\end{proof}
% Note that the family partition encodes the elements of any shortest
% sequence of link-and-cut operations for transforming $T_1$ into $T_2$:
% $v \in \cP_{(u,w)}$ corresponds to operation $\edge{v}{u}{w}$. It is
% easy to see, from the proof of Lemma~\ref{lemma:wellposed}, that a
% shortest sequence of valid link-and-cut operations can be obtained from
% $\cP$ by ordering the set of operations it encodes with respect to a
% \emph{depth-first traversal} (DFT) of $T_1$:
% $\edge{u}{p_{T_1}(u)}{p_{T_2}(u)}$ precedes
% $\edge{v}{p_{T_1}(v)}{p_{T_2}(v)}$ if $u$ precedes $v$ in a DFT of
% $T_1$. Hence the link-and-cut distance $d_{\ell}(T_1,T_2)$
% is equal to number of nodes that are in some set of the family partition $\cP$.
\section{Rearrangement Distance}\label{sec:rearrangement}
We start by showing that deciding the rearrangement distance between two
trees is NP-hard, via a reduction from 3-dimensional matching, one of
Karp's 21 NP-complete problems~\cite{karp1972reducibility}.
In 3-dimensional matching, we are given three disjoint sets $A$, $B$
and $C$, along with a set $\cT$ of triples $(a,b,c)$, such that $a \in
A$, $b \in B$ and $c \in C$.
%: essentially, a 3-uniform hypergraph $H$.
A \emph{matching} is then a subset $\cM \subseteq \cT$ such that for
every two triples $(a,b,c) \in \cM$, $(a',b',c') \in \cM$, it follows
that $a \neq a'$, $b \neq b'$ and $c \neq c'$, that is, all triples of
$\cM$ are pairwise disjoint. It is then NP-hard to decide for a given
$k$ if there is a matching $\cM$ of size
$k$~\cite{karp1972reducibility}, even in the case of 3-bounded
1-common 3-dimensional matching, which is a restriction of the problem
where the number of occurrences of any element in the triples is at
most 3, and each pair of triples has at most one element in
common~\cite{jiang-2015-approximation}. % We also make use of the following structure in this proof.
%\begin{definition}[movements graph]
% \label{definition:movements}
% Given two trees $T_1$ and $T_2$, the
% \emph{movements graph} $G$ has an edge for every element
% $\cP_{(u,w)}$ of the family partition $\cP$ of $T_1$ and $T_2$, that
% is, $E_G = \{ (u,w) ~:~ \cP_{(u,w)} \in \cP \}$, while its vertex
% set is $V_G = \bigcup_{(u,w) \in E_G} \{u,w\}$.
%\end{definition}
We now prove that computing the rearrangement distance is NP-hard. In the following, with a slight abuse of notation we will write $u\in t$, for some triple $t=(a,b,c)\in\mathcal{T}$, instead of $u\in \{a,b,c\}$.
\begin{theorem}
\label{theorem:rearrangement}
Given trees $T_1$ and $T_2$ and some integer
$k$, it is NP-hard to decide if $d(T_1, T_2) \leq k$.
\end{theorem}
\begin{figure}[ht]
\centering
\includegraphics[width=\linewidth]{Fig/NPtrees.pdf}
\vspace{1mm}
\begin{comment}
\tikzset{
level 1/.style={
sibling distance=1.8cm,
},
level 2/.style={
sibling distance=.6cm,
},
}
\centering
\begin{tikzpicture}[scale=0.7]
\node (root) {$r$}
child { node {$a$}
child { node {$s_a^1$} }
child { node {$s_a^2$} } }
child { node {$b$}
child { node {$s_b^1$} }
child { node {$s_b^2$} }
child { node {$t_b^1$} }
child { node {$t_b^2$} } }
child { node {$c$}
child { node {$s_c^1$} }
child { node {$s_c^2$} } }
child { node {$a'$}
child { node {$t_{a'}^1$} }
child { node {$t_{a'}^2$} } }
child { node {$c'$}
child { node {$t_{c'}^1$} }
child { node {$t_{c'}^2$} } } ;
\node[draw=none] at (0,-4) {\Large $\Tone$} ;
\end{tikzpicture}
\begin{tikzpicture}[scale=0.7]
\node (root) {$r$}
child { node {$a$}
child { node {$s_c^1$} }
child { node {$s_c^2$} } }
child { node {$b$}
child { node {$s_a^1$} }
child { node {$s_a^2$} }
child { node {$t_{a'}^1$} }
child { node {$t_{a'}^2$} } }
child { node {$c$}
child { node {$s_b^1$} }
child { node {$s_b^2$} } }
child { node {$a'$}
child { node {$t_{c'}^1$} }
child { node {$t_{c'}^2$} } }
child { node {$c'$}
child { node {$t_b^1$} }
child { node {$t_b^2$} } } ;
\node[draw=none] at (0,-4) {\Large $\Ttwo$} ;
\end{tikzpicture}
\end{comment}
\caption[Reduction from 3D matching]{The trees $\Tone$ (left) and $\Ttwo$ (right)
given instance $H$ of 3-dimensional matching with $A=\{a,a'\}$,
$B=\{b\}$ and $C=\{c,c'\}$ and $\cT=\{s = \{a,b,c\}, ~ t =
\{a',b,c'\}\}$.}
\label{fig:reductionNP}
\end{figure}
\begin{proof}
Reduction from 3-bounded 1-common 3-dimensional matching. We are
given an instance $H$ of 3-dimensional matching consisting of a set
$\cT$ of $m$ triples $(a,b,c)$ over the disjoint sets $A$, $B$, $C$.
We construct two trees $\Tone$ and $\Ttwo$ each with $|A| + |B| +
|C| + 6m + 1$ nodes, for which the rearrangement distance
$d(\Tone, \Ttwo) \leq 3n + 6(m-n)$ if and only if $H$ has a
3-dimensional matching of size $n$.
Consider such an instance $H$ of 3-bounded 1-common 3-dimensional matching. We construct two trees $\Tone$ and $\Ttwo$, both with the same root
$r$, and in both of them we add a node for every element of $A$, $B$ and $C$ as children of the root $r$. For each triple $t=(a,b,c)\in
\mathcal{T}$, in $\Ttwo$ we add a pair $\{t_v^1, t_v^2\}$ of
(uniquely labelled) children for each $v \in t$ t. In $\Tone$, we add the pairs $\{t_v^1, t_v^2\}$ to each of these
three nodes, but cyclically shifted with respect to $\Ttwo$,
i.e., we add $\{ t_b^1, t_b^2\}$ to $a$, $\{t_c^1, t_c^2\}$ to $b$, $\{t_a^1, t_a^2\}$ to
$c$, as shown in Figure~\ref{fig:reductionNP}. Note that, since each element of $U=A\cup B\cup C$ occurs at most three times in the triples, in both trees each direct descendant of $r$ has at most $6$ children; and since any two triples of $\mathcal{T}$ share at most one element, for both $\Tone$ and $\Ttwo$ it holds, by construction, that there are no two children of the root $x,y$ and two triples $s,t$ of $\mathcal{T}$ such that $s_u^1, s_u^2,t_{u'}^1, t_{u'}^2$ are all children of $x$ and $s_v^1, s_v^2,t_{v'}^1, t_{v'}^2$ are all children of $y$, for some $u,v\in s$ and $u',v'\in t$.
\begin{comment}
\giulia{How was this useful? I removed it for now, I don't think we used any of that in the proof.}
By construction of the trees \Tone{} and \Ttwo, we can assume that
each permutation involves only nodes of the universal set $U = A
\cup B \cup C$ or only nodes representing triples of $\cT$ (never a
mixture of the two). Moreover, no node of the universal set $U$ is
involved in a link-and-cut operation, since changing its parent
would mean that the new parent is a node that is not $r$, which is
not optimal. \giulia{I think we should prove the last two claims.} Finally, any permutation of nodes representing the
triples can be replaced by a sequence of link-and-cut operations
with the same cost, since they are leaves in both $\Tone$ and $\Ttwo$. The overall effect is that we can restrict
ourselves to sequences of operations consisting of permutations of
nodes of $U$ followed by link-and-cut operations on nodes
representing the triples.
\end{comment}
\paragraph{
3-dimensional matching of size $\boldsymbol{n\implies d(\Tone, \Ttwo) \leq 3n + 6(m-n)}$.}Let $M$ be a 3-dimensional matching of $H$. Then for each triple
$t=(a,b,c) \in M$, apply the permutation $(a,b,c)$ to $\Tone$. After
this permutation, both nodes $t_v^1, t_v^2$ (with $v\in \{a,b,c\}$)
have $v$ as new parent. Since $M$ is a matching, those
permutations are independent. For each triple $t=(a,b,c) \notin M$,
apply a link-and-cut operation to both nodes $t_v^1, t_v^2$ (with $v\in
\{a,b,c\}$) so that their new parent is $v$. It is immediate to
notice that the resulting tree is exactly $T_2$ and that the overall
cost of the operations is $3n + 6(m - n)$.
%Indeed, each of the $n$ triples of the matching implies a permutation of size $3$, and since each node in $U=A\cup B\cup C$ has at most $6$ children, at most $6(m-n)$ additional link-and-cut operations are needed to transform $\Tone$ into $\Ttwo$.
\gdv{Requires inverting
\Tone and \Ttwo}\murray{I think the slight change made in this
paragraph avoids this requirement (or am I missing something?)}\giulia{Gianluca is right. I inverted the names of the two trees and inverted the cyclic rotation with which we construct $\Tone$ (former $\Ttwo$). Could someone please double-check?}
\paragraph{
3-dimensional matching of size $\boldsymbol{ d(\Tone, \Ttwo) \leq 3n + 6(m-n)\implies}$ 3-dimensional matching of size $\boldsymbol{n}$.}Let us now consider a sequence $S$ of permutations followed by
link-and-cut operations that transforms $\Tone$ to $\Ttwo$. We will
show how to obtain a sequence of permutations followed by
link-and-cut operations that is not more expensive than $S$, and
where the permutations have cost 3, are disjoint, and each
permutation involves exactly the elements of a triple of $\cT$. By
the first part of the proof, this suffices to prove the NP-hardness.
First of all, no node is moved in two separate link-and-cut
operations. Moreover, either all nodes in a pair
$\{t_v^1, t_v^2\}$,\murray{is this right, GDV?} for some $t, v$, are moved
with link-and-cut operations, or none of them is moved.
Let $X$ be the set of pairs $\{t_v^1, t_v^2\}$ such that no node is moved
with link-and-cut operations. By the construction of our reduction
and the fact that the instance is 3-bounded and 1-common, the triple
$t$ associated with each pair $\{t_v^1, t_v^2\}$ of $X$ appears in exactly
one permutation.\murray{not sure if I follow this statement} Let $P$
be the set of such triples. Hence replacing the permutation with a
sequence of permutations, one for each triple in $P$, results in a
sequence where the permutations have cost 3, are disjoint, and each
permutation involves exactly the elements of a triple of $\cT$.
We have to modify the link-and-cut operations involving the sets
that are not in $X$, but no new operation is added, completing the
proof.\gdv{I don't like the proof, but it shows clearly that cycles
of the movement graphs corresponds to triples in the
instance. Hence we can remove the movement graph.}\murray{removed
the movements graph.}
%
% operations of total size $d(\Tone,\Ttwo)$ will consist of a
% permutation followed by a sequence of link-and-cut operations.
% Observe that any permutation involves for each cycle $\cC_t$ an
% edge, two edges or all three edges. Now, the rearrangement distance
% aims to solve cycles in the movements graph in the sense that after
% the operations, the movements graph has no edges. Observe that
% given a cycle $\cC_t$ of the movements graph $G$, then the minimum
% cost rearrangement to solve $\cC_t$ consists of applying a
% permutation of size $3$ involving the three vertices of the cycle,
% thus of total cost $3$. Observe that two cycles sharing a common
% vertex cannot both be solved by a permutation that is a cyclic shift
% of the vertices of the cycle, that is they cannot be both solved
% with cost $3$. Moreover, permutations of vertices cannot solve more
% than one vertex of a cycle $\cC_t$ if it is not a cyclic shift of
% the vertices of $\cC_t$, as cycles do not share edges. In case of a
% cyclic shift of two vertices of $\cC_t$, (1) a permutation of size
% $2$ and then four link-and-cut operations are required. If instead
% (2) at most a single vertex of $\cC_t$ is involved in a permutation,
% then six link-and-cut operations are required. We now detail how
% this implies that $d(\Tone, \Ttwo) \leq 3n + 6(m- n)$ if and only if
% $H$ has a 3-dimensional matching of size $n$.
%
% ($\Rightarrow$) Assume that $d(\Tone, \Ttwo) \leq 3n + 6(m - n)$.
% By the above observation on how cycles of the movements graph $G$
% are solved by the sequence of permutations, cases (1) and (2) for
% solving cycles have the same cost equal to $6$. Thus the only
% possible way to have a rearrangement distance less than or equal to
% $3n + 6 (m- n) $ is by taking $n$ disjoint cycles solved by the
% permutation operation of cost $3$. This implies a 3-dimensional
% matching of size $n$.
\end{proof}