Skip to content

Commit

Permalink
Merge pull request #390 from danielyxyang/fix/undirected_dfs
Browse files Browse the repository at this point in the history
fixed incorrect callbacks to finish_edge in undirected_dfs
  • Loading branch information
jeremy-murphy authored Oct 30, 2024
2 parents d108c96 + b671e3d commit 4d6a196
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/boost/graph/undirected_dfs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ namespace detail
if (v_color == Color::white())
{
vis.tree_edge(*ei, g);
src_e = *ei;
stack.push_back(std::make_pair(u,
std::make_pair(src_e, std::make_pair(++ei, ei_end))));
std::make_pair(src_e, std::make_pair(std::next(ei), ei_end))));
u = v;
src_e = *ei;
put(vertex_color, u, Color::gray());
vis.discover_vertex(u, g);
boost::tie(ei, ei_end) = out_edges(u, g);
Expand Down
104 changes: 104 additions & 0 deletions test/undirected_dfs_visitor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//=======================================================================
// Copyright 2024
// Author: Daniel Yang
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================

#include <vector>
#include <string>

#include <boost/config.hpp>
#include <boost/core/lightweight_test.hpp>

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/undirected_dfs.hpp>

using Graph = boost::adjacency_list<
boost::vecS,
boost::vecS,
boost::undirectedS,
boost::no_property,
boost::property<boost::edge_color_t, boost::default_color_type>>;

using Vertex = boost::graph_traits<Graph>::vertex_descriptor;
using Edge = boost::graph_traits<Graph>::edge_descriptor;

struct DFSVisitorLogger : boost::default_dfs_visitor {
std::vector<std::string> &log;

DFSVisitorLogger(std::vector<std::string> &log) : log(log) {}

void log_vertex(const Vertex v, const std::string &event) {
log.push_back("vertex " + std::to_string(v) + " " + event);
}
void log_edge(const Edge e, const std::string &event, const Graph &g) {
log.push_back("edge (" + std::to_string(boost::source(e, g)) + "," + std::to_string(boost::target(e, g)) + ") " + event);
}

void discover_vertex(Vertex v, const Graph &g) {
log_vertex(v, "discovered");
}
void finish_vertex(Vertex v, const Graph &g) {
log_vertex(v, "finished");
}
void examine_edge(Edge e, const Graph &g) {
log_edge(e, "examined", g);
}
void tree_edge(Edge e, const Graph &g) {
log_edge(e, "tree", g);
}
void back_edge(Edge e, const Graph &g) {
log_edge(e, "back", g);
}
void forward_or_cross_edge(Edge e, const Graph &g) {
log_edge(e, "forward_cross", g);
}
void finish_edge(Edge e, const Graph &g) {
log_edge(e, "finished", g);
}
};

int main() {
Graph g(3);
boost::add_edge(0, 1, g);
boost::add_edge(1, 2, g);

std::vector<std::string> expected_answer = {
"vertex 0 discovered",
"edge (0,1) examined",
"edge (0,1) tree",
"vertex 1 discovered",
"edge (1,0) examined",
"edge (1,0) finished",
"edge (1,2) examined",
"edge (1,2) tree",
"vertex 2 discovered",
"edge (2,1) examined",
"edge (2,1) finished",
"vertex 2 finished",
"edge (1,2) finished",
"vertex 1 finished",
"edge (0,1) finished",
"vertex 0 finished",
};
std::vector<std::string> actual_answer;

// run undirected_dfs
DFSVisitorLogger dfs_visitor_logger(actual_answer);
boost::undirected_dfs(g,
boost::visitor(dfs_visitor_logger)
.edge_color_map(boost::get(boost::edge_color, g)));

// check if all vertices and edges have been visited in the correct order
BOOST_TEST(expected_answer.size() == actual_answer.size());
if (expected_answer.size() == actual_answer.size()) {
for (int i = 0; i < expected_answer.size(); ++i) {
BOOST_TEST(expected_answer[i] == actual_answer[i]);
}
}

return boost::report_errors();
}

0 comments on commit 4d6a196

Please sign in to comment.