TheAlgorithms-C/d9/dcc/prim_8c.html

531 lines
35 KiB
HTML
Raw Normal View History

<!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.4"/>
<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 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.4 -->
<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"><span class="lineno"> 186</span>{ </div>
<div class="line"><span class="lineno"> 187</span> </div>
<div class="line"><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"><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"><span class="lineno"> 190</span> uint16_t V; <span class="comment">///&lt; number of vertices in V in G</span></div>
<div class="line"><span class="lineno"> 191</span> </div>
<div class="line"><span class="lineno"> 192</span> </div>
<div class="line"><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"><span class="lineno"> 194</span> {</div>
<div class="line"><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"><span class="lineno"> 196</span> }</div>
<div class="line"><span class="lineno"> 197</span> <span class="keywordflow">else</span></div>
<div class="line"><span class="lineno"> 198</span> {</div>
<div class="line"><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"><span class="lineno"> 200</span> }</div>
<div class="line"><span class="lineno"> 201</span> </div>
<div class="line"><span class="lineno"> 202</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><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"><span class="lineno"> 46</span>{</div>
<div class="line"><span class="lineno"> 47</span> uint16_t index = 0;</div>
<div class="line"><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"><span class="lineno"> 49</span> </div>
<div class="line"><span class="lineno"> 50</span> <span class="keywordflow">for</span> (uint16_t i = 0; i &lt; N; i++)</div>
<div class="line"><span class="lineno"> 51</span> {</div>
<div class="line"><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"><span class="lineno"> 53</span> {</div>
<div class="line"><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"><span class="lineno"> 55</span> index = i;</div>
<div class="line"><span class="lineno"> 56</span> }</div>
<div class="line"><span class="lineno"> 57</span> }</div>
<div class="line"><span class="lineno"> 58</span> <span class="keywordflow">return</span> index;</div>
<div class="line"><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"><span class="lineno"> 66</span>{</div>
<div class="line"><span class="lineno"> 67</span> uint16_t u, v;</div>
<div class="line"><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"><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"><span class="lineno"> 70</span> </div>
<div class="line"><span class="lineno"> 71</span> E_t[0] = 0; <span class="comment">// edges for current vertex</span></div>
<div class="line"><span class="lineno"> 72</span> V_t[0] = 1; <span class="comment">// list of visited vertices</span></div>
<div class="line"><span class="lineno"> 73</span> </div>
<div class="line"><span class="lineno"> 74</span> <span class="keywordflow">for</span> (uint16_t i = 1; i &lt; V; i++)</div>
<div class="line"><span class="lineno"> 75</span> {</div>
<div class="line"><span class="lineno"> 76</span> E_t[i] = G[i][0];</div>
<div class="line"><span class="lineno"> 77</span> path[i] = 0;</div>
<div class="line"><span class="lineno"> 78</span> V_t[i] = 0;</div>
<div class="line"><span class="lineno"> 79</span> }</div>
<div class="line"><span class="lineno"> 80</span> </div>
<div class="line"><span class="lineno"> 81</span> no_of_edges = V - 1;</div>
<div class="line"><span class="lineno"> 82</span> </div>
<div class="line"><span class="lineno"> 83</span> <span class="keywordflow">while</span> (no_of_edges &gt; 0)</div>
<div class="line"><span class="lineno"> 84</span> {</div>
<div class="line"><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"><span class="lineno"> 86</span> <span class="keywordflow">while</span> (V_t[u] == 1)</div>
<div class="line"><span class="lineno"> 87</span> {</div>
<div class="line"><span class="lineno"> 88</span> E_t[u] = INF;</div>
<div class="line"><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"><span class="lineno"> 90</span> }</div>
<div class="line"><span class="lineno"> 91</span> </div>
<div class="line"><span class="lineno"> 92</span> v = path[u];</div>
<div class="line"><span class="lineno"> 93</span> MST[v][u] = E_t[u];</div>
<div class="line"><span class="lineno"> 94</span> MST[u][v] = E_t[u];</div>
<div class="line"><span class="lineno"> 95</span> no_of_edges--;</div>
<div class="line"><span class="lineno"> 96</span> V_t[u] = 1;</div>
<div class="line"><span class="lineno"> 97</span> </div>
<div class="line"><span class="lineno"> 98</span> <span class="keywordflow">for</span> (uint16_t i = 1; i &lt; V; i++)</div>
<div class="line"><span class="lineno"> 99</span> {</div>
<div class="line"><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"><span class="lineno"> 101</span> {</div>
<div class="line"><span class="lineno"> 102</span> E_t[i] = G[u][i];</div>
<div class="line"><span class="lineno"> 103</span> path[i] = v;</div>
<div class="line"><span class="lineno"> 104</span> }</div>
<div class="line"><span class="lineno"> 105</span> }</div>
<div class="line"><span class="lineno"> 106</span> }</div>
<div class="line"><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"><span class="lineno"> 114</span>{</div>
<div class="line"><span class="lineno"> 115</span> </div>
<div class="line"><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"><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"><span class="lineno"> 118</span> </div>
<div class="line"><span class="lineno"> 119</span> V = 4;</div>
<div class="line"><span class="lineno"> 120</span> </div>
<div class="line"><span class="lineno"> 121</span> <span class="keywordflow">for</span>(uint16_t i = 0; i &lt; V; ++i)</div>
<div class="line"><span class="lineno"> 122</span> {</div>
<div class="line"><span class="lineno"> 123</span> <span class="keywordflow">for</span>(uint16_t j = 0; j &lt; V; ++j)</div>
<div class="line"><span class="lineno"> 124</span> {</div>
<div class="line"><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"><span class="lineno"> 126</span> }</div>
<div class="line"><span class="lineno"> 127</span> }</div>
<div class="line"><span class="lineno"> 128</span> </div>
<div class="line"><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"><span class="lineno"> 130</span> </div>
<div class="line"><span class="lineno"> 131</span> <span class="keywordflow">for</span>(uint16_t i = 0; i &lt; V; ++i)</div>
<div class="line"><span class="lineno"> 132</span> {</div>
<div class="line"><span class="lineno"> 133</span> <span class="keywordflow">for</span>(uint16_t j = 0; j &lt; V; ++j)</div>
<div class="line"><span class="lineno"> 134</span> {</div>
<div class="line"><span class="lineno"> 135</span> assert(MST[i][j] == solution[i][j]);</div>
<div class="line"><span class="lineno"> 136</span> }</div>
<div class="line"><span class="lineno"> 137</span> }</div>
<div class="line"><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"><span class="lineno"> 146</span>{</div>
<div class="line"><span class="lineno"> 147</span> printf(<span class="stringliteral">&quot;Enter the number of vertices: &quot;</span>);</div>
<div class="line"><span class="lineno"> 148</span> scanf(<span class="stringliteral">&quot; %hd&quot;</span>, &amp;V);</div>
<div class="line"><span class="lineno"> 149</span> </div>
<div class="line"><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"><span class="lineno"> 151</span> </div>
<div class="line"><span class="lineno"> 152</span> printf(<span class="stringliteral">&quot;Enter the adj matrix\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 153</span> uint16_t i, j;</div>
<div class="line"><span class="lineno"> 154</span> <span class="keywordflow">for</span> (i = 0; i &lt; V; ++i)</div>
<div class="line"><span class="lineno"> 155</span> {</div>
<div class="line"><span class="lineno"> 156</span> <span class="keywordflow">for</span> (j = 0; j &lt; V; ++j)</div>
<div class="line"><span class="lineno"> 157</span> {</div>
<div class="line"><span class="lineno"> 158</span> printf(<span class="stringliteral">&quot;G[%d][%d]: &quot;</span>, i, j);</div>
<div class="line"><span class="lineno"> 159</span> scanf(<span class="stringliteral">&quot; %hd&quot;</span>, &amp;G[i][j]);</div>
<div class="line"><span class="lineno"> 160</span> <span class="keywordflow">if</span> (G[i][j] == 0)</div>
<div class="line"><span class="lineno"> 161</span> G[i][j] = INF;</div>
<div class="line"><span class="lineno"> 162</span> }</div>
<div class="line"><span class="lineno"> 163</span> }</div>
<div class="line"><span class="lineno"> 164</span> </div>
<div class="line"><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"><span class="lineno"> 166</span> </div>
<div class="line"><span class="lineno"> 167</span> printf(<span class="stringliteral">&quot;minimum spanning tree:\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 168</span> <span class="keywordflow">for</span> (i = 0; i &lt; V; ++i)</div>
<div class="line"><span class="lineno"> 169</span> {</div>
<div class="line"><span class="lineno"> 170</span> printf(<span class="stringliteral">&quot;\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 171</span> <span class="keywordflow">for</span> (j = 0; j &lt; V; ++j)</div>
<div class="line"><span class="lineno"> 172</span> {</div>
<div class="line"><span class="lineno"> 173</span> printf(<span class="stringliteral">&quot;%d\t&quot;</span>, MST[i][j]);</div>
<div class="line"><span class="lineno"> 174</span> }</div>
<div class="line"><span class="lineno"> 175</span> }</div>
<div class="line"><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.4 </li>
</ul>
</div>
</body>
</html>