The friend circle problem

Problem statement

There are N students in a class. Some of them are friends,others are not. Their friendship is transitive; if A is a direct friend of B, and B is a direct friend of C, then A is an indirect friend of C. A friend circle is defined as a group of students who are direct or indirect friends.

The input matrix will have a number of rows and columns equal to the number of students in a class. A cell [i,j] will hold the value 1 if student i and student j are friends; otherwise, the cell will hold the value 0. For example, if the input is:

012301100111102011030001

Then there are 2 friend circles. Student 0 is friends with student 1 only, but student 1 is friends with both​ student 0 and student 2. These three students make one friend circle. Student 3, alone, makes another friend circle.

Similarly, if the input matrix is:

012301100111002001030001

Then there are three friend circles. Student 0 and student 1 are only friends with each other, so they make one friend circle. Student 2 and student 3 don’t have any friends, so they each make one friend circle.

Let’s try to solve this for any value of n.

Note that the input matrix will always be symmetric because the friendship is transitive.

We can try to think of the symmetric input matrix as an undirected graph. Let’s make an undirected graph for our first example:

svg viewer

​Notice that all friends (both direct and indirect), who should be in one friend circle are also in one connected component​ in the graph.

The number of connected components in the graph will give us the number of friend circles in the class.

Algorithm

We can treat our input matrix as an adjacency matrix; our task is to find the number of connected components. Here are the steps:

  • Initialize a list/array, visited, to keep track of visited vertices of size n with 0 as the initial value at each index.
  • For every vertex v, if visited[v] is 0, traverse the graph using DFS; else, move to the next v.
  • Set visited[v] to 1 for every v that the DFS traversal encounters.
  • When the DFS traversal terminates, increment the friend circles counter by 1 as it means that​ one whole connected component has been traversed.

Code

The following code snippet implements the above algorithm:

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved