boost/libs/graph/test/strong_components_test.cpp

126 lines
3.3 KiB
C++
Raw Permalink Normal View History

2018-01-12 21:47:58 +01:00
//=======================================================================
// Copyright 2014 Alexander Lauser.
// Authors: Alexander Lauser
//
// 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)
//=======================================================================
// This test is an adapted version of the MWE for Bug #10231 (showing
// incorrect root_map computation).
/* Output should be:
The example graph:
2021-10-05 21:37:46 +02:00
a --> b
b --> a c
c --> b
2018-01-12 21:47:58 +01:00
Vertex a is in component 0 and has root 0
Vertex b is in component 0 and has root 0
Vertex c is in component 0 and has root 0
*/
#include <boost/config.hpp>
#include <iostream>
#include <vector>
#include <boost/graph/strong_components.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>
int main(int, char*[])
{
2021-10-05 21:37:46 +02:00
using namespace boost;
2018-01-12 21:47:58 +01:00
2021-10-05 21:37:46 +02:00
adjacency_list< vecS, vecS, directedS > G;
2018-01-12 21:47:58 +01:00
2021-10-05 21:37:46 +02:00
typedef graph_traits<
adjacency_list< vecS, vecS, directedS > >::vertex_descriptor Vertex;
Vertex a = add_vertex(G);
Vertex b = add_vertex(G);
Vertex c = add_vertex(G);
2018-01-12 21:47:58 +01:00
2021-10-05 21:37:46 +02:00
add_edge(a, b, G);
add_edge(b, a, G);
add_edge(c, b, G);
add_edge(b, c, G);
2018-01-12 21:47:58 +01:00
#if VERBOSE
2021-10-05 21:37:46 +02:00
std::cout << "The example graph:" << std::endl;
const char* name = "abc";
print_graph(G, name);
std::cout << std::endl;
2018-01-12 21:47:58 +01:00
#endif
2021-10-05 21:37:46 +02:00
std::vector< int > component(num_vertices(G)),
discover_time(num_vertices(G));
std::vector< default_color_type > color(num_vertices(G));
std::vector< Vertex > root(num_vertices(G));
strong_components(G,
make_iterator_property_map(component.begin(), get(vertex_index, G)),
root_map(make_iterator_property_map(root.begin(), get(vertex_index, G)))
.color_map(
make_iterator_property_map(color.begin(), get(vertex_index, G)))
.discover_time_map(make_iterator_property_map(
discover_time.begin(), get(vertex_index, G))));
2018-01-12 21:47:58 +01:00
#if VERBOSE
2021-10-05 21:37:46 +02:00
for (std::vector< int >::size_type i = 0; i != component.size(); ++i)
std::cout << "Vertex " << name[i] << " is in component " << component[i]
<< " and has root " << root[i] << std::endl;
2018-01-12 21:47:58 +01:00
#endif
2021-10-05 21:37:46 +02:00
2018-01-12 21:47:58 +01:00
#if VERBOSE
2021-10-05 21:37:46 +02:00
bool test_failed;
2018-01-12 21:47:58 +01:00
#endif
2021-10-05 21:37:46 +02:00
int ret = 0;
//////////
2018-01-12 21:47:58 +01:00
#if VERBOSE
2021-10-05 21:37:46 +02:00
test_failed = false;
std::cerr << "Testing component-computation of strong_components ..."
<< std::endl;
2018-01-12 21:47:58 +01:00
#endif
2021-10-05 21:37:46 +02:00
for (std::vector< int >::size_type i = 0; i != component.size(); ++i)
{
if (component[i] != 0)
{
2018-01-12 21:47:58 +01:00
#if VERBOSE
2021-10-05 21:37:46 +02:00
test_failed = true;
2018-01-12 21:47:58 +01:00
#endif
2021-10-05 21:37:46 +02:00
ret = -1;
break;
}
2018-01-12 21:47:58 +01:00
}
#if VERBOSE
2021-10-05 21:37:46 +02:00
std::cerr << (test_failed ? " **** Failed." : " Passed.") << std::endl;
2018-01-12 21:47:58 +01:00
#endif
2021-10-05 21:37:46 +02:00
//////////
//////////
2018-01-12 21:47:58 +01:00
#if VERBOSE
2021-10-05 21:37:46 +02:00
test_failed = false;
std::cerr << "Testing root_map-computation of strong_components ..."
<< std::endl;
2018-01-12 21:47:58 +01:00
#endif
2021-10-05 21:37:46 +02:00
for (std::vector< int >::size_type i = 0; i != component.size(); ++i)
{
if (root[i] != 0)
{
2018-01-12 21:47:58 +01:00
#if VERBOSE
2021-10-05 21:37:46 +02:00
test_failed = true;
2018-01-12 21:47:58 +01:00
#endif
2021-10-05 21:37:46 +02:00
ret = -1;
break;
}
2018-01-12 21:47:58 +01:00
}
#if VERBOSE
2021-10-05 21:37:46 +02:00
std::cerr << (test_failed ? " **** Failed." : " Passed.") << std::endl;
2018-01-12 21:47:58 +01:00
#endif
2021-10-05 21:37:46 +02:00
//////////
if (ret == 0)
std::cout << "tests passed" << std::endl;
return ret;
2018-01-12 21:47:58 +01:00
}