Given an undirected graph, the task is to find the size of each connected component and print the number of unique sizes of connected components
As depicted above, the count(size of connected component) associated with the connected components are 2, 3 and 2. Now, the unique count of the components are 2 and 3. Hence the expected result is Count = 2
Input: N = 7 Output: 1 2 Count = 2 3 4 5 Count = 3 6 7 Count = 2 Unique Counts of connected components: 2 Input: N = 10 Output: 1 Count = 1 2 3 4 5 Count = 4 6 7 8 Count = 3 9 10 Count = 2 Unique Counts of connected components: 4
Prerequisites: Depth First Search
The basic idea is to utilize the Depth First Search traversal method to keep a track of the connected components in the undirected graph. An STL container Set is used to store the unique counts of all such components since it is known that a set has the property of storing unique elements in a sorted manner. Finally, extracting the size of the Set gives us the necessary result. The step-wise implementation is as follows:
- Initialize a hash container (Set), to store the unique counts of connected components.
- Recursively call Depth First Search traversal.
- For every vertex visited, store the count in the set container.
- The final size of the Set is the required result.
Below is the implementation of the above approach:
1 2 Count = 2 3 4 5 Count = 3 6 7 Count = 2 Unique Counts of connected components: 2
As evident from the above implementation, the graph is traversed using the Depth First Search algorithm. The individual counts are stored using Set container wherein the insertion operation takes O(1) time. The overall complexity is solely based on the time taken by the DFS algorithm to run recursively. Hence, the time complexity of the program is O(E + V) where E is the number of edges and V is the number of vertices of the graph.
Auxiliary Space: O(N)
- Program to count Number of connected components in an undirected graph
- Connected Components in an undirected graph
- Largest subarray sum of all connected components in undirected graph
- Sum of the minimum elements in all connected components of an undirected graph
- Clone an undirected graph with multiple connected components
- Maximum sum of values of nodes among all connected components of an undirected graph
- Maximum number of edges among all connected components of an undirected graph
- Convert undirected connected graph to strongly connected directed graph
- Product of lengths of all cycles in an undirected graph
- Cycles of length n in an undirected and connected graph
- Number of single cycle components in an undirected graph
- Octal equivalents of connected components in Binary valued graph
- Maximum decimal equivalent possible among all connected components of a Binary Valued Graph
- Check if longest connected component forms a palindrome in undirected graph
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Count number of edges in an undirected graph
- Count the number of Prime Cliques in an undirected graph
- Count total ways to reach destination from source in an undirected Graph
- Strongly Connected Components
- Number of connected components in a 2-D matrix of strings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.