VOOZH about

URL: https://www.geeksforgeeks.org/dsa/friendship-request-validation-in-a-network/

⇱ Friendship Request Validation in a Network - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Friendship Request Validation in a Network

Last Updated : 23 Jul, 2025

You have a network of individuals, each labeled with a number from 0 to n - 1. Some individuals cannot be friends due to specific rules. You also have a list of friend requests, processed one by one, the task is to determine whether each request can be accepted based on the established regulations. For each request, you need to tell the person u and v can be friends or not. If they are friends then they become friends for all future requests.

Examples:

Input: n = 3, restrictions[][] = [[0,1]], requests[][] = [[0, 2], [2, 1]]
Output: [true, false]
Explanation: Initially, no one is friends with each other.
Request 0: Person 0 and person 2 can be friends, so they become direct friends. (0--2)
Request 1: Person 2 and person 1 cannot be friends since person 0 and person 1 would be indirect friends (0--2--1).

Input: n = 5, restrictions[][] = [[0, 1], [1, 2], [2, 3]], requests[][] = [[0, 4], [1, 2], [3, 1], [3, 4]]
Output: [true, false, true, false]
Explanation: Initially, no one is friends with each other.
Request 0: Person 0 and person 4 can be friends, so they become direct friends. (0--4)
Request 1: Person 1 and person 2 cannot be friends since they are directly restricted. (1 and 2 have restrictions)
Request 2: Person 3 and Person 1 can be friends, so they become direct friends. (3--1)
Request 3: Person 3 and person 4 cannot be friends since person 0 and person 1 would be indirect friends (0--4--3--1).

Approach:

We can solve this problem Using Disjoint-Set-Union Concept .

  • Initialize two vectors, size_arr, and parent, to keep track of the size and parent of each set.
  • Implement three helper functions:
    • make_set(int v): Initialize a set for a person v, setting their parent as themselves.
    • find_set(int v): Find the representative of the set that person v belongs to, with path compression to optimize future find operations.
    • union_sets(int a, int b): Union two sets a and b, ensuring the smaller one becomes a subset of the larger one and maintaining balanced trees.
  • In the friendRequests function:
    • Initialize the parent and size_arr vectors for each person in the network.
    • Create a vector res to store the results of friend requests.
  • Iterate through each friend request in the requests vector:
    • Obtain the individuals involved in the request (u and v).
    • Find the representatives of their sets (par_u and par_v).
    • Assume the request is valid by default (flag = true).
  • Check if u and v belong to the same set (par_u and par_v are equal). If not, proceed to check if the request violates any restrictions.
  • For each restriction in the restrictions vector:
    • Obtain the individuals involved in the restriction (restr_u and restr_v).
    • Find the representatives of their sets (restr_paru and restr_parv).
    • Check if the request and restriction have the same pair of representatives, indicating a violation. If so, set flag to false and break the loop.
  • If the flag remains true (i.e., the request is valid), union the sets of u and v using the union_sets function.
  • Store the result of the request (either true for success or false for failure) in the res vector.
  • Finally, return the res vector containing the results of all friend requests.

This code effectively processes friend requests in a network while considering restrictions and ensures that friends are connected through a disjoint-set data structure with path compression for efficient operations.

Below is the implementation of the above Approach:


Output
[True,False,True,False,]

Time Complexity: O(n+m*log(n) + r*m)

  • n is the number of people in the network.
  • m is the number of friend requests.
  • r is the number of restrictions.

Auxiliary Space: O(n)

Comment
Article Tags: