LeetCode-547. Friend Circles(JAVA)朋友圈数量

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 adirect friend of B, and B is adirect friend of C, then A is anindirect 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 aredirect 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

错误点:比如一个2*2 矩阵,如果M[0][1],M[1][0]为1,其余两个为0,那么朋友圈数量应该为1,刚开始计算的时候没有考虑到可以传递,导致计算数量增多。

题解:声明一个visited,用于记录遍历过的结点。每次dfs找到一个原矩阵为1的位置(除了对角线),就把这个位置的列数变成行数再dfs,如果是在同一个圈里,最终会绕回已经遍历过的行,visited为true,return 0;如果不是同一个圈,则增加1。(mat[i][j]==1这个判断相当于i的邻接点,深度优先遍历

 
  1. public int findCircleNum(int[][] M) {

  2. if (M == null && M.length == 0)

  3. return 0;

  4. int n = M.length;

  5. boolean[] visited = new boolean[n];

  6. int count = 0;

  7. //如果dfs大于0,说明有未遍历的结点

  8. //只需要遍历所有结点

  9. for (int i = 0; i < n; i++)

  10. if (dfs(M, i, visited) > 0)

  11. count++;

  12. return count;

  13. }

  14.  
  15. private int dfs(int[][] mat, int i, boolean[] visited) {

  16. if (visited[i])

  17. return 0;

  18. visited[i] = true;

  19. int count = 1;

  20. for (int j = 0; j < visited.length; j++)

  21. if (i != j && mat[i][j] == 1)

  22. count += dfs(mat, j, visited);

  23. return count;

  24. }

 算法思想是几次深度优先遍历能遍历所有值为1 的结点,图的连通性。(for 是所有结点,M[i][j]==1这个判断相当于i的邻接点,深度优先遍历)

 
  1. public class Solution {

  2. public void dfs(int[][] M, int[] visited, int i) {

  3. for (int j = 0; j < M.length; j++) {

  4. if (M[i][j] == 1 && visited[j] == 0) {

  5. visited[j] = 1;

  6. dfs(M, visited, j);

  7. }

  8. }

  9. }

  10. public int findCircleNum(int[][] M) {

  11. int[] visited = new int[M.length];

  12. int count = 0;

  13. for (int i = 0; i < M.length; i++) {

  14. if (visited[i] == 0) {

  15. dfs(M, visited, i);

  16. count++;

  17. }

  18. }

  19. return count;

  20. }

  21. }

广度优先遍历:

 
  1. Queue<Integer> q = new LinkedList<>();

  2. public void bfs(int[][] M, int[] visited, int i) {

  3. // visit[i];

  4. q.offer(i);

  5. visited[i] = 1;

  6. while (!q.isEmpty()) {

  7. int node = q.poll();

  8. for (int j = 0; j < M.length; j++) {

  9. // 未被访问过且是邻接点,注意是node的邻接点

  10. if (visited[j] == 0 && M[node][j] == 1) {

  11. // visit[j];

  12. q.offer(j);

  13. visited[j] = 1;

  14. }

  15. }

  16. }

  17.  
  18. }

  19. public int findCircleNum(int[][] M) {

  20. int[] visited = new int[M.length];

  21. int count = 0;

  22. for (int i = 0; i < M.length; i++) {

  23. if (visited[i] == 0) {

  24. bfs(M, visited, i);

  25. count++;

  26. }

  27. }

  28. return count;

  29. }

猜你喜欢

转载自blog.csdn.net/kingmax54212008/article/details/81916560