Skip to content

Commit da967e0

Browse files
authored
Merge pull request #3207 from aimbot6120:swtfix
replaced recursion DFS to stack DFS
1 parent fe43396 commit da967e0

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

modules/text/src/text_detector_swt.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <unordered_map>
99
#include <limits>
10+
#include <stack>
1011

1112
using namespace std;
1213

@@ -91,15 +92,25 @@ void addEdge(std::vector< std::vector<int> >& adj, int u, int v)
9192
static
9293
void DFSUtil(int v, std::vector<bool> & visited, std::vector< std::vector<int> >& adj, int label, std::vector<int> &component_id)
9394
{
94-
// Mark the current node as visited and label it as belonging to the current component
95-
visited[v] = true;
96-
component_id[v] = label;
97-
// Recur for all the vertices
98-
// adjacent to this vertex
99-
for (size_t i = 0; i < adj[v].size(); i++) {
100-
int neighbour = adj[v][i];
101-
if (!visited[neighbour]) {
102-
DFSUtil(neighbour, visited, adj, label, component_id);
95+
stack<int> s;
96+
s.push(v);
97+
while(!s.empty()){
98+
v = s.top();
99+
s.pop();
100+
if(!visited[v])
101+
{
102+
// Mark the current node as visited and label it as belonging to the current component
103+
visited[v] = true;
104+
component_id[v] = label;
105+
// Recur for all the vertices
106+
// adjacent to this vertex
107+
for (size_t i = 0; i < adj[v].size(); i++) {
108+
int neighbour = adj[v][i];
109+
if(!visited[neighbour])
110+
{
111+
s.push(neighbour);
112+
}
113+
}
103114
}
104115
}
105116
}

0 commit comments

Comments
 (0)