I am trying to use the LEMON library and I am running into an issue with an algorithm that I am trying to implement. The idea of this algorithm is that there is a vector of vectors of nodes and I want to move the nodes around to different vectors (color classes) with certain restrictions. Here is the code that implements my algorithm:
bool moveColor() {
// pick the smallest color class
sort(colors.begin(), colors.end(), vectorSort);
vector< vector< ListGraph::Node > >::iterator smallestColor = colors.begin();
// shuffle this class
random_shuffle(smallestColor->begin(), smallestColor->end());
// try to move any of the nodes to any bigger class
bool foundNode = false;
vector< ListGraph::Node >::iterator movingNode;
vector< vector< ListGraph::Node > >::iterator destinationClass;
for (movingNode = smallestColor->begin(); movingNode != smallestColor->end() && !foundNode; ++movingNode) {
for (destinationClass = colors.begin()+1; destinationClass != colors.end() && !foundNode; ++destinationClass) {
ListGraph::NodeMap<bool> filter(g, false);
vector< ListGraph::Node >::iterator classNode;
for (classNode = destinationClass->begin(); classNode != destinationClass->end(); ++classNode) {
filter[*classNode] = true;
}
filter[*movingNode] = true;
FilterNodes<ListGraph> subgraph(g, filter);
if (acyclic(subgraph)) {
foundNode = true;
}
}
}
// if a movable node was found, move it. otherwise return false
if (foundNode) {
destinationClass->push_back(*movingNode);
smallestColor->erase(movingNode);
if (smallestColor->empty()) {
colors.erase(smallestColor);
}
return true;
}
return false;
}
This function is called in main in a loop until a node was unable to be moved. The main issue I am having with the code is the section that actually moves a node from one vector to another:
if (foundNode) {
destinationClass->push_back(*movingNode);
smallestColor->erase(movingNode);
if (smallestColor->empty()) {
colors.erase(smallestColor);
}
return true;
}
This part doesn't seem to work because I think the node is being deconstructed when the erase function is being called. Here is a sample of the node ids before and after this function was called:
NEW ROUND
1
3
0
5 2
7 6 4
NEW ROUND
3
0 32701
5 2
7 6 4
As you can see, the id for the node that was moved is not correct (32701 instead of 1). Any help is appreciated.