Check if a Tree can be split into K equal connected components
Last Updated :
28 Mar, 2023
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.
Examples:
Input: N = 15, K = 5
Below is the given tree with Number nodes = 15
Output: YES
Explanation:
Below is the 5 number of Connected Components can be made:
Approach:
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:
C++
#include <bits/stdc++.h>
using namespace std;
int flag = 0;
int DFS(vector< int > adj[], int k,
int i, int x)
{
int ans = 1;
for ( auto & it : adj[i]) {
if (it != k) {
ans += DFS(adj, i, it, x);
}
}
if (ans > x) {
flag = 1;
return 0;
}
else if (ans == x) {
ans = 0;
}
return ans;
}
void addEdge(vector< int > adj[],
int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
int main()
{
int N = 15, K = 5;
vector< int > adj[N + 1];
addEdge(adj, 1, 2);
addEdge(adj, 2, 3);
addEdge(adj, 2, 4);
addEdge(adj, 4, 5);
addEdge(adj, 5, 6);
addEdge(adj, 5, 7);
addEdge(adj, 4, 8);
addEdge(adj, 4, 9);
addEdge(adj, 8, 11);
addEdge(adj, 10, 11);
addEdge(adj, 11, 14);
addEdge(adj, 9, 12);
addEdge(adj, 12, 15);
addEdge(adj, 12, 13);
if (N % K == 0) {
DFS(adj, -1, 1, N / K);
}
cout << (flag ? "NO" : "YES" );
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int flag = 0 ;
static int DFS(Vector<Integer> adj[], int k,
int i, int x)
{
int ans = 1 ;
for ( int it : adj[i]) {
if (it != k) {
ans += DFS(adj, i, it, x);
}
}
if (ans > x) {
flag = 1 ;
return 0 ;
}
else if (ans == x) {
ans = 0 ;
}
return ans;
}
static void addEdge(Vector<Integer> adj[],
int u, int v)
{
adj[u].add(v);
adj[v].add(u);
}
public static void main(String[] args)
{
int N = 15 , K = 5 ;
Vector<Integer> []adj = new Vector[N + 1 ];
for ( int i= 0 ; i < N + 1 ; i++)
adj[i] = new Vector<Integer>();
addEdge(adj, 1 , 2 );
addEdge(adj, 2 , 3 );
addEdge(adj, 2 , 4 );
addEdge(adj, 4 , 5 );
addEdge(adj, 5 , 6 );
addEdge(adj, 5 , 7 );
addEdge(adj, 4 , 8 );
addEdge(adj, 4 , 9 );
addEdge(adj, 8 , 11 );
addEdge(adj, 10 , 11 );
addEdge(adj, 11 , 14 );
addEdge(adj, 9 , 12 );
addEdge(adj, 12 , 15 );
addEdge(adj, 12 , 13 );
if (N % K == 0 ) {
DFS(adj, - 1 , 1 , N / K);
}
System.out.print(flag== 1 ? "NO" : "YES" );
}
}
|
Python3
flag = 0
def DFS(adj, k, i, x):
ans = 1
for it in adj[i]:
if it is not k:
ans + = DFS(adj, i, it, x)
if (ans > x):
flag = 1
return 0
elif (ans = = x):
ans = 0
return ans
def addEdge(adj, u, v):
adj[u].append(v)
adj[v].append(u)
if __name__ = = "__main__" :
(N, K) = ( 15 , 5 )
adj = [[] for i in range (N + 1 )]
addEdge(adj, 1 , 2 );
addEdge(adj, 2 , 3 );
addEdge(adj, 2 , 4 );
addEdge(adj, 4 , 5 );
addEdge(adj, 5 , 6 );
addEdge(adj, 5 , 7 );
addEdge(adj, 4 , 8 );
addEdge(adj, 4 , 9 );
addEdge(adj, 8 , 11 );
addEdge(adj, 10 , 11 );
addEdge(adj, 11 , 14 );
addEdge(adj, 9 , 12 );
addEdge(adj, 12 , 15 );
addEdge(adj, 12 , 13 );
if (N % K = = 0 ):
DFS(adj, - 1 , 1 , N / / K)
if flag = = 1 :
print ( "NO" )
else :
print ( "YES" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int flag = 0;
static int DFS(List< int > []adj, int k,
int i, int x)
{
int ans = 1;
foreach ( int it in adj[i]) {
if (it != k) {
ans += DFS(adj, i, it, x);
}
}
if (ans > x) {
flag = 1;
return 0;
}
else if (ans == x) {
ans = 0;
}
return ans;
}
static void addEdge(List< int > []adj,
int u, int v)
{
adj[u].Add(v);
adj[v].Add(u);
}
public static void Main(String[] args)
{
int N = 15, K = 5;
List< int > []adj = new List< int >[N + 1];
for ( int i= 0; i < N + 1; i++)
adj[i] = new List< int >();
addEdge(adj, 1, 2);
addEdge(adj, 2, 3);
addEdge(adj, 2, 4);
addEdge(adj, 4, 5);
addEdge(adj, 5, 6);
addEdge(adj, 5, 7);
addEdge(adj, 4, 8);
addEdge(adj, 4, 9);
addEdge(adj, 8, 11);
addEdge(adj, 10, 11);
addEdge(adj, 11, 14);
addEdge(adj, 9, 12);
addEdge(adj, 12, 15);
addEdge(adj, 12, 13);
if (N % K == 0) {
DFS(adj, -1, 1, N / K);
}
Console.Write(flag==1 ? "NO" : "YES" );
}
}
|
Javascript
<script>
var flag = 0;
function DFS(adj, k, i, x)
{
var ans = 1;
adj[i].forEach(element => {
if (element != k) {
ans += DFS(adj, i, element, x);
}
});
if (ans > x) {
flag = 1;
return 0;
}
else if (ans == x) {
ans = 0;
}
return ans;
}
function addEdge(adj, u, v)
{
adj[u].push(v);
adj[v].push(u);
}
var N = 15, K = 5;
var adj = Array.from(Array(N+1), ()=> Array());
addEdge(adj, 1, 2);
addEdge(adj, 2, 3);
addEdge(adj, 2, 4);
addEdge(adj, 4, 5);
addEdge(adj, 5, 6);
addEdge(adj, 5, 7);
addEdge(adj, 4, 8);
addEdge(adj, 4, 9);
addEdge(adj, 8, 11);
addEdge(adj, 10, 11);
addEdge(adj, 11, 14);
addEdge(adj, 9, 12);
addEdge(adj, 12, 15);
addEdge(adj, 12, 13);
if (N % K == 0) {
DFS(adj, -1, 1, N / K);
}
document.write(flag ? "NO" : "YES" );
</script>
|
Time Complexity: O(V + E), where V is the number of vertices and E is the number of edges
Auxiliary Space: O(V) due to recursion stack space. Ignoring the space used by the adjacency list as it is given.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...