Queries to count connected components after removal of a vertex from a Tree
Given a Tree consisting of N nodes valued in the range [0, N) and an array Queries[] of Q integers consisting of values in the range [0, N). The task for each query is to remove the vertex valued Q[i] and count the connected components in the resulting graph.
Examples:
Input: N = 7, Edges[][2] = {{0, 1}, {0, 2}, {0, 3}, {1, 4}, {1, 5}, {3, 6}}, Queries[] = {0, 1, 6}
Output:
3 3 1
Explanation:
Query 1: Removal of the node valued 0 leads to removal of the edges (0, 1), (0, 2) and (0, 3). Therefore, the remaining graph has 3 connected components: [1, 4, 5], [2], [3, 6]
Query 2:Removal of the node valued 1 leads to removal of the edges (1, 4), (1, 5) and (1, 0). Therefore, remaining graph has 3 connected components: [4], [5], [2, 0, 3, 6]
Query 3:Removal of the node valued 6 leads to removal of the edges (3, 6). Therefore, the remaining graph has 1 connected component: [0, 1, 2, 3, 4, 5].
Input: N = 7, Edges[][2] = {{0, 1}, {0, 2}, {0, 3}, {1, 4}, {1, 5}, {3, 6}}, Queries[] = {5, 3, 2}
Output:1 2 1
Explanation:
Query 1: Removal of the node valued 5 leads to removal of the edge (1, 5). Therefore, the remaining graph has 1 connected component: [0, 1, 2, 3, 4, 6]
Query 2:Removal of the node valued 3 leads to removal of the edges (0, 3), (3, 6). Therefore, the remaining graph has 2 connected components: [0, 1, 2, 4, 5], [6]
Query 3: Removal of the node valued 2 leads to removal of the edge (0, 2). Therefore, the remaining graph has 1 connected component: [0, 1, 3, 4, 5, 6]
Approach: The idea is to observe that in a Tree, whenever a node is deleted, the nodes which were connected together to that node get separated. So, the count of connected components becomes equal to the degree of the deleted node.
Therefore, the approach is to precompute and store the degree of each node in an array. For every query, the count of the connected components is simply the degree of the corresponding node in the query.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 100005
int degree[MAX];
void DegreeOfNodes( int Edges[][2],
int N)
{
for ( int i = 0; i < N - 1; i++) {
degree[Edges[i][0]]++;
degree[Edges[i][1]]++;
}
}
void findConnectedComponents( int x)
{
cout << degree[x] << ' ' ;
}
void countCC( int N, int Q, int Queries[],
int Edges[][2])
{
DegreeOfNodes(Edges, N);
for ( int i = 0; i < Q; i++) {
findConnectedComponents(Queries[i]);
}
}
int main()
{
int N = 7, Q = 3;
int Queries[] = { 0, 1, 6 };
int Edges[][2] = { { 0, 1 }, { 0, 2 },
{ 0, 3 }, { 1, 4 },
{ 1, 5 }, { 3, 6 } };
countCC(N, Q, Queries, Edges);
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int MAX = 100005 ;
static int []degree = new int [MAX];
static void DegreeOfNodes( int [][]Edges,
int N)
{
for ( int i = 0 ; i < N - 1 ; i++)
{
degree[Edges[i][ 0 ]]++;
degree[Edges[i][ 1 ]]++;
}
}
static void findConnectedComponents( int x)
{
System.out.print(degree[x] + " " );
}
static void countCC( int N, int Q,
int Queries[],
int [][]Edges)
{
DegreeOfNodes(Edges, N);
for ( int i = 0 ; i < Q; i++)
{
findConnectedComponents(Queries[i]);
}
}
public static void main(String[] args)
{
int N = 7 , Q = 3 ;
int Queries[] = { 0 , 1 , 6 };
int [][]Edges = {{ 0 , 1 }, { 0 , 2 },
{ 0 , 3 }, { 1 , 4 },
{ 1 , 5 }, { 3 , 6 }};
countCC(N, Q, Queries, Edges);
}
}
|
Python3
MAX = 100005
degree = [ 0 ] * MAX
def DegreeOfNodes(Edges, N):
for i in range (N - 1 ):
degree[Edges[i][ 0 ]] + = 1
degree[Edges[i][ 1 ]] + = 1
def findConnectedComponents(x):
print (degree[x], end = " " )
def countCC(N, Q, Queries, Edges):
DegreeOfNodes(Edges, N)
for i in range (Q):
findConnectedComponents(Queries[i])
if __name__ = = '__main__' :
N = 7
Q = 3
Queries = [ 0 , 1 , 6 ]
Edges = [ [ 0 , 1 ], [ 0 , 2 ],
[ 0 , 3 ], [ 1 , 4 ],
[ 1 , 5 ], [ 3 , 6 ] ]
countCC(N, Q, Queries, Edges)
|
C#
using System;
class GFG{
static readonly int MAX = 100005;
static int []degree = new int [MAX];
static void DegreeOfNodes( int [,]Edges,
int N)
{
for ( int i = 0; i < N - 1; i++)
{
degree[Edges[i, 0]]++;
degree[Edges[i, 1]]++;
}
}
static void findConnectedComponents( int x)
{
Console.Write(degree[x] + " " );
}
static void countCC( int N, int Q,
int []Queries,
int [,]Edges)
{
DegreeOfNodes(Edges, N);
for ( int i = 0; i < Q; i++)
{
findConnectedComponents(Queries[i]);
}
}
public static void Main(String[] args)
{
int N = 7, Q = 3;
int []Queries = {0, 1, 6};
int [,]Edges = {{0, 1}, {0, 2},
{0, 3}, {1, 4},
{1, 5}, {3, 6}};
countCC(N, Q, Queries, Edges);
}
}
|
Javascript
<script>
var MAX = 100005
var degree = Array(MAX).fill(0)
function DegreeOfNodes(Edges, N)
{
for ( var i = 0; i < N - 1; i++) {
degree[Edges[i][0]]++;
degree[Edges[i][1]]++;
}
}
function findConnectedComponents(x)
{
document.write( degree[x] + ' ' );
}
function countCC(N, Q, Queries, Edges)
{
DegreeOfNodes(Edges, N);
for ( var i = 0; i < Q; i++) {
findConnectedComponents(Queries[i]);
}
}
var N = 7, Q = 3;
var Queries = [ 0, 1, 6 ];
var Edges = [ [ 0, 1 ], [ 0, 2 ],
[ 0, 3 ], [ 1, 4 ],
[ 1, 5 ], [ 3, 6 ] ];
countCC(N, Q, Queries, Edges);
</script>
|
Time Complexity: O(E + Q), where E is the number of edges(E = N – 1) and Q is the number of queries.
Auxiliary Space: O(V), where V is number the of vertices.
Last Updated :
07 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...