This website contains ALL LeetCode **Premium** problems for
**FREE!!**.

All leaked interview problems are collected from Internet.

All leaked interview problems are collected from Internet.

There are **N** students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a **direct** friend of B, and B is a **direct** friend of C, then A is an **indirect** friend of C. And we defined a friend circle is a group of students who are direct or indirect friends.

Given a **N*N** matrix **M** representing the friend relationship between students in the class. If M[i][j] = 1, then the i_{th} and j_{th} students are **direct** friends with each other, otherwise not. And you have to output the total number of friend circles among all the students.

**Example 1:**

Input:[[1,1,0], [1,1,0], [0,0,1]]Output:2Explanation:The 0_{th}and 1_{st}students are direct friends, so they are in a friend circle.

The 2_{nd}student himself is in a friend circle. So return 2.

**Example 2:**

Input:[[1,1,0], [1,1,1], [0,1,1]]Output:1Explanation:The 0_{th}and 1_{st}students are direct friends, the 1_{st}and 2_{nd}students are direct friends,

so the 0_{th}and 2_{nd}students are indirect friends. All of them are in the same friend circle, so return 1.

**Note:**

- N is in range [1,200].
- M[i][i] = 1 for all students.
- If M[i][j] = 1, then M[j][i] = 1.

b'

\n## Solution

\n

\n#### Approach #1 Using Depth First Search[Accepted]

\n\n

\n#### Approach #2 Using Breadth First Search[Accepted]

\n

\n#### Approach #3 Using Union-Find Method[Accepted]

\n

\n

'
\n\n

\n\n

**Algorithm**

The given matrix can be viewed as the Adjacency Matrix of a graph. By viewing the matrix in such a manner, our problem reduces\nto the problem of finding the number of connected components in an undirected graph. In order to understand the above statement, consider the\nexample matrix below:

\nM= [1 1 0 0 0 0\n\n 1 1 0 0 0 0\n\n 0 0 1 1 1 0\n\n 0 0 1 1 0 0\n\n 0 0 1 0 1 0\n\n 0 0 0 0 0 1]\n

If we view this matrix M as the adjancency matrix of a graph, the following graph is formed:

\n\nIn this graph, the node numbers represent the indices in the matrix M and an edge exists between the nodes numbered and ,\nif there is a 1 at the corresponding .

\nIn order to find the number of connected components in an undirected graph, one of the simplest methods is to make use of Depth First Search\nstarting from every node. We make use of array of size ( is of size ). This element\nis used to indicate that the node has already been visited while undergoing a Depth First Search from some node.

\nTo undergo DFS, we pick up a node and visit all its directly connected nodes. But, as soon as we visit any of those nodes, we recursively apply the same process to them as well. Thus, we try to go as deeper into the levels of the graph as possible starting from a current node first, leaving the other direct neighbour nodes to be visited later on.

\nThe depth first search for an arbitrary graph is shown below:

\n\n!?!../Documents/547_Friend_Circles_dfs.json:1000,563!?!

\nFrom the graph, we can see that the components which are connected can be reached starting from any single node of the connected group.\nThus, to find the number of connected components, we start from every node which isn\'t visited right now and apply DFS starting with it.\nWe increment the of connected components for every new starting node.

\n\n**Complexity Analysis**

- \n
- \n
Time complexity : . The complete matrix of size is traversed.

\n \n - \n
Space complexity : . array of size is used.

\n \n

\n

**Algorithm**

As discussed in the above method, if we view the given matrix as an adjacency matrix of a graph, we can use graph algorithms easily to find the number of connected components. This approach makes use of Breadth First Search for a graph.

\nIn case of Breadth First Search, we start from a particular node and visit all its directly connected nodes first. After all the direct neighbours have been visited, we apply the same process to the neighbour nodes as well. Thus, we exhaust the nodes of a graph on a level by level basis. An example of Breadth First Search is shown below:

\n\n!?!../Documents/547_Friend_Circles_bfs.json:1000,563!?!

\nIn this case also, we apply BFS starting from one of the nodes. We make use of a array to keep a track of the already visited nodes. We increment the of connected components whenever we need to start off with a new node as the root node for applying BFS which hasn\'t been already visited.

\n\n**Complexity Analysis**

- \n
- \n
Time complexity : . The complete matrix of size is traversed.

\n \n - \n
Space complexity : . A and array of size is used.

\n \n

\n

**Algorithm**

Another method that can be used to determine the number of connected components in a graph is the union find method. The method is simple.

\nWe make use of a array of size . We traverse over all the nodes of the graph. For every node traversed, we traverse over all the nodes directly connected to it and assign them to a single group which is represented by their node. This process is called forming a . Every group has a single node, whose own parent is given by .

\nFor every new pair of nodes found, we look for the parents of both the nodes. If the parents nodes are the same, it indicates that they have already been united into the same group. If the parent nodes differ, it means they are yet to be united. Thus, for the pair of nodes , while forming the union, we assign , which ultimately combines them into the same group.

\nThe following animation depicts the process for a simple matrix:

\n\n!?!../Documents/547_Friend_Circles_dfs.json:1000,563!?!

\nAt the end, we find the number of groups, or the number of parent nodes. Such nodes have their parents indicated by a . This gives us the required count.

\n\n**Complexity Analysis**

- \n
- Time complexity : . We traverse over the complete matrix once. Union and find operations take time in the worst case. \n
- Space complexity : . array of size is used. \n

\n

Analysis written by: @vinod23

\n