| 1 | // Output:
|
|---|
| 2 |
|
|---|
| 3 | // The example graph:
|
|---|
| 4 | // a --> b
|
|---|
| 5 | // b --> a c
|
|---|
| 6 | // c --> b
|
|---|
| 7 | //
|
|---|
| 8 | // Vertex a is in component 0 and has root 0
|
|---|
| 9 | // Vertex b is in component 0 and has root 0
|
|---|
| 10 | // Vertex c is in component 0 and has root 1
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 | #include <boost/config.hpp>
|
|---|
| 14 | #include <iostream>
|
|---|
| 15 | #include <vector>
|
|---|
| 16 | #include <boost/graph/strong_components.hpp>
|
|---|
| 17 | #include <boost/graph/adjacency_list.hpp>
|
|---|
| 18 | #include <boost/graph/graph_utility.hpp>
|
|---|
| 19 |
|
|---|
| 20 | int main(int, char*[])
|
|---|
| 21 | {
|
|---|
| 22 | using namespace boost;
|
|---|
| 23 | const char* name = "abc";
|
|---|
| 24 |
|
|---|
| 25 | adjacency_list<vecS, vecS, directedS> G;
|
|---|
| 26 |
|
|---|
| 27 | typedef graph_traits<adjacency_list<vecS, vecS, directedS> >::vertex_descriptor Vertex;
|
|---|
| 28 | Vertex a = add_vertex(G);
|
|---|
| 29 | Vertex b = add_vertex(G);
|
|---|
| 30 | Vertex c = add_vertex(G);
|
|---|
| 31 |
|
|---|
| 32 | add_edge(a, b, G);
|
|---|
| 33 | add_edge(b, a, G);
|
|---|
| 34 |
|
|---|
| 35 | add_edge(c, b, G);
|
|---|
| 36 | add_edge(b, c, G);
|
|---|
| 37 |
|
|---|
| 38 | std::cout << "The example graph:" << std::endl;
|
|---|
| 39 | print_graph(G, name);
|
|---|
| 40 | std::cout << std::endl;
|
|---|
| 41 |
|
|---|
| 42 | std::vector<int> component(num_vertices(G)), discover_time(num_vertices(G));
|
|---|
| 43 | std::vector<default_color_type> color(num_vertices(G));
|
|---|
| 44 | std::vector<Vertex> root(num_vertices(G));
|
|---|
| 45 | strong_components(G, make_iterator_property_map(component.begin(), get(vertex_index, G)),
|
|---|
| 46 | root_map(make_iterator_property_map(root.begin(), get(vertex_index, G))).
|
|---|
| 47 | color_map(make_iterator_property_map(color.begin(), get(vertex_index, G))).
|
|---|
| 48 | discover_time_map(make_iterator_property_map(discover_time.begin(), get(vertex_index, G))));
|
|---|
| 49 |
|
|---|
| 50 | std::vector<int>::size_type i;
|
|---|
| 51 | for (i = 0; i != component.size(); ++i)
|
|---|
| 52 | std::cout << "Vertex " << name[i]
|
|---|
| 53 | << " is in component " << component[i]
|
|---|
| 54 | << " and has root " << root[i] << std::endl;
|
|---|
| 55 |
|
|---|
| 56 | return 0;
|
|---|
| 57 | }
|
|---|