TheAlgorithms-C/db/de1/lcs_8c.html
2023-01-20 04:47:59 +00:00

490 lines
32 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.5"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: dynamic_programming/lcs.c 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.5 -->
<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('db/de1/lcs_8c.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="#enum-members">Enumerations</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle"><div class="title">lcs.c File Reference</div></div>
</div><!--header-->
<div class="contents">
<p><a href="https://en.wikipedia.org/wiki/Longest_common_subsequence_problem" target="_blank">Longest Common Subsequence</a> algorithm
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;stdio.h&gt;</code><br />
<code>#include &lt;stdlib.h&gt;</code><br />
<code>#include &lt;string.h&gt;</code><br />
<code>#include &lt;assert.h&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for lcs.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d9/dc3/lcs_8c__incl.svg" width="344" 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="enum-members" name="enum-members"></a>
Enumerations</h2></td></tr>
<tr class="memitem:a06fc87d81c62e9abb8790b6e5713c55b"><td class="memItemLeft" align="right" valign="top">enum &#160;</td><td class="memItemRight" valign="bottom">{ <b>LEFT</b>
, <b>UP</b>
, <b>DIAG</b>
}</td></tr>
<tr class="separator:a06fc87d81c62e9abb8790b6e5713c55b"><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:ac107a0b0233dacb9e8cd3a983d6740b6"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../db/de1/lcs_8c.html#ac107a0b0233dacb9e8cd3a983d6740b6">lcslen</a> (const char *s1, const char *s2, int l1, int l2, int **<a class="el" href="../../df/db3/struct_l.html">L</a>, int **B)</td></tr>
<tr class="memdesc:ac107a0b0233dacb9e8cd3a983d6740b6"><td class="mdescLeft">&#160;</td><td class="mdescRight">@breif Computes LCS between s1 and s2 using a dynamic-programming approach <a href="../../db/de1/lcs_8c.html#ac107a0b0233dacb9e8cd3a983d6740b6">More...</a><br /></td></tr>
<tr class="separator:ac107a0b0233dacb9e8cd3a983d6740b6"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a741c9173a9fef243518f5c2352b81fc0"><td class="memItemLeft" align="right" valign="top">char *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../db/de1/lcs_8c.html#a741c9173a9fef243518f5c2352b81fc0">lcsbuild</a> (const char *s1, int l1, int l2, int **<a class="el" href="../../df/db3/struct_l.html">L</a>, int **B)</td></tr>
<tr class="memdesc:a741c9173a9fef243518f5c2352b81fc0"><td class="mdescLeft">&#160;</td><td class="mdescRight">@breif Builds the LCS according to B using a traceback approach <a href="../../db/de1/lcs_8c.html#a741c9173a9fef243518f5c2352b81fc0">More...</a><br /></td></tr>
<tr class="separator:a741c9173a9fef243518f5c2352b81fc0"><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="../../db/de1/lcs_8c.html#aa8dca7b867074164d5f45b0f3851269d">test</a> ()</td></tr>
<tr class="memdesc:aa8dca7b867074164d5f45b0f3851269d"><td class="mdescLeft">&#160;</td><td class="mdescRight">Self-test implementations. <a href="../../db/de1/lcs_8c.html#aa8dca7b867074164d5f45b0f3851269d">More...</a><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="../../db/de1/lcs_8c.html#a0ddf1224851353fc92bfbff6f499fa97">main</a> (int argc, char *argv[])</td></tr>
<tr class="memdesc:a0ddf1224851353fc92bfbff6f499fa97"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main function. <a href="../../db/de1/lcs_8c.html#a0ddf1224851353fc92bfbff6f499fa97">More...</a><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 ><a href="https://en.wikipedia.org/wiki/Longest_common_subsequence_problem" target="_blank">Longest Common Subsequence</a> algorithm </p>
<p >From Wikipedia: The longest common subsequence (LCS) problem is the problem of finding the longest subsequence common to all sequences in a set of sequences (often just two sequences). </p><dl class="section author"><dt>Author</dt><dd><a href="https://github.com/itskurtz" target="_blank">Kurtz</a> </dd></dl>
</div><h2 class="groupheader">Enumeration Type Documentation</h2>
<a id="a06fc87d81c62e9abb8790b6e5713c55b" name="a06fc87d81c62e9abb8790b6e5713c55b"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a06fc87d81c62e9abb8790b6e5713c55b">&#9670;&#160;</a></span>anonymous enum</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">anonymous enum</td>
</tr>
</table>
</div><div class="memdoc">
<div class="fragment"><div class="line"><span class="lineno"> 15</span>{LEFT, UP, DIAG};</div>
</div><!-- fragment -->
</div>
</div>
<h2 class="groupheader">Function Documentation</h2>
<a id="a741c9173a9fef243518f5c2352b81fc0" name="a741c9173a9fef243518f5c2352b81fc0"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a741c9173a9fef243518f5c2352b81fc0">&#9670;&#160;</a></span>lcsbuild()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">char * lcsbuild </td>
<td>(</td>
<td class="paramtype">const char *&#160;</td>
<td class="paramname"><em>s1</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>l1</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>l2</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int **&#160;</td>
<td class="paramname"><em>L</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int **&#160;</td>
<td class="paramname"><em>B</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>@breif Builds the LCS according to B using a traceback approach </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">1</td><td>s1 first null-terminated string </td></tr>
<tr><td class="paramname">2</td><td>l1 length of s1 </td></tr>
<tr><td class="paramname">3</td><td>l2 length of s2 </td></tr>
<tr><td class="paramname">4</td><td><a class="el" href="../../df/db3/struct_l.html">L</a> matrix of size l1 x l2 </td></tr>
<tr><td class="paramname">5</td><td>B matrix of size l1 x l2 </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>lcs longest common subsequence </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 59</span> {</div>
<div class="line"><span class="lineno"> 60</span> <span class="keywordtype">int</span> i, j, lcsl;</div>
<div class="line"><span class="lineno"> 61</span> <span class="keywordtype">char</span> *lcs;</div>
<div class="line"><span class="lineno"> 62</span> lcsl = <a class="code hl_struct" href="../../df/db3/struct_l.html">L</a>[l1][l2];</div>
<div class="line"><span class="lineno"> 63</span> </div>
<div class="line"><span class="lineno"> 64</span> <span class="comment">/* my lcs is at least the empty symbol */</span></div>
<div class="line"><span class="lineno"> 65</span> lcs = (<span class="keywordtype">char</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#afdddaa949a93c1ef559a638e98f9c21b">calloc</a>(lcsl+1, <span class="keyword">sizeof</span>(<span class="keywordtype">char</span>)); <span class="comment">/* null-terminated \0 */</span></div>
<div class="line"><span class="lineno"> 66</span> <span class="keywordflow">if</span> (!lcs) {</div>
<div class="line"><span class="lineno"> 67</span> perror(<span class="stringliteral">&quot;calloc: &quot;</span>);</div>
<div class="line"><span class="lineno"> 68</span> <span class="keywordflow">return</span> NULL;</div>
<div class="line"><span class="lineno"> 69</span> }</div>
<div class="line"><span class="lineno"> 70</span> </div>
<div class="line"><span class="lineno"> 71</span> i = l1, j = l2;</div>
<div class="line"><span class="lineno"> 72</span> <span class="keywordflow">while</span> (i &gt; 0 &amp;&amp; j &gt; 0) {</div>
<div class="line"><span class="lineno"> 73</span> <span class="comment">/* walk the matrix backwards */</span></div>
<div class="line"><span class="lineno"> 74</span> <span class="keywordflow">if</span> (B[i][j] == DIAG) {</div>
<div class="line"><span class="lineno"> 75</span> lcs[--lcsl] = s1[i-1];</div>
<div class="line"><span class="lineno"> 76</span> i = i - 1;</div>
<div class="line"><span class="lineno"> 77</span> j = j - 1;</div>
<div class="line"><span class="lineno"> 78</span> }</div>
<div class="line"><span class="lineno"> 79</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (B[i][j] == LEFT)</div>
<div class="line"><span class="lineno"> 80</span> j = j - 1;</div>
<div class="line"><span class="lineno"> 81</span> <span class="keywordflow">else</span></div>
<div class="line"><span class="lineno"> 82</span> i = i - 1;</div>
<div class="line"><span class="lineno"> 83</span> }</div>
<div class="line"><span class="lineno"> 84</span> <span class="keywordflow">return</span> lcs;</div>
<div class="line"><span class="lineno"> 85</span>}</div>
<div class="ttc" id="amalloc__dbg_8h_html_afdddaa949a93c1ef559a638e98f9c21b"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#afdddaa949a93c1ef559a638e98f9c21b">calloc</a></div><div class="ttdeci">#define calloc(elemCount, elemSize)</div><div class="ttdoc">This macro replace the standard calloc function with calloc_dbg.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.h:22</div></div>
<div class="ttc" id="astruct_l_html"><div class="ttname"><a href="../../df/db3/struct_l.html">L</a></div><div class="ttdef"><b>Definition:</b> list.h:8</div></div>
</div><!-- fragment -->
</div>
</div>
<a id="ac107a0b0233dacb9e8cd3a983d6740b6" name="ac107a0b0233dacb9e8cd3a983d6740b6"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ac107a0b0233dacb9e8cd3a983d6740b6">&#9670;&#160;</a></span>lcslen()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void lcslen </td>
<td>(</td>
<td class="paramtype">const char *&#160;</td>
<td class="paramname"><em>s1</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const char *&#160;</td>
<td class="paramname"><em>s2</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>l1</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>l2</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int **&#160;</td>
<td class="paramname"><em>L</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int **&#160;</td>
<td class="paramname"><em>B</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>@breif Computes LCS between s1 and s2 using a dynamic-programming approach </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">1</td><td>s1 first null-terminated string </td></tr>
<tr><td class="paramname">2</td><td>s2 second null-terminated string </td></tr>
<tr><td class="paramname">3</td><td>l1 length of s1 </td></tr>
<tr><td class="paramname">4</td><td>l2 length of s2 </td></tr>
<tr><td class="paramname">5</td><td><a class="el" href="../../df/db3/struct_l.html">L</a> matrix of size l1 x l2 </td></tr>
<tr><td class="paramname">6</td><td>B matrix of size l1 x l2 </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 27</span> {</div>
<div class="line"><span class="lineno"> 28</span> <span class="comment">/* B is the directions matrix</span></div>
<div class="line"><span class="lineno"> 29</span><span class="comment"> L is the LCS matrix */</span></div>
<div class="line"><span class="lineno"> 30</span> <span class="keywordtype">int</span> i, j;</div>
<div class="line"><span class="lineno"> 31</span> </div>
<div class="line"><span class="lineno"> 32</span> <span class="comment">/* loop over the simbols in my sequences</span></div>
<div class="line"><span class="lineno"> 33</span><span class="comment"> save the directions according to the LCS */</span></div>
<div class="line"><span class="lineno"> 34</span> <span class="keywordflow">for</span> (i = 1; i &lt;= l1; ++i)</div>
<div class="line"><span class="lineno"> 35</span> <span class="keywordflow">for</span> (j = 1; j &lt;= l2; ++j)</div>
<div class="line"><span class="lineno"> 36</span> <span class="keywordflow">if</span> (s1[i-1] == s2[j-1]) {</div>
<div class="line"><span class="lineno"> 37</span> <a class="code hl_struct" href="../../df/db3/struct_l.html">L</a>[i][j] = 1 + <a class="code hl_struct" href="../../df/db3/struct_l.html">L</a>[i-1][j-1];</div>
<div class="line"><span class="lineno"> 38</span> B[i][j] = DIAG;</div>
<div class="line"><span class="lineno"> 39</span> }</div>
<div class="line"><span class="lineno"> 40</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<a class="code hl_struct" href="../../df/db3/struct_l.html">L</a>[i-1][j] &lt; <a class="code hl_struct" href="../../df/db3/struct_l.html">L</a>[i][j-1]) {</div>
<div class="line"><span class="lineno"> 41</span> <a class="code hl_struct" href="../../df/db3/struct_l.html">L</a>[i][j] = <a class="code hl_struct" href="../../df/db3/struct_l.html">L</a>[i][j-1];</div>
<div class="line"><span class="lineno"> 42</span> B[i][j] = LEFT;</div>
<div class="line"><span class="lineno"> 43</span> }</div>
<div class="line"><span class="lineno"> 44</span> <span class="keywordflow">else</span> {</div>
<div class="line"><span class="lineno"> 45</span> <a class="code hl_struct" href="../../df/db3/struct_l.html">L</a>[i][j] = <a class="code hl_struct" href="../../df/db3/struct_l.html">L</a>[i-1][j];</div>
<div class="line"><span class="lineno"> 46</span> B[i][j] = UP;</div>
<div class="line"><span class="lineno"> 47</span> }</div>
<div class="line"><span class="lineno"> 48</span>}</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"> 150</span> {</div>
<div class="line"><span class="lineno"> 151</span> <a class="code hl_function" href="../../db/de1/lcs_8c.html#aa8dca7b867074164d5f45b0f3851269d">test</a>(); <span class="comment">// run self-test implementations</span></div>
<div class="line"><span class="lineno"> 152</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 153</span>}</div>
<div class="ttc" id="alcs_8c_html_aa8dca7b867074164d5f45b0f3851269d"><div class="ttname"><a href="../../db/de1/lcs_8c.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> lcs.c:90</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="../../db/de1/lcs_8c_a0ddf1224851353fc92bfbff6f499fa97_cgraph.svg" width="268" 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="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="paramtype">void&#160;</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"> 90</span> {</div>
<div class="line"><span class="lineno"> 91</span> <span class="comment">/* https://en.wikipedia.org/wiki/Subsequence#Applications */</span></div>
<div class="line"><span class="lineno"> 92</span> <span class="keywordtype">int</span> **<a class="code hl_struct" href="../../df/db3/struct_l.html">L</a>, **B, j, l1, l2;</div>
<div class="line"><span class="lineno"> 93</span> </div>
<div class="line"><span class="lineno"> 94</span> <span class="keywordtype">char</span> *s1 = <span class="stringliteral">&quot;ACGGTGTCGTGCTATGCTGATGCTGACTTATATGCTA&quot;</span>;</div>
<div class="line"><span class="lineno"> 95</span> <span class="keywordtype">char</span> *s2 = <span class="stringliteral">&quot;CGTTCGGCTATCGTACGTTCTATTCTATGATTTCTAA&quot;</span>;</div>
<div class="line"><span class="lineno"> 96</span> <span class="keywordtype">char</span> *lcs;</div>
<div class="line"><span class="lineno"> 97</span> </div>
<div class="line"><span class="lineno"> 98</span> l1 = strlen(s1);</div>
<div class="line"><span class="lineno"> 99</span> l2 = strlen(s2);</div>
<div class="line"><span class="lineno"> 100</span> </div>
<div class="line"><span class="lineno"> 101</span> <a class="code hl_struct" href="../../df/db3/struct_l.html">L</a> = (<span class="keywordtype">int</span> **)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#afdddaa949a93c1ef559a638e98f9c21b">calloc</a>(l1+1, <span class="keyword">sizeof</span>(<span class="keywordtype">int</span> *));</div>
<div class="line"><span class="lineno"> 102</span> B = (<span class="keywordtype">int</span> **)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#afdddaa949a93c1ef559a638e98f9c21b">calloc</a>(l1+1, <span class="keyword">sizeof</span>(<span class="keywordtype">int</span> *));</div>
<div class="line"><span class="lineno"> 103</span> </div>
<div class="line"><span class="lineno"> 104</span> <span class="keywordflow">if</span> (!<a class="code hl_struct" href="../../df/db3/struct_l.html">L</a>) {</div>
<div class="line"><span class="lineno"> 105</span> perror(<span class="stringliteral">&quot;calloc: &quot;</span>);</div>
<div class="line"><span class="lineno"> 106</span> exit(1);</div>
<div class="line"><span class="lineno"> 107</span> }</div>
<div class="line"><span class="lineno"> 108</span> <span class="keywordflow">if</span> (!B) {</div>
<div class="line"><span class="lineno"> 109</span> perror(<span class="stringliteral">&quot;calloc: &quot;</span>);</div>
<div class="line"><span class="lineno"> 110</span> exit(1);</div>
<div class="line"><span class="lineno"> 111</span> }</div>
<div class="line"><span class="lineno"> 112</span> <span class="keywordflow">for</span> (j = 0; j &lt;= l1; j++) {</div>
<div class="line"><span class="lineno"> 113</span> <a class="code hl_struct" href="../../df/db3/struct_l.html">L</a>[j] = (<span class="keywordtype">int</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#afdddaa949a93c1ef559a638e98f9c21b">calloc</a>(l2+1, <span class="keyword">sizeof</span>(<span class="keywordtype">int</span>));</div>
<div class="line"><span class="lineno"> 114</span> <span class="keywordflow">if</span> (!<a class="code hl_struct" href="../../df/db3/struct_l.html">L</a>[j]) {</div>
<div class="line"><span class="lineno"> 115</span> perror(<span class="stringliteral">&quot;calloc: &quot;</span>);</div>
<div class="line"><span class="lineno"> 116</span> exit(1);</div>
<div class="line"><span class="lineno"> 117</span> }</div>
<div class="line"><span class="lineno"> 118</span> B[j] = (<span class="keywordtype">int</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#afdddaa949a93c1ef559a638e98f9c21b">calloc</a>(l2+1, <span class="keyword">sizeof</span>(<span class="keywordtype">int</span>));</div>
<div class="line"><span class="lineno"> 119</span> <span class="keywordflow">if</span> (!<a class="code hl_struct" href="../../df/db3/struct_l.html">L</a>[j]) {</div>
<div class="line"><span class="lineno"> 120</span> perror(<span class="stringliteral">&quot;calloc: &quot;</span>);</div>
<div class="line"><span class="lineno"> 121</span> exit(1);</div>
<div class="line"><span class="lineno"> 122</span> }</div>
<div class="line"><span class="lineno"> 123</span> }</div>
<div class="line"><span class="lineno"> 124</span> </div>
<div class="line"><span class="lineno"> 125</span> <a class="code hl_function" href="../../db/de1/lcs_8c.html#ac107a0b0233dacb9e8cd3a983d6740b6">lcslen</a>(s1, s2, l1, l2, <a class="code hl_struct" href="../../df/db3/struct_l.html">L</a>, B);</div>
<div class="line"><span class="lineno"> 126</span> lcs = <a class="code hl_function" href="../../db/de1/lcs_8c.html#a741c9173a9fef243518f5c2352b81fc0">lcsbuild</a>(s1, l1, l2, <a class="code hl_struct" href="../../df/db3/struct_l.html">L</a>, B);</div>
<div class="line"><span class="lineno"> 127</span> </div>
<div class="line"><span class="lineno"> 128</span> assert(<a class="code hl_struct" href="../../df/db3/struct_l.html">L</a>[l1][l2] == 27);</div>
<div class="line"><span class="lineno"> 129</span> assert(strcmp(lcs, <span class="stringliteral">&quot;CGTTCGGCTATGCTTCTACTTATTCTA&quot;</span>) == 0);</div>
<div class="line"><span class="lineno"> 130</span> </div>
<div class="line"><span class="lineno"> 131</span> printf(<span class="stringliteral">&quot;S1: %s\tS2: %s\n&quot;</span>, s1, s2);</div>
<div class="line"><span class="lineno"> 132</span> printf(<span class="stringliteral">&quot;LCS len:%3d\n&quot;</span>, <a class="code hl_struct" href="../../df/db3/struct_l.html">L</a>[l1][l2]);</div>
<div class="line"><span class="lineno"> 133</span> printf(<span class="stringliteral">&quot;LCS: %s\n&quot;</span>, lcs);</div>
<div class="line"><span class="lineno"> 134</span> </div>
<div class="line"><span class="lineno"> 135</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(lcs); </div>
<div class="line"><span class="lineno"> 136</span> <span class="keywordflow">for</span> (j = 0; j &lt;= l1; j++)</div>
<div class="line"><span class="lineno"> 137</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(<a class="code hl_struct" href="../../df/db3/struct_l.html">L</a>[j]), <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(B[j]);</div>
<div class="line"><span class="lineno"> 138</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(<a class="code hl_struct" href="../../df/db3/struct_l.html">L</a>);</div>
<div class="line"><span class="lineno"> 139</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(B);</div>
<div class="line"><span class="lineno"> 140</span> </div>
<div class="line"><span class="lineno"> 141</span> printf(<span class="stringliteral">&quot;All tests have successfully passed!\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 142</span>}</div>
<div class="ttc" id="alcs_8c_html_a741c9173a9fef243518f5c2352b81fc0"><div class="ttname"><a href="../../db/de1/lcs_8c.html#a741c9173a9fef243518f5c2352b81fc0">lcsbuild</a></div><div class="ttdeci">char * lcsbuild(const char *s1, int l1, int l2, int **L, int **B)</div><div class="ttdoc">@breif Builds the LCS according to B using a traceback approach</div><div class="ttdef"><b>Definition:</b> lcs.c:59</div></div>
<div class="ttc" id="alcs_8c_html_ac107a0b0233dacb9e8cd3a983d6740b6"><div class="ttname"><a href="../../db/de1/lcs_8c.html#ac107a0b0233dacb9e8cd3a983d6740b6">lcslen</a></div><div class="ttdeci">void lcslen(const char *s1, const char *s2, int l1, int l2, int **L, int **B)</div><div class="ttdoc">@breif Computes LCS between s1 and s2 using a dynamic-programming approach</div><div class="ttdef"><b>Definition:</b> lcs.c:27</div></div>
<div class="ttc" id="amalloc__dbg_8h_html_a9cc854374299a1dd933bf62029761768"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a></div><div class="ttdeci">#define free(ptr)</div><div class="ttdoc">This macro replace the standard free function with free_dbg.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.h:26</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="../../db/de1/lcs_8c_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg" width="170" 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="../../db/de1/lcs_8c.html">lcs.c</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.5 </li>
</ul>
</div>
</body>
</html>