# Answer to Question #15602 in Discrete Mathematics for Sujata Roy

Question #15602

Formula to determine the number of connected components of a graph G.

Expert's answer

There isn't common formula to determine the number of connected components of an arbitrary graph. You can find the number of connected components algorithmically.

## Comments

Assignment Expert10.10.12, 16:18It is straightforward to compute the connected components of a graph in linear time (in terms of the numbers of the vertices and edges of the graph) using either breadth-first search or depth-first search. In either case, a search that begins at some particular vertex v will find the entire connected component containing v (and no more) before returning. To find all the connected components of a graph, loop through its vertices, starting a new breadth first or depth first search whenever the loop reaches a vertex that has not already been included in a previously found connected component.

There are also efficient algorithms to dynamically track the connected components of a graph as vertices and edges are added, as a straightforward application of disjoint-set data structures. These algorithms require amortized O(α(n)) time per operation, where adding vertices and edges and determining the connected component in which a vertex falls are both operations, and α(n) is a very slow-growing inverse of the very quickly growing Ackermann function. A related problem is tracking connected components as all edges are deleted from a graph, one by one; an algorithm exists to solve this with constant time per query, and O(|V||E|) time to maintain the data structure; this is an amortized cost of O(|V|) per edge deletion. For forests, the cost can be reduced to O(q + |V|log|V|), or O(log|V|) amortized cost per edge deletion.

Sujata Roy07.10.12, 10:37Then sir please tell me about the algorithm.

## Leave a comment