|
<html><head><title>dlib C++ Library - disjoint_subsets.h</title></head><body bgcolor='white'><pre> |
|
<font color='#009900'>// Copyright (C) 2011 Davis E. King ([email protected]) |
|
</font><font color='#009900'>// License: Boost Software License See LICENSE.txt for the full license. |
|
</font><font color='#0000FF'>#ifndef</font> DLIB_DISJOINT_SUBsETS_Hh_ |
|
<font color='#0000FF'>#define</font> DLIB_DISJOINT_SUBsETS_Hh_ |
|
|
|
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='disjoint_subsets_abstract.h.html'>disjoint_subsets_abstract.h</a>" |
|
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>vector<font color='#5555FF'>></font> |
|
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../algs.h.html'>../algs.h</a>" |
|
|
|
<font color='#0000FF'>namespace</font> dlib |
|
<b>{</b> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'>class</font> <b><a name='disjoint_subsets'></a>disjoint_subsets</b> |
|
<b>{</b> |
|
<font color='#0000FF'>public</font>: |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='clear'></a>clear</b> <font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font> noexcept |
|
<b>{</b> |
|
items.<font color='#BB00BB'>clear</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='set_size'></a>set_size</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> new_size |
|
<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
items.<font color='#BB00BB'>resize</font><font face='Lucida Console'>(</font>new_size<font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'><</font> items.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
items[i].parent <font color='#5555FF'>=</font> i; |
|
items[i].rank <font color='#5555FF'>=</font> <font color='#979000'>0</font>; |
|
<b>}</b> |
|
<b>}</b> |
|
|
|
<font color='#0000FF'><u>size_t</u></font> <b><a name='size'></a>size</b> <font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> noexcept |
|
<b>{</b> |
|
<font color='#0000FF'>return</font> items.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> <b><a name='find_set'></a>find_set</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> item |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> |
|
<b>{</b> |
|
<font color='#009900'>// make sure requires clause is not broken |
|
</font> <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font>item <font color='#5555FF'><</font> <font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, |
|
"<font color='#CC0000'>\t unsigned long disjoint_subsets::find_set()</font>" |
|
<font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n\t item must be less than size()</font>" |
|
<font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n\t item: </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> item |
|
<font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n\t size(): </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> <font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> |
|
<font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n\t this: </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> <font color='#0000FF'>this</font> |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>items[item].parent <font color='#5555FF'>=</font><font color='#5555FF'>=</font> item<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>return</font> item; |
|
<b>}</b> |
|
<font color='#0000FF'>else</font> |
|
<b>{</b> |
|
<font color='#009900'>// find root of item |
|
</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> x <font color='#5555FF'>=</font> item; |
|
<font color='#0000FF'>do</font> |
|
<b>{</b> |
|
x <font color='#5555FF'>=</font> items[x].parent; |
|
<b>}</b> <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>items[x].parent <font color='#5555FF'>!</font><font color='#5555FF'>=</font> x<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#009900'>// do path compression |
|
</font> <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> root <font color='#5555FF'>=</font> x; |
|
x <font color='#5555FF'>=</font> item; |
|
<font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>items[x].parent <font color='#5555FF'>!</font><font color='#5555FF'>=</font> x<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> prev <font color='#5555FF'>=</font> x; |
|
x <font color='#5555FF'>=</font> items[x].parent; |
|
items[prev].parent <font color='#5555FF'>=</font> root; |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>return</font> root; |
|
<b>}</b> |
|
<b>}</b> |
|
|
|
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> <b><a name='merge_sets'></a>merge_sets</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> a, |
|
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> b |
|
<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#009900'>// make sure requires clause is not broken |
|
</font> <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font>a <font color='#5555FF'>!</font><font color='#5555FF'>=</font> b <font color='#5555FF'>&</font><font color='#5555FF'>&</font> |
|
a <font color='#5555FF'><</font> <font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&</font><font color='#5555FF'>&</font> |
|
b <font color='#5555FF'><</font> <font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&</font><font color='#5555FF'>&</font> |
|
<font color='#BB00BB'>find_set</font><font face='Lucida Console'>(</font>a<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> a <font color='#5555FF'>&</font><font color='#5555FF'>&</font> |
|
<font color='#BB00BB'>find_set</font><font face='Lucida Console'>(</font>b<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> b, |
|
"<font color='#CC0000'>\t unsigned long disjoint_subsets::merge_sets(a,b)</font>" |
|
<font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n\t invalid arguments were given to this function</font>" |
|
<font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n\t a: </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> a |
|
<font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n\t b: </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> b |
|
<font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n\t size(): </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> <font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> |
|
<font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n\t find_set(a): </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> <font color='#BB00BB'>find_set</font><font face='Lucida Console'>(</font>a<font face='Lucida Console'>)</font> |
|
<font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n\t find_set(b): </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> <font color='#BB00BB'>find_set</font><font face='Lucida Console'>(</font>b<font face='Lucida Console'>)</font> |
|
<font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n\t this: </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> <font color='#0000FF'>this</font> |
|
<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>items[a].rank <font color='#5555FF'>></font> items[b].rank<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
items[b].parent <font color='#5555FF'>=</font> a; |
|
<font color='#0000FF'>return</font> a; |
|
<b>}</b> |
|
<font color='#0000FF'>else</font> |
|
<b>{</b> |
|
items[a].parent <font color='#5555FF'>=</font> b; |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>items[a].rank <font color='#5555FF'>=</font><font color='#5555FF'>=</font> items[b].rank<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
items[b].rank <font color='#5555FF'>=</font> items[b].rank <font color='#5555FF'>+</font> <font color='#979000'>1</font>; |
|
<b>}</b> |
|
<font color='#0000FF'>return</font> b; |
|
<b>}</b> |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>private</font>: |
|
|
|
<font color='#009900'>/* |
|
See the book Introduction to Algorithms by Cormen, Leiserson, Rivest and Stein |
|
for a discussion of how this algorithm works. |
|
*/</font> |
|
|
|
<font color='#0000FF'>struct</font> <b><a name='data'></a>data</b> |
|
<b>{</b> |
|
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> rank; |
|
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> parent; |
|
<b>}</b>; |
|
|
|
<font color='#0000FF'>mutable</font> std::vector<font color='#5555FF'><</font>data<font color='#5555FF'>></font> items; |
|
|
|
<b>}</b>; |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_DISJOINT_SUBsETS_Hh_ |
|
</font> |
|
</pre></body></html> |