TheAlgorithms-C/df/df3/union__find_8c.html
2023-04-27 19:43:13 +00:00

336 lines
20 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.6"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: misc/union_find.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.6 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('df/df3/union__find_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="#define-members">Macros</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle"><div class="title">union_find.c File Reference</div></div>
</div><!--header-->
<div class="contents">
<p><a href="https://en.wikipedia.org/wiki/Disjoint-set_data_structure" target="_blank">Union find</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 />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for union_find.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d3/d60/union__find_8c__incl.svg" width="158" height="111"><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:a0592dba56693fad79136250c11e5a7fe"><td class="memItemLeft" align="right" valign="top"><a id="a0592dba56693fad79136250c11e5a7fe" name="a0592dba56693fad79136250c11e5a7fe"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><b>MAX_SIZE</b>&#160;&#160;&#160;1000</td></tr>
<tr class="memdesc:a0592dba56693fad79136250c11e5a7fe"><td class="mdescLeft">&#160;</td><td class="mdescRight">maximum number of elements in the set <br /></td></tr>
<tr class="separator:a0592dba56693fad79136250c11e5a7fe"><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:a3e13b69cce5a1b25ae034798092f3d86"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../df/df3/union__find_8c.html#a3e13b69cce5a1b25ae034798092f3d86">find</a> (int *p, int x)</td></tr>
<tr class="memdesc:a3e13b69cce5a1b25ae034798092f3d86"><td class="mdescLeft">&#160;</td><td class="mdescRight">Find index of or value in an array. <br /></td></tr>
<tr class="separator:a3e13b69cce5a1b25ae034798092f3d86"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:af2f3ff6d98641f7d3be11f071c97908a"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../df/df3/union__find_8c.html#af2f3ff6d98641f7d3be11f071c97908a">join</a> (int *p, int x, int y)</td></tr>
<tr class="memdesc:af2f3ff6d98641f7d3be11f071c97908a"><td class="mdescLeft">&#160;</td><td class="mdescRight">Function to join. <br /></td></tr>
<tr class="separator:af2f3ff6d98641f7d3be11f071c97908a"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../df/df3/union__find_8c.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
<tr class="memdesc:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main function. <br /></td></tr>
<tr class="separator:ae66f6b31b5ad750f1fe042a706a4e3d4"><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/Disjoint-set_data_structure" target="_blank">Union find</a> algorithm. </p>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="a3e13b69cce5a1b25ae034798092f3d86" name="a3e13b69cce5a1b25ae034798092f3d86"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a3e13b69cce5a1b25ae034798092f3d86">&#9670;&#160;</a></span>find()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int find </td>
<td>(</td>
<td class="paramtype">int *&#160;</td>
<td class="paramname"><em>p</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>x</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Find index of or value in an array. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in,out]</td><td class="paramname">p</td><td>array to search and update </td></tr>
<tr><td class="paramdir"></td><td class="paramname">x</td><td>value to search </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>value at the index <code>x</code> </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 18</span>{</div>
<div class="line"><span class="lineno"> 19</span> <span class="keywordflow">if</span> (x &gt;= <a class="code hl_define" href="../../df/df3/union__find_8c.html#a0592dba56693fad79136250c11e5a7fe">MAX_SIZE</a>)</div>
<div class="line"><span class="lineno"> 20</span> {</div>
<div class="line"><span class="lineno"> 21</span> fprintf(stderr, <span class="stringliteral">&quot;Out-of bounds value\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 22</span> exit(EXIT_FAILURE);</div>
<div class="line"><span class="lineno"> 23</span> }</div>
<div class="line"><span class="lineno"> 24</span> </div>
<div class="line"><span class="lineno"> 25</span> <span class="keywordflow">if</span> (p[x] == x)</div>
<div class="line"><span class="lineno"> 26</span> {</div>
<div class="line"><span class="lineno"> 27</span> <span class="keywordflow">return</span> x;</div>
<div class="line"><span class="lineno"> 28</span> }</div>
<div class="line"><span class="lineno"> 29</span> <span class="keywordflow">else</span></div>
<div class="line"><span class="lineno"> 30</span> {</div>
<div class="line"><span class="lineno"> 31</span> p[x] = <a class="code hl_function" href="../../df/df3/union__find_8c.html#a3e13b69cce5a1b25ae034798092f3d86">find</a>(p, p[x]);</div>
<div class="line"><span class="lineno"> 32</span> <span class="keywordflow">return</span> p[x];</div>
<div class="line"><span class="lineno"> 33</span> }</div>
<div class="line"><span class="lineno"> 34</span>}</div>
<div class="ttc" id="aunion__find_8c_html_a0592dba56693fad79136250c11e5a7fe"><div class="ttname"><a href="../../df/df3/union__find_8c.html#a0592dba56693fad79136250c11e5a7fe">MAX_SIZE</a></div><div class="ttdeci">#define MAX_SIZE</div><div class="ttdoc">maximum number of elements in the set</div><div class="ttdef"><b>Definition:</b> union_find.c:8</div></div>
<div class="ttc" id="aunion__find_8c_html_a3e13b69cce5a1b25ae034798092f3d86"><div class="ttname"><a href="../../df/df3/union__find_8c.html#a3e13b69cce5a1b25ae034798092f3d86">find</a></div><div class="ttdeci">int find(int *p, int x)</div><div class="ttdoc">Find index of or value in an array.</div><div class="ttdef"><b>Definition:</b> union_find.c:17</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="../../df/df3/union__find_8c_a3e13b69cce5a1b25ae034798092f3d86_cgraph.svg" width="54" height="60"><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="af2f3ff6d98641f7d3be11f071c97908a" name="af2f3ff6d98641f7d3be11f071c97908a"></a>
<h2 class="memtitle"><span class="permalink"><a href="#af2f3ff6d98641f7d3be11f071c97908a">&#9670;&#160;</a></span>join()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void join </td>
<td>(</td>
<td class="paramtype">int *&#160;</td>
<td class="paramname"><em>p</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>x</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>y</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Function to join. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in,out]</td><td class="paramname">p</td><td>array to join in </td></tr>
<tr><td class="paramdir"></td><td class="paramname">x</td><td>value or index to join to </td></tr>
<tr><td class="paramdir"></td><td class="paramname">y</td><td>value or index to join from </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 42</span>{ p[<a class="code hl_function" href="../../df/df3/union__find_8c.html#a3e13b69cce5a1b25ae034798092f3d86">find</a>(p, x)] = <a class="code hl_function" href="../../df/df3/union__find_8c.html#a3e13b69cce5a1b25ae034798092f3d86">find</a>(p, y); }</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="../../df/df3/union__find_8c_af2f3ff6d98641f7d3be11f071c97908a_cgraph.svg" width="143" height="60"><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="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">&#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">void&#160;</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Main function. </p>
<div class="fragment"><div class="line"><span class="lineno"> 46</span>{</div>
<div class="line"><span class="lineno"> 47</span> <span class="keywordtype">int</span> union_set[<a class="code hl_define" href="../../df/df3/union__find_8c.html#a0592dba56693fad79136250c11e5a7fe">MAX_SIZE</a>];</div>
<div class="line"><span class="lineno"> 48</span> </div>
<div class="line"><span class="lineno"> 49</span> <span class="comment">// Have all array indexes that you need to use reference themselves</span></div>
<div class="line"><span class="lineno"> 50</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; 10; i++)</div>
<div class="line"><span class="lineno"> 51</span> {</div>
<div class="line"><span class="lineno"> 52</span> union_set[i] = i;</div>
<div class="line"><span class="lineno"> 53</span> }</div>
<div class="line"><span class="lineno"> 54</span> <span class="comment">// p = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}</span></div>
<div class="line"><span class="lineno"> 55</span> </div>
<div class="line"><span class="lineno"> 56</span> <a class="code hl_function" href="../../df/df3/union__find_8c.html#af2f3ff6d98641f7d3be11f071c97908a">join</a>(union_set, 3, 5);</div>
<div class="line"><span class="lineno"> 57</span> printf(<span class="stringliteral">&quot;The array is now: &quot;</span>);</div>
<div class="line"><span class="lineno"> 58</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; 10; i++)</div>
<div class="line"><span class="lineno"> 59</span> {</div>
<div class="line"><span class="lineno"> 60</span> printf(<span class="stringliteral">&quot;%d &quot;</span>, union_set[i]);</div>
<div class="line"><span class="lineno"> 61</span> }</div>
<div class="line"><span class="lineno"> 62</span> printf(<span class="stringliteral">&quot;\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 63</span> <span class="comment">// Now 3 and 5 are groupped together, that is find(3) = find(5)</span></div>
<div class="line"><span class="lineno"> 64</span> <span class="comment">// p = {0, 1, 2, 5, 4, 5, 6, 7, 8, 9}</span></div>
<div class="line"><span class="lineno"> 65</span> </div>
<div class="line"><span class="lineno"> 66</span> <a class="code hl_function" href="../../df/df3/union__find_8c.html#af2f3ff6d98641f7d3be11f071c97908a">join</a>(union_set, 3, 8);</div>
<div class="line"><span class="lineno"> 67</span> printf(<span class="stringliteral">&quot;The array is now: &quot;</span>);</div>
<div class="line"><span class="lineno"> 68</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; 10; i++)</div>
<div class="line"><span class="lineno"> 69</span> {</div>
<div class="line"><span class="lineno"> 70</span> printf(<span class="stringliteral">&quot;%d &quot;</span>, union_set[i]);</div>
<div class="line"><span class="lineno"> 71</span> }</div>
<div class="line"><span class="lineno"> 72</span> printf(<span class="stringliteral">&quot;\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 73</span> </div>
<div class="line"><span class="lineno"> 74</span> <span class="comment">// Now 3, 5 and are groupped together, find(3) = find(5) = find(8)</span></div>
<div class="line"><span class="lineno"> 75</span> <span class="comment">// p = {0, 1, 2, 5, 4, 8, 6, 7, 8, 9}</span></div>
<div class="line"><span class="lineno"> 76</span> <a class="code hl_function" href="../../df/df3/union__find_8c.html#af2f3ff6d98641f7d3be11f071c97908a">join</a>(union_set, 0, 5);</div>
<div class="line"><span class="lineno"> 77</span> <span class="keywordflow">if</span> (<a class="code hl_function" href="../../df/df3/union__find_8c.html#a3e13b69cce5a1b25ae034798092f3d86">find</a>(union_set, 0) == <a class="code hl_function" href="../../df/df3/union__find_8c.html#a3e13b69cce5a1b25ae034798092f3d86">find</a>(union_set, 3))</div>
<div class="line"><span class="lineno"> 78</span> {</div>
<div class="line"><span class="lineno"> 79</span> printf(<span class="stringliteral">&quot;0 and 3 are groupped together\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 80</span> }</div>
<div class="line"><span class="lineno"> 81</span> printf(<span class="stringliteral">&quot;The array is now: &quot;</span>);</div>
<div class="line"><span class="lineno"> 82</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; 10; i++)</div>
<div class="line"><span class="lineno"> 83</span> {</div>
<div class="line"><span class="lineno"> 84</span> printf(<span class="stringliteral">&quot;%d &quot;</span>, union_set[i]);</div>
<div class="line"><span class="lineno"> 85</span> }</div>
<div class="line"><span class="lineno"> 86</span> printf(<span class="stringliteral">&quot;\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 87</span> </div>
<div class="line"><span class="lineno"> 88</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 89</span>}</div>
<div class="ttc" id="aunion__find_8c_html_af2f3ff6d98641f7d3be11f071c97908a"><div class="ttname"><a href="../../df/df3/union__find_8c.html#af2f3ff6d98641f7d3be11f071c97908a">join</a></div><div class="ttdeci">void join(int *p, int x, int y)</div><div class="ttdoc">Function to join.</div><div class="ttdef"><b>Definition:</b> union_find.c:42</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="../../df/df3/union__find_8c_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg" width="240" 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_a52f4d3d586a58b02f3a4ddf0a992157.html">misc</a></li><li class="navelem"><a class="el" href="../../df/df3/union__find_8c.html">union_find.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.6 </li>
</ul>
</div>
</body>
</html>