|
| 1 | +/* |
| 2 | + * JavaPermutationTools: A Java library for computation on permutations and sequences |
| 3 | + * Copyright 2005-2022 Vincent A. Cicirello, <https://www.cicirello.org/>. |
| 4 | + * |
| 5 | + * This file is part of JavaPermutationTools (https://jpt.cicirello.org/). |
| 6 | + * |
| 7 | + * JavaPermutationTools is free software: you can |
| 8 | + * redistribute it and/or modify it under the terms of the GNU |
| 9 | + * General Public License as published by the Free Software |
| 10 | + * Foundation, either version 3 of the License, or (at your |
| 11 | + * option) any later version. |
| 12 | + * |
| 13 | + * JavaPermutationTools is distributed in the hope |
| 14 | + * that it will be useful, but WITHOUT ANY WARRANTY; without even |
| 15 | + * the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 16 | + * PARTICULAR PURPOSE. See the GNU General Public License for more |
| 17 | + * details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU General Public License |
| 20 | + * along with JavaPermutationTools. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + */ |
| 22 | +package org.cicirello.permutations.distance; |
| 23 | + |
| 24 | +import org.cicirello.permutations.Permutation; |
| 25 | + |
| 26 | +/** |
| 27 | + * <p>Cycle distance is the count of the number of non-singleton permutation cycles |
| 28 | + * between a pair of permutations. Cycle distance is a semi-metric, satisfying all |
| 29 | + * of the metric properties except for the triangle inequality.</p> |
| 30 | + * |
| 31 | + * <p>It was introduced in the following article:</p> |
| 32 | + * |
| 33 | + * <p>Vincent A. Cicirello. 2022. <a href="https://www.cicirello.org/publications/applsci-12-05506.pdf">Cycle |
| 34 | + * Mutation: Evolving Permutations via Cycle Induction</a>, <i>Applied Sciences</i>, 12(11), Article 5506 (June 2022). |
| 35 | + * doi:<a href="https://doi.org/10.3390/app12115506">10.3390/app12115506</a></p> |
| 36 | + * |
| 37 | + * <p>Runtime: O(n), where n is the permutation length.</p> |
| 38 | + * |
| 39 | + * @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>, |
| 40 | + * <a href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a> |
| 41 | + */ |
| 42 | +public final class CycleDistance implements NormalizedPermutationDistanceMeasurer { |
| 43 | + |
| 44 | + /** |
| 45 | + * Constructs the distance measurer as specified in the class documentation. |
| 46 | + */ |
| 47 | + public CycleDistance() { |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * {@inheritDoc} |
| 53 | + * |
| 54 | + * @throws IllegalArgumentException if p1.length() is not equal to p2.length(). |
| 55 | + */ |
| 56 | + @Override |
| 57 | + public int distance(Permutation p1, Permutation p2) { |
| 58 | + if (p1.length() != p2.length()) { |
| 59 | + throw new IllegalArgumentException("Permutations must be the same length"); |
| 60 | + } |
| 61 | + boolean[] used = new boolean[p1.length()]; |
| 62 | + for (int k = 0; k < used.length; k++) { |
| 63 | + if (p1.get(k) == p2.get(k)) { |
| 64 | + used[p1.get(k)] = true; |
| 65 | + } |
| 66 | + } |
| 67 | + int i = 0; |
| 68 | + for (i = 0; i < used.length; i++) { |
| 69 | + if (!used[p1.get(i)]) { |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + int[] invP1 = p1.getInverse(); |
| 75 | + int cycleCount = 0; |
| 76 | + int iLast = i; |
| 77 | + |
| 78 | + while (i < used.length) { |
| 79 | + int j = p1.get(i); |
| 80 | + while (!used[j]) { |
| 81 | + used[j] = true; |
| 82 | + j = p2.get(i); |
| 83 | + i = invP1[j]; |
| 84 | + } |
| 85 | + cycleCount++; |
| 86 | + for (i = iLast + 1; i < used.length; i++) { |
| 87 | + if (!used[p1.get(i)]) { |
| 88 | + break; |
| 89 | + } |
| 90 | + } |
| 91 | + iLast = i; |
| 92 | + } |
| 93 | + return cycleCount; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public int max(int length) { |
| 98 | + return length >> 1; |
| 99 | + } |
| 100 | +} |
0 commit comments