int Graph::allPaths(graphNode &v, graphNode &w,vector<string> visited){
visited.push_back(v.name);
if ((v.name).compare(w.name)==0){
string str = vectorToString(visited);
all_paths.push_back(str);
return 1;
}
else{
map<string, map<string, graphNode> >::iterator pos;
pos = graph.find(v.name);
if(pos != graph.end()) {
map<string, graphNode> pn = pos->second;
map<string, graphNode>::iterator p = pn.begin();
while(p != pn.end()) {
string name = p->first;
vector<string>::iterator it;
it = find(visited.begin() , visited.end() , name);
if (it!=visited.end()) {
} //if
else{
allPaths(p->second,w,visited);
}
p++;
} //while
return 0;
} //if
else{
cout << v.name << " does not exists in the graph \n ";
}
} //else
}
I have about 510096 nodes in the graph and when I call the all_paths method, I get the following error:
terminate called after throwing an instance of 'std::bad_alloc'
I am not sure why this is happening? Could you help me with that?
One thing that I noticed is that some parts of the graph are not connected, as a result when I reach the leaf of the graph, it enters this part of the code "cout << v.name << " does not exists in the graph \n "; and I am not sure if the recursive part of the code can still handle such a situation or not. Do you have any suggestions for this part?
If installing more memory is not an option, you'll have to keep most of the structure in your file system. If you're using a 32-bit OS, you can't use std::fstream or the C file interface, since they're limited to seeking only within the first 2 GiB (2^31-1 bytes, to be exact).