Skip to content

Commit 3b7b483

Browse files
authored
Merge branch 'master' into needlemanwunsch
2 parents 2fa31fc + 061463a commit 3b7b483

File tree

6 files changed

+678
-0
lines changed

6 files changed

+678
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Implementation of the full Damerau–Levenshtein distance algorithm.
8+
*
9+
* This algorithm calculates the minimum number of operations required
10+
* to transform one string into another. Supported operations are:
11+
* insertion, deletion, substitution, and transposition of adjacent characters.
12+
*
13+
* Unlike the restricted version (OSA), this implementation allows multiple
14+
* edits on the same substring, computing the true edit distance.
15+
*
16+
* Time Complexity: O(n * m * max(n, m))
17+
* Space Complexity: O(n * m)
18+
*/
19+
public final class DamerauLevenshteinDistance {
20+
21+
private DamerauLevenshteinDistance() {
22+
// Utility class
23+
}
24+
25+
/**
26+
* Computes the full Damerau–Levenshtein distance between two strings.
27+
*
28+
* @param s1 the first string
29+
* @param s2 the second string
30+
* @return the minimum edit distance between the two strings
31+
* @throws IllegalArgumentException if either input string is null
32+
*/
33+
public static int distance(String s1, String s2) {
34+
validateInputs(s1, s2);
35+
36+
int n = s1.length();
37+
int m = s2.length();
38+
39+
Map<Character, Integer> charLastPosition = buildCharacterMap(s1, s2);
40+
int[][] dp = initializeTable(n, m);
41+
42+
fillTable(s1, s2, dp, charLastPosition);
43+
44+
return dp[n + 1][m + 1];
45+
}
46+
47+
/**
48+
* Validates that both input strings are not null.
49+
*
50+
* @param s1 the first string to validate
51+
* @param s2 the second string to validate
52+
* @throws IllegalArgumentException if either string is null
53+
*/
54+
private static void validateInputs(String s1, String s2) {
55+
if (s1 == null || s2 == null) {
56+
throw new IllegalArgumentException("Input strings must not be null.");
57+
}
58+
}
59+
60+
/**
61+
* Builds a character map containing all unique characters from both strings.
62+
* Each character is initialized with a position value of 0.
63+
*
64+
* This map is used to track the last occurrence position of each character
65+
* during the distance computation, which is essential for handling transpositions.
66+
*
67+
* @param s1 the first string
68+
* @param s2 the second string
69+
* @return a map containing all unique characters from both strings, initialized to 0
70+
*/
71+
private static Map<Character, Integer> buildCharacterMap(String s1, String s2) {
72+
Map<Character, Integer> charMap = new HashMap<>();
73+
for (char c : s1.toCharArray()) {
74+
charMap.putIfAbsent(c, 0);
75+
}
76+
for (char c : s2.toCharArray()) {
77+
charMap.putIfAbsent(c, 0);
78+
}
79+
return charMap;
80+
}
81+
82+
/**
83+
* Initializes the dynamic programming table for the algorithm.
84+
*
85+
* The table has dimensions (n+2) x (m+2) where n and m are the lengths
86+
* of the input strings. The extra rows and columns are used to handle
87+
* the transposition operation correctly.
88+
*
89+
* The first row and column are initialized with the maximum possible distance,
90+
* while the second row and column represent the base case of transforming
91+
* from an empty string.
92+
*
93+
* @param n the length of the first string
94+
* @param m the length of the second string
95+
* @return an initialized DP table ready for computation
96+
*/
97+
private static int[][] initializeTable(int n, int m) {
98+
int maxDist = n + m;
99+
int[][] dp = new int[n + 2][m + 2];
100+
101+
dp[0][0] = maxDist;
102+
103+
for (int i = 0; i <= n; i++) {
104+
dp[i + 1][0] = maxDist;
105+
dp[i + 1][1] = i;
106+
}
107+
108+
for (int j = 0; j <= m; j++) {
109+
dp[0][j + 1] = maxDist;
110+
dp[1][j + 1] = j;
111+
}
112+
113+
return dp;
114+
}
115+
116+
/**
117+
* Fills the dynamic programming table by computing the minimum edit distance
118+
* for each substring pair.
119+
*
120+
* This method implements the core algorithm logic, iterating through both strings
121+
* and computing the minimum cost of transforming substrings. It considers all
122+
* four operations: insertion, deletion, substitution, and transposition.
123+
*
124+
* The character position map is updated as we progress through the first string
125+
* to enable efficient transposition cost calculation.
126+
*
127+
* @param s1 the first string
128+
* @param s2 the second string
129+
* @param dp the dynamic programming table to fill
130+
* @param charLastPosition map tracking the last position of each character in s1
131+
*/
132+
private static void fillTable(String s1, String s2, int[][] dp, Map<Character, Integer> charLastPosition) {
133+
int n = s1.length();
134+
int m = s2.length();
135+
136+
for (int i = 1; i <= n; i++) {
137+
int lastMatchCol = 0;
138+
139+
for (int j = 1; j <= m; j++) {
140+
char char1 = s1.charAt(i - 1);
141+
char char2 = s2.charAt(j - 1);
142+
143+
int lastMatchRow = charLastPosition.get(char2);
144+
int cost = (char1 == char2) ? 0 : 1;
145+
146+
if (char1 == char2) {
147+
lastMatchCol = j;
148+
}
149+
150+
dp[i + 1][j + 1] = computeMinimumCost(dp, i, j, lastMatchRow, lastMatchCol, cost);
151+
}
152+
153+
charLastPosition.put(s1.charAt(i - 1), i);
154+
}
155+
}
156+
157+
/**
158+
* Computes the minimum cost among all possible operations at the current position.
159+
*
160+
* This method evaluates four possible operations:
161+
* 1. Substitution: replace character at position i with character at position j
162+
* 2. Insertion: insert character from s2 at position j
163+
* 3. Deletion: delete character from s1 at position i
164+
* 4. Transposition: swap characters that have been seen before
165+
*
166+
* The transposition cost accounts for the gap between the current position
167+
* and the last position where matching characters were found.
168+
*
169+
* @param dp the dynamic programming table
170+
* @param i the current position in the first string (1-indexed in the DP table)
171+
* @param j the current position in the second string (1-indexed in the DP table)
172+
* @param lastMatchRow the row index where the current character of s2 last appeared in s1
173+
* @param lastMatchCol the column index where the current character of s1 last matched in s2
174+
* @param cost the substitution cost (0 if characters match, 1 otherwise)
175+
* @return the minimum cost among all operations
176+
*/
177+
private static int computeMinimumCost(int[][] dp, int i, int j, int lastMatchRow, int lastMatchCol, int cost) {
178+
int substitution = dp[i][j] + cost;
179+
int insertion = dp[i + 1][j] + 1;
180+
int deletion = dp[i][j + 1] + 1;
181+
int transposition = dp[lastMatchRow][lastMatchCol] + i - lastMatchRow - 1 + 1 + j - lastMatchCol - 1;
182+
183+
return Math.min(Math.min(substitution, insertion), Math.min(deletion, transposition));
184+
}
185+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
/**
4+
* Smith–Waterman algorithm for local sequence alignment.
5+
* Finds the highest scoring local alignment between substrings of two sequences.
6+
*
7+
* Time Complexity: O(n * m)
8+
* Space Complexity: O(n * m)
9+
*/
10+
public final class SmithWaterman {
11+
12+
private SmithWaterman() {
13+
// Utility Class
14+
}
15+
16+
/**
17+
* Computes the Smith–Waterman local alignment score between two strings.
18+
*
19+
* @param s1 first string
20+
* @param s2 second string
21+
* @param matchScore score for a match
22+
* @param mismatchPenalty penalty for mismatch (negative)
23+
* @param gapPenalty penalty for insertion/deletion (negative)
24+
* @return the maximum local alignment score
25+
*/
26+
public static int align(String s1, String s2, int matchScore, int mismatchPenalty, int gapPenalty) {
27+
if (s1 == null || s2 == null) {
28+
throw new IllegalArgumentException("Input strings must not be null.");
29+
}
30+
31+
int n = s1.length();
32+
int m = s2.length();
33+
int maxScore = 0;
34+
35+
int[][] dp = new int[n + 1][m + 1];
36+
37+
for (int i = 1; i <= n; i++) {
38+
for (int j = 1; j <= m; j++) {
39+
int matchOrMismatch = (s1.charAt(i - 1) == s2.charAt(j - 1)) ? matchScore : mismatchPenalty;
40+
41+
dp[i][j] = Math.max(0,
42+
Math.max(Math.max(dp[i - 1][j - 1] + matchOrMismatch, // match/mismatch
43+
dp[i - 1][j] + gapPenalty // deletion
44+
),
45+
dp[i][j - 1] + gapPenalty // insertion
46+
));
47+
48+
if (dp[i][j] > maxScore) {
49+
maxScore = dp[i][j];
50+
}
51+
}
52+
}
53+
54+
return maxScore;
55+
}
56+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package com.thealgorithms.maths;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Implementation of the Sieve of Atkin, an optimized algorithm to generate
8+
* all prime numbers up to a given limit.
9+
*
10+
* The Sieve of Atkin uses quadratic forms and modular arithmetic to identify
11+
* prime candidates, then eliminates multiples of squares. It is more efficient
12+
* than the Sieve of Eratosthenes for large limits.
13+
*/
14+
public final class SieveOfAtkin {
15+
16+
private SieveOfAtkin() {
17+
// Utlity class; prevent instantiation
18+
}
19+
20+
/**
21+
* Generates a list of all prime numbers up to the specified limit
22+
* using the Sieve of Atkin algorithm.
23+
*
24+
* @param limit the upper bound up to which primes are generated; must be zero or positive
25+
* @return a list of prime numbers up to the limit; empty if the limit is less than 2
26+
*/
27+
public static List<Integer> generatePrimes(int limit) {
28+
if (limit < 1) {
29+
return List.of();
30+
}
31+
32+
boolean[] sieve = new boolean[limit + 1];
33+
int sqrtLimit = (int) Math.sqrt(limit);
34+
35+
markQuadraticResidues(limit, sqrtLimit, sieve);
36+
eliminateMultiplesOfSquares(limit, sqrtLimit, sieve);
37+
38+
List<Integer> primes = new ArrayList<>();
39+
if (limit >= 2) {
40+
primes.add(2);
41+
}
42+
if (limit >= 3) {
43+
primes.add(3);
44+
}
45+
46+
for (int i = 5; i <= limit; i++) {
47+
if (sieve[i]) {
48+
primes.add(i);
49+
}
50+
}
51+
52+
return primes;
53+
}
54+
55+
/**
56+
* Marks numbers in the sieve as prime candidates based on quadratic residues.
57+
*
58+
* This method iterates over all x and y up to sqrt(limit) and applies
59+
* the three quadratic forms used in the Sieve of Atkin. Numbers satisfying
60+
* the modulo conditions are toggled in the sieve array.
61+
*
62+
* @param limit the upper bound for primes
63+
* @param sqrtLimit square root of the limit
64+
* @param sieve boolean array representing potential primes
65+
*/
66+
private static void markQuadraticResidues(int limit, int sqrtLimit, boolean[] sieve) {
67+
for (int x = 1; x <= sqrtLimit; x++) {
68+
for (int y = 1; y <= sqrtLimit; y++) {
69+
applyQuadraticForm(4 * x * x + y * y, limit, sieve, 1, 5);
70+
applyQuadraticForm(3 * x * x + y * y, limit, sieve, 7);
71+
applyQuadraticForm(3 * x * x - y * y, limit, sieve, 11, x > y);
72+
}
73+
}
74+
}
75+
76+
/**
77+
* Toggles the sieve entry for a number if it satisfies one modulo condition.
78+
*
79+
* @param n the number to check
80+
* @param limit upper bound of primes
81+
* @param sieve boolean array representing potential primes
82+
* @param modulo the modulo condition number to check
83+
*/
84+
private static void applyQuadraticForm(int n, int limit, boolean[] sieve, int modulo) {
85+
if (n <= limit && n % 12 == modulo) {
86+
sieve[n] ^= true;
87+
}
88+
}
89+
90+
/**
91+
* Toggles the sieve entry for a number if it satisfies either of two modulo conditions.
92+
*
93+
* @param n the number to check
94+
* @param limit upper bound of primes
95+
* @param sieve boolean array representing potential primes
96+
* @param modulo1 first modulo condition number to check
97+
* @param modulo2 second modulo condition number to check
98+
*/
99+
private static void applyQuadraticForm(int n, int limit, boolean[] sieve, int modulo1, int modulo2) {
100+
if (n <= limit && (n % 12 == modulo1 || n % 12 == modulo2)) {
101+
sieve[n] ^= true;
102+
}
103+
}
104+
105+
/**
106+
* Toggles the sieve entry for a number if it satisfies the modulo condition and an additional boolean condition.
107+
*
108+
* This version is used for the quadratic form 3*x*x - y*y, which requires x > y.
109+
*
110+
* @param n the number to check
111+
* @param limit upper bound of primes
112+
* @param sieve boolean array representing potential primes
113+
* @param modulo the modulo condition number to check
114+
* @param condition an additional boolean condition that must be true
115+
*/
116+
private static void applyQuadraticForm(int n, int limit, boolean[] sieve, int modulo, boolean condition) {
117+
if (condition && n <= limit && n % 12 == modulo) {
118+
sieve[n] ^= true;
119+
}
120+
}
121+
122+
/**
123+
* Eliminates numbers that are multiples of squares from the sieve.
124+
*
125+
* All numbers that are multiples of i*i (where i is marked as prime) are
126+
* marked non-prime to finalize the sieve. This ensures only actual primes remain.
127+
*
128+
* @param limit the upper bound for primes
129+
* @param sqrtLimit square root of the limit
130+
* @param sieve boolean array representing potential primes
131+
*/
132+
private static void eliminateMultiplesOfSquares(int limit, int sqrtLimit, boolean[] sieve) {
133+
for (int i = 5; i <= sqrtLimit; i++) {
134+
if (!sieve[i]) {
135+
continue;
136+
}
137+
int square = i * i;
138+
for (int j = square; j <= limit; j += square) {
139+
sieve[j] = false;
140+
}
141+
}
142+
}
143+
}

0 commit comments

Comments
 (0)