I am running Tarjan Algorithm to find SCC in c++. Code is running perfectly for me when number of nodes are 80k. But When I ran the code with more than 80k graph nodes. It is giving me an error as stack overflow
Unhandled exception at 0x76d84b61 in Partition_2.exe: 0xC00000FD: Stack overflow.
I looked online for some suggestions, it says my functions passes maximum number of recursion. I have no idea what to do now?
Here is my code:
Tarjan::Tarjan(vector<LinkedList> &graph, vector<vector<LinkedList*>> &SCC)
{
cout<<endl<<"Finding SCC in graph ....."<<endl;
clock_t start, finish;
start = clock();
int num = 1;
vector<LinkedList*> stack;
for(size_t i = 0; i<graph.size(); i++)
{
if(graph[i].index == 0)
{
strongConnect(&graph[i], num, stack, SCC);
}
}
finish = clock();
if(SCC.size()>0)
{
cout<<"Number of cycles found in graph are "<<SCC.size()<<endl;
timeTracker=(finish-start);
cout<<"Time taken to find cycles in graph is "<<timeTracker<<" ms "<<endl;
}
else
cout<<"No cycles found in graph "<<endl<<endl;
}
void Tarjan::strongConnect(LinkedList* vertex, int &num, vector<LinkedList*> &stack, vector<vector<LinkedList*>> &cycles)
{
vertex->index = num;
vertex->lowlink = num;
num++;
vertex->inStack = true;
stack.push_back(vertex);
for(map<LinkedList*, string>::iterator it=vertex->childaddr.begin(); it!=vertex->childaddr.end(); it++)
{
if(it->first->index == 0)
{
strongConnect(it->first, num, stack, cycles);
vertex->lowlink = min(vertex->lowlink, it->first->lowlink);
}
else
{
if(it->first->inStack)
{
vertex->lowlink = min(vertex->lowlink, it->first->index);
}
}
}
if(vertex->lowlink == vertex -> index)
{
vector<LinkedList*> temp;
LinkedList* x;
do{
x=stack[stack.size()-1];
vector<LinkedList*>::iterator st = stack.end(); st--;
stack.erase(st);
x->inStack=false;
temp.push_back(x);
if(temp.size()>1)
x->inClique = true;
}while(vertex->node != x->node);
if(temp.size()>1)
{
temp[0]->inClique=true;
cycles.push_back(temp);
}
}
}
LinkedList.h
class LinkedList
{
public:
string node;
map<LinkedList*, string> childaddr;
bool inStack; // to find ssc
bool inClique; // To distinguish if node belongs to any clique or not
int index; // to find ssc
int lowlink; // to find ssc
bool visited;
};
I had spent weeks on this to figure out the solution but no gain. Please help me here. Thanks.