[LeetCode] 547. Friend Circles

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 ith and jth 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: 2
Explanation:The 0th and 1st students are direct friends, so they are in a friend circle. 
The 2nd student himself is in a friend circle. So return 2.

Example 2:

Input: 
[[1,1,0],
 [1,1,1],
 [0,1,1]]
Output: 1
Explanation:The 0th and 1st students are direct friends, the 1st and 2nd students are direct friends, 
so the 0th and 2nd students are indirect friends. All of them are in the same friend circle, so return 1.

Note:

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

朋友圈。班上有 N 名学生。其中有些人是朋友,有些则不是。他们的友谊具有是传递性。如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友。所谓的朋友圈,是指所有朋友的集合。给定一个 N * N 的矩阵 M,表示班级中学生之间的朋友关系。如果M[i][j] = 1,表示已知第 i 个和 j 个学生互为朋友关系,否则为不知道。你必须输出所有学生中的已知的朋友圈总数。

这是一道典型的union find的题目,我这里给出两种解法,一种DFS的解法,一种union find的解法。

DFS做法的思路是创建一个长度为M的数组,记录每个人是否被visit过,如果某个人没有被visit过,则对其做DFS遍历,同时count++,因为即使这个人没有朋友,他也可以自成一个朋友圈。DFS递归调用的时候,判断当前的人i和M中所有人的关系,并且记得每当遍历过一个人的时候,先把他mark成visit过,否则会stack overflow。

时间O(n^2)

空间O(n)

Java实现

 1 class Solution {
 2     public int findCircleNum(int[][] M) {
 3         // 记录每个人是否被visit过
 4         int[] visited = new int[M.length];
 5         int count = 0;
 6         for (int i = 0; i < visited.length; i++) {
 7             if (visited[i] == 0) {
 8                 dfs(M, visited, i);
 9                 count++;
10             }
11         }
12         return count;
13     }
14 
15     // 对于某一个人i 去看他跟每个人的关系 如果(i, j)是朋友且j没有被访问过 则递归去看j跟所有人的朋友关系
16     private void dfs(int[][] M, int[] visited, int i) {
17         for (int j = 0; j < M.length; j++) {
18             if (M[i][j] == 1 && visited[j] == 0) {
19                 visited[j] = 1;
20                 dfs(M, visited, j);
21             }
22         }
23     }
24 }

Union Find的做法需要依赖一个class。我这里同时提供两个帖子,一个有并查集的图解,一个有并查集的代码模板

时间O(V * E)

空间O(n)

Java实现

 1 class Solution {
 2     class UnionFind {
 3         private int count = 0;
 4         private int[] parent, rank;
 5 
 6         public UnionFind(int n) {
 7             count = n;
 8             parent = new int[n];
 9             rank = new int[n];
10             // 初始化每个节点的父节点是他自己
11             for (int i = 0; i < n; i++) {
12                 parent[i] = i;
13             }
14         }
15 
16         public int find(int p) {
17             while (p != parent[p]) {
18                 parent[p] = parent[parent[p]];
19                 p = parent[p];
20             }
21             return p;
22         }
23 
24         public void union(int p, int q) {
25             int rootP = find(p);
26             int rootQ = find(q);
27             if (rootP == rootQ) {
28                 return;
29             }
30             if (rank[rootQ] > rank[rootP]) {
31                 parent[rootP] = rootQ;
32             } else {
33                 parent[rootQ] = rootP;
34                 if (rank[rootP] == rank[rootQ]) {
35                     rank[rootP]++;
36                 }
37             }
38             count--;
39         }
40 
41         public int count() {
42             return count;
43         }
44     }
45 
46     public int findCircleNum(int[][] M) {
47         int n = M.length;
48         UnionFind uf = new UnionFind(n);
49         for (int i = 0; i < n - 1; i++) {
50             for (int j = i + 1; j < n; j++) {
51                 if (M[i][j] == 1) {
52                     uf.union(i, j);
53                 }
54             }
55         }
56         return uf.count();
57     }
58 }

相关题目

261. Graph Valid Tree

323. Number of Connected Components in an Undirected Graph

547. Friend Circles

LeetCode 题目总结

原文地址:https://www.cnblogs.com/cnoodle/p/12717288.html