我正在运行Tarjan算法以在c ++中查找SCC。当节点数为80k时,代码正在为我运行。但是当我运行超过80k图形节点的代码时。堆栈溢出给我一个错误
Partition_2.exe中0x76d84b61处的未处理异常:0xC00000FD:堆栈溢出。
我在线查看了一些建议,它说我的函数传递了最多的递归次数。我不知道现在该做什么?
这是我的代码:
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;
};
我花了好几周的时间来找出解决方案,但没有收获。请帮帮我。感谢。
答案 0 :(得分:0)
对于数百万个节点,或者如果您的程序需要在不控制编译器选项的计算机上进行编译,请考虑使用std :: stack数据结构和循环替换递归:http://www.codeproject.com/Articles/418776/How-to-replace-recursive-functions-using-stack-and(或者这个更短,但不是用C ++ http://haacked.com/archive/2007/03/04/Replacing_Recursion_With_a_Stack.aspx/)