Given Adjacency List representation of a tree and an integer K., the task is to find whether the given tree can be split into K equal Connected Components or not.
Note: Two connected components are said to be equal if they contain equal number of nodes.
Input: N = 15, K = 5
Beow is the given tree with Number nodes = 15
Below is the 5 number of Connected Components can be made:
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
- 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
- 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
- Maximum number of edges to be removed to contain exactly K connected components in the Graph
- Connected Components in an undirected graph
- Maximum decimal equivalent possible among all connected components of a Binary Valued Graph
- Octal equivalents of connected components in Binary valued graph
- Largest subarray sum of all connected components in undirected graph
- Program to count Number of connected components in an undirected graph
- Convert undirected connected graph to strongly connected directed graph
- Check if cells numbered 1 to K in a grid can be connected after removal of atmost one blocked cell
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Find height of a special binary tree whose leaf nodes are connected
- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
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.