Search code examples
c++recursionstack-overflowgraph-theorydepth-first-search

Avoiding stack overflow when implementing depth first search in c++


connected components of triangle faces in a mesh

I have implemented depth first search using the above link and works fine for most of my data samples. When my data sample is very large, the code reaches a break point, likely to be stack overflow as the recursive function gets too deep. Is there any way to avoid this? or do I have to use some other way like breadth first search / union find algorithm to find connected components in a graph.

#include <bits/stdc++.h>
using namespace std;
 
class Graph {
 
    // A function used by DFS
    void DFSUtil(int v);
 
public:
    int count;
    map<int, bool> visited;
    map<int, list<int> > adj;
    // function to add an edge to graph
    void addEdge(int v, int w);
 
    // prints DFS traversal of the complete graph
    void DFS();
};
 
void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w); // Add w to v’s list.
}
 
void Graph::DFSUtil(int v)
{
    // Mark the current node as visited and print it
    visited[v] = true;
    cout << v << " ";
 
    // Recur for all the vertices adjacent to this vertex
    list<int>::iterator i;
    for (i = adj[v].begin(); i != adj[v].end(); ++i)
        if (!visited[*i])
            DFSUtil(*i);
}
 
// The function to do DFS traversal. It uses recursive
// DFSUtil()
void Graph::DFS()
{   
    count = 0;
    // Call the recursive helper function to print DFS
    // traversal starting from all vertices one by one
    for (auto i : adj)
        if (visited[i.first] == false)
         {
            DFSUtil(i.first);
            count++;
         }
}
 
int main()
{
    // Create a graph given in the above diagram
    Graph g;
    for face in faces :
    {
       g.addEdge(face[0], face[1]);
       g.addEdge(face[0], face[2]);
       g.addEdge(face[1], face[0]);
       g.addEdge(face[1], face[2]);
       g.addEdge(face[2], face[0]);
       g.addEdge(face[2], face[1]);
    }
    
 
    cout << "Following is Depth First Traversal \n";
       
      // Function call
    g.DFS();
    cout << "number of connected components = " << g.count << "\n";
    return 0;
}

Solution

  • With a recursive algorithm, there is no way to avoid a stack overflow. But, you need to re-implement your algo with an iterative version using a stack.

    Here is a rough implementation .

    void Graph::DFSUtil(int v)
    {
        stack<int> stack;
        stack.push(v);
        cout << v << " ";
        while (!stack.empty())
        {
            int v = stack.top();
            stack.pop();
            if (!visited[v])
            {
                cout << v << " ";
                visited[v] = true;
            }
            for (int i = adj[v].begin(); i != adj[v].end(); ++i)
                if (!visited[*i])
                    stack.push(*i);
        }
    }