diff --git a/alib2algo/src/graph/traverse/Dfs.cpp b/alib2algo/src/graph/traverse/Dfs.cpp index 10094da1557242c126e9a2af5e1d05c341715efa..1bef737f5c6fddcf1d87397beed76a1a28029127 100644 --- a/alib2algo/src/graph/traverse/Dfs.cpp +++ b/alib2algo/src/graph/traverse/Dfs.cpp @@ -42,18 +42,20 @@ static void dfs_impl(const T &graph, const Node &start, Dfs::Function func) } template <typename T> -static void dfs2_impl(const T &graph, const Node &n, const Node &p, std::unordered_map<Node, bool> &visited, int &time, Dfs::FunctionExt func) +static bool dfs2_impl(const T &graph, const Node &n, const Node &p, std::unordered_map<Node, bool> &visited, int &time, Dfs::FunctionExt func) { int opened = ++time; visited[n] = true; for (const Node &e : graph.neighbors(n)) { if (visited.find(e) == visited.end()) { - dfs2_impl(graph, e, n, visited, time, func); + if (!dfs2_impl(graph, e, n, visited, time, func)) { + return false; + } } } - func(n, p, opened, ++time); + return func(n, p, opened, ++time); } void Dfs::dfs(const Graph &graph, const Node &start, Dfs::Function func) diff --git a/alib2algo/src/graph/traverse/Dfs.h b/alib2algo/src/graph/traverse/Dfs.h index f77ac7ac7313e343af1dc99d3ac4cfdb2ff6c0cd..7f068d4d3bac734c2e19a64f2458e7bfc04912f1 100644 --- a/alib2algo/src/graph/traverse/Dfs.h +++ b/alib2algo/src/graph/traverse/Dfs.h @@ -14,13 +14,13 @@ namespace traverse // func is called for each visited node, traversal stops when returning false // // bool Function(const Node &node); -// void FunctionExt(const Node &node, const Node &predecessor, int openTime, int closeTime); +// bool FunctionExt(const Node &node, const Node &predecessor, int openTime, int closeTime); class Dfs : public graph::VisitableGraphBase::const_visitor_type { public: typedef std::function<bool(const Node&)> Function; - typedef std::function<void(const Node&, const Node&, int, int)> FunctionExt; + typedef std::function<bool(const Node&, const Node&, int, int)> FunctionExt; static void dfs(const Graph &graph, const Node &start, Function func); static void dfs(const Graph &graph, const Node &start, FunctionExt func); diff --git a/alib2algo/test-src/graph/traverse/DfsTest.cpp b/alib2algo/test-src/graph/traverse/DfsTest.cpp index 32541be9b9760fbb6bb22c541867871d00215905..4610da03157d1053bacd1375d0801f1ef50342cf 100644 --- a/alib2algo/test-src/graph/traverse/DfsTest.cpp +++ b/alib2algo/test-src/graph/traverse/DfsTest.cpp @@ -188,6 +188,7 @@ void GraphDfsTest::testDfs2() opened[n] = o; closed[n] = c; predecessors[n] = p; + return true; }); CPPUNIT_ASSERT_EQUAL(5, counter); @@ -216,6 +217,7 @@ void GraphDfsTest::testDfs2() opened[n] = o; closed[n] = c; predecessors[n] = p; + return true; }); CPPUNIT_ASSERT_EQUAL(5, counter);