mirror of
https://github.com/boostorg/graph.git
synced 2026-01-19 04:12:11 +00:00
work on connected components
[SVN r9328]
This commit is contained in:
@@ -30,6 +30,7 @@
|
||||
|
||||
#include <boost/graph/adjacency_list.hpp>
|
||||
#include <boost/property_map.hpp>
|
||||
#include <boost/graph/iteration_macros.hpp>
|
||||
|
||||
/*
|
||||
Sample Output
|
||||
|
||||
@@ -34,15 +34,12 @@
|
||||
/*
|
||||
|
||||
This example demonstrates the usage of the connected_components
|
||||
algorithm on a directed and undirected graph. The example graphs
|
||||
come from "Introduction to Algorithms", Cormen, Leiserson, and
|
||||
Rivest p. 87 (though we number the vertices from zero instead of
|
||||
one).
|
||||
algorithm on a undirected graph. The example graphs come from
|
||||
"Introduction to Algorithms", Cormen, Leiserson, and Rivest p. 87
|
||||
(though we number the vertices from zero instead of one).
|
||||
|
||||
Sample output:
|
||||
|
||||
An undirected graph:
|
||||
|
||||
Total number of components: 3
|
||||
Vertex 0 is in component 0
|
||||
Vertex 1 is in component 0
|
||||
@@ -51,16 +48,6 @@
|
||||
Vertex 4 is in component 0
|
||||
Vertex 5 is in component 1
|
||||
|
||||
A directed graph:
|
||||
|
||||
Total number of components: 3
|
||||
Vertex 0 is in component 2
|
||||
Vertex 1 is in component 2
|
||||
Vertex 2 is in component 1
|
||||
Vertex 3 is in component 2
|
||||
Vertex 4 is in component 2
|
||||
Vertex 5 is in component 0
|
||||
|
||||
*/
|
||||
|
||||
using namespace std;
|
||||
@@ -82,50 +69,15 @@ int main(int , char* [])
|
||||
add_edge(4, 0, G);
|
||||
add_edge(2, 5, G);
|
||||
|
||||
std::vector<int> component(num_vertices(G));
|
||||
int num = connected_components(G, &component[0], get(vertex_color, G));
|
||||
|
||||
std::vector<int> c(num_vertices(G));
|
||||
int num = connected_components(G, &c[0], get(vertex_color, G),
|
||||
dfs_visitor<>());
|
||||
|
||||
cout << "An undirected graph:" << endl;
|
||||
cout << endl;
|
||||
std::vector<int>::iterator i;
|
||||
std::vector<int>::size_type i;
|
||||
cout << "Total number of components: " << num << endl;
|
||||
for (i = c.begin(); i != c.end(); ++i)
|
||||
cout << "Vertex " << i - c.begin() <<" is in component " << *i << endl;
|
||||
for (i = 0; i != component.size(); ++i)
|
||||
cout << "Vertex " << i <<" is in component " << component[i] << endl;
|
||||
cout << endl;
|
||||
}
|
||||
// Second example: the strongly connected components of a directed
|
||||
// graph
|
||||
{
|
||||
typedef property<vertex_discover_time_t, int,
|
||||
property< vertex_finish_time_t, int,
|
||||
property< vertex_color_t, default_color_type > > > VertexProperty;
|
||||
typedef adjacency_list< vecS, vecS, directedS, VertexProperty > Graph;
|
||||
Graph G;
|
||||
add_edge(0, 1, G);
|
||||
add_edge(1, 1, G);
|
||||
add_edge(1, 3, G);
|
||||
add_edge(1, 4, G);
|
||||
add_edge(4, 3, G);
|
||||
add_edge(3, 4, G);
|
||||
add_edge(3, 0, G);
|
||||
add_edge(5, 2, G);
|
||||
|
||||
typedef graph_traits<Graph>::vertex_descriptor Vertex;
|
||||
|
||||
std::vector<int> c(num_vertices(G));
|
||||
int num = connected_components(G, &c[0], get(vertex_color, G),
|
||||
dfs_visitor<>());
|
||||
|
||||
cout << "A directed graph:" << endl;
|
||||
cout << endl;
|
||||
cout << "Total number of components: " << num << endl;
|
||||
std::vector<int>::iterator i;
|
||||
for (i = c.begin(); i != c.end(); ++i)
|
||||
cout << "Vertex " << i - c.begin() <<" is in component " << *i << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
An undirected graph:
|
||||
|
||||
Total number of components: 3
|
||||
Vertex 0 is in component 0
|
||||
Vertex 1 is in component 0
|
||||
@@ -8,12 +6,3 @@ Vertex 3 is in component 2
|
||||
Vertex 4 is in component 0
|
||||
Vertex 5 is in component 1
|
||||
|
||||
A directed graph:
|
||||
|
||||
Total number of components: 3
|
||||
Vertex 0 is in component 2
|
||||
Vertex 1 is in component 2
|
||||
Vertex 2 is in component 1
|
||||
Vertex 3 is in component 2
|
||||
Vertex 4 is in component 2
|
||||
Vertex 5 is in component 0
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
#include <utility>
|
||||
#include <boost/graph/adjacency_list.hpp>
|
||||
#include <boost/pending/disjoint_sets.hpp>
|
||||
#include <boost/graph/connected_components.hpp>
|
||||
#include <boost/graph/dynamic_components.hpp>
|
||||
|
||||
/*
|
||||
|
||||
@@ -81,7 +81,7 @@ int main(int , char* [])
|
||||
disjoint_sets<Rank, Parent> ds(&rank[0], &parent[0]);
|
||||
|
||||
initialize_dynamic_components(G, ds);
|
||||
dynamic_connected_components(G, ds);
|
||||
dynamic_components(G, ds);
|
||||
|
||||
graph_traits<Graph>::edge_descriptor e;
|
||||
bool flag;
|
||||
|
||||
@@ -23,8 +23,11 @@
|
||||
// OR OTHER RIGHTS.
|
||||
//=======================================================================
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <boost/graph/tarjan_scc.hpp>
|
||||
|
||||
#include <boost/graph/strong_components.hpp>
|
||||
#include <boost/graph/adjacency_list.hpp>
|
||||
#include <boost/graph/graphviz.hpp>
|
||||
#include <boost/graph/graph_utility.hpp>
|
||||
@@ -35,23 +38,24 @@ int main(int, char*[])
|
||||
const char* name = "abcdefghij";
|
||||
|
||||
GraphvizGraph G;
|
||||
read_graphviz("scc2.dot", G);
|
||||
read_graphviz("scc.dot", G);
|
||||
|
||||
typedef graph_traits<GraphvizGraph>::vertex_descriptor Vertex;
|
||||
|
||||
std::vector<int> comp(num_vertices(G)), dfs_numbers(num_vertices(G));
|
||||
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));
|
||||
int num = tarjan_scc(G, &comp[0], &root[0], &color[0], &dfs_numbers[0]);
|
||||
int num = strong_components(G, &component[0], &root[0], &color[0],
|
||||
&discover_time[0]);
|
||||
|
||||
cout << "A directed graph:" << endl;
|
||||
std::cout << "A directed graph:" << std::endl;
|
||||
print_graph(G, name);
|
||||
cout << endl;
|
||||
cout << "Total number of components: " << num << endl;
|
||||
std::vector<int>::iterator i;
|
||||
for (i = comp.begin(); i != comp.end(); ++i)
|
||||
cout << "Vertex " << name[i - comp.begin()]
|
||||
<<" is in component " << *i << endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "Total number of components: " << num << std::endl;
|
||||
std::vector<int>::size_type i;
|
||||
for (i = 0; i != component.size(); ++i)
|
||||
std::cout << "Vertex " << name[i]
|
||||
<<" is in component " << component[i] << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
23
example/strong_components.expected
Normal file
23
example/strong_components.expected
Normal file
@@ -0,0 +1,23 @@
|
||||
A directed graph:
|
||||
a --> b f h
|
||||
b --> c a
|
||||
c --> d b
|
||||
d --> e
|
||||
e --> d
|
||||
f --> g
|
||||
g --> f d
|
||||
h --> i
|
||||
i --> h j e c
|
||||
j -->
|
||||
|
||||
Total number of components: 4
|
||||
Vertex a is in component 3
|
||||
Vertex b is in component 3
|
||||
Vertex c is in component 3
|
||||
Vertex d is in component 0
|
||||
Vertex e is in component 0
|
||||
Vertex f is in component 1
|
||||
Vertex g is in component 1
|
||||
Vertex h is in component 3
|
||||
Vertex i is in component 3
|
||||
Vertex j is in component 2
|
||||
Reference in New Issue
Block a user