TheAlgorithms-C/d9/dcc/prim_8c.html
2021-12-09 17:57:52 +00:00

531 lines
39 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">
<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.2"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: greedy_approach/prim.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 style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<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.2 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search",'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('d9/dcc/prim_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">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="summary">
<a href="#define-members">Macros</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle"><div class="title">prim.c File Reference</div></div>
</div><!--header-->
<div class="contents">
<p><a href="https://en.wikipedia.org/wiki/Prim%27s_algorithm" target="_blank">Prim's algorithm</a> implementation in C to find the MST of a weighted, connected graph.
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;stdio.h&gt;</code><br />
<code>#include &lt;string.h&gt;</code><br />
<code>#include &lt;assert.h&gt;</code><br />
<code>#include &lt;inttypes.h&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for prim.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../de/d1c/prim_8c__incl.svg" width="360" height="112"><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="define-members" name="define-members"></a>
Macros</h2></td></tr>
<tr class="memitem:a392fb874e547e582e9c66a08a1f23326"><td class="memItemLeft" align="right" valign="top">#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d9/dcc/prim_8c.html#a392fb874e547e582e9c66a08a1f23326">MAX</a>&#160;&#160;&#160;20</td></tr>
<tr class="memdesc:a392fb874e547e582e9c66a08a1f23326"><td class="mdescLeft">&#160;</td><td class="mdescRight">for IO operations <a href="../../d9/dcc/prim_8c.html#a392fb874e547e582e9c66a08a1f23326">More...</a><br /></td></tr>
<tr class="separator:a392fb874e547e582e9c66a08a1f23326"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a12c2040f25d8e3a7b9e1c2024c618cb6"><td class="memItemLeft" align="right" valign="top"><a id="a12c2040f25d8e3a7b9e1c2024c618cb6" name="a12c2040f25d8e3a7b9e1c2024c618cb6"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><b>INF</b>&#160;&#160;&#160;999</td></tr>
<tr class="separator:a12c2040f25d8e3a7b9e1c2024c618cb6"><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:a72ec66b10f86d67064d34f2b2717fdfc"><td class="memItemLeft" align="right" valign="top">uint16_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d9/dcc/prim_8c.html#a72ec66b10f86d67064d34f2b2717fdfc">minimum</a> (uint16_t arr[], uint16_t N)</td></tr>
<tr class="memdesc:a72ec66b10f86d67064d34f2b2717fdfc"><td class="mdescLeft">&#160;</td><td class="mdescRight">Finds index of minimum element in edge list for an arbitrary vertex. <a href="../../d9/dcc/prim_8c.html#a72ec66b10f86d67064d34f2b2717fdfc">More...</a><br /></td></tr>
<tr class="separator:a72ec66b10f86d67064d34f2b2717fdfc"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a0bea087eb586c437cf0a667f794c8853"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d9/dcc/prim_8c.html#a0bea087eb586c437cf0a667f794c8853">prim</a> (uint16_t G[][<a class="el" href="../../d2/d6d/bubble__sort__2_8c.html#a392fb874e547e582e9c66a08a1f23326">MAX</a>], uint16_t MST[][<a class="el" href="../../d2/d6d/bubble__sort__2_8c.html#a392fb874e547e582e9c66a08a1f23326">MAX</a>], uint16_t V)</td></tr>
<tr class="memdesc:a0bea087eb586c437cf0a667f794c8853"><td class="mdescLeft">&#160;</td><td class="mdescRight">Used to find MST of user-generated adj matrix G. <a href="../../d9/dcc/prim_8c.html#a0bea087eb586c437cf0a667f794c8853">More...</a><br /></td></tr>
<tr class="separator:a0bea087eb586c437cf0a667f794c8853"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:aff3f4c22009b2aa4e385a76a8a1cdc1c"><td class="memItemLeft" align="right" valign="top">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d9/dcc/prim_8c.html#aff3f4c22009b2aa4e385a76a8a1cdc1c">test</a> (uint16_t G[][<a class="el" href="../../d2/d6d/bubble__sort__2_8c.html#a392fb874e547e582e9c66a08a1f23326">MAX</a>], uint16_t MST[][<a class="el" href="../../d2/d6d/bubble__sort__2_8c.html#a392fb874e547e582e9c66a08a1f23326">MAX</a>], uint16_t V)</td></tr>
<tr class="memdesc:aff3f4c22009b2aa4e385a76a8a1cdc1c"><td class="mdescLeft">&#160;</td><td class="mdescRight">Self-test implementations. <a href="../../d9/dcc/prim_8c.html#aff3f4c22009b2aa4e385a76a8a1cdc1c">More...</a><br /></td></tr>
<tr class="separator:aff3f4c22009b2aa4e385a76a8a1cdc1c"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:addac7a8c7bfd3e5573dad1f6aeff7768"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d9/dcc/prim_8c.html#addac7a8c7bfd3e5573dad1f6aeff7768">user_graph</a> (uint16_t G[][<a class="el" href="../../d2/d6d/bubble__sort__2_8c.html#a392fb874e547e582e9c66a08a1f23326">MAX</a>], uint16_t MST[][<a class="el" href="../../d2/d6d/bubble__sort__2_8c.html#a392fb874e547e582e9c66a08a1f23326">MAX</a>], uint16_t V)</td></tr>
<tr class="memdesc:addac7a8c7bfd3e5573dad1f6aeff7768"><td class="mdescLeft">&#160;</td><td class="mdescRight">Function <a class="el" href="../../d9/dcc/prim_8c.html#addac7a8c7bfd3e5573dad1f6aeff7768" title="Function user_graph(); gets user input adj.">user_graph()</a>; gets user input adj. <a href="../../d9/dcc/prim_8c.html#addac7a8c7bfd3e5573dad1f6aeff7768">More...</a><br /></td></tr>
<tr class="separator:addac7a8c7bfd3e5573dad1f6aeff7768"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:abf9e6b7e6f15df4b525a2e7705ba3089"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d9/dcc/prim_8c.html#abf9e6b7e6f15df4b525a2e7705ba3089">main</a> (int argc, char const *argv[])</td></tr>
<tr class="memdesc:abf9e6b7e6f15df4b525a2e7705ba3089"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main function. <a href="../../d9/dcc/prim_8c.html#abf9e6b7e6f15df4b525a2e7705ba3089">More...</a><br /></td></tr>
<tr class="separator:abf9e6b7e6f15df4b525a2e7705ba3089"><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/Prim%27s_algorithm" target="_blank">Prim's algorithm</a> implementation in C to find the MST of a weighted, connected graph. </p>
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/sl1mb0" target="_blank">Timothy Maloney</a></dd></dl>
<p>Prim's algorithm uses a greedy approach to generate the MST of a weighted connected graph. The algorithm begins at an arbitrary vertex v, and selects a next vertex u, where v and u are connected by a weighted edge whose weight is the minimum of all edges connected to v. @references Page 319 "Introduction to the Design and Analysis of Algorithms" - Anany Levitin</p>
<p >To test - run './prim -test' <a class="el" href="../../d9/dcc/prim_8c.html#a0bea087eb586c437cf0a667f794c8853" title="Used to find MST of user-generated adj matrix G.">prim()</a> will find the MST of the following adj. matrix:</p>
<p >0 1 2 3 1 0 4 6 2 4 0 5 3 6 5 0</p>
<p >The minimum spanning tree for the above weighted connected graph is given by the following adj matrix:</p>
<p >0 1 2 3 1 0 0 0 2 0 0 0 3 0 0 0</p>
<p >The following <a href="https://visualgo.net/en/mst" target="_blank">link</a> provides a visual representation of graphs that can be used to test/verify the algorithm for different adj matrices and their weighted, connected graphs. </p>
</div><h2 class="groupheader">Macro Definition Documentation</h2>
<a id="a392fb874e547e582e9c66a08a1f23326" name="a392fb874e547e582e9c66a08a1f23326"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a392fb874e547e582e9c66a08a1f23326">&#9670;&nbsp;</a></span>MAX</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">#define MAX&#160;&#160;&#160;20</td>
</tr>
</table>
</div><div class="memdoc">
<p>for IO operations </p>
<p >for string comparison for assert() for uint16_t </p>
</div>
</div>
<h2 class="groupheader">Function Documentation</h2>
<a id="abf9e6b7e6f15df4b525a2e7705ba3089" name="abf9e6b7e6f15df4b525a2e7705ba3089"></a>
<h2 class="memtitle"><span class="permalink"><a href="#abf9e6b7e6f15df4b525a2e7705ba3089">&#9670;&nbsp;</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 const *&#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>
<p >&lt; weighted, connected graph G</p>
<p >&lt; adj matrix to hold minimum spanning tree of G</p>
<p >&lt; number of vertices in V in G</p>
<div class="fragment"><div class="line"><a id="l00186" name="l00186"></a><span class="lineno"> 186</span>{ </div>
<div class="line"><a id="l00187" name="l00187"></a><span class="lineno"> 187</span> </div>
<div class="line"><a id="l00188" name="l00188"></a><span class="lineno"> 188</span> uint16_t G[<a class="code hl_define" href="../../d9/dcc/prim_8c.html#a392fb874e547e582e9c66a08a1f23326">MAX</a>][<a class="code hl_define" href="../../d9/dcc/prim_8c.html#a392fb874e547e582e9c66a08a1f23326">MAX</a>]; <span class="comment">///&lt; weighted, connected graph G</span></div>
<div class="line"><a id="l00189" name="l00189"></a><span class="lineno"> 189</span> uint16_t MST[<a class="code hl_define" href="../../d9/dcc/prim_8c.html#a392fb874e547e582e9c66a08a1f23326">MAX</a>][<a class="code hl_define" href="../../d9/dcc/prim_8c.html#a392fb874e547e582e9c66a08a1f23326">MAX</a>]; <span class="comment">///&lt; adj matrix to hold minimum spanning tree of G</span></div>
<div class="line"><a id="l00190" name="l00190"></a><span class="lineno"> 190</span> uint16_t V; <span class="comment">///&lt; number of vertices in V in G</span></div>
<div class="line"><a id="l00191" name="l00191"></a><span class="lineno"> 191</span> </div>
<div class="line"><a id="l00192" name="l00192"></a><span class="lineno"> 192</span> </div>
<div class="line"><a id="l00193" name="l00193"></a><span class="lineno"> 193</span> <span class="keywordflow">if</span>(argc == 2 &amp;&amp; strcmp(argv[1],<span class="stringliteral">&quot;-test&quot;</span>) == 0)</div>
<div class="line"><a id="l00194" name="l00194"></a><span class="lineno"> 194</span> {</div>
<div class="line"><a id="l00195" name="l00195"></a><span class="lineno"> 195</span> <a class="code hl_function" href="../../d9/dcc/prim_8c.html#aff3f4c22009b2aa4e385a76a8a1cdc1c">test</a>(&amp;(*G),&amp;(*MST),V);</div>
<div class="line"><a id="l00196" name="l00196"></a><span class="lineno"> 196</span> }</div>
<div class="line"><a id="l00197" name="l00197"></a><span class="lineno"> 197</span> <span class="keywordflow">else</span></div>
<div class="line"><a id="l00198" name="l00198"></a><span class="lineno"> 198</span> {</div>
<div class="line"><a id="l00199" name="l00199"></a><span class="lineno"> 199</span> <a class="code hl_function" href="../../d9/dcc/prim_8c.html#addac7a8c7bfd3e5573dad1f6aeff7768">user_graph</a>(&amp;(*G),&amp;(*MST),V);</div>
<div class="line"><a id="l00200" name="l00200"></a><span class="lineno"> 200</span> }</div>
<div class="line"><a id="l00201" name="l00201"></a><span class="lineno"> 201</span> </div>
<div class="line"><a id="l00202" name="l00202"></a><span class="lineno"> 202</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><a id="l00203" name="l00203"></a><span class="lineno"> 203</span>}</div>
<div class="ttc" id="aprim_8c_html_a392fb874e547e582e9c66a08a1f23326"><div class="ttname"><a href="../../d9/dcc/prim_8c.html#a392fb874e547e582e9c66a08a1f23326">MAX</a></div><div class="ttdeci">#define MAX</div><div class="ttdoc">for IO operations</div><div class="ttdef"><b>Definition:</b> prim.c:36</div></div>
<div class="ttc" id="aprim_8c_html_addac7a8c7bfd3e5573dad1f6aeff7768"><div class="ttname"><a href="../../d9/dcc/prim_8c.html#addac7a8c7bfd3e5573dad1f6aeff7768">user_graph</a></div><div class="ttdeci">void user_graph(uint16_t G[][MAX], uint16_t MST[][MAX], uint16_t V)</div><div class="ttdoc">Function user_graph(); gets user input adj.</div><div class="ttdef"><b>Definition:</b> prim.c:145</div></div>
<div class="ttc" id="aprim_8c_html_aff3f4c22009b2aa4e385a76a8a1cdc1c"><div class="ttname"><a href="../../d9/dcc/prim_8c.html#aff3f4c22009b2aa4e385a76a8a1cdc1c">test</a></div><div class="ttdeci">static void test(uint16_t G[][MAX], uint16_t MST[][MAX], uint16_t V)</div><div class="ttdoc">Self-test implementations.</div><div class="ttdef"><b>Definition:</b> prim.c:113</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="../../d9/dcc/prim_8c_abf9e6b7e6f15df4b525a2e7705ba3089_cgraph.svg" width="414" height="112"><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="a72ec66b10f86d67064d34f2b2717fdfc" name="a72ec66b10f86d67064d34f2b2717fdfc"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a72ec66b10f86d67064d34f2b2717fdfc">&#9670;&nbsp;</a></span>minimum()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">uint16_t minimum </td>
<td>(</td>
<td class="paramtype">uint16_t&#160;</td>
<td class="paramname"><em>arr</em>[], </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">uint16_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>Finds index of minimum element in edge list for an arbitrary vertex. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">arr</td><td>graph row </td></tr>
<tr><td class="paramname">N</td><td>number of elements in arr </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>index of minimum element in arr </dd></dl>
<div class="fragment"><div class="line"><a id="l00046" name="l00046"></a><span class="lineno"> 46</span>{</div>
<div class="line"><a id="l00047" name="l00047"></a><span class="lineno"> 47</span> uint16_t index = 0;</div>
<div class="line"><a id="l00048" name="l00048"></a><span class="lineno"> 48</span> uint16_t <a class="code hl_define" href="../../d1/d6b/group__kohonen__2d.html#gac6afabdc09a49a433ee19d8a9486056d">min</a> = INF;</div>
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno"> 49</span> </div>
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno"> 50</span> <span class="keywordflow">for</span> (uint16_t i = 0; i &lt; N; i++)</div>
<div class="line"><a id="l00051" name="l00051"></a><span class="lineno"> 51</span> {</div>
<div class="line"><a id="l00052" name="l00052"></a><span class="lineno"> 52</span> <span class="keywordflow">if</span> (arr[i] &lt; <a class="code hl_define" href="../../d1/d6b/group__kohonen__2d.html#gac6afabdc09a49a433ee19d8a9486056d">min</a>)</div>
<div class="line"><a id="l00053" name="l00053"></a><span class="lineno"> 53</span> {</div>
<div class="line"><a id="l00054" name="l00054"></a><span class="lineno"> 54</span> <a class="code hl_define" href="../../d1/d6b/group__kohonen__2d.html#gac6afabdc09a49a433ee19d8a9486056d">min</a> = arr[i];</div>
<div class="line"><a id="l00055" name="l00055"></a><span class="lineno"> 55</span> index = i;</div>
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno"> 56</span> }</div>
<div class="line"><a id="l00057" name="l00057"></a><span class="lineno"> 57</span> }</div>
<div class="line"><a id="l00058" name="l00058"></a><span class="lineno"> 58</span> <span class="keywordflow">return</span> index;</div>
<div class="line"><a id="l00059" name="l00059"></a><span class="lineno"> 59</span>}</div>
<div class="ttc" id="agroup__kohonen__2d_html_gac6afabdc09a49a433ee19d8a9486056d"><div class="ttname"><a href="../../d1/d6b/group__kohonen__2d.html#gac6afabdc09a49a433ee19d8a9486056d">min</a></div><div class="ttdeci">#define min(a, b)</div><div class="ttdoc">shorthand for minimum value</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:43</div></div>
</div><!-- fragment -->
</div>
</div>
<a id="a0bea087eb586c437cf0a667f794c8853" name="a0bea087eb586c437cf0a667f794c8853"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a0bea087eb586c437cf0a667f794c8853">&#9670;&nbsp;</a></span>prim()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void prim </td>
<td>(</td>
<td class="paramtype">uint16_t&#160;</td>
<td class="paramname"><em>G</em>[][MAX], </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">uint16_t&#160;</td>
<td class="paramname"><em>MST</em>[][MAX], </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">uint16_t&#160;</td>
<td class="paramname"><em>V</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Used to find MST of user-generated adj matrix G. </p>
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<div class="fragment"><div class="line"><a id="l00066" name="l00066"></a><span class="lineno"> 66</span>{</div>
<div class="line"><a id="l00067" name="l00067"></a><span class="lineno"> 67</span> uint16_t u, v;</div>
<div class="line"><a id="l00068" name="l00068"></a><span class="lineno"> 68</span> uint16_t E_t[<a class="code hl_define" href="../../d9/dcc/prim_8c.html#a392fb874e547e582e9c66a08a1f23326">MAX</a>], path[<a class="code hl_define" href="../../d9/dcc/prim_8c.html#a392fb874e547e582e9c66a08a1f23326">MAX</a>];</div>
<div class="line"><a id="l00069" name="l00069"></a><span class="lineno"> 69</span> uint16_t V_t[<a class="code hl_define" href="../../d9/dcc/prim_8c.html#a392fb874e547e582e9c66a08a1f23326">MAX</a>], no_of_edges;</div>
<div class="line"><a id="l00070" name="l00070"></a><span class="lineno"> 70</span> </div>
<div class="line"><a id="l00071" name="l00071"></a><span class="lineno"> 71</span> E_t[0] = 0; <span class="comment">// edges for current vertex</span></div>
<div class="line"><a id="l00072" name="l00072"></a><span class="lineno"> 72</span> V_t[0] = 1; <span class="comment">// list of visited vertices</span></div>
<div class="line"><a id="l00073" name="l00073"></a><span class="lineno"> 73</span> </div>
<div class="line"><a id="l00074" name="l00074"></a><span class="lineno"> 74</span> <span class="keywordflow">for</span> (uint16_t i = 1; i &lt; V; i++)</div>
<div class="line"><a id="l00075" name="l00075"></a><span class="lineno"> 75</span> {</div>
<div class="line"><a id="l00076" name="l00076"></a><span class="lineno"> 76</span> E_t[i] = G[i][0];</div>
<div class="line"><a id="l00077" name="l00077"></a><span class="lineno"> 77</span> path[i] = 0;</div>
<div class="line"><a id="l00078" name="l00078"></a><span class="lineno"> 78</span> V_t[i] = 0;</div>
<div class="line"><a id="l00079" name="l00079"></a><span class="lineno"> 79</span> }</div>
<div class="line"><a id="l00080" name="l00080"></a><span class="lineno"> 80</span> </div>
<div class="line"><a id="l00081" name="l00081"></a><span class="lineno"> 81</span> no_of_edges = V - 1;</div>
<div class="line"><a id="l00082" name="l00082"></a><span class="lineno"> 82</span> </div>
<div class="line"><a id="l00083" name="l00083"></a><span class="lineno"> 83</span> <span class="keywordflow">while</span> (no_of_edges &gt; 0)</div>
<div class="line"><a id="l00084" name="l00084"></a><span class="lineno"> 84</span> {</div>
<div class="line"><a id="l00085" name="l00085"></a><span class="lineno"> 85</span> u = <a class="code hl_function" href="../../d9/dcc/prim_8c.html#a72ec66b10f86d67064d34f2b2717fdfc">minimum</a>(E_t, V);</div>
<div class="line"><a id="l00086" name="l00086"></a><span class="lineno"> 86</span> <span class="keywordflow">while</span> (V_t[u] == 1)</div>
<div class="line"><a id="l00087" name="l00087"></a><span class="lineno"> 87</span> {</div>
<div class="line"><a id="l00088" name="l00088"></a><span class="lineno"> 88</span> E_t[u] = INF;</div>
<div class="line"><a id="l00089" name="l00089"></a><span class="lineno"> 89</span> u = <a class="code hl_function" href="../../d9/dcc/prim_8c.html#a72ec66b10f86d67064d34f2b2717fdfc">minimum</a>(E_t, V);</div>
<div class="line"><a id="l00090" name="l00090"></a><span class="lineno"> 90</span> }</div>
<div class="line"><a id="l00091" name="l00091"></a><span class="lineno"> 91</span> </div>
<div class="line"><a id="l00092" name="l00092"></a><span class="lineno"> 92</span> v = path[u];</div>
<div class="line"><a id="l00093" name="l00093"></a><span class="lineno"> 93</span> MST[v][u] = E_t[u];</div>
<div class="line"><a id="l00094" name="l00094"></a><span class="lineno"> 94</span> MST[u][v] = E_t[u];</div>
<div class="line"><a id="l00095" name="l00095"></a><span class="lineno"> 95</span> no_of_edges--;</div>
<div class="line"><a id="l00096" name="l00096"></a><span class="lineno"> 96</span> V_t[u] = 1;</div>
<div class="line"><a id="l00097" name="l00097"></a><span class="lineno"> 97</span> </div>
<div class="line"><a id="l00098" name="l00098"></a><span class="lineno"> 98</span> <span class="keywordflow">for</span> (uint16_t i = 1; i &lt; V; i++)</div>
<div class="line"><a id="l00099" name="l00099"></a><span class="lineno"> 99</span> {</div>
<div class="line"><a id="l00100" name="l00100"></a><span class="lineno"> 100</span> <span class="keywordflow">if</span> (V_t[i] == 0 &amp;&amp; G[u][i] &lt; E_t[i])</div>
<div class="line"><a id="l00101" name="l00101"></a><span class="lineno"> 101</span> {</div>
<div class="line"><a id="l00102" name="l00102"></a><span class="lineno"> 102</span> E_t[i] = G[u][i];</div>
<div class="line"><a id="l00103" name="l00103"></a><span class="lineno"> 103</span> path[i] = v;</div>
<div class="line"><a id="l00104" name="l00104"></a><span class="lineno"> 104</span> }</div>
<div class="line"><a id="l00105" name="l00105"></a><span class="lineno"> 105</span> }</div>
<div class="line"><a id="l00106" name="l00106"></a><span class="lineno"> 106</span> }</div>
<div class="line"><a id="l00107" name="l00107"></a><span class="lineno"> 107</span>}</div>
<div class="ttc" id="aprim_8c_html_a72ec66b10f86d67064d34f2b2717fdfc"><div class="ttname"><a href="../../d9/dcc/prim_8c.html#a72ec66b10f86d67064d34f2b2717fdfc">minimum</a></div><div class="ttdeci">uint16_t minimum(uint16_t arr[], uint16_t N)</div><div class="ttdoc">Finds index of minimum element in edge list for an arbitrary vertex.</div><div class="ttdef"><b>Definition:</b> prim.c:45</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="../../d9/dcc/prim_8c_a0bea087eb586c437cf0a667f794c8853_cgraph.svg" width="180" height="38"><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="aff3f4c22009b2aa4e385a76a8a1cdc1c" name="aff3f4c22009b2aa4e385a76a8a1cdc1c"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aff3f4c22009b2aa4e385a76a8a1cdc1c">&#9670;&nbsp;</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">uint16_t&#160;</td>
<td class="paramname"><em>G</em>[][MAX], </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">uint16_t&#160;</td>
<td class="paramname"><em>MST</em>[][MAX], </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">uint16_t&#160;</td>
<td class="paramname"><em>V</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</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"><a id="l00114" name="l00114"></a><span class="lineno"> 114</span>{</div>
<div class="line"><a id="l00115" name="l00115"></a><span class="lineno"> 115</span> </div>
<div class="line"><a id="l00116" name="l00116"></a><span class="lineno"> 116</span> uint16_t <a class="code hl_function" href="../../d9/dcc/prim_8c.html#aff3f4c22009b2aa4e385a76a8a1cdc1c">test</a>[4][4] = {{0,1,2,3},{1,0,4,6},{2,4,0,5},{3,6,5,0}};</div>
<div class="line"><a id="l00117" name="l00117"></a><span class="lineno"> 117</span> uint16_t solution[4][4] = {{0,1,2,3},{1,0,0,0},{2,0,0,0},{3,0,0,0}};</div>
<div class="line"><a id="l00118" name="l00118"></a><span class="lineno"> 118</span> </div>
<div class="line"><a id="l00119" name="l00119"></a><span class="lineno"> 119</span> V = 4;</div>
<div class="line"><a id="l00120" name="l00120"></a><span class="lineno"> 120</span> </div>
<div class="line"><a id="l00121" name="l00121"></a><span class="lineno"> 121</span> <span class="keywordflow">for</span>(uint16_t i = 0; i &lt; V; ++i)</div>
<div class="line"><a id="l00122" name="l00122"></a><span class="lineno"> 122</span> {</div>
<div class="line"><a id="l00123" name="l00123"></a><span class="lineno"> 123</span> <span class="keywordflow">for</span>(uint16_t j = 0; j &lt; V; ++j)</div>
<div class="line"><a id="l00124" name="l00124"></a><span class="lineno"> 124</span> {</div>
<div class="line"><a id="l00125" name="l00125"></a><span class="lineno"> 125</span> G[i][j] = <a class="code hl_function" href="../../d9/dcc/prim_8c.html#aff3f4c22009b2aa4e385a76a8a1cdc1c">test</a>[i][j];</div>
<div class="line"><a id="l00126" name="l00126"></a><span class="lineno"> 126</span> }</div>
<div class="line"><a id="l00127" name="l00127"></a><span class="lineno"> 127</span> }</div>
<div class="line"><a id="l00128" name="l00128"></a><span class="lineno"> 128</span> </div>
<div class="line"><a id="l00129" name="l00129"></a><span class="lineno"> 129</span> <a class="code hl_function" href="../../d9/dcc/prim_8c.html#a0bea087eb586c437cf0a667f794c8853">prim</a>(&amp;(*G),&amp;(*MST),V);</div>
<div class="line"><a id="l00130" name="l00130"></a><span class="lineno"> 130</span> </div>
<div class="line"><a id="l00131" name="l00131"></a><span class="lineno"> 131</span> <span class="keywordflow">for</span>(uint16_t i = 0; i &lt; V; ++i)</div>
<div class="line"><a id="l00132" name="l00132"></a><span class="lineno"> 132</span> {</div>
<div class="line"><a id="l00133" name="l00133"></a><span class="lineno"> 133</span> <span class="keywordflow">for</span>(uint16_t j = 0; j &lt; V; ++j)</div>
<div class="line"><a id="l00134" name="l00134"></a><span class="lineno"> 134</span> {</div>
<div class="line"><a id="l00135" name="l00135"></a><span class="lineno"> 135</span> assert(MST[i][j] == solution[i][j]);</div>
<div class="line"><a id="l00136" name="l00136"></a><span class="lineno"> 136</span> }</div>
<div class="line"><a id="l00137" name="l00137"></a><span class="lineno"> 137</span> }</div>
<div class="line"><a id="l00138" name="l00138"></a><span class="lineno"> 138</span>}</div>
<div class="ttc" id="aprim_8c_html_a0bea087eb586c437cf0a667f794c8853"><div class="ttname"><a href="../../d9/dcc/prim_8c.html#a0bea087eb586c437cf0a667f794c8853">prim</a></div><div class="ttdeci">void prim(uint16_t G[][MAX], uint16_t MST[][MAX], uint16_t V)</div><div class="ttdoc">Used to find MST of user-generated adj matrix G.</div><div class="ttdef"><b>Definition:</b> prim.c:65</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="../../d9/dcc/prim_8c_aff3f4c22009b2aa4e385a76a8a1cdc1c_cgraph.svg" width="272" height="62"><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="addac7a8c7bfd3e5573dad1f6aeff7768" name="addac7a8c7bfd3e5573dad1f6aeff7768"></a>
<h2 class="memtitle"><span class="permalink"><a href="#addac7a8c7bfd3e5573dad1f6aeff7768">&#9670;&nbsp;</a></span>user_graph()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void user_graph </td>
<td>(</td>
<td class="paramtype">uint16_t&#160;</td>
<td class="paramname"><em>G</em>[][MAX], </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">uint16_t&#160;</td>
<td class="paramname"><em>MST</em>[][MAX], </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">uint16_t&#160;</td>
<td class="paramname"><em>V</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Function <a class="el" href="../../d9/dcc/prim_8c.html#addac7a8c7bfd3e5573dad1f6aeff7768" title="Function user_graph(); gets user input adj.">user_graph()</a>; gets user input adj. </p>
<p >matrix and finds MST of that graph </p><dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<div class="fragment"><div class="line"><a id="l00146" name="l00146"></a><span class="lineno"> 146</span>{</div>
<div class="line"><a id="l00147" name="l00147"></a><span class="lineno"> 147</span> printf(<span class="stringliteral">&quot;Enter the number of vertices: &quot;</span>);</div>
<div class="line"><a id="l00148" name="l00148"></a><span class="lineno"> 148</span> scanf(<span class="stringliteral">&quot; %hd&quot;</span>, &amp;V);</div>
<div class="line"><a id="l00149" name="l00149"></a><span class="lineno"> 149</span> </div>
<div class="line"><a id="l00150" name="l00150"></a><span class="lineno"> 150</span> assert(V &lt;= <a class="code hl_define" href="../../d9/dcc/prim_8c.html#a392fb874e547e582e9c66a08a1f23326">MAX</a>);</div>
<div class="line"><a id="l00151" name="l00151"></a><span class="lineno"> 151</span> </div>
<div class="line"><a id="l00152" name="l00152"></a><span class="lineno"> 152</span> printf(<span class="stringliteral">&quot;Enter the adj matrix\n&quot;</span>);</div>
<div class="line"><a id="l00153" name="l00153"></a><span class="lineno"> 153</span> uint16_t i, j;</div>
<div class="line"><a id="l00154" name="l00154"></a><span class="lineno"> 154</span> <span class="keywordflow">for</span> (i = 0; i &lt; V; ++i)</div>
<div class="line"><a id="l00155" name="l00155"></a><span class="lineno"> 155</span> {</div>
<div class="line"><a id="l00156" name="l00156"></a><span class="lineno"> 156</span> <span class="keywordflow">for</span> (j = 0; j &lt; V; ++j)</div>
<div class="line"><a id="l00157" name="l00157"></a><span class="lineno"> 157</span> {</div>
<div class="line"><a id="l00158" name="l00158"></a><span class="lineno"> 158</span> printf(<span class="stringliteral">&quot;G[%d][%d]: &quot;</span>, i, j);</div>
<div class="line"><a id="l00159" name="l00159"></a><span class="lineno"> 159</span> scanf(<span class="stringliteral">&quot; %hd&quot;</span>, &amp;G[i][j]);</div>
<div class="line"><a id="l00160" name="l00160"></a><span class="lineno"> 160</span> <span class="keywordflow">if</span> (G[i][j] == 0)</div>
<div class="line"><a id="l00161" name="l00161"></a><span class="lineno"> 161</span> G[i][j] = INF;</div>
<div class="line"><a id="l00162" name="l00162"></a><span class="lineno"> 162</span> }</div>
<div class="line"><a id="l00163" name="l00163"></a><span class="lineno"> 163</span> }</div>
<div class="line"><a id="l00164" name="l00164"></a><span class="lineno"> 164</span> </div>
<div class="line"><a id="l00165" name="l00165"></a><span class="lineno"> 165</span> <a class="code hl_function" href="../../d9/dcc/prim_8c.html#a0bea087eb586c437cf0a667f794c8853">prim</a>(&amp;(*G),&amp;(*MST),V);</div>
<div class="line"><a id="l00166" name="l00166"></a><span class="lineno"> 166</span> </div>
<div class="line"><a id="l00167" name="l00167"></a><span class="lineno"> 167</span> printf(<span class="stringliteral">&quot;minimum spanning tree:\n&quot;</span>);</div>
<div class="line"><a id="l00168" name="l00168"></a><span class="lineno"> 168</span> <span class="keywordflow">for</span> (i = 0; i &lt; V; ++i)</div>
<div class="line"><a id="l00169" name="l00169"></a><span class="lineno"> 169</span> {</div>
<div class="line"><a id="l00170" name="l00170"></a><span class="lineno"> 170</span> printf(<span class="stringliteral">&quot;\n&quot;</span>);</div>
<div class="line"><a id="l00171" name="l00171"></a><span class="lineno"> 171</span> <span class="keywordflow">for</span> (j = 0; j &lt; V; ++j)</div>
<div class="line"><a id="l00172" name="l00172"></a><span class="lineno"> 172</span> {</div>
<div class="line"><a id="l00173" name="l00173"></a><span class="lineno"> 173</span> printf(<span class="stringliteral">&quot;%d\t&quot;</span>, MST[i][j]);</div>
<div class="line"><a id="l00174" name="l00174"></a><span class="lineno"> 174</span> }</div>
<div class="line"><a id="l00175" name="l00175"></a><span class="lineno"> 175</span> }</div>
<div class="line"><a id="l00176" name="l00176"></a><span class="lineno"> 176</span>}</div>
</div><!-- fragment -->
</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_a4300992e825aa9b9238bf43160a241e.html">greedy_approach</a></li><li class="navelem"><a class="el" href="../../d9/dcc/prim_8c.html">prim.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.2 </li>
</ul>
</div>
</body>
</html>