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.
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 = 6 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.
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.
This article is contributed by Raj. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Count of different groups using Graph
- Size of the Largest Trees in a Forest formed by the given Graph
- Largest component size in a graph formed by connecting non-co-prime nodes
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Minimum number of groups of nodes such that no ancestor is present in the same group
- Minimize Cash Flow among a given set of friends who have borrowed money from each other
- Number of sink nodes in a graph
- Number of Triangles in an Undirected Graph
- Minimum number of edges between two vertices of a graph using DFS
- Maximum number of bridges in a path of a given graph
- Maximum number of edges in Bipartite graph
- Number of Simple Graph with N Vertices and M Edges
- Count number of edges in an undirected graph
- Minimum number of edges between two vertices of a Graph
- Total number of Spanning Trees in a Graph
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Program to find the number of region in Planar Graph
- Number of spanning trees of a weighted complete Graph
- Maximum number of nodes which can be reached from each node in a graph.
- Number of shortest paths in an unweighted and directed graph