From 548c2fd9d04bc65fc2ae555ab83a1868d7dabd7e Mon Sep 17 00:00:00 2001 From: "duaraghav8@gmail" Date: Tue, 24 May 2016 00:33:40 +0530 Subject: [PATCH] Add category String & Edit Distance Algorithm --- algorithm/category.json | 6 +++ algorithm/string/edit_distance/desc.json | 18 +++++++++ .../edit_distance/dynamic_programming/code.js | 40 +++++++++++++++++++ .../edit_distance/dynamic_programming/data.js | 10 +++++ 4 files changed, 74 insertions(+) create mode 100644 algorithm/string/edit_distance/desc.json create mode 100644 algorithm/string/edit_distance/dynamic_programming/code.js create mode 100644 algorithm/string/edit_distance/dynamic_programming/data.js diff --git a/algorithm/category.json b/algorithm/category.json index 528e8276..415d28b1 100644 --- a/algorithm/category.json +++ b/algorithm/category.json @@ -27,6 +27,12 @@ "heap" : "Heap Sort" } }, + "string": { + "name": "String", + "list": { + "edit_distance": "Edit Distance" + } + }, "etc": { "name": "Uncategorized", "list": { diff --git a/algorithm/string/edit_distance/desc.json b/algorithm/string/edit_distance/desc.json new file mode 100644 index 00000000..d945d7bb --- /dev/null +++ b/algorithm/string/edit_distance/desc.json @@ -0,0 +1,18 @@ +{ + "Edit-Distance": "Given two strings str1 (length M) and str2 (length N) and below operations that can performed on str1. Find minimum number of edits (operations) required to convert str1 into str2.
Insert
Remove
Replace
All of the above operations are of equal cost", + "Applications": [ + "Displaing Near-Proximity Words", + "Information Retrieval (eg- Lucene API)", + "Natural Language Processing" + ], + "Complexity": { + "time": "worst O(|M|.|N|)", + "space": "worst O(|M|.|N|)" + }, + "References": [ + "Wikipedia" + ], + "files": { + "dynamic_programming": "Distance from str1 to str2 using Dynamic Programming (2D Array method)" + } +} diff --git a/algorithm/string/edit_distance/dynamic_programming/code.js b/algorithm/string/edit_distance/dynamic_programming/code.js new file mode 100644 index 00000000..4373ab71 --- /dev/null +++ b/algorithm/string/edit_distance/dynamic_programming/code.js @@ -0,0 +1,40 @@ +tracer._pace (200); +tracer._print ('Initialized DP Table'); +tracer._print ('Y-Axis (Top to Bottom): ' + str1); +tracer._print ('X-Axis (Left to Right): ' + str2); + +var dist = (function editDistance (str1, str2, table) { + //display grid with words + tracer._print ('*** ' + str2.split ('').join (' ')); + table.forEach (function (item, index) { + var character = (index === 0) ? '*' : str1 [index-1]; + tracer._print (character + '\t' + item); + }); + + //begin ED execution + for (var i = 1; i < str1.length+1; i++) { + for (var j = 1; j < str2.length+1; j++) { + if (str1 [i-1] === str2 [j-1]) { + tracer._select (i-1, j-1); + table [i] [j] = table [i-1] [j-1]; + tracer._notify (i, j); + tracer._deselect (i-1, j-1); + } + else { + tracer._select (i-1, j); + tracer._select (i, j-1); + tracer._select (i-1, j-1); + table [i] [j] = Math.min (table [i-1] [j], table [i] [j-1], table [i-1] [j-1]) + 1; + tracer._notify (i, j); + tracer._deselect (i-1, j); + tracer._deselect (i, j-1); + tracer._deselect (i-1, j-1); + } + } + } + + tracer._select (str1.length, str2.length); + return table [str1.length] [str2.length]; +}) (str1, str2, table); + +tracer._print ('Minimum Edit Distance: ' + dist); \ No newline at end of file diff --git a/algorithm/string/edit_distance/dynamic_programming/data.js b/algorithm/string/edit_distance/dynamic_programming/data.js new file mode 100644 index 00000000..86eae52d --- /dev/null +++ b/algorithm/string/edit_distance/dynamic_programming/data.js @@ -0,0 +1,10 @@ +var tracer = new Array2DTracer (); +var str1 = 'stack', str2 = 'racket', table = new Array (str1.length + 1); + +table [0] = Array.apply (null, {length: str2.length + 1}).map (Number.call, Number); +for (var i = 1; i < str1.length+1; i++) { + table [i] = Array.apply (null, Array (str2.length+1)).map (Number.prototype.valueOf, -1); + table [i] [0] = i +} + +tracer._setData (table); \ No newline at end of file