Files
C-Plus-Plus/d8/d99/connected__components__with__dsu_8cpp.html
2024-02-12 20:07:08 +00:00

452 lines
31 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.10.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C++: graph/connected_components_with_dsu.cpp File Reference</title>
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../clipboard.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>
<script type="text/javascript" src="../../cookie.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.10.0 -->
<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');
$(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 */
$(function(){initNavTree('d8/d99/connected__components__with__dsu_8cpp.html','../../'); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div class="header">
<div class="summary">
<a href="#namespaces">Namespaces</a> &#124;
<a href="#func-members">Functions</a> &#124;
<a href="#var-members">Variables</a> </div>
<div class="headertitle"><div class="title">connected_components_with_dsu.cpp File Reference</div></div>
</div><!--header-->
<div class="contents">
<p><a href="https://en.wikipedia.org/wiki/Disjoint_union" target="_blank">Disjoint union</a>
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;iostream&gt;</code><br />
<code>#include &lt;set&gt;</code><br />
<code>#include &lt;vector&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for connected_components_with_dsu.cpp:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/d74/connected__components__with__dsu_8cpp__incl.svg" width="227" height="126"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="namespaces" name="namespaces"></a>
Namespaces</h2></td></tr>
<tr class="memitem:df/dce/namespacegraph" id="r_df/dce/namespacegraph"><td class="memItemLeft" align="right" valign="top">namespace &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../df/dce/namespacegraph.html">graph</a></td></tr>
<tr class="memdesc:df/dce/namespacegraph"><td class="mdescLeft">&#160;</td><td class="mdescRight"><a class="el" href="../../dc/d61/classgraph_1_1_graph.html">Graph</a> Algorithms. <br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:de/db4/namespacedisjoint__union" id="r_de/db4/namespacedisjoint__union"><td class="memItemLeft" align="right" valign="top">namespace &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../de/db4/namespacedisjoint__union.html">disjoint_union</a></td></tr>
<tr class="memdesc:de/db4/namespacedisjoint__union"><td class="mdescLeft">&#160;</td><td class="mdescRight">Functions for <a href="https://en.wikipedia.org/wiki/Disjoint_union" target="_blank">Disjoint union</a> implementation. <br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:ae91ed94113c56191b75fe45f688d6e62" id="r_ae91ed94113c56191b75fe45f688d6e62"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#ae91ed94113c56191b75fe45f688d6e62">graph::disjoint_union::make_set</a> ()</td></tr>
<tr class="memdesc:ae91ed94113c56191b75fe45f688d6e62"><td class="mdescLeft">&#160;</td><td class="mdescRight">function the initialize every node as it's own parent <br /></td></tr>
<tr class="separator:ae91ed94113c56191b75fe45f688d6e62"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a469384d8a4197a9b24482ce7c321a85e" id="r_a469384d8a4197a9b24482ce7c321a85e"><td class="memItemLeft" align="right" valign="top">int64_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a469384d8a4197a9b24482ce7c321a85e">graph::disjoint_union::find_set</a> (int64_t val)</td></tr>
<tr class="memdesc:a469384d8a4197a9b24482ce7c321a85e"><td class="mdescLeft">&#160;</td><td class="mdescRight">Find the component where following node belongs to. <br /></td></tr>
<tr class="separator:a469384d8a4197a9b24482ce7c321a85e"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a67cb7472f310a798f555fe45cdf50145" id="r_a67cb7472f310a798f555fe45cdf50145"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a67cb7472f310a798f555fe45cdf50145">graph::disjoint_union::union_sets</a> (int64_t node1, int64_t node2)</td></tr>
<tr class="memdesc:a67cb7472f310a798f555fe45cdf50145"><td class="mdescLeft">&#160;</td><td class="mdescRight">Merge 2 components to become one. <br /></td></tr>
<tr class="separator:a67cb7472f310a798f555fe45cdf50145"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ac2d6698b71384a352ec4b81b31b13141" id="r_ac2d6698b71384a352ec4b81b31b13141"><td class="memItemLeft" align="right" valign="top">uint32_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#ac2d6698b71384a352ec4b81b31b13141">graph::disjoint_union::no_of_connected_components</a> ()</td></tr>
<tr class="memdesc:ac2d6698b71384a352ec4b81b31b13141"><td class="mdescLeft">&#160;</td><td class="mdescRight">Find total no. of connected components. <br /></td></tr>
<tr class="separator:ac2d6698b71384a352ec4b81b31b13141"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:aa8dca7b867074164d5f45b0f3851269d" id="r_aa8dca7b867074164d5f45b0f3851269d"><td class="memItemLeft" align="right" valign="top">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#aa8dca7b867074164d5f45b0f3851269d">test</a> ()</td></tr>
<tr class="memdesc:aa8dca7b867074164d5f45b0f3851269d"><td class="mdescLeft">&#160;</td><td class="mdescRight">Test Implementations. <br /></td></tr>
<tr class="separator:aa8dca7b867074164d5f45b0f3851269d"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4" id="r_ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#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><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="var-members" name="var-members"></a>
Variables</h2></td></tr>
<tr class="memitem:a7ab30d5b9594d90071a53c6d147158ef" id="r_a7ab30d5b9594d90071a53c6d147158ef"><td class="memItemLeft" align="right" valign="top"><a id="a7ab30d5b9594d90071a53c6d147158ef" name="a7ab30d5b9594d90071a53c6d147158ef"></a>
uint32_t&#160;</td><td class="memItemRight" valign="bottom"><b>graph::disjoint_union::number_of_nodes</b> = 0</td></tr>
<tr class="separator:a7ab30d5b9594d90071a53c6d147158ef"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:aa93bd9d3409fe4eeb7e5658eddd75fa8" id="r_aa93bd9d3409fe4eeb7e5658eddd75fa8"><td class="memItemLeft" align="right" valign="top"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>&lt; int64_t &gt;&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#aa93bd9d3409fe4eeb7e5658eddd75fa8">graph::disjoint_union::parent</a> {}</td></tr>
<tr class="separator:aa93bd9d3409fe4eeb7e5658eddd75fa8"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:af675d8149b6a82cb7fa07f39210b8a2b" id="r_af675d8149b6a82cb7fa07f39210b8a2b"><td class="memItemLeft" align="right" valign="top"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>&lt; uint32_t &gt;&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#af675d8149b6a82cb7fa07f39210b8a2b">graph::disjoint_union::connected_set_size</a> {}</td></tr>
<tr class="separator:af675d8149b6a82cb7fa07f39210b8a2b"><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_union" target="_blank">Disjoint union</a> </p>
<p>The Disjoint union is the technique to find connected component in graph efficiently.</p>
<h3><a class="anchor" id="autotoc_md77"></a>
Algorithm</h3>
<p>In <a class="el" href="../../da/d9a/class_graph.html">Graph</a>, if you have to find out the number of connected components, there are 2 options</p><ol type="1">
<li>Depth first search</li>
<li>Disjoint union 1st option is inefficient, Disjoint union is the most optimal way to find this.</li>
</ol>
<dl class="section author"><dt>Author</dt><dd>Unknown author </dd>
<dd>
<a href="https://github.com/sagarpandyansit" target="_blank">Sagar Pandya</a> </dd></dl>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="a469384d8a4197a9b24482ce7c321a85e" name="a469384d8a4197a9b24482ce7c321a85e"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a469384d8a4197a9b24482ce7c321a85e">&#9670;&#160;</a></span>find_set()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int64_t graph::disjoint_union::find_set </td>
<td>(</td>
<td class="paramtype">int64_t</td> <td class="paramname"><span class="paramname"><em>val</em></span></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Find the component where following node belongs to. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">val</td><td>parent of val should be found </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>parent of val </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 49</span> {</div>
<div class="line"><span class="lineno"> 50</span> <span class="keywordflow">while</span> (parent[val] != val) {</div>
<div class="line"><span class="lineno"> 51</span> parent[val] = parent[parent[val]];</div>
<div class="line"><span class="lineno"> 52</span> val = parent[val];</div>
<div class="line"><span class="lineno"> 53</span> }</div>
<div class="line"><span class="lineno"> 54</span> <span class="keywordflow">return</span> val;</div>
<div class="line"><span class="lineno"> 55</span>}</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="../../d8/d99/connected__components__with__dsu_8cpp_a469384d8a4197a9b24482ce7c321a85e_cgraph.svg" width="151" height="76"><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</td> <td class="paramname"><span class="paramname"></span></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Main function. </p>
<dl class="section return"><dt>Returns</dt><dd>0 on exit </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 112</span> {</div>
<div class="line"><span class="lineno"> 113</span> <a class="code hl_function" href="#aa8dca7b867074164d5f45b0f3851269d">test</a>(); <span class="comment">// Execute the tests</span></div>
<div class="line"><span class="lineno"> 114</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 115</span>}</div>
<div class="ttc" id="aconnected__components__with__dsu_8cpp_html_aa8dca7b867074164d5f45b0f3851269d"><div class="ttname"><a href="#aa8dca7b867074164d5f45b0f3851269d">test</a></div><div class="ttdeci">static void test()</div><div class="ttdoc">Test Implementations.</div><div class="ttdef"><b>Definition</b> connected_components_with_dsu.cpp:92</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="../../d8/d99/connected__components__with__dsu_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg" width="271" height="36"><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="ae91ed94113c56191b75fe45f688d6e62" name="ae91ed94113c56191b75fe45f688d6e62"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae91ed94113c56191b75fe45f688d6e62">&#9670;&#160;</a></span>make_set()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void graph::disjoint_union::make_set </td>
<td>(</td>
<td class="paramname"><span class="paramname"></span></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>function the initialize every node as it's own parent </p>
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 38</span> {</div>
<div class="line"><span class="lineno"> 39</span> <span class="keywordflow">for</span> (uint32_t i = 1; i &lt;= number_of_nodes; i++) {</div>
<div class="line"><span class="lineno"> 40</span> parent[i] = i;</div>
<div class="line"><span class="lineno"> 41</span> connected_set_size[i] = 1;</div>
<div class="line"><span class="lineno"> 42</span> }</div>
<div class="line"><span class="lineno"> 43</span>}</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="../../d8/d99/connected__components__with__dsu_8cpp_ae91ed94113c56191b75fe45f688d6e62_cgraph.svg" width="151" height="76"><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="ac2d6698b71384a352ec4b81b31b13141" name="ac2d6698b71384a352ec4b81b31b13141"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ac2d6698b71384a352ec4b81b31b13141">&#9670;&#160;</a></span>no_of_connected_components()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">uint32_t graph::disjoint_union::no_of_connected_components </td>
<td>(</td>
<td class="paramname"><span class="paramname"></span></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Find total no. of connected components. </p>
<dl class="section return"><dt>Returns</dt><dd>Number of connected components </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 80</span> {</div>
<div class="line"><span class="lineno"> 81</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/set.html">std::set&lt;int64_t&gt;</a> temp; <span class="comment">// temp set to count number of connected components</span></div>
<div class="line"><span class="lineno"> 82</span> <span class="keywordflow">for</span> (uint32_t i = 1; i &lt;= number_of_nodes; i++) temp.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/set/insert.html">insert</a>(<a class="code hl_function" href="#a469384d8a4197a9b24482ce7c321a85e">find_set</a>(i));</div>
<div class="line"><span class="lineno"> 83</span> <span class="keywordflow">return</span> temp.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/set/size.html">size</a>(); <span class="comment">// return the size of temp set</span></div>
<div class="line"><span class="lineno"> 84</span>}</div>
<div class="ttc" id="aconnected__components__with__dsu_8cpp_html_a469384d8a4197a9b24482ce7c321a85e"><div class="ttname"><a href="#a469384d8a4197a9b24482ce7c321a85e">graph::disjoint_union::find_set</a></div><div class="ttdeci">int64_t find_set(int64_t val)</div><div class="ttdoc">Find the component where following node belongs to.</div><div class="ttdef"><b>Definition</b> connected_components_with_dsu.cpp:49</div></div>
<div class="ttc" id="ainsert_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/set/insert.html">std::set::insert</a></div><div class="ttdeci">T insert(T... args)</div></div>
<div class="ttc" id="aset_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/set.html">std::set</a></div></div>
<div class="ttc" id="asize_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/set/size.html">std::set::size</a></div><div class="ttdeci">T size(T... args)</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d8/d99/connected__components__with__dsu_8cpp_ac2d6698b71384a352ec4b81b31b13141_cgraph.svg" width="406" height="175"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
<a id="aa8dca7b867074164d5f45b0f3851269d" name="aa8dca7b867074164d5f45b0f3851269d"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aa8dca7b867074164d5f45b0f3851269d">&#9670;&#160;</a></span>test()</h2>
<div class="memitem">
<div class="memproto">
<table class="mlabels">
<tr>
<td class="mlabels-left">
<table class="memname">
<tr>
<td class="memname">static void test </td>
<td>(</td>
<td class="paramname"><span class="paramname"></span></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>Test Implementations. </p>
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 92</span> {</div>
<div class="line"><span class="lineno"> 93</span> <span class="keyword">namespace </span><a class="code hl_class" href="../../dd/d1f/classdsu.html">dsu</a> = graph::disjoint_union;</div>
<div class="line"><span class="lineno"> 94</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_istream.html">std::cin</a> &gt;&gt; dsu::number_of_nodes;</div>
<div class="line"><span class="lineno"> 95</span> dsu::parent.resize(dsu::number_of_nodes + 1);</div>
<div class="line"><span class="lineno"> 96</span> dsu::connected_set_size.resize(dsu::number_of_nodes + 1);</div>
<div class="line"><span class="lineno"> 97</span> dsu::make_set();</div>
<div class="line"><span class="lineno"> 98</span> uint32_t edges = 0;</div>
<div class="line"><span class="lineno"> 99</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_istream.html">std::cin</a> &gt;&gt; edges; <span class="comment">// no of edges in the graph</span></div>
<div class="line"><span class="lineno"> 100</span> <span class="keywordflow">while</span> (edges--) {</div>
<div class="line"><span class="lineno"> 101</span> int64_t node_a = 0, node_b = 0;</div>
<div class="line"><span class="lineno"> 102</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_istream.html">std::cin</a> &gt;&gt; node_a &gt;&gt; node_b;</div>
<div class="line"><span class="lineno"> 103</span> dsu::union_sets(node_a, node_b);</div>
<div class="line"><span class="lineno"> 104</span> }</div>
<div class="line"><span class="lineno"> 105</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; dsu::no_of_connected_components() &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 106</span>}</div>
<div class="ttc" id="abasic_istream_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/basic_istream.html">std::cin</a></div></div>
<div class="ttc" id="abasic_ostream_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a></div></div>
<div class="ttc" id="aclassdsu_html"><div class="ttname"><a href="../../dd/d1f/classdsu.html">dsu</a></div><div class="ttdoc">Disjoint sets union data structure, class based representation.</div><div class="ttdef"><b>Definition</b> dsu_path_compression.cpp:33</div></div>
<div class="ttc" id="aendl_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a></div><div class="ttdeci">T endl(T... args)</div></div>
</div><!-- 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="../../d8/d99/connected__components__with__dsu_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg" width="174" height="36"><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="a67cb7472f310a798f555fe45cdf50145" name="a67cb7472f310a798f555fe45cdf50145"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a67cb7472f310a798f555fe45cdf50145">&#9670;&#160;</a></span>union_sets()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void graph::disjoint_union::union_sets </td>
<td>(</td>
<td class="paramtype">int64_t</td> <td class="paramname"><span class="paramname"><em>node1</em>, </span></td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int64_t</td> <td class="paramname"><span class="paramname"><em>node2</em></span>&#160;)</td>
</tr>
</table>
</div><div class="memdoc">
<p>Merge 2 components to become one. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">node1</td><td>1st component </td></tr>
<tr><td class="paramname">node2</td><td>2nd component </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 62</span> {</div>
<div class="line"><span class="lineno"> 63</span> node1 = <a class="code hl_function" href="#a469384d8a4197a9b24482ce7c321a85e">find_set</a>(node1); <span class="comment">// find the parent of node1</span></div>
<div class="line"><span class="lineno"> 64</span> node2 = <a class="code hl_function" href="#a469384d8a4197a9b24482ce7c321a85e">find_set</a>(node2); <span class="comment">// find the parent of node2</span></div>
<div class="line"><span class="lineno"> 65</span> </div>
<div class="line"><span class="lineno"> 66</span> <span class="comment">// If parents of both nodes are not same, combine them</span></div>
<div class="line"><span class="lineno"> 67</span> <span class="keywordflow">if</span> (node1 != node2) {</div>
<div class="line"><span class="lineno"> 68</span> <span class="keywordflow">if</span> (connected_set_size[node1] &lt; connected_set_size[node2]) {</div>
<div class="line"><span class="lineno"> 69</span> <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/algorithm/swap.html">std::swap</a>(node1, node2); <span class="comment">// swap both components</span></div>
<div class="line"><span class="lineno"> 70</span> }</div>
<div class="line"><span class="lineno"> 71</span> parent[node2] = node1; <span class="comment">// make node1 as parent of node2.</span></div>
<div class="line"><span class="lineno"> 72</span> connected_set_size[node1] +=</div>
<div class="line"><span class="lineno"> 73</span> connected_set_size[node2]; <span class="comment">// sum the size of both as they combined</span></div>
<div class="line"><span class="lineno"> 74</span> }</div>
<div class="line"><span class="lineno"> 75</span>}</div>
<div class="ttc" id="aswap_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/algorithm/swap.html">std::swap</a></div><div class="ttdeci">T swap(T... args)</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d8/d99/connected__components__with__dsu_8cpp_a67cb7472f310a798f555fe45cdf50145_cgraph.svg" width="340" height="126"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
<h2 class="groupheader">Variable Documentation</h2>
<a id="af675d8149b6a82cb7fa07f39210b8a2b" name="af675d8149b6a82cb7fa07f39210b8a2b"></a>
<h2 class="memtitle"><span class="permalink"><a href="#af675d8149b6a82cb7fa07f39210b8a2b">&#9670;&#160;</a></span>connected_set_size</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>&lt;uint32_t&gt; graph::disjoint_union::connected_set_size {}</td>
</tr>
</table>
</div><div class="memdoc">
<div class="fragment"><div class="line"><span class="lineno"> 33</span>{}; <span class="comment">// size of each set</span></div>
</div><!-- fragment -->
</div>
</div>
<a id="aa93bd9d3409fe4eeb7e5658eddd75fa8" name="aa93bd9d3409fe4eeb7e5658eddd75fa8"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aa93bd9d3409fe4eeb7e5658eddd75fa8">&#9670;&#160;</a></span>parent</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>&lt;int64_t&gt; graph::disjoint_union::parent {}</td>
</tr>
</table>
</div><div class="memdoc">
<div class="fragment"><div class="line"><span class="lineno"> 32</span>{}; <span class="comment">// parent of each node</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_12552d7fa429bf94a2e32e5cf39f7e69.html">graph</a></li><li class="navelem"><a class="el" href="../../d8/d99/connected__components__with__dsu_8cpp.html">connected_components_with_dsu.cpp</a></li>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.10.0 </li>
</ul>
</div>
</body>
</html>