Skip to content

Commit

Permalink
Add Bellman-Ford unit tests
Browse files Browse the repository at this point in the history
Negative weight cycles not tested
  • Loading branch information
pratzl committed Aug 16, 2024
1 parent 4a88f06 commit 5ad7fe6
Show file tree
Hide file tree
Showing 4 changed files with 716 additions and 8 deletions.
14 changes: 7 additions & 7 deletions include/graph/algorithm/bellman_ford_shortest_paths.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,21 @@ bool bellman_ford_shortest_paths(
for (id_type k = 0; k < N; ++k) {
bool at_least_one_edge_relaxed = false;
for (auto&& [uid, vid, uv, w] : views::edgelist(g, weight)) {
visitor.examine_edge({uid, vid, uv});
if (relax(uv, uid, w)) {
visitor.on_examine_edge({uid, vid, uv});
if (relax_target(uv, uid, w)) {
at_least_one_edge_relaxed = true;
visitor.edge_relaxed({uid, vid, uv});
visitor.on_edge_relaxed({uid, vid, uv});
} else
visitor.edge_not_relaxed({uid, vid, uv});
visitor.on_edge_not_relaxed({uid, vid, uv});
}
if (!at_least_one_edge_relaxed)
break;
}

// Check for negative weight cycles
for (auto&& [uid, vid, uv, w] : views::edgelist(g, weight)) {
if (compare(combine(distance[uid], w), distance[vid])) {
visitor.edge_not_minimized({uid, vid, uv});
if (compare(combine(distances[uid], w), distances[vid])) {
visitor.on_edge_not_minimized({uid, vid, uv});

# if ENABLE_EVAL_NEG_WEIGHT_CYCLE // for debugging
// A negative cycle exists; find a vertex on the cycle
Expand All @@ -190,7 +190,7 @@ bool bellman_ford_shortest_paths(
# endif
return false;
} else {
visitor.edge_minimized({uid, vid, uv});
visitor.on_edge_minimized({uid, vid, uv});
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ set(UNITTEST_SOURCES
"edgelist_tests.cpp"
"edgelist_view_tests.cpp"
"examples_tests.cpp"
"bellman_shortest_paths_tests.cpp"
"dijkstra_shortest_paths_tests.cpp"
"transitive_closure_tests.cpp"
"dfs_tests.cpp"
Expand Down
Loading

0 comments on commit 5ad7fe6

Please sign in to comment.