Skip to content

Commit

Permalink
replaced recursion DFS to stack DFS
Browse files Browse the repository at this point in the history
replaced recursion DFS to stack DFS
  • Loading branch information
aimbot6120 committed Mar 28, 2022
1 parent 508c8db commit 65ab595
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions modules/text/src/text_detector_swt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <unordered_map>
#include <limits>
#include <stack>

using namespace std;

Expand Down Expand Up @@ -91,15 +92,25 @@ void addEdge(std::vector< std::vector<int> >& adj, int u, int v)
static
void DFSUtil(int v, std::vector<bool> & visited, std::vector< std::vector<int> >& adj, int label, std::vector<int> &component_id)
{
// Mark the current node as visited and label it as belonging to the current component
visited[v] = true;
component_id[v] = label;
// Recur for all the vertices
// adjacent to this vertex
for (size_t i = 0; i < adj[v].size(); i++) {
int neighbour = adj[v][i];
if (!visited[neighbour]) {
DFSUtil(neighbour, visited, adj, label, component_id);
stack<int> s;
s.push(v);
while(!s.empty()){
v = s.top();
s.pop();
if(!visited[v])
{
// Mark the current node as visited and label it as belonging to the current component
visited[v] = true;
component_id[v] = label;
// Recur for all the vertices
// adjacent to this vertex
for (size_t i = 0; i < adj[v].size(); i++) {
int neighbour = adj[v][i];
if(!visited[neighbour])
{
s.push(neighbour);
}
}
}
}
}
Expand Down

0 comments on commit 65ab595

Please sign in to comment.