Incremental Connected ComponentsThis section describes a family of functions and classes that work together to calculate the connected components of an undirected graph. The algorithm used here is based on the disjoint-sets (fast union-find) data structure [8,27] which is a good method to use for situations where the graph is growing (edges are being added) and the connected components information needs to be updated repeatedly. This method does not cover the situation where edges are both added and removed from the graph, hence it is called incremental[42] (and not fully dynamic). The disjoint-sets class is described in Section Disjoint Sets. The following five operations are the primary functions that you will use to calculate and maintain the connected components. The objects used here are a graph g, a disjoint-sets structure ds, and vertices u and v.
ComplexityThe time complexity for the whole process is O(V + E alpha(E,V)) where E is the total number of edges in the graph (by the end of the process) and V is the number of vertices. alpha is the inverse of Ackermann's function which has explosive recursively exponential growth. Therefore its inverse function grows very slowly. For all practical purposes alpha(m,n) <= 4 which means the time complexity is only slightly larger than O(V + E).
ExampleMaintain the connected components of a graph while adding edges using the disjoint-sets data structure. The full source code for this example can be found in examples/incremental_components.cpp.
typedef adjacency_list <vecS, vecS, undirectedS> Graph; typedef graph_traits<Graph>::vertex_descriptor Vertex; typedef graph_traits<Graph>::vertices_size_type size_type; const int N = 6; Graph G(N); std::vector<size_type> rank(num_vertices(G)); std::vector<Vertex> parent(num_vertices(G)); typedef size_type* Rank; typedef Vertex* Parent; disjoint_sets<Rank, Parent> ds(&rank[0], &parent[0]); initialize_incremental_components(G, ds); incremental_components(G, ds); graph_traits<Graph>::edge_descriptor e; bool flag; boost::tie(e,flag) = add_edge(0, 1, G); ds.union_set(0,1); boost::tie(e,flag) = add_edge(1, 4, G); ds.union_set(1,4); boost::tie(e,flag) = add_edge(4, 0, G); ds.union_set(4,0); boost::tie(e,flag) = add_edge(2, 5, G); ds.union_set(2,5); cout << "An undirected graph:" << endl; print_graph(G, get(vertex_index, G)); cout << endl; graph_traits<Graph>::vertex_iterator i,end; for (boost::tie(i, end) = vertices(G); i != end; ++i) cout << "representative[" << *i << "] = " << ds.find_set(*i) << endl;; cout << endl; typedef component_index<unsigned int> Components; Components components(&parent[0], &parent[0] + parent.size()); for (Components::size_type c = 0; c < components.size(); ++c) { cout << "component " << c << " contains: "; Components::value_type::iterator j = components[c].begin(), jend = components[c].end(); for ( ; j != jend; ++j) cout << *j << " "; cout << endl; }
initialize_incremental_components
template <class VertexListGraph, class DisjointSets> void initialize_incremental_components(VertexListGraph& G, DisjointSets& ds) This prepares the disjoint-sets data structure for the incremental connected components algorithm by making each vertex in the graph a member of its own component (or set).
Where Definedboost/graph/incremental_components.hpp
incremental_components
template <class EdgeListGraph, class DisjointSets> void incremental_components(EdgeListGraph& g, DisjointSets& ds) This function calculates the connected components of the graph, embedding the results in the disjoint-sets data structure.
Where Definedboost/graph/incremental_components.hpp
Requirements on Types
same_component
template <class Vertex, class DisjointSet> bool same_component(Vertex u, Vertex v, DisjointSet& ds) This function determines whether u and v are in the same component.
Where Definedboost/graph/incremental_components.hpp
Requirements on Types
component_index
component_index<Index> The is a class that provides an STL container-like view for the components of the graph. Each component is a container-like object, and the component_index object provides access to the component objects via operator[]. A component_index object is initialized with the parents property in the disjoint-sets calculated from the incremental_components() function.
Where Definedboost/graph/incremental_components.hpp
Members
|