{"id":103,"date":"2023-07-21T21:33:50","date_gmt":"2023-07-21T20:33:50","guid":{"rendered":"https:\/\/techedges.in\/?page_id=103"},"modified":"2023-07-22T11:46:32","modified_gmt":"2023-07-22T10:46:32","slug":"explanation-of-edit-distance-levenshtein-distance","status":"publish","type":"page","link":"https:\/\/techedges.in\/index.php\/explanation-of-edit-distance-levenshtein-distance\/","title":{"rendered":"Explanation of Edit Distance (Levenshtein Distance):"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>The Edit Distance, also known as Levenshtein Distance, is a widely used algorithmic concept in computer science and natural language processing. It measures the similarity between two strings by calculating the minimum number of single-character insertions, deletions, or substitutions required to transform one string into the other.<\/p>\n\n\n\n<p><strong>Step-by-Step Approach:<\/strong><\/p>\n\n\n\n<ol>\n<li>We are given two input strings, <code>str1<\/code> and <code>str2<\/code>.<\/li>\n\n\n\n<li>The goal is to find the minimum number of edit operations needed to transform <code>str1<\/code> into <code>str2<\/code>.<\/li>\n\n\n\n<li>The possible edit operations are insertion, deletion, and substitution of a single character.<\/li>\n\n\n\n<li>To solve this problem, we use a Dynamic Programming approach.<\/li>\n\n\n\n<li>We create a 2D DP array of size <code>(m+1) x (n+1)<\/code>, where <code>m<\/code> and <code>n<\/code> are the lengths of <code>str1<\/code> and <code>str2<\/code>, respectively.<\/li>\n\n\n\n<li>Initialize the DP array with base cases: <code>dp[i][0] = i<\/code> and <code>dp[0][j] = j<\/code>, representing the number of edit operations required to convert an empty string to <code>str1<\/code> or <code>str2<\/code>.<\/li>\n\n\n\n<li>We then iterate through the DP array, starting from <code>(1, 1)<\/code> to <code>(m, n)<\/code>.<\/li>\n\n\n\n<li>For each position <code>(i, j)<\/code> in the DP array, we check the characters at <code>str1[i-1]<\/code> and <code>str2[j-1]<\/code>. If they are equal, no edit operation is needed at this position, so we set <code>dp[i][j]<\/code> to the same value as <code>dp[i-1][j-1]<\/code>.<\/li>\n\n\n\n<li>If the characters are different, we consider three edit operations:<\/li>\n<\/ol>\n\n\n\n<ul>\n<li>Insertion: <code>dp[i][j] = dp[i][j-1] + 1<\/code><\/li>\n\n\n\n<li>Deletion: <code>dp[i][j] = dp[i-1][j] + 1<\/code><\/li>\n\n\n\n<li>Substitution: <code>dp[i][j] = dp[i-1][j-1] + 1<\/code><\/li>\n<\/ul>\n\n\n\n<ol>\n<li>We update <code>dp[i][j]<\/code> to the minimum value among the three edit operations.<\/li>\n\n\n\n<li>After completing the iteration, the value at <code>dp[m][n]<\/code> represents the minimum edit distance between <code>str1<\/code> and <code>str2<\/code>.<\/li>\n<\/ol>\n\n\n\n<p><strong>Java Solution:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class EditDistance {\n\n    public int minDistance(String word1, String word2) {\n        int m = word1.length();\n        int n = word2.length();\n\n        int&#91;]&#91;] dp = new int&#91;m + 1]&#91;n + 1];\n\n        for (int i = 0; i &lt;= m; i++) {\n            dp&#91;i]&#91;0] = i;\n        }\n\n        for (int j = 0; j &lt;= n; j++) {\n            dp&#91;0]&#91;j] = j;\n        }\n\n        for (int i = 1; i &lt;= m; i++) {\n            for (int j = 1; j &lt;= n; j++) {\n                if (word1.charAt(i - 1) == word2.charAt(j - 1)) {\n                    dp&#91;i]&#91;j] = dp&#91;i - 1]&#91;j - 1];\n                } else {\n                    int insertion = dp&#91;i]&#91;j - 1] + 1;\n                    int deletion = dp&#91;i - 1]&#91;j] + 1;\n                    int substitution = dp&#91;i - 1]&#91;j - 1] + 1;\n                    dp&#91;i]&#91;j] = Math.min(Math.min(insertion, deletion), substitution);\n                }\n            }\n        }\n\n        return dp&#91;m]&#91;n];\n    }\n\n    public static void main(String&#91;] args) {\n        String word1 = \"intention\";\n        String word2 = \"execution\";\n\n        EditDistance editDistance = new EditDistance();\n        System.out.println(\"Minimum Edit Distance: \" + editDistance.minDistance(word1, word2));\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Python Solution:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class EditDistance:\n\n    def minDistance(self, word1: str, word2: str) -&gt; int:\n        m, n = len(word1), len(word2)\n\n        dp = &#91;&#91;0] * (n + 1) for _ in range(m + 1)]\n\n        for i in range(m + 1):\n            dp&#91;i]&#91;0] = i\n\n        for j in range(n + 1):\n            dp&#91;0]&#91;j] = j\n\n        for i in range(1, m + 1):\n            for j in range(1, n + 1):\n                if word1&#91;i - 1] == word2&#91;j - 1]:\n                    dp&#91;i]&#91;j] = dp&#91;i - 1]&#91;j - 1]\n                else:\n                    insertion = dp&#91;i]&#91;j - 1] + 1\n                    deletion = dp&#91;i - 1]&#91;j] + 1\n                    substitution = dp&#91;i - 1]&#91;j - 1] + 1\n                    dp&#91;i]&#91;j] = min(insertion, deletion, substitution)\n\n        return dp&#91;m]&#91;n]\n\nif __name__ == \"__main__\":\n    word1 = \"intention\"\n    word2 = \"execution\"\n\n    edit_distance = EditDistance()\n    print(\"Minimum Edit Distance:\", edit_distance.minDistance(word1, word2))<\/code><\/pre>\n\n\n\n<p><strong>Explanation of Java\/Python Solution:<\/strong><\/p>\n\n\n\n<p>The Java and Python solutions implement the Edit Distance (Levenshtein Distance) problem using Dynamic Programming to find the minimum number of edit operations required to transform one string into another.<\/p>\n\n\n\n<p>The <code>minDistance<\/code> method in Java and <code>minDistance<\/code> method in Python take two input strings <code>word1<\/code> and <code>word2<\/code> and return the minimum edit distance between the two strings.<\/p>\n\n\n\n<p>The provided example with <code>word1 = \"intention\"<\/code> and <code>word2 = \"execution\"<\/code> demonstrates how the Dynamic Programming approach calculates the minimum edit distance as <code>5<\/code>. The minimum number of edit operations required to convert &#8220;intention&#8221; to &#8220;execution&#8221; is <code>5<\/code>, which includes 3 deletions and 2 insertions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Edit Distance, also known as Levenshtein Distance, is a widely used algorithmic concept in computer science and natural language processing. It measures the similarity between two strings by calculating the minimum number of single-character insertions, deletions, or substitutions required to transform one string into the other. Step-by-Step Approach: Java Solution: Python Solution: Explanation of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"saved_in_kubio":false,"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"aioseo_notices":[],"kubio_ai_page_context":{"short_desc":"","purpose":"general"},"_links":{"self":[{"href":"https:\/\/techedges.in\/index.php\/wp-json\/wp\/v2\/pages\/103"}],"collection":[{"href":"https:\/\/techedges.in\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/techedges.in\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/techedges.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techedges.in\/index.php\/wp-json\/wp\/v2\/comments?post=103"}],"version-history":[{"count":3,"href":"https:\/\/techedges.in\/index.php\/wp-json\/wp\/v2\/pages\/103\/revisions"}],"predecessor-version":[{"id":117,"href":"https:\/\/techedges.in\/index.php\/wp-json\/wp\/v2\/pages\/103\/revisions\/117"}],"wp:attachment":[{"href":"https:\/\/techedges.in\/index.php\/wp-json\/wp\/v2\/media?parent=103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}