323. Number of Connected Components in an Undirected Graph

323. Number of Connected Components in an Undirected Graph


// dfs 

class Solution{
  public int countComponents(int n, int[][] edges){
    // int[] , default means unexplored is 0, 1 means visited 
    int count = 0;
    List<List<Integer>> list = new ArrayList<>();
    for(int i = 0; i < n; i++){
      list.add(new ArrayList<>());
    }

    for(int i = 0; i < edges.length; i++){
      list.get(edges[i][0]).add(edges[i][1]);
      list.get(edges[i][1]).add(edges[i][0]);
    }
    
    
    int[] visited = new int[n]; // default value is 0, unvisited . 1 means visited 
    
    for(int i = 0; i < n; i++){
      if(visited[i] == 0){
        count++;
        dfs(i, list, visited);
      }
    }
    return count;
  }
  
  private void dfs(int i, List<List<Integer>> list, int[] visited){
    visited[i] = 1;
    for(int nei : list.get(i)){
      if(visited[nei] == 0){
        dfs(nei, list, visited);
      }
    }
  }
}



//// bfs 
class Solution{
  public int countComponents(int n, int[][] edges){
    // int[] , default means unexplored is 0, 1 means visited 
    int count = 0;
    int[] visited = new int[n];
    List<List<Integer>> list = new ArrayList<>();
    for(int i = 0; i < n; i++){
      list.add(new ArrayList<>());
    }
    
    for(int[] edge : edges){
      list.get(edge[0]).add(edge[1]);
      list.get(edge[1]).add(edge[0]);
    }
    
    for(int i = 0; i < n; i++){
      if(visited[i] == 0){
        count++;
        bfs(i, visited, list);
      }
    }
    return count;
  }
  private void bfs(int i, int[] visited, List<List<Integer>> list){
    visited[i] = 1;
    Queue<Integer> queue = new LinkedList<>();
    queue.offer(i);
    while(!queue.isEmpty()){
      int cur = queue.poll();
      for(int nei : list.get(cur)){
        if(visited[nei] != 1){
          visited[nei] = 1;
          queue.offer(nei);
        }
      }
    }
  }
}

 

// uf

public class Solution {
    
    public int countComponents(int n, int[][] edges) {
        if (n <= 1) {
            return n;
        }
        int[] roots = new int[n];
        for (int i = 0; i < n; i++) {
            roots[i] = i;
        }
        for (int[] edge : edges) {
            int x = find(roots, edge[0]);
            int y = find(roots, edge[1]);
            if (x != y) {
                roots[x] = y;
                n--;
            }
        }
        
        return n;
    }
    
    public int find(int[] roots, int id) {
        int x = id;
        while (roots[id] != id) {
            id = roots[id];
        }
        while (roots[x] != id) {
            int fa = roots[x];
            roots[x] = id;
            x = fa;
        }
        
        return id;
    }
}

 

Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to find the number of connected components in an undirected graph.

Example 1:

Input: n = 5 and edges = [[0, 1], [1, 2], [3, 4]]

     0          3
     |          |
     1 --- 2    4 

Output: 2

Example 2:

Input: n = 5 and edges = [[0, 1], [1, 2], [2, 3], [3, 4]]

     0           4
     |           |
     1 --- 2 --- 3

Output:  1

Note:
You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.

posted on 2018-08-09 17:52  猪猪&#128055;  阅读(98)  评论(0)    收藏  举报

导航