Advertisement
1988coder

323. Number of Connected Components in an Undirected Graph

Jan 19th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.26 KB | None | 0 0
  1.  
  2. // LeetCode URL: https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5.  
  6. /**
  7.  * Using DFS
  8.  *
  9.  * Time Complexity: O(N + E)
  10.  *
  11.  * Space Complexity: O(N + E)
  12.  *
  13.  * N = Number of vertices in the graph. E = Number of edges in the graph.
  14.  */
  15. class Solution1 {
  16.     public int countComponents(int n, int[][] edges) {
  17.         if (edges == null || n <= 0) {
  18.             return 0;
  19.         }
  20.         if (n == 1 || edges.length == 0) {
  21.             return n;
  22.         }
  23.  
  24.         HashMap<Integer, HashSet<Integer>> graph = buildGraph(n, edges);
  25.         HashSet<Integer> visited = new HashSet<>();
  26.  
  27.         int count = 0;
  28.         for (int i = 0; i < n; i++) {
  29.             if (!visited.contains(i)) {
  30.                 dfsHelper(graph, i, visited);
  31.                 count++;
  32.             }
  33.         }
  34.  
  35.         return count;
  36.     }
  37.  
  38.     private void dfsHelper(HashMap<Integer, HashSet<Integer>> graph, int node, HashSet<Integer> visited) {
  39.         if (visited.contains(node)) {
  40.             return;
  41.         }
  42.         visited.add(node);
  43.         for (int neigh : graph.get(node)) {
  44.             if (!visited.contains(neigh)) {
  45.                 dfsHelper(graph, neigh, visited);
  46.             }
  47.         }
  48.     }
  49.  
  50.     private HashMap<Integer, HashSet<Integer>> buildGraph(int n, int[][] edges) {
  51.         HashMap<Integer, HashSet<Integer>> graph = new HashMap<>();
  52.         for (int i = 0; i < n; i++) {
  53.             graph.put(i, new HashSet<>());
  54.         }
  55.         for (int[] e : edges) {
  56.             graph.get(e[0]).add(e[1]);
  57.             graph.get(e[1]).add(e[0]);
  58.         }
  59.         return graph;
  60.     }
  61. }
  62.  
  63. /**
  64.  * Using Union Find
  65.  *
  66.  * Time Complexity: O(N + E). N to create the parents array. E to perform union
  67.  * E times.
  68.  *
  69.  * Space Complexity: O(N)
  70.  *
  71.  * N = Number of vertices in the graph. E = Number of edges in the graph.
  72.  */
  73. class Solution2 {
  74.     public int countComponents(int n, int[][] edges) {
  75.         if (edges == null || n <= 0) {
  76.             return 0;
  77.         }
  78.         if (n == 1 || edges.length == 0) {
  79.             return n;
  80.         }
  81.  
  82.         UnionFind graph = new UnionFind(n);
  83.  
  84.         int count = n;
  85.         for (int[] e : edges) {
  86.             if (graph.union(e[0], e[1])) {
  87.                 count--;
  88.             }
  89.         }
  90.         return count;
  91.     }
  92.  
  93.     class UnionFind {
  94.         int[] parents;
  95.         int[] ranks;
  96.  
  97.         UnionFind(int n) {
  98.             ranks = new int[n];
  99.             parents = new int[n];
  100.             for (int i = 0; i < n; i++) {
  101.                 parents[i] = i;
  102.             }
  103.         }
  104.  
  105.         public int find(int i) {
  106.             if (i == parents[i]) {
  107.                 return i;
  108.             }
  109.             parents[i] = find(parents[i]);
  110.             return parents[i];
  111.         }
  112.  
  113.         public boolean union(int a, int b) {
  114.             int x = find(a);
  115.             int y = find(b);
  116.  
  117.             if (x == y) {
  118.                 return false;
  119.             }
  120.  
  121.             if (ranks[x] >= ranks[y]) {
  122.                 ranks[x] = ranks[x] == ranks[y] ? ranks[x] + 1 : ranks[x];
  123.                 parents[y] = x;
  124.             } else {
  125.                 parents[x] = y;
  126.             }
  127.             return true;
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement