Note: Two connected components are said to be equal if they contain equal number of nodes.
The idea is to use Depth First Search(DFS) traversal on the given tree of N nodes to find whether the given tree can be split into K equal Connected Components or not. Following are the steps:
- Start DFS Traversal with the root of the tree.
- For every vertex not visited during DFS traversal, recursively call DFS for that vertex keeping the count of nodes traverse during every DFS recursive call.
- If the count of nodes is equals to (N/K) then we got our one of the set of Connected Components.
- If the total number of the set of Connected Components of (N/K) nodes is equal to K. Then the given graph can be split into K equals Connected Components.
Below is the implementation of the above approach:
Time Complexity: O(V + E), where V is the number of vertices and E is the number of edges
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Queries to count connected components after removal of a vertex from a Tree
- Check if the length of all connected components is a Fibonacci number
- Strongly Connected Components
- Tarjan's Algorithm to find Strongly Connected Components
- Connected Components in an undirected graph
- Sum of the minimum elements in all connected components of an undirected graph
- Maximum number of edges among all connected components of an undirected graph
- Clone an undirected graph with multiple connected components
- Program to count Number of connected components in an undirected graph
- Maximum sum of values of nodes among all connected components of an undirected graph
- Maximum decimal equivalent possible among all connected components of a Binary Valued Graph
- Largest subarray sum of all connected components in undirected graph
- Octal equivalents of connected components in Binary valued graph
- Maximum number of edges to be removed to contain exactly K connected components in the Graph
- Count of unique lengths of connected components for an undirected graph using STL
- Convert undirected connected graph to strongly connected directed graph
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Check if cells numbered 1 to K in a grid can be connected after removal of atmost one blocked cell
- Find height of a special binary tree whose leaf nodes are connected
- Number of times an array can be partitioned repetitively into two subarrays with equal sum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
Improved By : Rajput-Ji