Skip to content

Commit b9ff5ec

Browse files
authored
Merge pull request #201 from cicirello/fix-reversal-dist-max
Fix ReversalDistance.max in the general case
2 parents 08284e1 + ff12488 commit b9ff5ec

File tree

3 files changed

+11
-22
lines changed

3 files changed

+11
-22
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## [Unreleased] - 2022-03-17
7+
## [Unreleased] - 2022-04-16
88

99
### Added
1010

@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
### Removed
1616

1717
### Fixed
18+
* ReversalDistance.max method now handles general case properly.
1819

1920
### CI/CD
2021

src/main/java/org/cicirello/permutations/distance/ReversalDistance.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public final class ReversalDistance implements NormalizedPermutationDistanceMeas
5252

5353
private byte[] dist;
5454
private final int PERM_LENGTH;
55-
private int maxd;
56-
55+
5756
/**
5857
* Construct the distance measure. Default handles permutations of length n=5.
5958
*/
@@ -71,7 +70,6 @@ public ReversalDistance(int n) {
7170
if (n > 12 || n < 0) throw new IllegalArgumentException("Requires 0 <= n <= 12.");
7271
PERM_LENGTH = n;
7372
int fact = 1;
74-
maxd = 0;
7573
for (int i = 2; i <= n; i++) fact *= i;
7674
dist = new byte[fact];
7775
final byte BYTE_MAX = 0x7f;
@@ -83,7 +81,6 @@ public ReversalDistance(int n) {
8381
int v = p.toInteger();
8482
p.reverse(i,j);
8583
dist[v] = 1;
86-
maxd = 1;
8784
}
8885
}
8986
int visited = n * (n-1) / 2 + 1;
@@ -99,7 +96,7 @@ public ReversalDistance(int n) {
9996
int v = p.toInteger();
10097
p.reverse(i,j);
10198
if (v > 0 && dist[v]==BYTE_MAX) {
102-
maxd = dist[v] = (byte)(d + 1);
99+
dist[v] = (byte)(d + 1);
103100
visited++;
104101
}
105102
}
@@ -129,19 +126,16 @@ public int distance(Permutation p1, Permutation p2) {
129126
return dist[new Permutation(r2).toInteger()];
130127
}
131128

132-
/**
133-
* {@inheritDoc}
134-
*
135-
* @throws IllegalArgumentException if length is not equal to the
136-
* the permutation length for which this was configured at time
137-
* of construction.
138-
*/
139129
@Override
140130
public int max(int length) {
141-
if (PERM_LENGTH != length) {
142-
throw new IllegalArgumentException("This distance measurer was not configured for length: " + length);
131+
if (length > 1) {
132+
// Source: Bafna, V.; Pevzner, P.A. Genome Rearrangements and Sorting
133+
// by Reversals. SIAM Journal on Computing 1996, 25, 272–289. 570
134+
// doi:10.1137/S0097539793250627.
135+
return length - 1;
136+
} else {
137+
return 0;
143138
}
144-
return maxd;
145139
}
146140

147141
}

src/test/java/org/cicirello/permutations/distance/PermutationDistanceMaxTests.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ public void testReversalDistance() {
4343
assertEquals(2, d.max(3));
4444
d = new ReversalDistance(4);
4545
assertEquals(3, d.max(4));
46-
47-
final ReversalDistance df = d;
48-
IllegalArgumentException thrown = assertThrows(
49-
IllegalArgumentException.class,
50-
() -> df.max(7)
51-
);
5246
}
5347

5448
@Test

0 commit comments

Comments
 (0)