mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-03-21 04:18:34 +08:00
411 lines
28 KiB
HTML
411 lines
28 KiB
HTML
<!-- HTML header for doxygen 1.12.0-->
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
|
|
<meta name="generator" content="Doxygen 1.14.0"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>TheAlgorithms/C++: dynamic_programming/minimum_edit_distance.cpp File Reference</title>
|
|
<link rel="icon" href="../../favicon.svg" type="image/x-icon" />
|
|
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../jquery.js"></script>
|
|
<script type="text/javascript" src="../../dynsections.js"></script>
|
|
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@xpack-3rd-party/doxygen-awesome-css@2.2.0-1/doxygen-awesome-darkmode-toggle.js"></script>
|
|
<script type="text/javascript">
|
|
DoxygenAwesomeDarkModeToggle.init()
|
|
</script>
|
|
<script type="text/javascript" src="../../clipboard.js"></script>
|
|
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../navtreedata.js"></script>
|
|
<script type="text/javascript" src="../../navtree.js"></script>
|
|
<script type="text/javascript" src="../../cookie.js"></script>
|
|
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../search/searchdata.js"></script>
|
|
<script type="text/javascript" src="../../search/search.js"></script>
|
|
<script type="text/javascript">
|
|
window.MathJax = {
|
|
options: {
|
|
ignoreHtmlClass: 'tex2jax_ignore',
|
|
processHtmlClass: 'tex2jax_process'
|
|
},
|
|
loader: {
|
|
load: ['[tex]/ams']
|
|
},
|
|
tex: {
|
|
macros: {},
|
|
packages: ['base','configmacros','ams']
|
|
}
|
|
};
|
|
</script>
|
|
<script type="text/javascript" id="MathJax-script" async="async" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
|
|
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
|
|
<link href="../../doxygen-awesome.css" rel="stylesheet" type="text/css"/>
|
|
</head>
|
|
<body>
|
|
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
|
<div id="titlearea">
|
|
<table cellspacing="0" cellpadding="0">
|
|
<tbody>
|
|
<tr id="projectrow">
|
|
<td id="projectlogo"><img alt="Logo" src="../../project_logo.png"/></td>
|
|
<td id="projectalign">
|
|
<div id="projectname">TheAlgorithms/C++<span id="projectnumber"> 1.0.0</span>
|
|
</div>
|
|
<div id="projectbrief">All the algorithms implemented in C++</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.14.0 -->
|
|
<script type="text/javascript">
|
|
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
|
|
</script>
|
|
<script type="text/javascript">
|
|
$(function() { codefold.init(); });
|
|
</script>
|
|
<script type="text/javascript" src="../../menudata.js"></script>
|
|
<script type="text/javascript" src="../../menu.js"></script>
|
|
<script type="text/javascript">
|
|
$(function() {
|
|
initMenu('../../',true,false,'search.php','Search',true);
|
|
$(function() { init_search(); });
|
|
});
|
|
</script>
|
|
<div id="main-nav"></div>
|
|
</div><!-- top -->
|
|
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
|
<div id="nav-tree">
|
|
<div id="nav-tree-contents">
|
|
<div id="nav-sync" class="sync"></div>
|
|
</div>
|
|
</div>
|
|
<div id="splitbar" style="-moz-user-select:none;"
|
|
class="ui-resizable-handle">
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript">
|
|
$(function(){initNavTree('da/d52/minimum__edit__distance_8cpp.html','../../',''); });
|
|
</script>
|
|
<div id="container">
|
|
<div id="doc-content">
|
|
<!-- window showing the filter options -->
|
|
<div id="MSearchSelectWindow"
|
|
onmouseover="return searchBox.OnSearchSelectShow()"
|
|
onmouseout="return searchBox.OnSearchSelectHide()"
|
|
onkeydown="return searchBox.OnSearchSelectKey(event)">
|
|
</div>
|
|
|
|
<!-- iframe showing the search results (closed by default) -->
|
|
<div id="MSearchResultsWindow">
|
|
<div id="MSearchResults">
|
|
<div class="SRPage">
|
|
<div id="SRIndex">
|
|
<div id="SRResults"></div>
|
|
<div class="SRStatus" id="Loading">Loading...</div>
|
|
<div class="SRStatus" id="Searching">Searching...</div>
|
|
<div class="SRStatus" id="NoMatches">No Matches</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="header">
|
|
<div class="headertitle"><div class="title">minimum_edit_distance.cpp File Reference</div></div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
|
|
<p>Implementation of <a href="https://en.wikipedia.org/wiki/Edit_distance" target="_blank">Minimum Edit Distance</a> using Dynamic Programing.
|
|
<a href="#details">More...</a></p>
|
|
<div class="textblock"><code>#include <cassert></code><br />
|
|
<code>#include <cstdint></code><br />
|
|
<code>#include <iostream></code><br />
|
|
<code>#include <vector></code><br />
|
|
</div><div class="textblock"><div class="dynheader">
|
|
Include dependency graph for minimum_edit_distance.cpp:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" loading="lazy" frameborder="0" src="../../de/d3d/minimum__edit__distance_8cpp__incl.svg" width="336" height="126"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
|
|
</div>
|
|
</div>
|
|
<p><a href="../../da/d52/minimum__edit__distance_8cpp_source.html">Go to the source code of this file.</a></p>
|
|
<table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 id="header-namespaces" class="groupheader"><a id="namespaces" name="namespaces"></a>
|
|
Namespaces</h2></td></tr>
|
|
<tr class="memitem:dynamic_5Fprogramming" id="r_dynamic_5Fprogramming"><td class="memItemLeft" align="right" valign="top">namespace  </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d24/namespacedynamic__programming.html">dynamic_programming</a></td></tr>
|
|
<tr class="memdesc:dd/d24/namespacedynamic__programming"><td class="mdescLeft"> </td><td class="mdescRight">Dynamic Programming algorithms. <br /></td></tr>
|
|
<tr class="memitem:Minimum" id="r_Minimum"><td class="memItemLeft" align="right" valign="top">namespace  </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d4/d12/namespace_minimum.html">Minimum</a></td></tr>
|
|
<tr class="memdesc:d4/d12/namespace_minimum"><td class="mdescLeft"> </td><td class="mdescRight">Implementation of <a href="https://en.wikipedia.org/wiki/Edit_distance" target="_blank">Minimum Edit Distance</a> algorithm. <br /></td></tr>
|
|
</table><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 id="header-func-members" class="groupheader"><a id="func-members" name="func-members"></a>
|
|
Functions</h2></td></tr>
|
|
<tr class="memitem:a0138c226bd79ffe6d839c787cfc60347" id="r_a0138c226bd79ffe6d839c787cfc60347"><td class="memItemLeft" align="right" valign="top">uint64_t </td><td class="memItemRight" valign="bottom"><a class="el" href="#a0138c226bd79ffe6d839c787cfc60347">dynamic_programming::minimum_edit_distance::min</a> (uint64_t x, uint64_t y, uint64_t z)</td></tr>
|
|
<tr class="memdesc:a0138c226bd79ffe6d839c787cfc60347"><td class="mdescLeft"> </td><td class="mdescRight">Takes input of the cost of three operations: Insert, Replace and Delete and return the minimum cost among them. <br /></td></tr>
|
|
<tr class="memitem:ade2fcfe0359f3c7691bfaa04b14943e2" id="r_ade2fcfe0359f3c7691bfaa04b14943e2"><td class="memItemLeft" align="right" valign="top">uint64_t </td><td class="memItemRight" valign="bottom"><a class="el" href="#ade2fcfe0359f3c7691bfaa04b14943e2">dynamic_programming::minimum_edit_distance::editDistDP</a> (std::string str1, std::string str2, uint64_t m, uint64_t n)</td></tr>
|
|
<tr class="memdesc:ade2fcfe0359f3c7691bfaa04b14943e2"><td class="mdescLeft"> </td><td class="mdescRight">Calculates and stores the result of all the sub-problems, so that we don't have to recur to compute the minimum cost of a particular operation if it is already computed and stored in the <span class="tt">dp</span> vector. <br /></td></tr>
|
|
<tr class="memitem:aa8dca7b867074164d5f45b0f3851269d" id="r_aa8dca7b867074164d5f45b0f3851269d"><td class="memItemLeft" align="right" valign="top">static void </td><td class="memItemRight" valign="bottom"><a class="el" href="#aa8dca7b867074164d5f45b0f3851269d">test</a> ()</td></tr>
|
|
<tr class="memdesc:aa8dca7b867074164d5f45b0f3851269d"><td class="mdescLeft"> </td><td class="mdescRight">Self-test implementations. <br /></td></tr>
|
|
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4" id="r_ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
|
|
<tr class="memdesc:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="mdescLeft"> </td><td class="mdescRight">main function <br /></td></tr>
|
|
</table>
|
|
<a name="details" id="details"></a><h2 id="header-details" class="groupheader">Detailed Description</h2>
|
|
<div class="textblock"><p>Implementation of <a href="https://en.wikipedia.org/wiki/Edit_distance" target="_blank">Minimum Edit Distance</a> using Dynamic Programing. </p>
|
|
<p>Given two strings str1 & str2 and we have to calculate the minimum number of operations (Insert, Remove, Replace) required to convert str1 to str2.</p>
|
|
<h3 class="doxsection"><a class="anchor" id="autotoc_md73"></a>
|
|
Algorithm</h3>
|
|
<p>We will solve this problem using Naive recursion. But as we are approaching with a DP solution. So, we will take a DP array to store the solution of all sub-problems so that we don't have to perform recursion again and again. Now to solve the problem, We can traverse all characters from either right side of the strings or left side. Suppose we will do it from the right side. So, there are two possibilities for every pair of characters being traversed.</p><ol type="1">
|
|
<li>If the last characters of two strings are the same, Ignore the characters and get the count for the remaining string. So, we get the solution for lengths m-1 and n-1 in a DP array.</li>
|
|
<li>Else, (If last characters are not the same), we will consider all three operations (Insert, Remove, Replace) on the last character of the first string and compute the minimum cost for all three operations and take the minimum of three values in the DP array. For Insert: Recur for m and n-1 For Remove: Recur for for m-1 and n For Replace: Recur for for m-1 and n-1</li>
|
|
</ol>
|
|
<dl class="section author"><dt>Author</dt><dd><a href="../../github.com/nirzak" target="_blank">Nirjas Jakilim</a> </dd></dl>
|
|
|
|
<p class="definition">Definition in file <a class="el" href="../../da/d52/minimum__edit__distance_8cpp_source.html">minimum_edit_distance.cpp</a>.</p>
|
|
</div><a name="doc-func-members" id="doc-func-members"></a><h2 id="header-doc-func-members" class="groupheader">Function Documentation</h2>
|
|
<a id="ade2fcfe0359f3c7691bfaa04b14943e2" name="ade2fcfe0359f3c7691bfaa04b14943e2"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#ade2fcfe0359f3c7691bfaa04b14943e2">◆ </a></span>editDistDP()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">uint64_t dynamic_programming::minimum_edit_distance::editDistDP </td>
|
|
<td>(</td>
|
|
<td class="paramtype">std::string</td> <td class="paramname"><span class="paramname"><em>str1</em></span>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">std::string</td> <td class="paramname"><span class="paramname"><em>str2</em></span>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">uint64_t</td> <td class="paramname"><span class="paramname"><em>m</em></span>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">uint64_t</td> <td class="paramname"><span class="paramname"><em>n</em></span> )</td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Calculates and stores the result of all the sub-problems, so that we don't have to recur to compute the minimum cost of a particular operation if it is already computed and stored in the <span class="tt">dp</span> vector. </p>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramname">dp</td><td>vector to store the computed minimum costs </td></tr>
|
|
<tr><td class="paramname">str1</td><td>to pass the 1st string </td></tr>
|
|
<tr><td class="paramname">str2</td><td>to pass the 2nd string </td></tr>
|
|
<tr><td class="paramname">m</td><td>the length of str1 </td></tr>
|
|
<tr><td class="paramname">n</td><td>the length of str2 </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<dl class="section return"><dt>Returns</dt><dd>dp[m][n] the minimum cost of operations needed to convert str1 to str2 </dd></dl>
|
|
<p>Create a table to store results of subproblems</p>
|
|
<p>creasting 2D vector dp to store the results of subproblems</p>
|
|
<p>Fill d[][] in bottom up manner</p>
|
|
<p>If first string is empty, only option is to insert all characters of second string</p>
|
|
<p><a class="el" href="../../d4/d12/namespace_minimum.html" title="Implementation of Minimum Edit Distance algorithm.">Minimum</a> operations = j</p>
|
|
<p>If second string is empty, only option is to remove all characters of second string</p>
|
|
<p><a class="el" href="../../d4/d12/namespace_minimum.html" title="Implementation of Minimum Edit Distance algorithm.">Minimum</a> operations = i</p>
|
|
<p>If last characters are same, ignore last char and recur for remaining string</p>
|
|
<p>If the last character is different, consider all possibilities and find the minimum</p>
|
|
<p>returning the minimum cost of operations needed to convert str1 to str2</p>
|
|
|
|
<p class="definition">Definition at line <a class="el" href="../../da/d52/minimum__edit__distance_8cpp_source.html#l00092">92</a> of file <a class="el" href="../../da/d52/minimum__edit__distance_8cpp_source.html">minimum_edit_distance.cpp</a>.</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 93</span> {</div>
|
|
<div class="line"><span class="lineno"> 95</span> std::vector<std::vector<uint64_t>> <a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>(</div>
|
|
<div class="line"><span class="lineno"> 96</span> m + 1,</div>
|
|
<div class="line"><span class="lineno"> 97</span> std::vector<uint64_t>(</div>
|
|
<div class="line"><span class="lineno"> 98</span> n +</div>
|
|
<div class="line"><span class="lineno"> 99</span> 1)); </div>
|
|
<div class="line"><span class="lineno"> 100</span></div>
|
|
<div class="line"><span class="lineno"> 102</span> <span class="keywordflow">for</span> (uint64_t i = 0; i <= m; i++) {</div>
|
|
<div class="line"><span class="lineno"> 103</span> <span class="keywordflow">for</span> (uint64_t j = 0; j <= n; j++) {</div>
|
|
<div class="line"><span class="lineno"> 106</span> <span class="keywordflow">if</span> (i == 0) {</div>
|
|
<div class="line"><span class="lineno"> 107</span> <a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>[i][j] = j; </div>
|
|
<div class="line"><span class="lineno"> 108</span> }</div>
|
|
<div class="line"><span class="lineno"> 109</span></div>
|
|
<div class="line"><span class="lineno"> 112</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (j == 0) {</div>
|
|
<div class="line"><span class="lineno"> 113</span> <a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>[i][j] = i; </div>
|
|
<div class="line"><span class="lineno"> 114</span> }</div>
|
|
<div class="line"><span class="lineno"> 115</span></div>
|
|
<div class="line"><span class="lineno"> 118</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (str1[i - 1] == str2[j - 1]) {</div>
|
|
<div class="line"><span class="lineno"> 119</span> <a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>[i][j] = <a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>[i - 1][j - 1];</div>
|
|
<div class="line"><span class="lineno"> 120</span> }</div>
|
|
<div class="line"><span class="lineno"> 121</span></div>
|
|
<div class="line"><span class="lineno"> 124</span> <span class="keywordflow">else</span> {</div>
|
|
<div class="line"><span class="lineno"> 125</span> dp[i][j] = 1 + <a class="code hl_function" href="#a0138c226bd79ffe6d839c787cfc60347">min</a>(dp[i][j - 1], <span class="comment">// Insert</span></div>
|
|
<div class="line"><span class="lineno"> 126</span> dp[i - 1][j], <span class="comment">// Remove</span></div>
|
|
<div class="line"><span class="lineno"> 127</span> dp[i - 1][j - 1]); <span class="comment">// Replace</span></div>
|
|
<div class="line"><span class="lineno"> 128</span> }</div>
|
|
<div class="line"><span class="lineno"> 129</span> }</div>
|
|
<div class="line"><span class="lineno"> 130</span> }</div>
|
|
<div class="line"><span class="lineno"> 131</span> </div>
|
|
<div class="line"><span class="lineno"> 132</span> <span class="keywordflow">return</span> dp[m][n]; </div>
|
|
<div class="line"><span class="lineno"> 134</span>}</div>
|
|
<div class="ttc" id="aminimum__edit__distance_8cpp_html_a0138c226bd79ffe6d839c787cfc60347"><div class="ttname"><a href="#a0138c226bd79ffe6d839c787cfc60347">dynamic_programming::minimum_edit_distance::min</a></div><div class="ttdeci">uint64_t min(uint64_t x, uint64_t y, uint64_t z)</div><div class="ttdoc">Takes input of the cost of three operations: Insert, Replace and Delete and return the minimum cost a...</div><div class="ttdef"><b>Definition</b> <a href="../../da/d52/minimum__edit__distance_8cpp_source.html#l00068">minimum_edit_distance.cpp:68</a></div></div>
|
|
<div class="ttc" id="anamespacedp_html"><div class="ttname"><a href="../../df/d88/namespacedp.html">dp</a></div><div class="ttdoc">for std::vector</div><div class="ttdef"><b>Definition</b> <a href="../../de/d8c/partition__problem_8cpp_source.html#l00039">partition_problem.cpp:39</a></div></div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">◆ </a></span>main()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">int main </td>
|
|
<td>(</td>
|
|
<td class="paramtype">void</td> <td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>main function </p>
|
|
<dl class="section return"><dt>Returns</dt><dd>0 on exit </dd></dl>
|
|
|
|
<p class="definition">Definition at line <a class="el" href="../../da/d52/minimum__edit__distance_8cpp_source.html#l00171">171</a> of file <a class="el" href="../../da/d52/minimum__edit__distance_8cpp_source.html">minimum_edit_distance.cpp</a>.</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 171</span> {</div>
|
|
<div class="line"><span class="lineno"> 172</span> <a class="code hl_function" href="#aa8dca7b867074164d5f45b0f3851269d">test</a>(); <span class="comment">// run self-test implementations</span></div>
|
|
<div class="line"><span class="lineno"> 173</span> <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><span class="lineno"> 174</span>}</div>
|
|
<div class="ttc" id="aminimum__edit__distance_8cpp_html_aa8dca7b867074164d5f45b0f3851269d"><div class="ttname"><a href="#aa8dca7b867074164d5f45b0f3851269d">test</a></div><div class="ttdeci">static void test()</div><div class="ttdoc">Self-test implementations.</div><div class="ttdef"><b>Definition</b> <a href="../../da/d52/minimum__edit__distance_8cpp_source.html#l00142">minimum_edit_distance.cpp:142</a></div></div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="a0138c226bd79ffe6d839c787cfc60347" name="a0138c226bd79ffe6d839c787cfc60347"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a0138c226bd79ffe6d839c787cfc60347">◆ </a></span>min()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">uint64_t dynamic_programming::minimum_edit_distance::min </td>
|
|
<td>(</td>
|
|
<td class="paramtype">uint64_t</td> <td class="paramname"><span class="paramname"><em>x</em></span>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">uint64_t</td> <td class="paramname"><span class="paramname"><em>y</em></span>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">uint64_t</td> <td class="paramname"><span class="paramname"><em>z</em></span> )</td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Takes input of the cost of three operations: Insert, Replace and Delete and return the minimum cost among them. </p>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramname">x</td><td>used to pass minimum cost of Insert operations </td></tr>
|
|
<tr><td class="paramname">y</td><td>used to pass minimum cost of Replace operations </td></tr>
|
|
<tr><td class="paramname">z</td><td>used to pass minimum cost of Delete operations </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<dl class="section return"><dt>Returns</dt><dd>x if <span class="tt">x</span> is the minimum value </dd>
|
|
<dd>
|
|
y if <span class="tt">y</span> is the minimum value </dd>
|
|
<dd>
|
|
z if <span class="tt">z</span> is the minimum value </dd></dl>
|
|
<p>returns x, if x is the minimum value</p>
|
|
<p>returns y, if y is the minimum value</p>
|
|
<p>returns z if z is the minimum value</p>
|
|
|
|
<p class="definition">Definition at line <a class="el" href="../../da/d52/minimum__edit__distance_8cpp_source.html#l00068">68</a> of file <a class="el" href="../../da/d52/minimum__edit__distance_8cpp_source.html">minimum_edit_distance.cpp</a>.</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 68</span> {</div>
|
|
<div class="line"><span class="lineno"> 69</span> <span class="keywordflow">if</span> (x <= y && x <= z) {</div>
|
|
<div class="line"><span class="lineno"> 70</span> <span class="keywordflow">return</span> x; </div>
|
|
<div class="line"><span class="lineno"> 71</span> }</div>
|
|
<div class="line"><span class="lineno"> 72</span> <span class="keywordflow">if</span> (y <= x && y <= z) {</div>
|
|
<div class="line"><span class="lineno"> 73</span> <span class="keywordflow">return</span> y; </div>
|
|
<div class="line"><span class="lineno"> 74</span> } <span class="keywordflow">else</span> {</div>
|
|
<div class="line"><span class="lineno"> 75</span> <span class="keywordflow">return</span> z; </div>
|
|
<div class="line"><span class="lineno"> 76</span> }</div>
|
|
<div class="line"><span class="lineno"> 77</span>}</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="aa8dca7b867074164d5f45b0f3851269d" name="aa8dca7b867074164d5f45b0f3851269d"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#aa8dca7b867074164d5f45b0f3851269d">◆ </a></span>test()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="mlabels">
|
|
<tr>
|
|
<td class="mlabels-left">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void test </td>
|
|
<td>(</td>
|
|
<td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
</td>
|
|
<td class="mlabels-right">
|
|
<span class="mlabels"><span class="mlabel static">static</span></span> </td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Self-test implementations. </p>
|
|
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
|
|
|
|
<p class="definition">Definition at line <a class="el" href="../../da/d52/minimum__edit__distance_8cpp_source.html#l00142">142</a> of file <a class="el" href="../../da/d52/minimum__edit__distance_8cpp_source.html">minimum_edit_distance.cpp</a>.</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 142</span> {</div>
|
|
<div class="line"><span class="lineno"> 143</span> <span class="comment">// 1st test</span></div>
|
|
<div class="line"><span class="lineno"> 144</span> std::string str1 = <span class="stringliteral">"INTENTION"</span>; <span class="comment">// Sample input of 1st string</span></div>
|
|
<div class="line"><span class="lineno"> 145</span> std::string str2 = <span class="stringliteral">"EXECUTION"</span>; <span class="comment">// Sample input of 2nd string</span></div>
|
|
<div class="line"><span class="lineno"> 146</span> uint64_t expected_output1 = 5; <span class="comment">// Expected minimum cost</span></div>
|
|
<div class="line"><span class="lineno"> 147</span> uint64_t output1 = <a class="code hl_function" href="#ade2fcfe0359f3c7691bfaa04b14943e2">dynamic_programming::minimum_edit_distance::editDistDP</a>(</div>
|
|
<div class="line"><span class="lineno"> 148</span> str1, str2, str1.length(),</div>
|
|
<div class="line"><span class="lineno"> 149</span> str2.length()); <span class="comment">// calling the editDistDP function and storing the</span></div>
|
|
<div class="line"><span class="lineno"> 150</span> <span class="comment">// result on output1</span></div>
|
|
<div class="line"><span class="lineno"> 151</span> assert(output1 ==</div>
|
|
<div class="line"><span class="lineno"> 152</span> expected_output1); <span class="comment">// comparing the output with the expected output</span></div>
|
|
<div class="line"><span class="lineno"> 153</span> std::cout << <span class="stringliteral">"Minimum Number of Operations Required: "</span> << output1</div>
|
|
<div class="line"><span class="lineno"> 154</span> << std::endl;</div>
|
|
<div class="line"><span class="lineno"> 155</span> </div>
|
|
<div class="line"><span class="lineno"> 156</span> <span class="comment">// 2nd test</span></div>
|
|
<div class="line"><span class="lineno"> 157</span> std::string str3 = <span class="stringliteral">"SATURDAY"</span>;</div>
|
|
<div class="line"><span class="lineno"> 158</span> std::string str4 = <span class="stringliteral">"SUNDAY"</span>;</div>
|
|
<div class="line"><span class="lineno"> 159</span> uint64_t expected_output2 = 3;</div>
|
|
<div class="line"><span class="lineno"> 160</span> uint64_t output2 = <a class="code hl_function" href="#ade2fcfe0359f3c7691bfaa04b14943e2">dynamic_programming::minimum_edit_distance::editDistDP</a>(</div>
|
|
<div class="line"><span class="lineno"> 161</span> str3, str4, str3.length(), str4.length());</div>
|
|
<div class="line"><span class="lineno"> 162</span> assert(output2 == expected_output2);</div>
|
|
<div class="line"><span class="lineno"> 163</span> std::cout << <span class="stringliteral">"Minimum Number of Operations Required: "</span> << output2</div>
|
|
<div class="line"><span class="lineno"> 164</span> << std::endl;</div>
|
|
<div class="line"><span class="lineno"> 165</span>}</div>
|
|
<div class="ttc" id="aminimum__edit__distance_8cpp_html_ade2fcfe0359f3c7691bfaa04b14943e2"><div class="ttname"><a href="#ade2fcfe0359f3c7691bfaa04b14943e2">dynamic_programming::minimum_edit_distance::editDistDP</a></div><div class="ttdeci">uint64_t editDistDP(std::string str1, std::string str2, uint64_t m, uint64_t n)</div><div class="ttdoc">Calculates and stores the result of all the sub-problems, so that we don't have to recur to compute t...</div><div class="ttdef"><b>Definition</b> <a href="../../da/d52/minimum__edit__distance_8cpp_source.html#l00092">minimum_edit_distance.cpp:92</a></div></div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
</div><!-- contents -->
|
|
</div><!-- doc-content -->
|
|
<div id="page-nav" class="page-nav-panel">
|
|
<div id="page-nav-resize-handle"></div>
|
|
<div id="page-nav-tree">
|
|
<div id="page-nav-contents">
|
|
</div><!-- page-nav-contents -->
|
|
</div><!-- page-nav-tree -->
|
|
</div><!-- page-nav -->
|
|
</div><!-- container -->
|
|
<!-- start footer part -->
|
|
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
|
<ul>
|
|
<li class="navelem"><a href="../../dir_8a20dd5bfd5341a725342bf72b6b686f.html">dynamic_programming</a></li><li class="navelem"><a href="../../da/d52/minimum__edit__distance_8cpp.html">minimum_edit_distance.cpp</a></li>
|
|
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.14.0 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|