feat: add Levenshtein edit distance algorithm#295
feat: add Levenshtein edit distance algorithm#295DiogoRibeiro7 wants to merge 2 commits intoTheAlgorithms:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a Levenshtein (edit distance) implementation to the R algorithms collection, with an optional backtracking routine to return one optimal edit-operation sequence, plus accompanying documentation and directory indexing.
Changes:
- Added
dynamic_programming/edit_distance.rwithedit_distance()andedit_distance_with_path(). - Added
documentation/edit_distance.mdwith basic usage examples. - Updated
DIRECTORY.mdto list the new Dynamic Programming entry.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| dynamic_programming/edit_distance.r | Introduces Levenshtein distance DP implementation and path reconstruction output. |
| documentation/edit_distance.md | Adds user-facing usage snippet for the new functions. |
| DIRECTORY.md | Adds a new link entry for “Edit Distance” under Dynamic Programming. |
dynamic_programming/edit_distance.r
Outdated
| for (i in 2:(m + 1)) { | ||
| for (j in 2:(n + 1)) { |
There was a problem hiding this comment.
The loops for (i in 2:(m + 1)) / for (j in 2:(n + 1)) break for empty strings (e.g., m == 0 makes 2:(m+1) evaluate to c(2, 1)), which can index outside dp and error. Use an empty-safe iteration pattern (e.g., seq_len(m) + 1, seq_len(n) + 1) or early-return when m == 0 or n == 0.
| for (i in 2:(m + 1)) { | |
| for (j in 2:(n + 1)) { | |
| for (i in seq_len(m) + 1L) { | |
| for (j in seq_len(n) + 1L) { |
| 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 2:(m + 1)) { | ||
| for (j in 2:(n + 1)) { | ||
| cost <- if (substr(str1, i - 1, i - 1) == substr(str2, j - 1, j - 1)) 0L else 1L | ||
| dp[i, j] <- min( |
There was a problem hiding this comment.
Same empty-string issue as above: for (i in 2:(m + 1)) / for (j in 2:(n + 1)) can iterate invalid indices when m == 0 or n == 0, leading to out-of-bounds dp access. Please make the iteration empty-safe or handle m == 0 / n == 0 up front (and still produce a valid operations list).
DIRECTORY.md
Outdated
| * [Matrix Chain Multiplication](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/matrix_chain_multiplication.r) | ||
| * [Minimum Path Sum](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/minimum_path_sum.r) | ||
| * [Subset Sum](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/subset_sum.r) | ||
| * [Edit Distance](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/edit_distance.r) |
There was a problem hiding this comment.
This repository already contains a Levenshtein distance implementation (string_manipulation/levenshtein.r, also linked in DIRECTORY.md). Adding a second Levenshtein implementation under Dynamic Programming may be redundant/confusing for users; consider consolidating (extend the existing implementation to optionally return a path) or clearly differentiating this entry (e.g., mention that it returns an operation sequence).
| * [Edit Distance](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/edit_distance.r) | |
| * [Edit Distance (Levenshtein, Dynamic Programming)](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/edit_distance.r) |
documentation/edit_distance.md
Outdated
| 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") |
There was a problem hiding this comment.
source("../dynamic_programming/edit_distance.r") depends on the working directory being documentation/. If a user runs this from the repo root (common), it will point outside the repository. Consider using a repo-root-relative path (e.g., dynamic_programming/edit_distance.r) or embedding the relevant function code in the doc, consistent with other docs in this folder.
| source("../dynamic_programming/edit_distance.r") | |
| source("dynamic_programming/edit_distance.r") |
|
It's already implemented here R/string_manipulation/levenshtein.r Line 5 in 2349f54 |
Description
dynamic_programming/edit_distance.rimplementing:edit_distance(str1, str2)for Levenshtein distanceedit_distance_with_path(str1, str2)to return distance plus one optimal operation sequencedocumentation/edit_distance.mdwith usage examples.DIRECTORY.mdto include Edit Distance under Dynamic Programming.Validation
edit_distance("kitten", "sitting")edit_distance_with_path("kitten", "sitting")33