Skip to content
Snippets Groups Projects
Commit 9eaa71f9 authored by David Rosca's avatar David Rosca
Browse files

DFS: Change second variant to also offer canceling of traversal

parent 5cd6cfc0
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
......@@ -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);
......
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment