-
-
Notifications
You must be signed in to change notification settings - Fork 346
feat: add Levenshtein edit distance algorithm #295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
DiogoRibeiro7
wants to merge
2
commits into
TheAlgorithms:master
from
DiogoRibeiro7:edit-distance-feature
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Edit Distance | ||
|
|
||
| Levenshtein edit distance calculates the minimum number of single-character insertions, deletions, and substitutions required to transform one string into another. | ||
|
|
||
| ``` r | ||
| source("dynamic_programming/edit_distance.r") | ||
|
|
||
| # Compute the edit distance | ||
| distance <- edit_distance("kitten", "sitting") | ||
| print(distance) | ||
|
|
||
| # Reconstruct the optimal sequence of operations | ||
| result <- edit_distance_with_path("kitten", "sitting") | ||
| print(result$distance) | ||
| print(result$operations) | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| # edit_distance.r | ||
| # Levenshtein edit distance algorithm in R | ||
| # Computes the minimum number of insertions, deletions, and substitutions | ||
| # required to transform one string into another. | ||
| # Time Complexity: O(m * n) | ||
| # Space Complexity: O(m * n) | ||
|
|
||
| # Compute the Levenshtein distance between two strings | ||
| edit_distance <- function(str1, str2) { | ||
| #' @param str1: First string | ||
| #' @param str2: Second string | ||
| #' @return: Integer edit distance | ||
| if (!is.character(str1) || !is.character(str2)) { | ||
| stop("Both inputs must be character strings.") | ||
| } | ||
| if (length(str1) != 1 || length(str2) != 1) { | ||
| stop("Each input must be a single string.") | ||
| } | ||
|
|
||
| m <- nchar(str1) | ||
| n <- nchar(str2) | ||
| dp <- matrix(0L, nrow = m + 1, ncol = n + 1) | ||
|
|
||
| # base cases: transform empty prefix | ||
| dp[, 1] <- seq(0L, m) | ||
| dp[1, ] <- seq(0L, n) | ||
|
|
||
| for (i in seq_len(m) + 1L) { | ||
| for (j in seq_len(n) + 1L) { | ||
| cost <- if (substr(str1, i - 1, i - 1) == substr(str2, j - 1, j - 1)) 0L else 1L | ||
| dp[i, j] <- min( | ||
| dp[i - 1, j] + 1L, # deletion | ||
| dp[i, j - 1] + 1L, # insertion | ||
| dp[i - 1, j - 1] + cost # substitution | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| return(dp[m + 1, n + 1]) | ||
| } | ||
|
|
||
| # Compute the edit distance and reconstruct an optimal alignment path | ||
| edit_distance_with_path <- function(str1, str2) { | ||
| #' @param str1: First string | ||
| #' @param str2: Second string | ||
| #' @return: List with distance, operations, and dp table | ||
| if (!is.character(str1) || !is.character(str2)) { | ||
| stop("Both inputs must be character strings.") | ||
| } | ||
| if (length(str1) != 1 || length(str2) != 1) { | ||
| stop("Each input must be a single string.") | ||
| } | ||
|
|
||
| m <- nchar(str1) | ||
| n <- nchar(str2) | ||
| dp <- matrix(0L, nrow = m + 1, ncol = n + 1) | ||
| dp[, 1] <- seq(0L, m) | ||
| dp[1, ] <- seq(0L, n) | ||
|
|
||
| for (i in seq_len(m) + 1L) { | ||
| for (j in seq_len(n) + 1L) { | ||
| cost <- if (substr(str1, i - 1, i - 1) == substr(str2, j - 1, j - 1)) 0L else 1L | ||
| dp[i, j] <- min( | ||
| dp[i - 1, j] + 1L, | ||
| dp[i, j - 1] + 1L, | ||
| dp[i - 1, j - 1] + cost | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| i <- m + 1 | ||
| j <- n + 1 | ||
| ops <- character() | ||
|
|
||
| while (i > 1 || j > 1) { | ||
| if (i > 1 && j > 1 && dp[i, j] == dp[i - 1, j - 1] + | ||
| (substr(str1, i - 1, i - 1) != substr(str2, j - 1, j - 1))) { | ||
| if (substr(str1, i - 1, i - 1) == substr(str2, j - 1, j - 1)) { | ||
| ops <- c("match", ops) | ||
| } else { | ||
| ops <- c(sprintf("substitute '%s' -> '%s'", substr(str1, i - 1, i - 1), substr(str2, j - 1, j - 1)), ops) | ||
| } | ||
| i <- i - 1 | ||
| j <- j - 1 | ||
| } else if (i > 1 && dp[i, j] == dp[i - 1, j] + 1L) { | ||
| ops <- c(sprintf("delete '%s'", substr(str1, i - 1, i - 1)), ops) | ||
| i <- i - 1 | ||
| } else { | ||
| ops <- c(sprintf("insert '%s'", substr(str2, j - 1, j - 1)), ops) | ||
| j <- j - 1 | ||
| } | ||
| } | ||
|
|
||
| return(list( | ||
| distance = dp[m + 1, n + 1], | ||
| operations = ops, | ||
| dp_table = dp | ||
| )) | ||
| } | ||
|
|
||
| # Example usage: | ||
| # print(edit_distance("kitten", "sitting")) | ||
| # result <- edit_distance_with_path("kitten", "sitting") | ||
| # print(result$distance) | ||
| # print(result$operations) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same empty-string issue as above:
for (i in 2:(m + 1))/for (j in 2:(n + 1))can iterate invalid indices whenm == 0orn == 0, leading to out-of-boundsdpaccess. Please make the iteration empty-safe or handlem == 0/n == 0up front (and still produce a validoperationslist).