C++ 递归算法调试(大数据量)
我使用基于DFS的算法来计算大型图中的强分量。该算法在小图上运行良好,但在大图上我遇到了一个问题。所以在大约28K的DFS函数递归调用之后,程序就挂起了。VS2010给我一个消息VS正忙,没有崩溃没有错误。为了弄清楚发生了什么,我打印了一些信息(由于速度低,无法在调试中运行)。我发现程序挂起在位置4,没有到达位置1(注意代码)C++ 递归算法调试(大数据量),c++,visual-studio,debugging,C++,Visual Studio,Debugging,我使用基于DFS的算法来计算大型图中的强分量。该算法在小图上运行良好,但在大图上我遇到了一个问题。所以在大约28K的DFS函数递归调用之后,程序就挂起了。VS2010给我一个消息VS正忙,没有崩溃没有错误。为了弄清楚发生了什么,我打印了一些信息(由于速度低,无法在调试中运行)。我发现程序挂起在位置4,没有到达位置1(注意代码) //主DFS函数 void DFS(向量和图形、int源节点、布尔*顶点、成对和方向){ 这是stackoverflow错误,我第一次没有得到它,但在从DFS列表中删除一
//主DFS函数
void DFS(向量和图形、int源节点、布尔*顶点、成对和方向){
这是stackoverflow错误,我第一次没有得到它,但在从DFS列表中删除一些参数后,我遇到了StackOverflower错误。我猜是由于递归调用太多,导致堆栈溢出。所以为什么我没有得到堆栈溢出错误?stackoverflow通常会导致一个独特的崩溃,但不能保证我假设(毕竟是UB)。我认为这还没有“完成”,并且花了相当长的时间“恢复”?似乎有三个论点(共四个)可以放置在外部。使重载只接收int source_node
,查看递归的数量是否允许更改您最终做了什么来修复stackoverflow错误?我正在尝试在大型图上为DFS编写类似的算法,我怀疑我遇到了类似的问题。
// Main DFS function
void DFS(vector<Edge>& graph, int source_node, bool *vertex_visited, pair<int, int>& direction){
cout << "\r" << "Position 1" << std::flush;
// mark vertex as visited
vertex_visited[source_node - 1] = false;
// array for all neighbour edges
vector<vector<Edge>::iterator> all_neighbours;
// doesent matter
if (direction.second){
size_of_scc++;
}
// binary search of edges incident with source vertex
pair<vector<Edge>::iterator, bool> itera = find_if_binary_for_edges(graph.begin(), graph.end(), source_node);
cout << "\r" << "Position 2" << std::flush;
// push all incident edges to all_neighbours vector
if (itera.second){
pair<vector<Edge>::iterator, vector<Edge>::iterator> bounds = find_all_in_range(itera.first, graph);
vector<Edge>::iterator it = bounds.first;
while (it != bounds.second){
all_neighbours.push_back(it++);
}
}
cout << "\r" << "Position 3" << std::flush;
// if this vertex wasn't visited in the past cal DFS from neighbour vertex
for (vector<vector<Edge>::iterator>::iterator it = all_neighbours.begin(); it != all_neighbours.end(); ++it){
if (vertex_visited[(**it)[1] - 1]){
cout << "\r" << "Position 4" << std::flush;
DFS(graph, (**it)[1], vertex_visited, direction);
};
}
// need this stuff for SCC computation
cout << "\r" << "Position 5" << std::flush;
if (direction.first)
finishing_times[finishing_times_counter++] = source_node;
}