Queries to check if vertices X and Y are in the same Connected Component of an Undirected Graph
Last Updated :
24 Mar, 2021
Given an undirected graph consisting of N vertices and M edges and queries Q[][] of the type {X, Y}, the task is to check if the vertices X and Y are in the same connected component of the Graph.
Examples:
Input: Q[][] = {{1, 5}, {3, 2}, {5, 2}}
Graph:
1-3-4 2
|
5
Output: Yes No No
Explanation:
From the given graph, it can be observed that the vertices {1, 5} are in the same connected component.
But {3, 2} and {5, 2} are from different components.
Input: Q[][] = {{1, 9}, {2, 8}, {3, 5}, {7, 9}}
Graph:
1-3-4 2-5-6 7-9
|
8
Output: No Yes No Yes
Explanation:
From the given graph, it can be observed that the vertices {2, 8} and {7, 9} is from same connected component.
But {1, 9} and {3, 5} are from different components.
Approach: The idea is to use the Disjoint Set-Union to solve the problem. The basic interface of the Disjoint set union data structure used is as follows:
- make_set(v): To create a new set consisting of the new element v.
- find_set(v): Returns the representative of the set that contains the element v. This is optimized using Path Compression.
- union_set(a, b): Merges the two specified sets (the set in which the element is located, and the set in which the element b is located). Two connected vertices are merged to form a single set(Connected Components).
- Initially, all the vertices will be a different set (i.e parent of itself ) and are formed using make_set function.
- The vertices will be merged if two of them are connected using union_set function.
- Now, for each query, use the find_set function to check if the given two vertices are from the same set or not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX_NODES 100005
int parent[MAX_NODES];
int size_set[MAX_NODES];
void make_set( int v)
{
parent[v] = v;
size_set[v] = 1;
}
int find_set( int v)
{
if (v == parent[v])
return v;
return parent[v]
= find_set(parent[v]);
}
void union_set( int a, int b)
{
a = find_set(a);
b = find_set(b);
if (a != b) {
if (size_set[a] < size_set[b])
swap(a, b);
parent[b] = a;
size_set[a] += size_set[b];
}
}
string check( int a, int b)
{
a = find_set(a);
b = find_set(b);
return (a == b) ? "Yes" : "No" ;
}
int main()
{
int n = 5, m = 3;
make_set(1);
make_set(2);
make_set(3);
make_set(4);
make_set(5);
union_set(1, 3);
union_set(3, 4);
union_set(3, 5);
int q = 3;
cout << check(1, 5) << endl;
cout << check(3, 2) << endl;
cout << check(5, 2) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int MAX_NODES = 100005 ;
static int []parent = new int [MAX_NODES];
static int []size_set = new int [MAX_NODES];
static void make_set( int v)
{
parent[v] = v;
size_set[v] = 1 ;
}
static int find_set( int v)
{
if (v == parent[v])
return v;
return parent[v] = find_set(parent[v]);
}
static void union_set( int a, int b)
{
a = find_set(a);
b = find_set(b);
if (a != b) {
if (size_set[a] < size_set[b])
{
a = a+b;
b = a-b;
a = a-b;
}
parent[b] = a;
size_set[a] += size_set[b];
}
}
static String check( int a, int b)
{
a = find_set(a);
b = find_set(b);
return (a == b) ? "Yes" : "No" ;
}
public static void main(String[] args)
{
int n = 5 , m = 3 ;
make_set( 1 );
make_set( 2 );
make_set( 3 );
make_set( 4 );
make_set( 5 );
union_set( 1 , 3 );
union_set( 3 , 4 );
union_set( 3 , 5 );
int q = 3 ;
System.out.print(check( 1 , 5 ) + "\n" );
System.out.print(check( 3 , 2 ) + "\n" );
System.out.print(check( 5 , 2 ) + "\n" );
}
}
|
Python3
MAX_NODES = 100005
parent = [ 0 for i in range (MAX_NODES)];
size_set = [ 0 for i in range (MAX_NODES)];
def make_set(v):
parent[v] = v;
size_set[v] = 1 ;
def find_set(v):
if (v = = parent[v]):
return v;
parent[v] = find_set(parent[v]);
return parent[v]
def union_set(a, b):
a = find_set(a);
b = find_set(b);
if (a ! = b):
if (size_set[a] <
size_set[b]):
swap(a, b);
parent[b] = a;
size_set[a] + = size_set[b];
def check(a, b):
a = find_set(a);
b = find_set(b);
if a = = b:
return ( "Yes" )
else :
return ( "No" )
if __name__ = = "__main__" :
n = 5
m = 3 ;
make_set( 1 );
make_set( 2 );
make_set( 3 );
make_set( 4 );
make_set( 5 );
union_set( 1 , 3 );
union_set( 3 , 4 );
union_set( 3 , 5 );
q = 3 ;
print (check( 1 , 5 ))
print (check( 3 , 2 ))
print (check( 5 , 2 ))
|
C#
using System;
class GFG{
static readonly int MAX_NODES = 100005;
static int []parent = new int [MAX_NODES];
static int []size_set = new int [MAX_NODES];
static void make_set( int v)
{
parent[v] = v;
size_set[v] = 1;
}
static int find_set( int v)
{
if (v == parent[v])
return v;
return parent[v] = find_set(parent[v]);
}
static void union_set( int a, int b)
{
a = find_set(a);
b = find_set(b);
if (a != b)
{
if (size_set[a] < size_set[b])
{
a = a + b;
b = a - b;
a = a - b;
}
parent[b] = a;
size_set[a] += size_set[b];
}
}
static String check( int a, int b)
{
a = find_set(a);
b = find_set(b);
return (a == b) ? "Yes" : "No" ;
}
public static void Main(String[] args)
{
make_set(1);
make_set(2);
make_set(3);
make_set(4);
make_set(5);
union_set(1, 3);
union_set(3, 4);
union_set(3, 5);
Console.Write(check(1, 5) + "\n" );
Console.Write(check(3, 2) + "\n" );
Console.Write(check(5, 2) + "\n" );
}
}
|
Javascript
<script>
let MAX_NODES = 100005;
let parent = new Array(MAX_NODES);
let size_set = new Array(MAX_NODES);
function make_set(v)
{
parent[v] = v;
size_set[v] = 1;
}
function find_set(v)
{
if (v == parent[v])
return v;
return parent[v]
= find_set(parent[v]);
}
function union_set(a, b)
{
a = find_set(a);
b = find_set(b);
if (a != b) {
if (size_set[a] < size_set[b])
{
let temp = a;
a = b;
b = temp;
}
parent[b] = a;
size_set[a] += size_set[b];
}
}
function check(a, b)
{
a = find_set(a);
b = find_set(b);
return (a == b) ? "Yes" : "No" ;
}
let n = 5, m = 3;
make_set(1);
make_set(2);
make_set(3);
make_set(4);
make_set(5);
union_set(1, 3);
union_set(3, 4);
union_set(3, 5);
let q = 3;
document.write(check(1, 5) + "</br>" );
document.write(check(3, 2) + "</br>" );
document.write(check(5, 2) + "</br>" );
</script>
|
Time Complexity: O(N + M + sizeof(Q))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...