Files
C-Plus-Plus/da/d52/minimum__edit__distance_8cpp.html
2023-01-22 19:44:10 +00:00

440 lines
33 KiB
HTML

<!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.9.6"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C++: dynamic_programming/minimum_edit_distance.cpp File Reference</title>
<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>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.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/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.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="projectalign">
<div id="projectname">Algorithms_in_C++<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C++.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.6 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</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">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('da/d52/minimum__edit__distance_8cpp.html','../../'); initResizable(); });
/* @license-end */
</script>
<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="summary">
<a href="#namespaces">Namespaces</a> &#124;
<a href="#func-members">Functions</a> </div>
<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 &lt;cassert&gt;</code><br />
<code>#include &lt;iostream&gt;</code><br />
<code>#include &lt;vector&gt;</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" frameborder="0" src="../../de/d3d/minimum__edit__distance_8cpp__incl.svg" width="256" height="124"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="namespaces" name="namespaces"></a>
Namespaces</h2></td></tr>
<tr class="memitem:dd/d24/namespacedynamic__programming"><td class="memItemLeft" align="right" valign="top">namespace &#160;</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">&#160;</td><td class="mdescRight">Dynamic Programming algorithms. <br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:d4/d12/namespace_minimum"><td class="memItemLeft" align="right" valign="top">namespace &#160;</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">&#160;</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>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a0138c226bd79ffe6d839c787cfc60347"><td class="memItemLeft" align="right" valign="top">uint64_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d52/minimum__edit__distance_8cpp.html#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">&#160;</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="separator:a0138c226bd79ffe6d839c787cfc60347"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ade2fcfe0359f3c7691bfaa04b14943e2"><td class="memItemLeft" align="right" valign="top">uint64_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d52/minimum__edit__distance_8cpp.html#ade2fcfe0359f3c7691bfaa04b14943e2">dynamic_programming::minimum_edit_distance::editDistDP</a> (<a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> str1, <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> str2, uint64_t m, uint64_t n)</td></tr>
<tr class="memdesc:ade2fcfe0359f3c7691bfaa04b14943e2"><td class="mdescLeft">&#160;</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 <code>dp</code> vector. <br /></td></tr>
<tr class="separator:ade2fcfe0359f3c7691bfaa04b14943e2"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:aa8dca7b867074164d5f45b0f3851269d"><td class="memItemLeft" align="right" valign="top">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d52/minimum__edit__distance_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a> ()</td></tr>
<tr class="memdesc:aa8dca7b867074164d5f45b0f3851269d"><td class="mdescLeft">&#160;</td><td class="mdescRight">Self-test implementations. <br /></td></tr>
<tr class="separator:aa8dca7b867074164d5f45b0f3851269d"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a0ddf1224851353fc92bfbff6f499fa97"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d52/minimum__edit__distance_8cpp.html#a0ddf1224851353fc92bfbff6f499fa97">main</a> (int argc, char *argv[])</td></tr>
<tr class="memdesc:a0ddf1224851353fc92bfbff6f499fa97"><td class="mdescLeft">&#160;</td><td class="mdescRight">main function <br /></td></tr>
<tr class="separator:a0ddf1224851353fc92bfbff6f499fa97"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 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 &amp; str2 and we have to calculate the minimum number of operations (Insert, Remove, Replace) required to convert str1 to str2.</p>
<h3><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>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="ade2fcfe0359f3c7691bfaa04b14943e2" name="ade2fcfe0359f3c7691bfaa04b14943e2"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ade2fcfe0359f3c7691bfaa04b14943e2">&#9670;&#160;</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"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a>&#160;</td>
<td class="paramname"><em>str1</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a>&#160;</td>
<td class="paramname"><em>str2</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">uint64_t&#160;</td>
<td class="paramname"><em>m</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">uint64_t&#160;</td>
<td class="paramname"><em>n</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></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 <code>dp</code> 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>
<div class="fragment"><div class="line"><span class="lineno"> 88</span> {<span class="comment"></span></div>
<div class="line"><span class="lineno"> 89</span><span class="comment"> /// Create a table to store results of subproblems</span></div>
<div class="line"><span class="lineno"> 90</span><span class="comment"></span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector&lt;std::vector&lt;uint64_t&gt;</a>&gt;<a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>(m+1, <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector&lt;uint64_t&gt;</a>(n+1)); <span class="comment">/// creasting 2D vector dp to store the results of subproblems</span></div>
<div class="line"><span class="lineno"> 91</span><span class="comment"></span> </div>
<div class="line"><span class="lineno"> 92</span><span class="comment"> /// Fill d[][] in bottom up manner</span></div>
<div class="line"><span class="lineno"> 93</span><span class="comment"></span> <span class="keywordflow">for</span> (uint64_t i = 0; i &lt;= m; i++) {</div>
<div class="line"><span class="lineno"> 94</span> <span class="keywordflow">for</span> (uint64_t j = 0; j &lt;= n; j++) {<span class="comment"></span></div>
<div class="line"><span class="lineno"> 95</span><span class="comment"> /// If first string is empty, only option is to</span></div>
<div class="line"><span class="lineno"> 96</span><span class="comment"> /// insert all characters of second string</span></div>
<div class="line"><span class="lineno"> 97</span><span class="comment"></span> <span class="keywordflow">if</span> (i == 0) {</div>
<div class="line"><span class="lineno"> 98</span> <a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>[i][j] = j; <span class="comment">/// Minimum operations = j</span></div>
<div class="line"><span class="lineno"> 99</span> }</div>
<div class="line"><span class="lineno"> 100</span><span class="comment"></span> </div>
<div class="line"><span class="lineno"> 101</span><span class="comment"> /// If second string is empty, only option is to</span></div>
<div class="line"><span class="lineno"> 102</span><span class="comment"> /// remove all characters of second string</span></div>
<div class="line"><span class="lineno"> 103</span><span class="comment"></span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (j == 0) {</div>
<div class="line"><span class="lineno"> 104</span> <a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>[i][j] = i; <span class="comment">/// Minimum operations = i</span></div>
<div class="line"><span class="lineno"> 105</span> }</div>
<div class="line"><span class="lineno"> 106</span><span class="comment"></span> </div>
<div class="line"><span class="lineno"> 107</span><span class="comment"> /// If last characters are same, ignore last char</span></div>
<div class="line"><span class="lineno"> 108</span><span class="comment"> /// and recur for remaining string</span></div>
<div class="line"><span class="lineno"> 109</span><span class="comment"></span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (str1[i - 1] == str2[j - 1]) {</div>
<div class="line"><span class="lineno"> 110</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"> 111</span> }</div>
<div class="line"><span class="lineno"> 112</span><span class="comment"></span> </div>
<div class="line"><span class="lineno"> 113</span><span class="comment"> /// If the last character is different, consider all</span></div>
<div class="line"><span class="lineno"> 114</span><span class="comment"> /// possibilities and find the minimum</span></div>
<div class="line"><span class="lineno"> 115</span><span class="comment"></span> <span class="keywordflow">else</span> {</div>
<div class="line"><span class="lineno"> 116</span> <a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>[i][j] = 1 + <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/algorithm/min.html">min</a>(<a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>[i][j - 1], <span class="comment">// Insert</span></div>
<div class="line"><span class="lineno"> 117</span> <a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>[i - 1][j], <span class="comment">// Remove</span></div>
<div class="line"><span class="lineno"> 118</span> <a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>[i - 1][j - 1]); <span class="comment">// Replace</span></div>
<div class="line"><span class="lineno"> 119</span> }</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"> 122</span> </div>
<div class="line"><span class="lineno"> 123</span> <span class="keywordflow">return</span> <a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>[m][n]; <span class="comment">/// returning the minimum cost of operations needed to convert str1 to str2</span></div>
<div class="line"><span class="lineno"> 124</span>}</div>
<div class="ttc" id="amin_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/algorithm/min.html">std::min</a></div><div class="ttdeci">T min(T... args)</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> partition_problem.cpp:39</div></div>
<div class="ttc" id="avector_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a></div></div>
</div><!-- fragment -->
</div>
</div>
<a id="a0ddf1224851353fc92bfbff6f499fa97" name="a0ddf1224851353fc92bfbff6f499fa97"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a0ddf1224851353fc92bfbff6f499fa97">&#9670;&#160;</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">int&#160;</td>
<td class="paramname"><em>argc</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">char *&#160;</td>
<td class="paramname"><em>argv</em>[]&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>main function </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">argc</td><td>commandline argument count (ignored) </td></tr>
<tr><td class="paramname">argv</td><td>commandline array of arguments (ignored) </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>0 on exit </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 160</span> {</div>
<div class="line"><span class="lineno"> 161</span> <a class="code hl_function" href="../../da/d52/minimum__edit__distance_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a>(); <span class="comment">// run self-test implementations</span></div>
<div class="line"><span class="lineno"> 162</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 163</span>}</div>
<div class="ttc" id="aminimum__edit__distance_8cpp_html_aa8dca7b867074164d5f45b0f3851269d"><div class="ttname"><a href="../../da/d52/minimum__edit__distance_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a></div><div class="ttdeci">static void test()</div><div class="ttdoc">Self-test implementations.</div><div class="ttdef"><b>Definition:</b> minimum_edit_distance.cpp:132</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/d52/minimum__edit__distance_8cpp_a0ddf1224851353fc92bfbff6f499fa97_cgraph.svg" width="324" height="86"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="a0138c226bd79ffe6d839c787cfc60347" name="a0138c226bd79ffe6d839c787cfc60347"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a0138c226bd79ffe6d839c787cfc60347">&#9670;&#160;</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&#160;</td>
<td class="paramname"><em>x</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">uint64_t&#160;</td>
<td class="paramname"><em>y</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">uint64_t&#160;</td>
<td class="paramname"><em>z</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></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 <code>x</code> is the minimum value </dd>
<dd>
y if <code>y</code> is the minimum value </dd>
<dd>
z if <code>z</code> 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>
<div class="fragment"><div class="line"><span class="lineno"> 63</span> {</div>
<div class="line"><span class="lineno"> 64</span> <span class="keywordflow">if</span> (x &lt;= y &amp;&amp; x &lt;= z) {</div>
<div class="line"><span class="lineno"> 65</span> <span class="keywordflow">return</span> x; <span class="comment">/// returns x, if x is the minimum value</span></div>
<div class="line"><span class="lineno"> 66</span> }</div>
<div class="line"><span class="lineno"> 67</span> <span class="keywordflow">if</span> (y &lt;= x &amp;&amp; y &lt;= z) {</div>
<div class="line"><span class="lineno"> 68</span> <span class="keywordflow">return</span> y; <span class="comment">/// returns y, if y is the minimum value</span></div>
<div class="line"><span class="lineno"> 69</span> }</div>
<div class="line"><span class="lineno"> 70</span> <span class="keywordflow">else</span> {</div>
<div class="line"><span class="lineno"> 71</span> <span class="keywordflow">return</span> z; <span class="comment">/// returns z if z is the minimum value</span></div>
<div class="line"><span class="lineno"> 72</span> }</div>
<div class="line"><span class="lineno"> 73</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="aa8dca7b867074164d5f45b0f3851269d" name="aa8dca7b867074164d5f45b0f3851269d"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aa8dca7b867074164d5f45b0f3851269d">&#9670;&#160;</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">static void test </td>
<td>(</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</td>
<td class="mlabels-right">
<span class="mlabels"><span class="mlabel">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>
<div class="fragment"><div class="line"><span class="lineno"> 132</span> {</div>
<div class="line"><span class="lineno"> 133</span> <span class="comment">// 1st test</span></div>
<div class="line"><span class="lineno"> 134</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> str1 = <span class="stringliteral">&quot;INTENTION&quot;</span>; <span class="comment">// Sample input of 1st string</span></div>
<div class="line"><span class="lineno"> 135</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> str2 = <span class="stringliteral">&quot;EXECUTION&quot;</span>; <span class="comment">// Sample input of 2nd string</span></div>
<div class="line"><span class="lineno"> 136</span> uint64_t expected_output1 = 5; <span class="comment">// Expected minimum cost</span></div>
<div class="line"><span class="lineno"> 137</span> uint64_t output1 = <a class="code hl_function" href="../../da/d52/minimum__edit__distance_8cpp.html#ade2fcfe0359f3c7691bfaa04b14943e2">dynamic_programming::minimum_edit_distance::editDistDP</a>(</div>
<div class="line"><span class="lineno"> 138</span> str1, str2, str1.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string/size.html">length</a>(), str2.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string/size.html">length</a>()); <span class="comment">// calling the editDistDP function and storing the result on output1</span></div>
<div class="line"><span class="lineno"> 139</span> assert(output1 == expected_output1); <span class="comment">// comparing the output with the expected output</span></div>
<div class="line"><span class="lineno"> 140</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Minimum Number of Operations Required: &quot;</span> &lt;&lt; output1</div>
<div class="line"><span class="lineno"> 141</span> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 142</span> </div>
<div class="line"><span class="lineno"> 143</span> <span class="comment">// 2nd test</span></div>
<div class="line"><span class="lineno"> 144</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> str3 = <span class="stringliteral">&quot;SATURDAY&quot;</span>;</div>
<div class="line"><span class="lineno"> 145</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> str4 = <span class="stringliteral">&quot;SUNDAY&quot;</span>;</div>
<div class="line"><span class="lineno"> 146</span> uint64_t expected_output2 = 3;</div>
<div class="line"><span class="lineno"> 147</span> uint64_t output2 = <a class="code hl_function" href="../../da/d52/minimum__edit__distance_8cpp.html#ade2fcfe0359f3c7691bfaa04b14943e2">dynamic_programming::minimum_edit_distance::editDistDP</a>(</div>
<div class="line"><span class="lineno"> 148</span> str3, str4, str3.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string/size.html">length</a>(), str4.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string/size.html">length</a>());</div>
<div class="line"><span class="lineno"> 149</span> assert(output2 == expected_output2);</div>
<div class="line"><span class="lineno"> 150</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Minimum Number of Operations Required: &quot;</span> &lt;&lt; output2</div>
<div class="line"><span class="lineno"> 151</span> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 152</span>}</div>
<div class="ttc" id="abasic_ostream_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a></div></div>
<div class="ttc" id="abasic_string_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a></div></div>
<div class="ttc" id="aendl_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a></div><div class="ttdeci">T endl(T... args)</div></div>
<div class="ttc" id="aminimum__edit__distance_8cpp_html_ade2fcfe0359f3c7691bfaa04b14943e2"><div class="ttname"><a href="../../da/d52/minimum__edit__distance_8cpp.html#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> minimum_edit_distance.cpp:88</div></div>
<div class="ttc" id="asize_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/string/basic_string/size.html">std::string::length</a></div><div class="ttdeci">T length(T... args)</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/d52/minimum__edit__distance_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg" width="226" height="86"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_8a20dd5bfd5341a725342bf72b6b686f.html">dynamic_programming</a></li><li class="navelem"><a class="el" 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.9.6 </li>
</ul>
</div>
</body>
</html>