And check the collection of Ufset class

Source: Internet
Author: User
Tags bool constructor continue int size rand

/*

Name: And look up the Ufset class

Copyright: A column originating in goal00001111; allowing for free reprint, but must indicate author and source

author:goal00001111

date:23-12-08 15:21

Description: The common algorithm of finding and merging is realized, and the compression path and the algorithm of calculating by size are realized, and the two are tested and compared.

The analysis of the algorithm discussed in flops "a simple and interesting data structure--and search the collection":

Http://blog.csdn.net/goal00001111/archive/2008/12/24/3595844.aspx

*/

#include <iostream>
#include <time.h>
using namespace Std;
const int MAX = 50000; Maximum number of members of the collection
Class ufset{//And collection classes
Public
Ufset (int s = MAX); Constructors
Ufset (const ufset & obj); Copy Constructors
~ufset ()//destructor
{
delete []father;
}
Ufset & operator = (const ufset & obj); Assignment function
int findfather (int pos); Looking for serial number POS of the Patriarch Monseigneur
BOOL Unite (int posi, int posj)//merge member Posi and POSJ into the same family
int Findfatherandreducepath (int pos); Find the patriarch and compress the path
BOOL Unionbysize (int posi, int posj); Ask for and by size
BOOL Samefamily (int posi, int posj); Judge whether members Posi and POSJ belong to the same family
void Printufset ();
Private
int *father; And a collection of member arrays, storing the father nodes of the elements
int size; And check the number of members
};
Ufset::ufset (int s): size (s)//constructor
{
Father = new Int[size + 1];
for (int i=0; i<=size; i++)//all array element values are initialized to-1
Father[i] =-1;
}
Ufset::ufset (const Ufset & obj)//copy constructor
{
size = Obj.size;
Father = new Int[size + 1];
for (int i=0; i<=size; i++)
Father[i] = Obj.father[i];
}
Ufset & ufset::operator = (const Ufset & obj)//Assignment function
{
if (this = = &obj)//Check self assignment
return *this;
delete []father; Releasing the original memory resource
size = Obj.size; Allocate new memory resources and copy content
Father = new Int[size + 1];
for (int i=0; i<=size; i++)
Father[i] = Obj.father[i];
return *this; Returns a reference to this object
}
int ufset::findfather (int pos)//Looking for the ordinal number of the Patriarch of Pos. If the POS itself is the patriarch, return itself
{
if (Father[pos] < 0)
return POS;
Return Findfather (Father[pos]);
}
BOOL Ufset::unite (int posi, int posj)//merge members Posi and POSJ into the same family
{
First of all, to find their own patriarch.
int FI = Findfather (posi);
int FJ = Findfather (POSJ);
if (FI = = FJ)///If it is under the same patriarch, you do not have to merge, that is, merge failed
return false;
Else
FATHER[FJ] = FI; Otherwise fi when the patriarch: Who Let Posi stand in front of POSJ!
return true;
}
int Ufset::findfatherandreducepath (int pos)/Find patriarch and Compress path
{
if (Father[pos] < 0)
return POS;
If he is not a patriarch, he will find the patriarch and point his way to the patriarch.
return Father[pos] = Findfatherandreducepath (Father[pos]);
}
BOOL Ufset::unionbysize (int posi, int posj)/by size and
{
First of all, to find their own patriarch.
int FI = Findfatherandreducepath (posi);
int FJ = Findfatherandreducepath (POSJ);
if (FI = = FJ)///If it is under the same patriarch, you do not have to merge, that is, merge failed
return false;
else if (Father[fi] < FATHER[FJ])
{//If Patriarch Fi's strength is stronger than FJ, that is |fi|>|fj|, then FI is the patriarch and modifies Father[fi] and FATHER[FJ]
FATHER[FI] + = FATHER[FJ];
FATHER[FJ] = FI;
}
else//otherwise FJ as Patriarch
{
FATHER[FJ] + = Father[fi];
FATHER[FI] = FJ;
}
return true;
}
BOOL Ufset::samefamily (int posi, int posj)//Judge whether members Posi and POSJ belong to the same family
{
Return Findfatherandreducepath (posi) = = Findfatherandreducepath (POSJ);
}
void Ufset: All elements of the output collection:P Rintufset ()/
{
for (int i=0; i<=size; i++)
cout << father[i] << ';
cout << Endl;
}
int main ()
{
time_t StartTime, Endtime;
Ufset obj;
int p, q;
Time (&starttime);
for (int i=0; i<max; i++)
{
p = rand ()% MAX + 1;
Q = rand ()% MAX + 1;
if (p = = q)
Continue
cout << P << "-" << Q << "";
if (i% 10 = 0)
cout << Endl;
Obj. Unite (P, q);
}
while (1)
{
p = rand ()% MAX + 1;
Q = rand ()% MAX + 1;
if (p = = q)
Continue
if (obj. Samefamily (P, Q))
cout << Endl << P << "Chess" << Q << Endl;
Else
cout << Endl << P << "!≡" << q << Endl;
Break
}
Time (&endtime);
cout << difftime (Endtime, starttime) << Endl;
//////////////////////////////////////////////////////////////////////////
Time (&starttime);
for (int i=0; i<max; i++)
{
p = rand ()% MAX + 1;
Q = rand ()% MAX + 1;
if (p = = q)
Continue
cout << P << "-" << Q << "";
if (i% 10 = 0)
cout << Endl;
Obj. Unionbysize (P, q);
}
while (1)
{
p = rand ()% MAX + 1;
Q = rand ()% MAX + 1;
if (p = = q)
Continue
if (obj. Samefamily (P, Q))
cout << Endl << P << "Chess" << Q << Endl;
Else
cout << Endl << P << "!≡" << q << Endl;
Break
}
Time (&endtime);
cout << difftime (Endtime, starttime) << Endl;
System ("pause");
return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.