I’ve been coding a solution for the Graph Coloring Problem using Brown’s algorithm. The algorithm is working just fine but, to add a little efficiency I’m trying to sort the nodes by their degree. To do so I’m using stl::sort(). Nonetheless, after sort has ordered the elements, the adyacency list of each element has been modified. I’ve got 4 files:
nodo.h:
class Nodo{
private:
int id;
int color;
vector<Nodo*> adyacentes;
public:
int grado;
vector<Nodo*> getAdyacentes();
int getId();
int getColor();
}
nodo.cpp -> Includes the basic implementation of the getters.
grafo.h:
class Grafo{
private:
Nodo *nodos;
int tam;
public:
void colorearBrown();
void imprimir();
}
grafo.cpp:
void Grafo::imprimir(){
cout << setw(6) << "Numero" << setw(5) << " Color" << " Nodos adyacentes" << endl;
for(int i = 0; i < tam; ++ i){
cout << setw(6) << nodos[i].getId() << setw(5) << nodos[i].getColor() << " ";
vector<Nodo*> ady = nodos[i].getAdyacentes();
for(vector<Nodo*>::iterator it = ady.begin(); it != ady.end(); ++ it){
if (it != ady.begin()){
cout << ",";
}
cout << (*it)->getId();
}
cout << endl;
}
}
bool operator<(const Nodo& a, const Nodo& b){
return (a.grado >= b.grado);
}
void Grafo::colorearBrown(){
imprimir();
sort(&nodos[0], &nodos[tam]);
cout << endl << endl;
imprimir();
}
And that’s it. Assuming I’ve a correctly loaded graph in a ‘Grafo’ object, I run the method colorearBrown() and get the following output (part):
.
.
.
16 -1 1,22,36,43,47,48
17 -1 10,13,20,22,44,47
18 -1 27,32,48
19 -1 4,32,36
20 -1 6,8,10,12,17,33,36,38,43,45,48
21 -1 4,6,45
22 -1 6,16,17,26,30,31
23 -1 3,8,10,14,24,26,32,36
.
.
.
Numero Color Nodos adyacentes
20 -1 4,42,22,32,10,3,34,50,18,19,25
7 -1 20,32,6,13,3,50,2,18,31,19
36 -1 7,14,16,44,12,38,30,1,50,37
14 -1 22,30,49,24,2,11,40,5
.
.
.
See how the list of adyacent nodes changes on node 20(here), but this is repeated for all nodes.
The program perfectly compiles and, if not sorted, the nodes stay the same way throughout the hole execution.
Any ideas on why the sorting algorithm is messing with my structure would be helpful.
std::sortcopies (or moves, in C++11) the objects. However you have pointers in your objects, which point at the place where the object is. After the sort, there’s another object at that place.Think of it as people sitting on chairs. You give each person a list of chairs where the “adjacent” people sit. And then you ask the people to change places. People changing places does not change what is written on the paper.
The people are the node objects, the places are the positions in he array, and the pieces of paper with the lists are your vectors of pointers to adjacent nodes.
A simple solution to that problem would be using a
std::listand sorting with itssortmember function. That member function doesn’t move objects around, but just relinks the internal nodes, therefore your pointers will continue to point to the correct object. One disadvantage of that solution (which may or may not matter in your case) is that the list doesn’t offer random access.