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
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Check if the length of all connected components is a Fibonacci number
- Strongly Connected Components
- Connected Components in an undirected graph
- Clone an undirected graph with multiple connected components
- Largest subarray sum of all connected components in undirected graph
- Tarjan's Algorithm to find Strongly Connected Components
- 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
- Count of unique lengths of connected components for an undirected graph using STL
- Maximum sum of values of nodes among all connected components of an undirected graph
- Program to count Number of connected components in an undirected graph
- Octal equivalents of connected components in Binary valued graph
- Check if the given binary tree has a sub-tree with equal no of 1's and 0's | Set 2
- Check if the given Binary Tree have a Subtree with equal no of 1's and 0's
- Check if a directed graph is connected or not
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.
Improved By : Rajput-Ji