From 885f237217d66490bc09e4b7e8e1f91713221be6 Mon Sep 17 00:00:00 2001 From: Kirill Zhdanovich Date: Tue, 29 Jul 2014 22:12:59 +0200 Subject: [PATCH] Apply diff https://github.com/Project-OSRM/osrm-backend/pull/1133 to develop branch --- Algorithms/StronglyConnectedComponents.h | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/Algorithms/StronglyConnectedComponents.h b/Algorithms/StronglyConnectedComponents.h index d0dd554d6..d8508fe49 100644 --- a/Algorithms/StronglyConnectedComponents.h +++ b/Algorithms/StronglyConnectedComponents.h @@ -228,7 +228,7 @@ class TarjanSCC TIMER_START(SCC_RUN); // The following is a hack to distinguish between stuff that happens // before the recursive call and stuff that happens after - std::stack> recursion_stack; + std::stack recursion_stack; // true = stuff before, false = stuff after call std::stack tarjan_stack; std::vector components_index(m_node_based_graph->GetNumberOfNodes(), @@ -237,25 +237,32 @@ class TarjanSCC std::vector tarjan_node_list(m_node_based_graph->GetNumberOfNodes()); unsigned component_index = 0, size_of_current_component = 0; int index = 0; - NodeID last_node = m_node_based_graph->GetNumberOfNodes(); - for (const NodeID node : boost::irange(0u, last_node)) + const NodeID last_node = m_node_based_graph->GetNumberOfNodes(); + std::vector processing_node_before_recursion(m_node_based_graph->GetNumberOfNodes(), true); + for(const NodeID node : boost::irange(0u, last_node)) { if (SPECIAL_NODEID == components_index[node]) { - recursion_stack.emplace(true, TarjanStackFrame(node, node)); + recursion_stack.emplace(TarjanStackFrame(node, node)); } while (!recursion_stack.empty()) { - const bool before_recursion = recursion_stack.top().first; - TarjanStackFrame currentFrame = recursion_stack.top().second; + TarjanStackFrame currentFrame = recursion_stack.top(); const NodeID v = currentFrame.v; recursion_stack.pop(); + const bool before_recursion = processing_node_before_recursion[v]; + + if (before_recursion && tarjan_node_list[v].index != UINT_MAX) + { + continue; + } if (before_recursion) { // Mark frame to handle tail of recursion - recursion_stack.emplace(false, currentFrame); + recursion_stack.emplace(currentFrame); + processing_node_before_recursion[v] = false; // Mark essential information for SCC tarjan_node_list[v].index = index; @@ -271,7 +278,7 @@ class TarjanSCC m_node_based_graph->GetTarget(current_edge); if (SPECIAL_NODEID == tarjan_node_list[vprime].index) { - recursion_stack.emplace(true, TarjanStackFrame(vprime, v)); + recursion_stack.emplace(TarjanStackFrame(vprime, v)); } else { @@ -285,6 +292,7 @@ class TarjanSCC } else { + processing_node_before_recursion[v] = true; tarjan_node_list[currentFrame.parent].low_link = std::min(tarjan_node_list[currentFrame.parent].low_link, tarjan_node_list[v].low_link);