在 listS 而不是 vecS 上做强连通分量的图的例子并不多。这是 vecS 的等效示例
#include <boost/config.hpp>
#include <vector>
#include <iostream>
#include <boost/graph/strong_components.hpp>
#include <boost/graph/adjacency_list.hpp>
int
main()
{
using namespace boost;
typedef adjacency_list < vecS, vecS, directedS > Graph;
const int N = 6;
Graph G(N);
add_edge(0, 1, G);
add_edge(1, 1, G);
add_edge(1, 3, G);
add_edge(1, 4, G);
add_edge(3, 4, G);
add_edge(3, 0, G);
add_edge(4, 3, G);
add_edge(5, 2, G);
std::vector<int> c(N);
int num = strong_components
(G, make_iterator_property_map(c.begin(), get(vertex_index, G), c[0]));
auto l=get(vertex_index, G);
std::cout << "Total number of components: " << num << std::endl;
std::vector < int >::iterator i;
for (i = c.begin(); i != c.end(); ++i)
std::cout << "Vertex " << i - c.begin()
<< " is in component " << *i << std::endl;
return EXIT_SUCCESS;
}
但是当我从 vecS 更改为 listS 时,它会中断。我知道问题是由于顶点索引和输出向量索引中的某种类型的不匹配,但我无法完全想出解决它的方法。最接近的答案是哪些 VertexList 类型对 depth_first_search 有效,但这适用于 DFS,不能外推到 SCC。