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 email@example.com. 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.
- 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 Triangles in an Undirected Graph
- Number of sink nodes in a graph
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Number of Simple Graph with N Vertices and M Edges
- Maximum number of edges in Bipartite graph
- Count number of edges in an undirected graph
- Total number of Spanning Trees in a Graph
- Minimum number of edges between two vertices of a Graph
- Minimum number of edges between two vertices of a graph using DFS
- Total number of Spanning trees in a Cycle Graph
- Number of shortest paths in an unweighted and directed graph
- Number of single cycle components in an undirected graph
- Program to find the number of region in Planar Graph