13#include <gtest/gtest.h>
43 bool has_negative_cycle =
bf.has_negative_cycle(n0);
62 bool has_negative_cycle =
bf.has_negative_cycle(n0);
82 bool negative_cycle =
bf.paint_spanning_tree(n0);
120 bool negative_cycle =
bf.faster_paint_spanning_tree(n0);
161 auto result =
bf.search_negative_cycle(n0, 0.5, 2);
164 size_t iterations =
get<1>(result);
177 bool has_negative_cycle =
bf.has_negative_cycle(n0);
195 bool has_negative_cycle =
bf.has_negative_cycle(n0);
217 bool has_negative_cycle =
bf.has_negative_cycle(n0);
237 bool has_negative_cycle =
bf.has_negative_cycle(n0);
247 std::vector<Node*>
nodes;
254 for (
int i = 0; i <
num_nodes - 1; ++i) {
259 for (
int i = 0; i <
num_nodes - 5; i += 5) {
264 bool has_negative_cycle =
bf.has_negative_cycle(
nodes[0]);
365 bool has_negative_cycle =
bf.has_negative_cycle(n0);
385 bool has_negative_cycle =
bf.has_negative_cycle(n0);
402 bf.paint_spanning_tree(
nullptr);
403 }
catch (
const std::domain_error&) {
410 bf.faster_paint_spanning_tree(
nullptr);
411 }
catch (
const std::domain_error&) {
418 bf.has_negative_cycle(
nullptr);
419 }
catch (
const std::domain_error&) {
454 bf.paint_spanning_tree(n0);
457 bf.build_tree(tree,
false);
482 bf.paint_spanning_tree(n0);
488 for (
size_t i = 0; i < tree_arcs.
size(); ++i) {
489 if (tree_arcs(i) !=
nullptr) {
511 auto weights =
bf.compute_nodes_weights();
533 bf.compute_nodes_weights();
534 }
catch (
const std::domain_error&) {
595 bf.paint_spanning_tree(n0);
599 int distance =
bf.get_min_path(n3, path);
625 bf.paint_spanning_tree(n0);
651 bf.paint_spanning_tree(n0);
653 auto arcs =
bf.extract_min_spanning_tree();
657 for (
size_t i = 0; i <
arcs.size(); ++i)
658 if (
arcs(i) !=
nullptr)
666 for (
size_t i = 0; i <
arcs.size(); ++i) {
688 bf.paint_spanning_tree(n0);
691 bf.build_tree(tree,
true);
701 for (
typename GT::Node_Iterator it(tree); it.has_curr(); it.next()) {
702 auto node = it.get_curr();
703 int info = node->get_info();
727 auto weights =
bf.compute_nodes_weights();
734 auto &
pair = it.get_curr();
797 auto [
path1,
iter1] =
bf.search_negative_cycle(n0, 0.1, 1);
802 auto [
path2,
iter2] =
bf.search_negative_cycle(n0, 0.9, 1);
821 const size_t N = 100;
824 std::vector<Node*>
nodes;
825 for (
size_t i = 0; i <
N; ++i)
829 for (
size_t i = 0; i <
N - 1; ++i)
833 bool negative_cycle =
bf.paint_spanning_tree(
nodes[0]);
839 int distance =
bf.get_min_path(
nodes[
N - 1], path);
861 bf.paint_spanning_tree(n0);
880 bf.get_min_path(n1, path);
881 }
catch (
const std::domain_error&) {
959 auto [path,
iter] =
bf.search_negative_cycle(n0, 0.5, 1);
997 bf.build_tree(tree,
true);
998 }
catch (
const std::domain_error&) {
1019 bf.paint_spanning_tree(n0);
1031 g2.insert_arc(
m0,
m1, 5);
1034 bf2.paint_spanning_tree(
m0);
1091 bool has_neg =
bf.has_negative_cycle(n0);
1100 const size_t N = 500;
1102 std::vector<Node*>
nodes;
1103 for (
size_t i = 0; i <
N; ++i)
1106 for (
size_t i = 0; i <
N - 1; ++i)
1115 int dist =
bf.get_min_path(
nodes[
N - 1], path);
1136 int dist =
bf.get_min_path(n1, path);
1152 bf.paint_spanning_tree(n0);
1158 bf.get_min_path(n2, path);
1159 }
catch (
const std::domain_error&) {
1170 const size_t N = 20;
1172 std::vector<Node*>
nodes;
1173 for (
size_t i = 0; i <
N; ++i)
1177 for (
size_t i = 0; i <
N; ++i)
1178 for (
size_t j = 0; j <
N; ++j)
1202 bf.paint_spanning_tree(n0);
1229 bf.get_distance(n1);
1230 }
catch (
const std::domain_error&) {
1247 bf.paint_spanning_tree(n0);
1251 bf.get_distance(n2);
1252 }
catch (
const std::domain_error&) {
1270 bf.extract_min_spanning_tree();
1271 }
catch (
const std::domain_error&) {
1286 bf.paint_spanning_tree(n0);
1290 bf.get_distance(
nullptr);
1291 }
catch (
const std::domain_error&) {
1312 bf.paint_spanning_tree(n0);
1341 GTEST_SKIP() <<
"Graph type does not support parallel arcs (multigraph)";
1345 bool has_neg =
bf.paint_spanning_tree(n0);
1349 int d =
bf.get_min_path(n2, path);
1369 GTEST_SKIP() <<
"Graph type does not support parallel arcs (multigraph)";
1373 bool has_neg =
bf.paint_spanning_tree(n0);
1377 int d =
bf.get_min_path(n2, path);
1404 GTEST_SKIP() <<
"Graph type does not support parallel arcs (multigraph)";
1408 bool has_neg =
bf.paint_spanning_tree(n0);
1412 int d =
bf.get_min_path(n3, path);
1430 GTEST_SKIP() <<
"Graph type does not support parallel arcs (multigraph)";
1434 bool has_neg =
bf.paint_spanning_tree(n0);
1443 ::testing::InitGoogleTest(&
argc,
argv);
Bellman-Ford algorithm for single-source shortest paths.
WeightedDigraph::Node Node
Bellman-Ford algorithm for shortest paths with negative weights.
Default distance accessor for arc weights.
Generic directed graph (digraph) wrapper template.
typename BaseGraph::Arc Arc
typename BaseGraph::Node Node
size_t size() const noexcept
Return the current dimension of array.
void next()
Advances the iterator to the next filtered element.
constexpr bool is_empty() const noexcept
Return true if list is empty.
size_t size() const noexcept
Count the number of elements of the list.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Iterator on nodes and arcs of a path.
bool has_current_node() const noexcept
Return true if the iterator has a current node.
bool is_empty() const noexcept
Return true if the path is empty.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
constexpr size_t get_num_arcs() const noexcept
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
bool has_cycle(const GT &g)
Return true if an undirected graph has at least one cycle.
#define IS_ARC_VISITED(p, bit)
Determine whether the bit field is or not set to one.
Main namespace for Aleph-w library functions.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Filtered iterator on all the arcs of a graph.
Detects if a negative cycle exists and eventually computes it.
Default filter for filtered iterators on arcs.
Arc of graph implemented with double-linked adjacency lists.
Generic graph and digraph implementations.