|
| 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>K-Cycle distance is the count of the number of non-singleton permutation cycles |
| 28 | + * of length at most K. Specifically, each non-singleton cycle contributes to the |
| 29 | + * total distance the number of cycles of length at most K necessary to transform |
| 30 | + * the cycle to all fixed points. K-cycle distance is a metric provided that K ≤ 4. |
| 31 | + * However, if K > 4, it is only a semi-metric because it fails to satisfy the |
| 32 | + * triangle inequality when K ≥ 5.</p> |
| 33 | + * |
| 34 | + * <p>K-Cycle distance was introduced in the following article:</p> |
| 35 | + * |
| 36 | + * <p>Vincent A. Cicirello. 2022. <a href="https://www.cicirello.org/publications/applsci-12-05506.pdf">Cycle |
| 37 | + * Mutation: Evolving Permutations via Cycle Induction</a>, <i>Applied Sciences</i>, 12(11), Article 5506 (June 2022). |
| 38 | + * doi:<a href="https://doi.org/10.3390/app12115506">10.3390/app12115506</a></p> |
| 39 | + * |
| 40 | + * <p>Runtime: O(n), where n is the permutation length.</p> |
| 41 | + * |
| 42 | + * @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>, |
| 43 | + * <a href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a> |
| 44 | + */ |
| 45 | +public final class KCycleDistance implements NormalizedPermutationDistanceMeasurer { |
| 46 | + |
| 47 | + private final int maxCycleLength; |
| 48 | + |
| 49 | + private int lastLength; |
| 50 | + private int precomputedMax; |
| 51 | + |
| 52 | + /** |
| 53 | + * Constructs the distance measurer as specified in the class documentation. |
| 54 | + * |
| 55 | + * @param k The maximum length cycle that is considered an atomic edit operation, such that k is greater than or |
| 56 | + * equal to 2. |
| 57 | + * |
| 58 | + * @throws IllegalArgumentException if k is less than 2 |
| 59 | + */ |
| 60 | + public KCycleDistance(int k) { |
| 61 | + if (k < 2) { |
| 62 | + throw new IllegalArgumentException("k must be at least 2"); |
| 63 | + } |
| 64 | + this.maxCycleLength = k; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * {@inheritDoc} |
| 69 | + * |
| 70 | + * @throws IllegalArgumentException if p1.length() is not equal to p2.length(). |
| 71 | + */ |
| 72 | + @Override |
| 73 | + public int distance(Permutation p1, Permutation p2) { |
| 74 | + if (p1.length() != p2.length()) { |
| 75 | + throw new IllegalArgumentException("Permutations must be the same length"); |
| 76 | + } |
| 77 | + boolean[] used = new boolean[p1.length()]; |
| 78 | + for (int k = 0; k < used.length; k++) { |
| 79 | + if (p1.get(k) == p2.get(k)) { |
| 80 | + used[p1.get(k)] = true; |
| 81 | + } |
| 82 | + } |
| 83 | + int i = 0; |
| 84 | + for (i = 0; i < used.length; i++) { |
| 85 | + if (!used[p1.get(i)]) { |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + int[] invP1 = p1.getInverse(); |
| 91 | + int cycleCount = 0; |
| 92 | + int iLast = i; |
| 93 | + |
| 94 | + while (i < used.length) { |
| 95 | + int j = p1.get(i); |
| 96 | + int cycleLength = 0; |
| 97 | + while (!used[j]) { |
| 98 | + used[j] = true; |
| 99 | + cycleLength++; |
| 100 | + j = p2.get(i); |
| 101 | + i = invP1[j]; |
| 102 | + } |
| 103 | + |
| 104 | + if (cycleLength > maxCycleLength) { |
| 105 | + cycleCount += (int)Math.ceil((cycleLength-1.0)/(maxCycleLength-1.0)); |
| 106 | + } else { |
| 107 | + cycleCount++; |
| 108 | + } |
| 109 | + |
| 110 | + for (i = iLast + 1; i < used.length; i++) { |
| 111 | + if (!used[p1.get(i)]) { |
| 112 | + break; |
| 113 | + } |
| 114 | + } |
| 115 | + iLast = i; |
| 116 | + } |
| 117 | + return cycleCount; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public int max(int length) { |
| 122 | + if (length != lastLength) { |
| 123 | + lastLength = length; |
| 124 | + precomputedMax = Math.max( |
| 125 | + length >> 1, |
| 126 | + (int)Math.ceil((length-1.0)/(maxCycleLength-1.0)) |
| 127 | + ); |
| 128 | + } |
| 129 | + return precomputedMax; |
| 130 | + } |
| 131 | +} |
0 commit comments