Number of groups formed in a graph of friends
Last Updated :
18 Sep, 2023
Given n friends and their friendship relations, find the total number of groups that exist. And the number of ways of new groups that can be formed consisting of people from every existing group.
If no relation is given for any person then that person has no group and singularly forms a group. If a is a friend of b and b is a friend of c, then a b and c form a group.
Examples:
Input : Number of people = 6
Relations : 1 - 2, 3 - 4
and 5 - 6
Output: Number of existing Groups = 3
Number of new groups that can
be formed = 8
Explanation: The existing groups are
(1, 2), (3, 4), (5, 6). The new 8 groups
that can be formed by considering a
member of every group are (1, 3, 5),
(1, 3, 6), (1, 4, 5), (1, 4, 6), (2,
3, 5), (2, 3, 6), (2, 4, 5) and (2, 4,
6).
Input: Number of people = 4
Relations : 1 - 2 and 2 - 3
Output: Number of existing Groups = 2
Number of new groups that can
be formed = 3
Explanation: The existing groups are
(1, 2, 3) and (4). The new groups that
can be formed by considering a member
of every group are (1, 4), (2, 4), (3, 4).
To count number of groups, we need to simply count connected components in the given undirected graph. Counting connected components can be easily done using DFS or BFS. Since this is an undirected graph, the number of times a Depth First Search starts from an unvisited vertex for every friend is equal to the number of groups formed.
To count number of ways in which we form new groups can be done using simply formula which is (N1)*(N2)*….(Nn) where Ni is the no of people in i-th group.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
class Graph {
int V;
list< int >* adj;
int countUtil( int v, bool visited[]);
public :
Graph( int V);
void addRelation( int v, int w);
void countGroups();
};
Graph::Graph( int V)
{
this ->V = V;
adj = new list< int >[V];
}
void Graph::addRelation( int v, int w)
{
v--;
w--;
adj[v].push_back(w);
adj[w].push_back(v);
}
int Graph::countUtil( int v, bool visited[])
{
int count = 1;
visited[v] = true ;
for ( auto i=adj[v].begin(); i!=adj[v].end(); ++i)
if (!visited[*i])
count = count + countUtil(*i, visited);
return count;
}
void Graph::countGroups()
{
bool * visited = new bool [V];
memset (visited, 0, V* sizeof ( int ));
int existing_groups = 0, new_groups = 1;
for ( int i = 0; i < V; i++)
{
if (visited[i] == false )
{
existing_groups++;
new_groups = new_groups *
countUtil(i, visited);
}
}
if (existing_groups == 1)
new_groups = 0;
cout << "No. of existing groups are "
<< existing_groups << endl;
cout << "No. of new groups that can be"
" formed are " << new_groups
<< endl;
}
int main()
{
int n = 6;
Graph g(n);
g.addRelation(1, 2);
g.addRelation(3, 4);
g.addRelation(5, 6);
g.countGroups();
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class Graph{
private int V;
private LinkedList<Integer> adj[];
@SuppressWarnings ( "unchecked" ) Graph( int v)
{
V = v;
adj = new LinkedList[V];
for ( int i = 0 ; i < V; i++)
{
adj[i] = new LinkedList();
}
}
public void addRelation( int v, int w)
{
v--;
w--;
adj[v].add(w);
adj[w].add(v);
}
int countUtil( int v, boolean visited[])
{
int count = 1 ;
visited[v] = true ;
Iterator<Integer> i = adj[v].listIterator();
while (i.hasNext())
{
int n = i.next();
if (!visited[n])
count = count + countUtil(n, visited);
}
return count;
}
void countGroups()
{
boolean visited[] = new boolean [V];
int existing_groups = 0 , new_groups = 1 ;
for ( int i = 0 ; i < V; i++)
{
if (visited[i] == false )
{
existing_groups++;
new_groups = new_groups *
countUtil(i, visited);
}
}
if (existing_groups == 1 )
new_groups = 0 ;
System.out.println( "No. of existing groups are " +
existing_groups);
System.out.println( "No. of new groups that " +
"can be formed are " +
new_groups);
}
public static void main(String[] args)
{
int n = 6 ;
Graph g = new Graph(n);
g.addRelation( 1 , 2 );
g.addRelation( 3 , 4 );
g.addRelation( 5 , 6 );
g.countGroups();
}
}
|
Python3
class Graph:
def __init__( self , V):
self .V = V
self .adj = [[] for i in range (V)]
def addRelation( self , v, w):
v - = 1
w - = 1
self .adj[v].append(w)
self .adj[w].append(v)
def countUtil( self , v, visited):
count = 1
visited[v] = True
i = 0
while i ! = len ( self .adj[v]):
if ( not visited[ self .adj[v][i]]):
count = count + self .countUtil( self .adj[v][i],
visited)
i + = 1
return count
def countGroups( self ):
visited = [ 0 ] * self .V
existing_groups = 0
new_groups = 1
for i in range ( self .V):
if (visited[i] = = False ):
existing_groups + = 1
new_groups = (new_groups *
self .countUtil(i, visited))
if (existing_groups = = 1 ):
new_groups = 0
print ( "No. of existing groups are" ,
existing_groups)
print ( "No. of new groups that" ,
"can be formed are" , new_groups)
if __name__ = = '__main__' :
n = 6
g = Graph(n)
g.addRelation( 1 , 2 )
g.addRelation( 3 , 4 )
g.addRelation( 5 , 6 )
g.countGroups()
|
C#
using System;
using System.Collections.Generic;
class Graph {
private int V;
private List< int >[] adj;
public Graph( int V)
{
this .V = V;
this .adj = new List< int >[ V ];
for ( int i = 0; i < V; i++) {
adj[i] = new List< int >();
}
}
public void AddRelation( int v, int w)
{
v--;
w--;
adj[v].Add(w);
adj[w].Add(v);
}
private int CountUtil( int v, bool [] visited)
{
int count = 1;
visited[v] = true ;
foreach ( int i in adj[v])
{
if (!visited[i]) {
count += CountUtil(i, visited);
}
}
return count;
}
public void CountGroups()
{
bool [] visited = new bool [V];
for ( int i = 0; i < V; i++) {
visited[i] = false ;
}
int existing_groups = 0, new_groups = 1;
for ( int i = 0; i < V; i++) {
if (!visited[i]) {
existing_groups++;
new_groups
= new_groups * CountUtil(i, visited);
}
}
if (existing_groups == 1) {
new_groups = 0;
}
Console.WriteLine( "No. of existing groups are "
+ existing_groups);
Console.WriteLine(
"No. of new groups that can be formed are "
+ new_groups);
}
}
class GFG {
static void Main( string [] args)
{
int n = 6;
Graph g = new Graph(n);
g.AddRelation(1, 2);
g.AddRelation(3, 4);
g.AddRelation(5, 6);
g.CountGroups();
}
}
|
Javascript
class Graph {
constructor(V) {
this .V = V;
this .adj = new Array(V);
for (let i = 0; i < V; i++) {
this .adj[i] = [];
}
}
addRelation(v, w) {
v--;
w--;
this .adj[v].push(w);
this .adj[w].push(v);
}
countUtil(v, visited) {
let count = 1;
visited[v] = true ;
for (let i of this .adj[v]) {
if (!visited[i]) {
count = count + this .countUtil(i, visited);
}
}
return count;
}
countGroups() {
let visited = new Array( this .V);
for (let i = 0; i < this .V; i++) {
visited[i] = false ;
}
let existing_groups = 0, new_groups = 1;
for (let i = 0; i < this .V; i++) {
if (!visited[i]) {
existing_groups++;
new_groups = new_groups * this .countUtil(i, visited);
}
}
if (existing_groups === 1) {
new_groups = 0;
}
console.log( "No. of existing groups are " + existing_groups);
console.log( "No. of new groups that can be formed are " + new_groups);
}
}
let n = 6;
let g = new Graph(n);
g.addRelation(1, 2);
g.addRelation(3, 4);
g.addRelation(5, 6);
g.countGroups();
|
Output
No. of existing groups are 3
No. of new groups that can be formed are 8
Time complexity: O(N + R) where N is the number of people and R is the number of relations.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...