Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 深度优先搜索中的堆栈溢出_C++_Stack Overflow_Depth First Search_Connected Components - Fatal编程技术网

C++ 深度优先搜索中的堆栈溢出

C++ 深度优先搜索中的堆栈溢出,c++,stack-overflow,depth-first-search,connected-components,C++,Stack Overflow,Depth First Search,Connected Components,我正在写一个DFS连接的组件标签,基本思想非常简单,只需将DFS递归地应用于四个邻居(左、右、上、下) 问题是当连接区域太大时,比如说100*100像素,它会得到一个运行时错误 0xC00000FD: Stack overflow (: 0x00000001, 0x001D2EB4) 我认为这是因为它太深了。有什么优化或解决方案吗 代码如下: void DFS_Traversal(cv::Mat &InputMat, cv::Mat &LabelMat, cv::Point2

我正在写一个DFS连接的组件标签,基本思想非常简单,只需将DFS递归地应用于四个邻居(左、右、上、下)

问题是当连接区域太大时,比如说100*100像素,它会得到一个运行时错误

0xC00000FD: Stack overflow (:  0x00000001, 0x001D2EB4)
我认为这是因为它太深了。有什么优化或解决方案吗

代码如下:

void DFS_Traversal(cv::Mat &InputMat, cv::Mat &LabelMat, cv::Point2i cur_SP, int Thresh, int cur_Label){

    if (cur_SP.y > 2 && cur_SP.y < (InputMat.rows - 2) && cur_SP.x > 2 && cur_SP.x < (InputMat.cols - 2)){
        uchar* pre_Input_rowPtr = InputMat.ptr<uchar>(cur_SP.y - 1);
        uchar* cur_Input_rowPtr = InputMat.ptr<uchar>(cur_SP.y);
        uchar* next_Input_rowPtr = InputMat.ptr<uchar>(cur_SP.y + 1);
        uchar* pre_Label_rowPtr = LabelMat.ptr<uchar>(cur_SP.y - 1);
        uchar* cur_Label_rowPtr = LabelMat.ptr<uchar>(cur_SP.y);
        uchar* next_Label_rowPtr = LabelMat.ptr<uchar>(cur_SP.y + 1);

        //cur_Label_rowPtr[cur_SP.x] = cur_Label;

        //Left Point
        if ( cur_Label_rowPtr[cur_SP.x - 1] == 0 && std::abs(cur_Input_rowPtr[cur_SP.x] - cur_Input_rowPtr[cur_SP.x - 1]) < Thresh){
            cv::Point2i left_Point(cur_SP.x - 1, cur_SP.y);

            cur_Label_rowPtr[cur_SP.x - 1] = cur_Label;
            DFS_Traversal(InputMat, LabelMat, left_Point, Thresh, cur_Label);
        }
        //Right Point
        if ( cur_Label_rowPtr[cur_SP.x + 1] == 0 && std::abs(cur_Input_rowPtr[cur_SP.x] - cur_Input_rowPtr[cur_SP.x + 1]) < Thresh){
            cv::Point2i right_Point(cur_SP.x + 1, cur_SP.y);

            cur_Label_rowPtr[cur_SP.x + 1] = cur_Label;
            DFS_Traversal(InputMat, LabelMat, right_Point, Thresh, cur_Label);
        }
        //Up Point
        if ( pre_Label_rowPtr[cur_SP.x] == 0 && std::abs(cur_Input_rowPtr[cur_SP.x] - pre_Input_rowPtr[cur_SP.x]) < Thresh){
            cv::Point2i up_Point(cur_SP.x, cur_SP.y - 1);

            pre_Label_rowPtr[cur_SP.x] = cur_Label;
            DFS_Traversal(InputMat, LabelMat, up_Point, Thresh, cur_Label);
        }
        //Down Point
        if ( next_Label_rowPtr[cur_SP.x] == 0 && std::abs(cur_Input_rowPtr[cur_SP.x] - next_Input_rowPtr[cur_SP.x]) < Thresh){
            cv::Point2i down_Point(cur_SP.x, cur_SP.y + 1);

            next_Label_rowPtr[cur_SP.x] = cur_Label;
            DFS_Traversal(InputMat, LabelMat, down_Point, Thresh, cur_Label);
        }

    }
    return;

}
void DFS_遍历(cv::Mat&InputMat,cv::Mat&LabelMat,cv::Point2i cur_SP,int Thresh,int cur_Label){
如果(当前SP.y>2和当前SP.y<(InputMat.rows-2)和当前SP.x>2和当前SP.x<(InputMat.cols-2)){
uchar*pre_Input_rowPtr=InputMat.ptr(当前SP.y-1);
uchar*cur_Input_rowPtr=InputMat.ptr(cur_SP.y);
uchar*next_Input_rowPtr=InputMat.ptr(当前SP.y+1);
uchar*pre_Label_rowPtr=LabelMat.ptr(cur_SP.y-1);
uchar*cur_Label_rowPtr=LabelMat.ptr(cur_SP.y);
uchar*next_Label_rowPtr=LabelMat.ptr(当前SP.y+1);
//cur_Label_rowPtr[cur_SP.x]=cur_Label;
//左撇子
if(cur_Label_rowPtr[cur_SP.x-1]==0&&std::abs(cur_Input_rowPtr[cur_SP.x]-cur_Input_rowPtr[cur_SP.x-1])

在装有8G RAM的笔记本电脑上运行时,无溢出的最大面积为72*72,接近5000个递归级别。如何使用DFS做得更好?

将递归替换为循环并使用显式堆栈(任何列表都可以)

堆栈将模拟调用堆栈,但不会有如此紧密的边界

请参见下面的迭代实现:

iterativeInorder(node)
  s ← empty stack
  while (not s.isEmpty() or node ≠ null)
    if (node ≠ null)
      s.push(node)
      node ← node.left
    else
      node ← s.pop()
      visit(node)
      node ← node.right