From da967e070ef98a93b25ba349d08c16b765c26069 Mon Sep 17 00:00:00 2001 From: Pranay Pandit <55682468+aimbot6120@users.noreply.github.com> Date: Sat, 2 Apr 2022 03:44:58 +0530 Subject: [PATCH] Merge pull request #3207 from aimbot6120:swtfix replaced recursion DFS to stack DFS --- modules/text/src/text_detector_swt.cpp | 29 ++++++++++++++++++-------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/modules/text/src/text_detector_swt.cpp b/modules/text/src/text_detector_swt.cpp index a976f9ff5..75178a5df 100644 --- a/modules/text/src/text_detector_swt.cpp +++ b/modules/text/src/text_detector_swt.cpp @@ -7,6 +7,7 @@ #include #include +#include using namespace std; @@ -91,15 +92,25 @@ void addEdge(std::vector< std::vector >& adj, int u, int v) static void DFSUtil(int v, std::vector & visited, std::vector< std::vector >& adj, int label, std::vector &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 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); + } + } } } }