This section is devoted to proving our two main results, namely the fact that sorting signed linear permutations by cyclic SSOs is in \(\mathsf {P}\) (Theorem 1), and, consequently, that sorting signed circular permutations by SSOs is also in \(\mathsf {P}\) (Theorem 2). For this, we study in depth (and provide properties of) sorting signed linear permutations by cyclic SSOs, which heavily rely on the cp-graph we introduced in “Preliminaries and notations” section.
Properties of VD-vectors
Before we provide a series of lemmas that will lead to our final algorithm, we begin with the three following properties, which will prove useful in this section.
In Property 2 we will show that if a VD-vector X has a displacement value \(x_i\) whose absolute value is greater than or equal to n, then there is a crossing value \(c_{ij}(X)\) (in absolute value) greater than one. In Property 3 we will show that if a VD-vector X has a crossing value \(c_{ij}(X)\) greater than zero, then elements \(\pi _k\), \(k \in [i..j]\), must be in the same component in its corresponding cp-graph. Property 4 is an extension of Property 3, where we show that if a VD-vector X has a crossing value \(c_{ij}(X)\) (in absolute value) greater than one, then all elements are in the same component in its corresponding cp-graph.
Property 2
Let
\(X = (x_1, x_2, ..., x_n) \in \mathbb {Z}^n\)
be a VD-vector for
\(\pi\)
. If there exists
\(1\le i\le n\)
such that
\(|x_i| \ge n\)
, then there exists
\(1\le j\ne i\le n\)
such that
\(|c_{ij}(X)| > 1.\)
Proof
Let \(X \in \mathbb {Z}^n\) be a VD-vector for \(\pi\), and let us suppose that there exists \(1\le i\le n\) such that \(|x_i| \ge n\). We know, by definition, that \(x_i=\sum _{j=1, j\ne i}^{n} c_{ij}(X)\). Since there are \(n-1\) crossing values of the form \(c_{ij}(X)\) (one for each \(j\ne i\)), this necessarily implies that \(|c_{ij}(X)| > 1\) for some \(1\le j\ne i\le n\). \(\square\)
Property 3
Let
\(X = (x_1, x_2, ..., x_n) \in \mathbb {Z}^n\)
be a VD-vector for
\(\pi\)
. If
\(c_{ij}(X) > 0\)
(resp.
\(c_{ij}(X) < 0\)
) for some
\(1\le i\ne j\le n\)
, then, for any
\(k \in [i+1..j-1]\)
(resp.
\(k \in [j+1..i-1]\)
), we have
\(|c_{ik}(X)| + |c_{jk}(X)| \ne 0\)
and
\(\{\{\pi _i,\pi _k\},\{\pi _j,\pi _k\}\} \cap E(G^X_{\pi }) \ne \emptyset.\)
Proof
Let X be a VD-vector such that for two elements \(\pi _i\) and \(\pi _j\), \(1\le i\ne j\le n\), \(c_{ij}(X) \ne 0\). Since \(c_{ij}(X) = -c_{ji}(X)\), let us suppose, without loss of generality, that \(c_{ij}(X) = \gamma\) with \(\gamma \ge 1\). Let \(r_1 = i - j\) and \(s_1 = (i + x_i) - (j + x_j)\). Since \(c_{ij}(X)\) is positive we have, by definition of crossing value, that \(r_1 \le s_1\).
Suppose first that \(i < j\). Since \(r_1 = i - j < 0\) and \(r_1 \le s_1\), then \(s_1 > \gamma -1\), otherwise \(c_{ij}(X) < \gamma\). We have that \(x_i > x_j +j -i + \gamma - 1\). Suppose that we have an element \(\pi _k\) with \(i< k < j\) such that \(c_{ik}(X) = c_{jk}(X) = 0\). For \(c_{ik}(X)\), we have that \(r_2 = i - k < 0\), so \(s_2 = (i + x_i) - (k + x_k) < 0\), otherwise \(c_{ik}(X) \ne 0\). It follows that \(x_k > x_i + i - k\), and, since \(x_i > x_j +j -i + \gamma - 1\), we have that \(x_k > x_j +j -k + \gamma - 1\). For \(c_{jk}(X)\), we have that \(r_3 = j - k > 0\), so \(s_3 = (j + x_j) - (k + x_k) > 0\), otherwise \(c_{jk}(X) \ne 0\). It follows that \(x_k < x_j + j - k\), which is a contradiction to the fact that \(x_k > x_j +j -k + \gamma - 1\) and \(\gamma > 0\), so we conclude that \(|c_{ik}(X)| + |c_{jk}(X)| \ne 0\).
Now let us suppose \(i > j\). In this case, we can split the interval that goes from i to j into \([i+1..n] \cup [1..j-1]\). Since \(r_1 = i - j > 0\) and \(r_1 \le s_1\), then \(s_1 \ge \gamma {n}\), otherwise \(c_{ij}(X) < \gamma\). It follows that \(x_i \ge x_j +j -i +\gamma {n}\). Suppose that we have an element \(\pi _k\) such that \(k \in [i+1..n] \cup [1..j-1]\) and \(c_{ik}(X) = c_{jk}(X) = 0\). We have to consider two cases: when \(k \in [1..j-1]\) (i.e., \(k < \min (i,j)\)) and when \(k \in [i+1..n]\) (i.e., \(k > \max (i,j)\)):
-
1.
\(k < \min (i,j)\): in this case, for both \(c_{ik}(X)\) and \(c_{jk}(X)\), we have that \(r_2 = i - k > 0\) and \(r_3 = j - k > 0\), so \(0< s_2 < n\) and \(0< s_3 < n\), otherwise \(|c_{ik}(X)| + |c_{jk}(X)| \ne 0\). For \(c_{ik}(X)\) and \(s_2 = (i + x_i) - (k + x_k) < n\), we have \(x_i + i - x_k - k < n\), resulting in \(x_k > x_i + i -k -n\). Since \(x_i \ge x_j +j -i +\gamma {n}\), we have that \(x_k > x_j + j -k+(\gamma -1)n\). For \(c_{jk}(X)\) and \(s_3 = (j + x_j) - (k + x_k) > 0\), we have \(x_j + j - x_k - k > 0\), resulting in \(x_k < x_j +j -k\), which is a contradiction to the fact that \(x_k > x_j + j -k +(\gamma -1)n\) and \((\gamma -1) \ge 0\).
-
2.
\(k > \max (i,j)\): in this case, for both \(c_{ik}(X)\) and \(c_{jk}(X)\), we have that \(r_2 = i - k < 0\) and \(r_3 = j - k < 0\), so \(-n< s_2 < 0\) and \(-n< s_3 < 0\), otherwise \(|c_{ik}(X)| + |c_{jk}(X)| \ne 0\). For \(c_{ik}(X)\) and \(s_2 = (i + x_i) - (k + x_k) < 0\), we have \(x_i + i - x_k - k < 0\), resulting in \(x_k > x_i + i -k\). Since \(x_i \ge x_j +j -i +\gamma {n}\), we have that \(x_k > x_j + j -k +\gamma {n}\). For \(c_{jk}(X)\) and \(s_3 = (j + x_j) - (k + x_k) > -n\), we have \(x_j + j - x_k - k > -n\), resulting in \(x_k < x_j +j -k +n\), which is a contradiction to the fact that \(x_k > x_j + j -k +\gamma {n}\) and \(\gamma \ge 1\).
In all cases, it follows that \(|c_{ik}(X)| + |c_{jk}(X)| \ne 0\), thus \(\{\{\pi _i,\pi _k\},\{\pi _j,\pi _k\}\} \cap E(G^X_{\pi }) \ne \emptyset\). \(\square\)
Property 4
Let
\(X = (x_1, x_2, ..., x_n) \in \mathbb {Z}^n\)
be a VD-vector for
\(\pi\)
. If there exists a
\(c_{ij}(X)\)
such that
\(|c_{ij}(X)| > 1\)
then
\(cc(G^X_{\pi })=1.\)
Proof
Let X be a VD-vector for some permutation \(\pi\) such that \(|c_{ij}(X)|> 1\) for some \(1\le i\ne j\le n\). Let us suppose, without loss of generality, that \(c_{ij}(X)>1\) (recall that \(c_{ij}(X)= -c_{ji}(X)\) by definition), and for readability, let \(c_{ij}(X)=\gamma\). Let \(r_1 = i - j\) and \(s_1 = (i + x_i) - (j + x_j)\). Since \(c_{ij}(X)\) is positive we have, by definition of crossing value, that \(r_1 \le s_1\). Let us now consider two cases: either \(i < j\), or \(i > j\).
Suppose first \(i < j\). In this case, \(r_1 = i - j\) is such that \({-n}< r_1 < 0\). Since \(r_1 \le s_1\), we have that \(s_1 = (i + x_i) - (j + x_j) \ge (\gamma -1)n\) (we use (\(\gamma -1\)) because \(\gamma = |\{k \in [r_1..s_1] : k \equiv 0 \pmod {n}\}| = |\{k \in [r_1..0] : k \equiv 0 \pmod {n}\}| + |\{k \in [1..s_1] : k \equiv 0 \pmod {n}\}| = 1 + |\{k \in [1..s_1] : k \equiv 0 \pmod {n}\}|\), so \((\gamma - 1) = |\{k \in [1..s_1] : k \equiv 0 \pmod {n}\}|\) and \(s_1 \ge (\gamma -1)n\)), otherwise \(c_{ij}(X) < \gamma\). It follows that \(x_i \ge (\gamma -1)n + x_j +j -i\). Now suppose that we have an element \(\pi _k\) such that \(c_{ik}(X) = c_{jk}(X) = 0\). We have three cases:
-
1.
\(k< i < j\): in this case, \(r_2 = i - k > 0\) and \(r_3 = j - k > 0\), so we must have that \(0< s_2 < n\) and \(0< s_3 < n\), with \(s_2 = (i + x_i) - (k + x_k)\) and \(s_3 = (j + x_j) - (k + x_k)\). For \(s_2 < n\), we have \(x_k > x_i + i -k -n\), and since \(x_i \ge (\gamma -1)n + x_j +j -i\) we have that \(x_k > (\gamma -2)n +x_j +j -k\). For \(s_3 > 0\), we have \(x_k < x_j + j -k\), but this is not possible since \((\gamma -2)n \ge 0\).
-
2.
\(i< k < j\): in this case, \(r_2 = i - k < 0\) and \(r_3 = j - k > 0\), so \(-n< s_2 < 0\) and \(0< s_3 < n\), with \(s_2 = (i + x_i) - (k + x_k)\) and \(s_3 = (j + x_j) - (k + x_k)\). For \(s_2 < 0\), we have \(x_k > x_i + i -k\), and since \(x_i \ge (\gamma -1)n + x_j +j -i\) we have that \(x_k > (\gamma -1)n +x_j +j -k\). For \(s_3 > 0\), we have \(x_k < x_j + j -k\), but this is not possible since \((\gamma -1)n > 0\).
-
3.
\(i< j < k\): in this case, \(r_2 = i - k < 0\) and \(r_3 = j - k < 0\), so \(-n< s_2 < 0\) and \(-n< s_3 < 0\), with \(s_2 = (i + x_i) - (k + x_k)\) and \(s_3 = (j + x_j) - (k + x_k)\). For \(s_2 < 0\), we have \(x_k > x_i + i -k\), and since \(x_i \ge (\gamma -1)n + x_j +j -i\) we have that \(x_k > (\gamma -1)n +x_j +j -k\). For \(s_3 > -n\), we have \(x_k < x_j + j -k+n\), but this is not possible since \((\gamma -1)n \ge n\).
Now let us consider \(i > j\). In this case, \(r_1 = i - j\) is such that \(0< r_1 < n\). Since \(r_1 \le s_1\), we have that \(s_1 = (i + x_i) - (j + x_j) \ge \gamma {n}\), otherwise \(c_{ij}(X) < \gamma\). It follows that \(x_i \ge \gamma {n} + x_j +j -i\). Now suppose that we have an element \(\pi _k\) such that \(c_{ik}(X) = c_{jk}(X) = 0\). We also have three cases:
-
1.
\(k< j < i\): in this case, \(r_2 = i - k > 0\) and \(r_3 = j - k > 0\), so we must have that \(0< s_2 < n\) and \(0< s_3 < n\), with \(s_2 = (i + x_i) - (k + x_k)\) and \(s_3 = (j + x_j) - (k + x_k)\). For \(s_2 < n\), we have \(x_k > x_i + i -k -n\), and, since \(x_i \ge \gamma {n} + x_j +j -i\), we have that \(x_k > (\gamma -1)n +x_j +j -k\). For \(s_3 > 0\), we have \(x_k < x_j + j -k\), but this is not possible since \((\gamma -1)n > 0\).
-
2.
\(j< k < i\): in this case, \(r_2 = i - k > 0\) and \(r_3 = j - k < 0\), so \(0< s_2 < n\) and \(-n< s_3 < 0\), with \(s_2 = (i + x_i) - (k + x_k)\) and \(s_3 = (j + x_j) - (k + x_k)\). For \(s_2 < n\), we have \(x_k > x_i + i -k -n\), and, since \(x_i \ge \gamma {n} + x_j +j -i\), we have that \(x_k > (\gamma -1)n +x_j +j -k\). For \(s_3 > -n\), we have \(x_k < x_j + j -k+n\), but this is not possible since \((\gamma -1)n \ge n\).
-
3.
\(j< i < k\): in this case, \(r_2 = i - k < 0\) and \(r_3 = j - k < 0\), so \(-n< s_2 < 0\) and \(-n< s_3 < 0\), with \(s_2 = (i + x_i) - (k + x_k)\) and \(s_3 = (j + x_j) - (k + x_k)\). For \(s_2 < 0\), we have \(x_k > x_i + i -k\), and, since \(x_i \ge \gamma {n} + x_j +j -i\), we have that \(x_k > \gamma {n} +x_j +j -k\). For \(s_3 > -n\), we have \(x_k < x_j + j -k+n\), but this is not possible since \(\gamma {n} > n\).
It follows that if \(c_{ij}(X) > 1\), then, for any element \(\pi _k\) with \(k \not \in \{i,j\}\), we have that \(|c_{ik}(X)| + |c_{jk}(X)| \ne 0\), so \(\{\{\pi _i,\pi _k\},\{\pi _j,\pi _k\}\} \cap E(G^X_{\pi }) \ne \emptyset\). Since \(c_{ij}(X) > 1\), we also have that \(\{\pi _i,\pi _j\}\in G^X_{\pi }\), so \(cc(G^X_{\pi })=1\). \(\square\)
SSOs and the cp-graphs
In this section we provide five lemmas relating SSOs with cp-graphs. In Lemma 1 (resp. Lemma 2) we will analyze the difference in the number of odd components in the cp-graph when we apply a 1-reversal (resp. a swap, i.e., a 2-reversal or a 2-transposition) to the permutation \(\pi\). In Lemma 3 we will show that we can always apply a swap induced by X without increasing the number of odd components in the resulting cp-graph. Let \(|\mathcal {S}|\) denote the length of a sorting sequence \(\mathcal {S}\). In Lemma 4 (resp. Lemma 5) we will show that if a sorting sequence \(\mathcal {S}\) has an SSO that increases the number of odd components (resp. the weight of an edge), then there is another sorting sequence \(\mathcal {S'}\) with \(|\mathcal {S'}| \le |\mathcal {S}|\) such that \(\mathcal {S'}\) does not contain such SSOs.
Lemma 1
Let X and
\(X'\)
be two VD-vectors of
\(\mathbb {Z}^n\)
such that X is a VD-vector for
\(\pi\)
and
\(X'\)
is a VD-vector for
\(\pi ' = \pi \cdot \alpha\)
, where
\(\alpha\)
is a cyclic SSO. If
\(\alpha\)
is a 1-reversal, then
\(X' = X,\)
\(cn(X') = cn(X)\)
, and
\(\Delta {cc^-} = cc^-(G^{X'}_{\pi '}) - cc^-(G^X_{\pi }) \in \{-1, 1\}.\)
Proof
If \(\alpha\) is a 1-reversal, we have that \(|\pi '_i| = |\pi _i|\) for every \(1 \le i \le n\), which implies that \(X' = X\), \(cn(X') = cn(X)\), and \(cc(G^{X'}_{\pi '}) = cc(G^X_{\pi })\). Now if the connected component impacted by \(\alpha\) in \(G^X_{\pi }\) is even (resp. odd), then it will become odd (resp. even) in \(G^{X'}_{\pi '}\), thus \(\Delta {cc^-} \in \{-1, 1\}\). \(\square\)
Lemma 2
Let X and \(X'\) be two VD-vectors of \(\mathbb {Z}^n\) such that X is a VD-vector for \(\pi\) and \(X'\) is a VD-vector for \(\pi ' = \pi \cdot \alpha\), where \(\alpha\) is a cyclic SSO. If \(\alpha\) is a 2-reversal or a 2-transposition induced by X in \(\pi\), then \(cn(X') = cn(X)-1\) and either \(cc(G^{X'}_{\pi '}) = cc(G^X_{\pi })\) and \(\Delta {cc^-} = cc^-(G^{X'}_{\pi '}) - cc^-(G^X_{\pi }) = 0\) or \(cc(G^{X'}_{\pi '}) > cc(G^X_{\pi })\) and \(\Delta {cc^-} \in \{0,2\}\).
Proof
Since \(\alpha\) is a cyclic SSO induced by X, the crossing values between elements \(\pi _i\) and \(\pi _{i\pmod {n}+1}\) impacted by \(\alpha\) are different from zero, which implies that \(e = \{\pi _i, \pi _{i\pmod {n}+1}\} \in E(G^{X}_{\pi })\). By definition, in \(G^{X'}_{\pi '}\) this edge either decreases its weight by one or is removed, so \(cc(G^{X'}_{\pi '}) \ge cc(G^X_{\pi })\).
Suppose first \(cc(G^{X'}_{\pi '}) = cc(G^X_{\pi })\). This means that the SSO applied to \(\pi\) leaves the connected component \(C_{\alpha }\) to which it is applied in \(G^{X}_{\pi }\) unchanged. If the SSO is a 2-reversal (resp. a 2-transposition), two (resp. zero) elements inside \(C_{\alpha }\) have changed sign. In both cases, we have that \(\Delta {cc^-} = cc^-(G^{X'}_{\pi '}) - cc^-(G^X_{\pi }) = 0\).
Now let us suppose \(cc(G^{X'}_{\pi '}) > cc(G^X_{\pi })\). This means that \(C_{\alpha }\) has been split into two connected components \(C_{1}\) and \(C_{2}\), thus \(cc(G^{X'}_{\pi '}) = cc(G^X_{\pi })+1\). If the SSO is a 2-transposition, zero elements of \(\pi\) have changed sign. If the SSO is a 2-reversal, two elements changed sign such that one element is in \(C_{1}\) and the other is in \(C_{2}\). In both cases, if \(C_{\alpha }\) is odd then \(C_{1}\) and \(C_{2}\) have distinct parities, and \(\Delta {cc^-} = 0\); if \(C_{\alpha }\) is even then \(C_{1}\) and \(C_{2}\) have the same parity, and \(\Delta {cc^-} \in \{0, 2\}\). \(\square\)
At this point, we know by Lemma 1 that a 1-reversal always increases or decreases the number of odd components by one, and by Lemma 2 that a 2-reversal or a 2-transposition induced by X can only increase by two or leave the number of odd components unchanged in the cp-graph.
Lemma 3
Let
\(X \in \mathbb {Z}^n\)
be a VD-vector for
\(\pi\)
. If
\(cn(X) > 0\)
, it is always possible to find a cyclic SSO
\(\alpha\)
induced by X such that
\(X'\)
is a VD-vector for
\(\pi ' = \pi \cdot \alpha\)
and
\(\Delta {cc^-} = cc^-(G^{X'}_{\pi '}) - cc^-(G^X_{\pi }) = 0.\)
Proof
Let \(\alpha\) be a swap induced by X (recall that by definition, \(\alpha\) is either a 2-reversal or a 2-transposition), and let \(\pi ' = \pi \cdot \alpha\). Note that, since \(\alpha\) is induced by X, applying it to \(\pi\) necessarily decreases by one unit the weight of an edge from \(G^X_{\pi }\) in \(G^{X'}_{\pi '}\), thus \(cn(X') = cn(X)-1\) and \(X' \ne X\).
If \(cc(G^{X'}_{\pi '}) = cc(G^X_{\pi })\), then, by Lemma 2, we know that \(\Delta {cc^-}=0\) and we are done.
Otherwise, we necessarily have \(cc(G^{X'}_{\pi '}) > cc(G^{X}_{\pi })\). As shown in the proof of Lemma 2, if the component \(C_{\alpha }\) impacted by \(\alpha\) in \(G^X_{\pi }\) is odd, we know that \(\Delta {cc^-}=0\) and we are done. Now suppose that the component \(C_{\alpha }\) impacted by \(\alpha\) in \(G^X_{\pi }\) is even. Let us consider the two components obtained from \(C_{\alpha }\) after \(\alpha\) is applied. If both components are even, then trivially \(\Delta {cc^-} = 0\) and we are done again. Finally, if both components are odd, then we can replace \(\alpha\) by \(\alpha '\), where \(\alpha '\) (i) acts on the same elements of \(\pi\) as \(\alpha\), and (ii) is a 2-transposition (resp. a 2-reversal) if \(\alpha\) is a 2-reversal (resp. a 2-transposition). Note that \(\alpha '\) is induced by X, and that applying \(\alpha '\) also yields two connected components on \(V(C_{\alpha })\). Moreover, since 2-reversals change signs while 2-transpositions do not, the two components obtained in the new cp-graph after \(\alpha '\) is applied on \(\pi\) are both even. Thus \(\Delta {cc^-} = 0\) and \(\alpha '\) is the sought SSO. \(\square\)
Lemma 4
Let
\(\mathcal {S}\)
be a sequence of cyclic SSOs that sorts a permutation
\(\pi\)
, and let
\(X \in \mathbb {Z}^n\)
be its associated VD-vector. If
\(\mathcal {S}\)
is a minimum-length sequence of all sorting sequences induced by X, then
\(\mathcal {S}\)
does not contain SSOs that increase the number of odd components.
Proof
We will prove the following: if a sorting sequence \(\mathcal {S}\) for \(\pi\), of VD-vector X, contains a cyclic SSO that increases the number of odd components at some point in \(G^X_{\pi }\), then we can always find an alternate sorting sequence \(\mathcal {S'}\) for \(\pi\), also with associated VD-vector X, that contains no cyclic SSO that increases the number of odd components, and such that \(|\mathcal {S'}| < |\mathcal {S}|\).
Note that, in order to sort a permutation, we need to end up with a cp-graph with n even components. From Lemmas 1 and 2, we have that only 1-reversals can decrease the number of odd components. Then, \(\mathcal {S}\) contains at least \(cc^-(G^X_{\pi }) 1\)-reversals. Suppose that \(\mathcal {S}\) is a minimum-length sequence that sorts \(\pi\), and that \(\mathcal {S}\) has an SSO \(\alpha\) that increases the number of odd components.
If \(\alpha\) is a 1-reversal, then it is necessarily applied to an even component. Thus, the total number of 1-reversals of \(\mathcal {S}\) must be greater than or equal to \(cc^-(G^X_{\pi }) + 2\). In that case, let \(\mathcal {S'}= \mathcal {S} - \{\alpha ,\alpha '\}\), where \(\alpha '\) is a 1-reversal applied to the odd component created by \(\alpha\). Note that we apply the same sequence of swaps in \(\mathcal {S'}\) and \(\mathcal {S}\), so both sequences are induced by X.
If \(\alpha\) is a 2-reversal (resp. a 2-transposition), then, as shown in the proof of Lemma 2, it is necessarily applied to an even component \(C_{\alpha }\), transforming it into two odd components. Thus, the total number of 1-reversals in \(\mathcal {S}\) is greater than or equal to \(cc^-(G^X_{\pi }) + 2\). Let \(\mathcal {S'}\) be the sequence obtained from \(\mathcal {S}\) by changing \(\alpha\) into the 2-transposition (resp. 2-reversal) acting on the same elements as \(\alpha\), and by removing the two 1-reversals applied to the odd components created by \(\alpha\). Because \(\alpha\) has been transformed into a 2-transposition (resp. a 2-reversal), it now creates two even components from \(C_{\alpha }\). Note that we apply the same sequence of swaps in \(\mathcal {S'}\) and \(\mathcal {S}\) (in this case they differ only at the type of swap but it uses the same pair of elements), so both sequences are induced by X.
In the above cases, the new sequence \(\mathcal {S'}\) is also a sorting sequence for \(\pi\), and of length \(|\mathcal {S'}| = |\mathcal {S}| - 2\), a contradiction to the fact that \(\mathcal {S}\) is of minimum length. Thus \(\mathcal {S}\) does not contain SSOs that increase the number of odd components. \(\square\)
Lemma 5
Given a permutation
\(\pi\)
, let
\(\mathcal {S}\)
be a sequence of cyclic SSOs that sorts
\(\pi\)
, and let
\(X \in \mathbb {Z}^n\)
be its associated VD-vector. If
\(\mathcal {S}\)
is a minimum-length sequence of all sorting sequences induced by X, then
\(\mathcal {S}\)
only uses cyclic SSOs that do not increase the edge weights in
\(G^X_{\pi }.\)
Proof
We will prove the following: if a sorting sequence \(\mathcal {S}\) for \(\pi\), of VD-vector X, contains a cyclic SSO that increases the weight of an edge e at some point in \(G^X_{\pi }\), then we can always find an alternate sorting sequence \(\mathcal {S'}\) for \(\pi\), also with associated VD-vector X, that contains no cyclic SSO that increases the weight of an edge, and such that \(|\mathcal {S'}| \le |\mathcal {S}|\).
Suppose, without loss of generality, that a cyclic SSO in \(\mathcal {S}\) increases the weight of an edge e in the cp-graph, and consider the first such SSO, say \(\alpha\). Note that, since 1-reversals do not change the cp-graph, \(\alpha\) is necessarily a 2-reversal or a 2-transposition. Note also that since this swap increases the weight of an edge, it is not induced by X.
If \(\alpha\) is applied to two elements in the same component, then \(cc(G^{X'}_{\pi '}) = cc(G^X_{\pi })\) and \(cc^-(G^{X'}_{\pi '}) = cc^-(G^X_{\pi })\), with \(\pi ' = \pi \cdot \rho\).
Otherwise, \(\alpha\) is merging two components, say A and B, and \(cc(G^{X'}_{\pi '}) = cc(G^X_{\pi }) - 1\). If both components are odd, then the resultant component will be even, so \(cc^-(G^{X'}_{\pi '}) = cc^-(G^X_{\pi }) - 2\), and \(cc^-(G^{X'}_{\pi '}) = cc^-(G^{X}_{\pi })\) otherwise. Since we increase the weight of e, then \(cn(X') = cn(X)+1\). It follows that \(cn(X') + cc^-(G^{X'}_{\pi '}) \ge cn(X) + cc^-(G^X_{\pi }) - 1\).
Let \(\alpha '\) be the operation that decreases the weight of e at some point during the sorting such that \(\pi ''' = \pi ''\cdot \alpha '\), where \(\pi ''\) is the permutation with all operations before \(\alpha '\) in the sorting sequence applied. By Lemma 3 we have that \(\alpha '\) decreases the crossing number by one unit and keeps the same number of odd components so \(cn(X''') + cc^{-}(G^{X'''}_{\pi '''}) = cn(X'') + cc^{-}(G^{X''}_{\pi ''}) - 1\). It follows that both \(\alpha\) and \(\alpha '\) are decreasing the sum of crossing number and odd components by one unit at most.
Let \(\mathcal {S}' = \mathcal {S} - \{\alpha ,\alpha '\}\). If \(\alpha\) merged two odd components A and B, we add at the beginning of \(\mathcal {S}'\) two 1-reversals: one applied to any element \(\pi _i \in A\), the other to any \(\pi _j \in B\). As shown in the proof of Lemma 1, each 1-reversal here decreases the number of odd components by exactly one unit and keeps the same crossing number. It follows that the newly built sequence is not longer than \(\mathcal {S}\), sorts \(\pi\), and uses cyclic SSOs which never increase the weight of edges in the cp-graph, so the only swaps it contains are induced by X. \(\square\)
A polynomial-time algorithm for Sorting Signed Permutations by SSOs
In this section, we first provide a closed formula for computing the length of a sorting sequence of cyclic SSOs for signed linear permutations based on its associated VD-vector X. Then, we provide a polynomial-time algorithm for sorting signed circular permutations by SSOs.
Lemma 6
Let
\(\mathcal {S}\)
be a minimum-length sequence of cyclic SSOs that sorts a signed linear permutation
\(\pi\)
, and let X be its associated VD-vector. Then
\(d(\pi ) = cn(X) + cc^-(G^X_{\pi }).\)
Proof
Let us partition \(\mathcal {S}\) into two sequences \(\mathcal {S}_1\) and \(\mathcal {S}_2\) in which \(\mathcal {S}_1\) (resp. \(\mathcal {S}_2\)) contains all 1-reversals (resp. swaps) of \(\mathcal {S}\). In addition, since 1-reversals do not modify the order of elements in the permutation, we can assume, without loss of generality, that the swaps of \(\mathcal {S}_2\) are applied first. We will show that \(|\mathcal {S}_1| = cc^-(G^X_{\pi })\). To see this, suppose that we apply a swap (i.e., a 2-reversal or a 2-transposition) \(\alpha\) of \(\mathcal {S}_2\) in \(\pi\), obtaining a permutation \(\pi '\), and let \(\mathcal {S'}=\mathcal {S}-\{\alpha \}\), and \(X'\) its associated VD-vector. Then, by Lemma 3, we know that \(cc^-(G^{X'}_{\pi '})= cc^-(G^X_{\pi })\). In addition, by Lemma 4, the number of odd components is not increased by \(\mathcal {S}\) and, by Lemma 1, \(cc^-(G^X_{\pi })\) can be reduced only by 1-reversals.
Note that the sum of weights of edges in \(G^X_{\pi }\) is \(cn(X)\), and, by Lemma 2, applying any cyclic SSO \(\alpha \in \mathcal {S}_2\) either increases or decreases this sum by one unit, thus \(|\mathcal {S}_2| \ge cn(X)\). By Lemma 5, we can assume that \(\mathcal {S}_2\) contains no cyclic SSO that increases the weight of an edge, so it follows that \(|\mathcal {S}_2| = cn(X)\). \(\square\)
Lemma 6 shows us that the problem of sorting a signed permutation \(\pi\) by cyclic SSOs is equivalent to the following optimization problem: find a VD-vector \(X \in \mathbb {Z}^n\) for \(\pi\) which minimizes \(cn(X)+cc^-(G^X_{\pi })\).
We will now prove that finding such a VD-vector can be achieved in polynomial time. First, we will introduce Lemma 7, where we show that if a VD-vector X has a cp-graph with only one component, then any VD-vector \(X'\) with \(cn(X') \ge cn(X)\) has \(d(\pi ,X') \ge d(\pi ,X)\). Then, in Lemma 8, we will prove that any VD-vector \(X^*\) such that \(d(\pi ,X^*) = d(\pi )\) necessarily belongs to one of two sets that we will define. Finally, we will show in Theorem 1 that we can find a VD-vector \(X^*\) such that \(d(\pi ,X^*) = d(\pi )\) in polynomial time.
Lemma 7
Consider two VD-vectors X and
\(X'\)
of
\(\mathbb {Z}^n\)
for a signed linear permutation
\(\pi\)
, such that
\(cn(X') \ge cn(X)\)
. If
\(cc(G^X_{\pi }) = 1\)
, then
\(d(\pi ,X') \ge d(\pi ,X).\)
Proof
We know, by Lemma 3, that we can apply \(cn(X)\) induced swaps in \(\pi\) while keeping \(cc^-(G^X_{\pi })\) odd components. Using Lemmas 4 and 5, we have that \(d(\pi ,X) = cn(X)+cc^-(G^X_{\pi })\). Using the same argument, we have that \(d(\pi ,X') = cn(X')+cc^-(G^{X'}_{\pi })\).
Since \(cc(G^X_{\pi }) = 1\), then all elements from \(\pi\) are in the same component, and \(cc^-(G^X_{\pi }) = 1\) (resp. \(cc^-(G^X_{\pi }) = 0\)) if there is an odd (resp. even) number of negative elements in \(\pi\), and any \(X'\) is such that \(cc^-(G^{X'}_{\pi }) \ge cc^-(G^X_{\pi })\) (if \(\pi\) has an odd number of negative elements then for any VD-vector \(X'\) there is at least one odd component in \(G^{X'}_{\pi }\)).
Since we also have that \(cn(X') \ge cn(X)\), it follows that \(d(\pi ,X') = cn(X') + cc^-(G^{X'}_{\pi }) \ge cn(X)+cc^-(G^X_{\pi })\), and the lemma follows. \(\square\)
Let \(cn(\pi ) = \min (cn(X) :~X \text{ is } \text{ a } \text{ VD-vector } \text{ for } \pi )\), i.e., \(cn(\pi )\) is the minimum crossing number over all VD-vectors for \(\pi\).
Lemma 8
Let S be the set of all VD-vectors
\(X \in \mathbb {Z}^n\)
such that
\(cn(X) = cn(\pi )\)
, and let
\(S'\)
be the set of all VD-vectors
\(X' \not \in S\)
such that
\(X' = T_{i,j}(X)\)
, for some
\(i,j\in [1..n]\)
with
\(i\ne j\)
. Then there exists a VD-vector
\(X^*\in S \cup S'\)
such that
\(d(\pi ,X) = d(\pi).\)
Proof
Recall that for any VD-vector \(X'' \not \in S\) there is a strictly contracting transformation that we can apply. Consider a sequence of strictly contracting transformations applied to \(X''\) until we reach a VD-vector \(X \in S\). Here we will prove by contradiction that there is always a VD-vector \(X'\) in this sequence such that \(X' \in S \cup S'\) and \(d(X',\pi ) < d(X'',\pi )\), i.e., every VD-vector \(X''\) outside \(S \cup S'\) has \(d(\pi ,X'') > d(\pi )\).
If there is a VD-vector \(X \in S\) such that \(cc(G^X_{\pi }) = 1\), then, by Lemma 7, for any \(X' \not \in S\) such that \(cn(X') \ge cn(X)+1\), we have \(d(\pi ,X') \ge d(\pi ,X)+1\), and it follows that the VD-vector \(X^*\) with \(d(\pi ,X^*) = d(\pi )\) is such that \(X^* \in S\).
Now suppose that a VD-vector \(X \in S\) is such that \(cc(G^X_{\pi }) \ge 2\). Since \(X \in S\), X does not admit a strictly contracting transformation, so, for any two distinct \(x_a,x_b \in X\), we have that \(x_a - x_b \le n\).
Let \(X' \in S'\) be a VD-vector such that \(X' = T_{i,j}(X)\) for some \(1\le i\ne j\le n\), and let \(X''\) be a VD-vector such that \(X'' = T_{k,l}(X')\) for some \(1\le k\ne l\le n\). As we can see in Fig. 2, if \(k = j\) and \(l=i\) then \(X'' = X\) and \(X'' \in S\). If \(k = j\) (resp. \(l=i\)), then \(X'' = T_{i,l}(X)\) (resp. \(X'' = T_{k,j}(X)\)) and \(X'' \in S'\).
Suppose now that \(X \in S\), \(X' \in S'\), and \(X'' = T_{k,l}(X')\) is such that \(X'' \not \in S \cup S'\). Since \(X'' \not \in S \cup S'\), then \(i \not \in \{k,l\}\) and \(j \not \in \{k,l\}\), and we have that \(x'_k = x_k\) and \(x'_l = x_l\). This means that, as shown in Fig. 2, \(X''\) can be obtained by four different transformations of VD-vectors from \(S'\): \(T_{k,l}(X')\), \(T_{k,j}(Y')\), \(T_{i,j}(Z')\), and \(T_{i,l}(W')\), such that \(X' = T_{i,j}(X)\), \(Y' = T_{i,l}(X)\), \(Z' = T_{k,l}(X)\), and \(W' = T_{k,j}(X)\). The VD-vectors \(X', Y', Z'\), and \(W'\) are in \(S'\), and we say they are adjacent to \(X''\).
Recall that, since \(X \in S\) and \(X'' \not \in S \cup S'\), we have that \(x_i - x_j < n\), \(x_i - x_l < n\), \(x_k - x_j < n\), and \(x_k - x_l < n\), otherwise at least one VD-vector between \(W'\), \(X'\), \(Y'\), and \(Z'\) would be generated by a contracting transformation and is in S, and as a consequence \(X'' \in S'\). It follows that \(cn(X'')> cn(X') > cn(X)\).
Let us suppose that for any VD-vector \(V\in S'\) adjacent to \(X''\) we have \(d(\pi ,X'')<d(\pi ,V)\). Using Lemma 7 we have that values \(cc(G^{X'}_{\pi })\), \(cc(G^{Y'}_{\pi })\), \(cc(G^{Z'}_{\pi })\), and \(cc(G^{W'}_{\pi })\) must be strictly greater than 1, otherwise \(d(\pi ,X'') \ge d(\pi ,V)\). This observation implies that values \(c_{ij}(X)\), \(c_{il}(X)\), \(c_{kj}(X)\), and \(c_{kl}(X)\) are all equal to 1 (recall that, by definition of transformation, if \(X' = T_{i,j}(X)\), then \(c_{ij}(X') = c_{ij}(X)-2\)), otherwise, in at least one of the four VD-vectors in \(S'\) adjacent to \(X''\) there is a crossing value (in absolute value) greater than one, and, by Property 4, the corresponding cp-graph would have only one connected component.
Hence, we conclude that the four elements \(\pi _i,\pi _j,\pi _k\), and \(\pi _l\) are in the same component in \(G^X_{\pi }\). For the same reason, we have that \(x_i,x_k > 0\) and \(x_j,x_l < 0\), otherwise at least one of the four VD-vectors in \(S'\) contains a displacement value involved in the transformation whose absolute value is greater than or equal to n (recall that, by definition of transformation, if \(X' = T_{i,j}(X)\), then \(x'_{i} = x_i - n\) and \(x'_j = x_j + n\)), which implies, by Property 2, that this VD-vector has a crossing value with absolute value greater than 1 and, by Property 4, that the cp-graph of this VD-vector has only one component.
Now we argue that \(X''\) cannot be a vector such that \(d(\pi ,X'') = d(\pi )\). Note that since \(c_{ij}(X)=c_{il}(X)=c_{kj}(X)=c_{kl}(X) = 1\) we know, by Property 3, that all elements \(\pi _a\) with \(a \in [i..j] \cup [i..l] \cup [k..j] \cup [k..l]\) must be in the same component. Suppose, without loss of generality, that \(i < k\) and \(j < l\). We show in Fig. 3 all the possible configurations for these intervals, depending on their relative positions. We can see that we always have either (i) a VD-vector X with only one component, so by Lemma 7 \(d(\pi ,X) \le cn(X'')\) (Fig. 3a–f), or (ii) a VD-vector \(X'\) obtained from X with only one component, so by Lemma 7 \(d(\pi ,X') \le cn(X'')\) (Fig. 3g–j), and it follows that \(d(\pi ,X'') > d(\pi )\). Thus the VD-vector \(X^*\) with \(d(\pi ,X^*) = d(\pi )\) necessarily satisfies \(X^* \in S \cup S'\). \(\square\)
Theorem 1
Finding a VD-vector X for
\(\pi\)
that minimizes
\(cn(X) + cc^-(G^X_{\pi })\)
can be achieved in polynomial time, and thus sorting signed linear permutations by cyclic SSOs is in
\(\mathsf {P}.\)
Proof
Given a permutation \(\pi\), we first compute a VD-vector X (lines 1–3 of Algorithm 1), then iteratively apply strictly contracting transformations \(T_{i,j}(x)\) (lines 4–7) until none exists.
Now let S be the set with all VD-vectors X such that \(cn(X) = cn(\pi )\). Jerrum [13] proved that (i) when no further strictly contracting transformations can be performed on a VD-vector X, we have that \(cn(X) = cn(\pi )\); (ii) for any two VD-vectors X and \(X'\) such that \(cn(X) = cn(X') = cn(\pi )\), we can go from X to \(X'\) by a sequence of contracting transformations, i.e., we do not need to go through a VD-vector that is not in S. The above properties are in the context of VD-vectors for unsigned permutations, but since VD-vectors for signed permutations do not take into account the signs of the elements we have that (i) and (ii) also apply in our context.
By (i), we have that \(X \in S\) (line 9), and, by (ii), we know that we can use X to generate the remaining VD-vectors that are also in S (lines 10–16). If there is a VD-vector \(X \in S\) such that \(cc(G^X_{\pi }) = 1\) then, by Lemma 7, we have that for any VD-vector \(X' d(\pi ,X') \ge d(\pi ,X)\), so \(d(\pi ) = d(\pi ,X)\) and we can just return this value as the sorting distance (line 15). Otherwise, by Lemma 8 the VD-vector \(X^*\) with \(d(\pi ,X^*) = d(\pi )\) satisfies \(X^* \in S \cup S'\), where \(S'\) is the set of all VD-vectors obtained by some \(T_{i,j}(X)\), with \(1\le i\ne j\le n\) and \(X \in S\) (instructions in lines 17–21 generate all the VD-vectors of \(S'\)).
Now we argue that the set of instructions in lines 10–16 of Algorithm 1 either provides all VD-vectors of S or at least a VD-vector \(X' \in S\) such that \(cc(G^{X'}_{\pi }) = 1\). Note that if it provides all VD-vectors of S, we just need to generate \(S'\) and find the VD-vector \(X^*\) with \(d(\pi ,X^*) = d(\pi )\). Otherwise, if we have a VD-vector \(X' \in S\) such that \(cc(G^{X'}_{\pi }) = 1\) then, by Lemma 7, we have that, for any VD-vector \(X^*\), \(d(\pi ,X^*) \ge d(\pi ,X')\), so \(d(\pi ) = d(\pi ,X')\). Arguing by contradiction we show that this set of instructions always leads to one of these two situations.
Suppose that the instructions in lines 10–16 do not provide all VD-vectors in S and that, for all VD-vectors X provided, we have \(cc(G^{X}_{\pi }) > 1\). We have the following facts:
-
Since, for any \(X \in S\), \(cc(G^{X}_{\pi }) > 1\), we must have, by Property 4, that \(c_{ij}(X) \in \{-1,0,+1\}\), with \(1 \le i \ne j \le n\).
-
For a VD-vector \(X'\) such that \(X' = T_{i,j}(X)\) with \(X \in S\), if \(X' \in S\), then \(cn(X') = cn(X)\), which implies, by Property 1, that \(x_i - x_j = n\).
-
Since, for any \(X \in S\), \(\max (X)-\min (X)\le n\), if we have \(x_i-x_j=n\) and \(x_k-x_l=n\) with \(i \ne k\) and \(j\ne l\), then \(x_i = x_k = \max (X)\) and \(x_j = x_l = \min (X)\) (otherwise we have that \(x_i-x_l>n\) or \(x_k-x_j>n\)).
-
Note that, by definition of displacement value, \(x_k=\sum \nolimits _{l=1}^{n} c_{kl}(X)\). Since, for any \(X \in S\), we have \(c_{kl}(X) \in \{-1,0,+1\}\), it follows, by definition of cp-graph, that \(x_i\) (resp. \(x_j\)) is located in a component with at least \(|x_i|+1\) (resp. \(|x_j|+1\)) elements on \(G^{X}_{\pi }\). If \(x_i - x_j = n\) then \(x_i\) and \(x_j\) must be in the same component, otherwise \(G^{X}_{\pi }\) would have at least \(|x_i| + |x_j| + 2 > n\) vertices. Besides, this component has at least \(\max (|x_i|,|x_j|)+1 \ge \frac{n}{2}+1\) vertices. For the same reason, if we have more than one distinct pair of elements \(x_i,x_j\) such that \(x_i - x_j = n\), then all these pairs must be in the same component.
Suppose now that we have a VD-vector \(X''\) such that \(cn(X'') = cn(\pi )\) (which implies that \(X'' \in S\)), and suppose that \(X''\) cannot be obtained by one contracting transformation on X (i.e., Algorithm 1 cannot generate \(X''\)). We will show that if \(X''\) requires at least two contracting transformations then X admits a contracting transformation using only indices from the first and the second transformation such that the resulting VD-vector \(X^*\) has only one component, and, by Lemma 7, \(X^*\) already has \(d(\pi ,X'') = d(\pi )\).
Let us assume then, without loss of generality, that \(X'' = T_{k,l}(T_{i,j}(X))\), where \(T_{i,j}\) and \(T_{k,l}\) are two distinct contracting transformations (i.e., \(x_i - x_j = x_k - x_l = n\) with \(i\ne l\) and \(j\ne k\)). In a similar way as explained in the proof of Lemma 8, this means that the VD-vector \(X''\) can be reached by four distinct pairs of transformations (namely \(T_{k,l}(T_{i,j}(X))\), \(T_{k,j}(T_{i,l}(X))\), \(T_{i,l}(T_{k,j}(X))\), and \(T_{i,j}(T_{k,l}(X))\)). However, by Property 1, and using the above mentioned facts, we can conclude that \(x_i = x_k = \max (X)\) and \(x_j = x_l = \min (X)\), so these four intermediate VD-vectors between X and \(X''\) are also in S, since these transformations are contracting transformations.
Since these four vectors are generated by Algorithm 1 and are in the set S, and since, for any \(X \in S\), we assumed that \(cc(G^{X}_{\pi }) > 1\), we have, by Property 4, that \(c_{ij}(X)=c_{il}(X)=c_{kj}(X)=c_{kl}(X) = 1\). Now we can use Fig. 3 again to show that, with the above properties and whatever the order in which elements \(x_i\), \(x_j\), \(x_k\), and \(x_l\) appear, we always have at least one vector \(X' \in S\) reachable from X by one contracting transformation such that \(cc(G^{X'}_{\pi }) = 1\), a contradiction to the fact that S has no VD-vector \(X'\) such that \(cc(G^{X'}_{\pi }) = 1\).
It follows that Algorithm 1 provides either all VD-vectors \(X \in S\) or at least a VD-vector \(X' \in S\) such that \(cc(G^{X'}_{\pi }) = 1\).
Algorithm 1 presents the above mentioned procedure, which consists in finding a VD-vector X that minimizes \(cn(X) + cc^-(G^X_{\pi })\), the latter value being the sought distance. We now turn to evaluating the computational complexity of Algorithm 1. Our primary goal is to ensure polynomiality of the algorithm, and our analysis can certainly be improved.
The loop in lines 1–2, line 3, and the loop in lines 4–7 run in linear time each. Line 8 takes \(O(n^2)\) time to compute \(cn(X)\) plus \(O(|V|+|E|)=O(n^2)\) time to compute \(cc^-(G^{X'}_{\pi })\), resulting in \(O(n^2)\) time. Line 9 runs in linear time. The loop in lines 10–16 runs in \(O(n^4)\) time: it iterates in \(O(n^2)\) and line 12 runs in \(O(n^2)\) time. The loop in lines 17–21 runs in \(O(n^6)\) time: it iterates in \(O(n^4)\) and line 20 runs in \(O(n^2)\) time. The overall running time complexity of our algorithm is then \(O(n^6)\).
An example where Algorithm 1 does not generate all VD-vectors in S is given in Fig. 4, and an example where Algorithm 1 generates all VD-vectors in \(S \cup S'\) is given in Fig. 5. \(\square\)
Theorem 1 shows that computing the sorting distance for signed permutations, using cyclic SSOs, is in \(\mathsf {P}\). From this, one can easily derive a polynomial-time algorithm for sorting signed circular permutations by SSOs: it suffices to cut the circular permutation (n different cuts are possible), and to decide which extremity is left and which is right (2 possible cases). Once this is done, we are left with a linear permutation, which can be sorted using cyclic SSOs using Algorithm 1. The sorting distance is the minimum value obtained by Algorithm 1 over the 2n possible linear permutations obtained from the circular one. Thus we have the following result.
Theorem 2
Sorting signed circular permutations by SSOs is in
\(\mathsf {P}.\)
Computing a sorting sequence
Algorithm 1 only returns the length of a minimum-length sorting sequence, not the sequence itself. However, we can easily provide a minimum-length sequence using the cp-graph \(G^X_{\pi }\), where \(\pi\) is the input signed permutation and X is a VD-vector such that \(d(\pi ,X) = d(\pi )\). For this, we iteratively remove each edge from \(G^X_{\pi }\) by applying a swap over two adjacent elements connected by an edge, and choosing between a 2-reversal or a 2-transposition so that the number of odd components does not increase, as shown in Lemma 3. When \(G^X_{\pi }\) has no more edges, we just need to apply \(cc^-(G^X_{\pi }) 1\)-reversals over the remaining negative elements.