本文整理汇总了C++中DisjointSets类的典型用法代码示例。如果您正苦于以下问题:C++ DisjointSets类的具体用法?C++ DisjointSets怎么用?C++ DisjointSets使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DisjointSets类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char *argv[])
{
// put command line args into variables
const char *inputFile = argv[1];
const char *outputFile = argv[2];
cout << endl << "input file: " << inputFile << endl;
cout << "output file: " << outputFile << endl;
// use Image struct
Image newImage;
Image * im;
im = &newImage;
DisjointSets * setPointer = NULL;
setPointer = new DisjointSets(1);
// read image, get # of columns and rows
readImage(im, inputFile);
numCols = getNCols(im);
numRows = getNRows(im);
scanImage(im, setPointer);
resolveEquivalences(im, setPointer);
setColors(im, setPointer->NumSets());
writeImage(im, outputFile);
return 0;
}
示例2: listSets
static list<int> listSets(const DisjointSets &ds)
{
list<int> sets;
for (int i = 0; i < ds.NumElements(); ++i)
{
if (ds.FindSet(i) == i)
sets.push_front(i);
}
return sets;
}
示例3: listElements
static list<int> listElements(const DisjointSets &ds, int set)
{
list<int> elements;
for (int i = 0; i < ds.NumElements(); ++i)
{
if (ds.FindSet(i) == ds.FindSet(set))
elements.push_front(i);
}
return elements;
}
示例4: bottomRemove
void SquareMaze::bottomRemove(int x, int y, DisjointSets & maze,int & count,int rindex)
{
if (maze.find(rindex) != maze.find(rindex+maze_width))
{
setWall(x, y, 1, false);
maze.setunion(rindex, rindex+maze_width);
count++;
}
}
示例5: rightRemove
void SquareMaze::rightRemove(int x, int y, DisjointSets & maze,int & count,int rindex)
{
if (maze.find(rindex) != (maze.find(rindex+1)))
{
setWall(x, y, 0, false);
maze.setunion(rindex, rindex+1);
count++;
}
}
示例6: dynamic_connected_components
void
dynamic_connected_components(EdgeListGraph& g, DisjointSets& ds)
{
typename graph_traits<EdgeListGraph>::edge_iterator e, end;
for (tie(e,end) = edges(g); e != end; ++e)
ds.link(source(*e,g),target(*e,g));
}
示例7: expandSet
/*
* set cannot handle over 255 elements. if this occurs, find elements
* in current set, copy into a vector. push elements in the vector
* into a new disjoint set, reassign pointer.
*
*/
void expandSet(DisjointSets** s)
{
DisjointSets * newPointer = NULL;
vector <int> currElements = getLabels(*s);
cout << "expandSet()" << currElements.size() << endl;
newPointer = new DisjointSets(1);
for (vector<int>::iterator it = currElements.begin(); it!=currElements.end(); ++it)
{
cout << distance(currElements.begin(), it) << " "<< *it << endl;
newPointer->AddElements(*it);
}
s = &newPointer;
}
示例8: initialize_dynamic_components
void
initialize_dynamic_components(VertexListGraph& G,
DisjointSets& ds)
{
typename graph_traits<VertexListGraph>
::vertex_iterator v, vend;
for (tie(v, vend) = vertices(G); v != vend; ++v)
ds.make_set(*v);
}
示例9: sort
/**
* Finds a minimal spanning tree on a graph.
* THIS FUNCTION IS GRADED.
*
* @param graph - the graph to find the MST of
*
* @todo Label the edges of a minimal spanning tree as "MST"
* in the graph. They will appear blue when graph.savePNG() is called.
*
* @note Use your disjoint sets class from MP 7.1 to help you with
* Kruskal's algorithm. Copy the files into the libdsets folder.
* @note You may call std::sort (http://www.cplusplus.com/reference/algorithm/sort/)
* instead of creating a priority queue.
*/
void GraphTools::findMST(Graph & graph)
{
vector<Edge> eds = graph.getEdges();
sort(eds.begin(), eds.end(),myfunction);
DisjointSets vers;
vector <Vertex> vertex_list = graph.getVertices();
vers.addelements(vertex_list.size());
for (int i = 0; i < eds.size(); i++)
{
Vertex u = eds[i].source;
Vertex v = eds[i].dest;
if (vers.find(u) != vers.find(v))
{
vers.setunion(u,v);
graph.setEdgeLabel(u,v,"MST");
}
}
}
示例10: findMST
/**
* Finds a minimal spanning tree on a graph.
* THIS FUNCTION IS GRADED.
*
* @param graph - the graph to find the MST of
*
* @todo Label the edges of a minimal spanning tree as "MST"
* in the graph. They will appear blue when graph.savePNG() is called.
*
* @note Use your disjoint sets class from MP 7.1 to help you with
* Kruskal's algorithm. Copy the files into the libdsets folder.
* @note You may call std::sort (http://www.cplusplus.com/reference/algorithm/sort/)
* instead of creating a priority queue.
*/
void GraphTools::findMST(Graph & graph)
{
vector<Edge> theEdges = graph.getEdges();
std::sort(theEdges.begin(), theEdges.end());
DisjointSets theVertexSet;
vector<Vertex> theVertices = graph.getVertices();
theVertexSet.addelements(theVertices.size());
int size = theVertices.size();
int count = 0;
vector<Edge>::iterator iter;
for(iter = theEdges.begin(); iter != theEdges.end(); iter++)
{
if(count == size - 1) break;
if(theVertexSet.find(iter->source) != theVertexSet.find(iter->dest))
{
theVertexSet.setunion(iter->source, iter->dest);
graph.setEdgeLabel(iter->source, iter->dest, "MST");
count++;
}
}
}
示例11: makeMaze
void SquareMaze::makeMaze(int width, int height)
{
right.clear();
bottom.clear();
maze_width = width;
maze_height = height;
blocks = maze_width * maze_height;
right.resize(blocks,true);
bottom.resize(blocks,true);
DisjointSets maze;
maze.addelements(blocks);
srand(time(NULL));
//int x, y;
bool rightmost=true, bottommost=true;
int count = 0;
//int rindex;
while (count< (blocks - 1))
{
int rindex = rand() % blocks;
int x = rindex%maze_width;
int y = rindex/maze_width;
if(x==(maze_width-1))
rightmost=true;
else rightmost=false;
if(y==(maze_height-1))
bottommost=true;
else bottommost=false;
if (( rightmost == true ) && ( bottommost == true ))
{
rightmost=rightmost;
}
else if (!rightmost && !bottommost)
{
int action=rand()%3;
if (action == 0)
rightRemove(x,y, maze,count,rindex);
else if (action == 1)
bottomRemove(x,y, maze,count,rindex);
else {
if ((maze.find(rindex) != maze.find(rindex+1)) && (maze.find(rindex) != maze.find(rindex+maze_width))&& (maze.find(rindex+1) != maze.find(rindex+1)))
{
setWall(x, y, 0, false);
setWall(x, y, 1, false);
maze.setunion(rindex, rindex+1);
maze.setunion(rindex, rindex+maze_width);
count+=2;
}
}
}
else if(bottommost)
{
int action=rand()%2;
if (action== 1)rightRemove(x,y, maze,count,rindex);
}
else
{
int action =rand()%2;
if (action == 1)bottomRemove(x,y, maze,count,rindex);
}
}
}
示例12: printElementSets
void printElementSets(const DisjointSets & s)
{
for (int i = 0; i < s.NumElements(); ++i)
cout << s.FindSet(i) << " ";
cout << endl;
}
示例13: blocks
void SquareMaze::makeMaze(int width,int height)
{
width1=width;
height1=height;
int size=width*height;
right.clear();
down.clear();//clearing right and down walls just in case
right=vector<bool>(size, true);//set all walls so they are there we will remove later
down=vector<bool>(size, true);
vector<int> blocks(size);//vector for indicies
// map<int, int> maps;
for(int i=0;i<size; i++)
{
blocks.push_back(i);//pushing back the indidces for the maze
// for(int j=0; j<height1; j++)
// {
// // maps.insert(make_pair(i,j));
// }
}
DisjointSets set;
set.addelements(size);//used to stop cycles from being created
srand(time(0));
// std::random_shuffle(maps.begin(), maps.end());
std::random_shuffle(blocks.begin(), blocks.end());//randomly shuffles indicies so it doesnt create the same maze
// map<int, int>::iterator it;
vector<int> :: iterator it;
for(it=blocks.begin(); it!=blocks.end(); it++)
{
int x=*it%width;//x and y coords for the index
int y=*it/width;
int index=*it; //actual index
if(x!=width1-1 ) //cant go any farther to the right in this case so dont want to do it
{
int index1=index+1;
if(set.find(index)!=set.find(index1)) //checking if the one to the right of the index is in the same set
{
setWall(x,y,0,false);//if they are not in the same set we can remove the wall
set.setunion(index, index1); //we connect them so they are in the same set
//works because if we try to remove something in the same set it will create a cycle
}
}
if(y!=height1-1)
{
int index2=index+width1;
if(set.find(index)!= set.find(index2))
{
setWall(x,y,1,false); //do the same for if we want to go down
set.setunion(index, index2);
}
}
}
}
示例14: SetEquivalent
// Add the fact that v1 and v2 are equivalent
// return true if v1 was not already equivalent to v2
// and false if v1 was already equivalent to v2
bool SetEquivalent(int v1, int v2)
{
bool const already_equiv = false;
if (v1 == v2)
return already_equiv;
ensureElementExists(v1);
ensureElementExists(v2);
const int r1 = disjoint_sets_.find_set(v1);
const int r2 = disjoint_sets_.find_set(v2);
if (r1 == r2)
return already_equiv;
root_set_map_t::const_iterator it1 = rootSetMap_.find(r1);
assert(it1 != rootSetMap_.end());
std::list<int> s1 = it1->second;
root_set_map_t::const_iterator it2 = rootSetMap_.find(r2);
assert(it2 != rootSetMap_.end());
std::list<int> s2 = it2->second;
s1.splice(s1.begin(), s2); // union the related sets
disjoint_sets_.link(r1, r2); // union the disjoint sets
// associate the combined related set with the new root
int const new_root = disjoint_sets_.find_set(v1);
if (new_root != r1) {
rootSetMap_.erase(it1);
} else {
rootSetMap_.erase(it2);
}
rootSetMap_[new_root] = s1;
return !already_equiv;
}
示例15: AreEquivalent
// Return true if v1 and v2 are equivalent
bool AreEquivalent(int v1, int v2) const
{
if (v1 == v2) {
return true;
}
// The element does not exist, so we do not
// know any equivalence information,
// hence, v1 and v2 are not equivalent.
if (!elementExists(v1) || !elementExists(v2)) {
return false;
}
return disjoint_sets_.find_set(v1) == disjoint_sets_.find_set(v2);
}