/**
Number of Connected Components in an Undirected Graph
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:
0 3
| |
1 --- 2 4
Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], return 2.
Example 2:
0 4
| |
1 --- 2 --- 3
Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [3, 4]], return 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.
*/
import java.util.*;
public class NumberOfConnectedComponents{
/*
-similar to union find
-first each node is on its own component
-as we iterate through the edges and if we see the two nodes of the edge are in different component, we create a new entry to have them in a single component
*/
public static int countComponents(int n, int[][] edges) {
if (n <= 1) {
return n;
}
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
map.put(i, i);
}
for (int[] edge : edges) {
int fa_x = find(map, edge[0]);
int fa_y = find(map, edge[1]);
System.out.println(fa_x +"..."+fa_y+"..."+n);
if (fa_x != fa_y) {
map.put(fa_x, fa_y);
n--;
}
}
return n;
}
static int find(HashMap<Integer, Integer> map, int x) {
int parent_comp = map.get(x);
while (parent_comp != map.get(parent_comp)) {
parent_comp = map.get(parent_comp);
}
return parent_comp;
}
public static void main(String args[]){
int[][] edges = new int[4][2];
edges[0]= new int[]{0,1};
edges[1]= new int[]{1,2};
edges[2]= new int[]{3,4};
edges[3]= new int[]{0,5};
int comp = countComponents(6, edges);
System.out.println(comp);
}
}
Number of Connected Components in an Undirected Graph
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:
0 3
| |
1 --- 2 4
Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], return 2.
Example 2:
0 4
| |
1 --- 2 --- 3
Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [3, 4]], return 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.
*/
import java.util.*;
public class NumberOfConnectedComponents{
/*
-similar to union find
-first each node is on its own component
-as we iterate through the edges and if we see the two nodes of the edge are in different component, we create a new entry to have them in a single component
*/
public static int countComponents(int n, int[][] edges) {
if (n <= 1) {
return n;
}
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
map.put(i, i);
}
for (int[] edge : edges) {
int fa_x = find(map, edge[0]);
int fa_y = find(map, edge[1]);
System.out.println(fa_x +"..."+fa_y+"..."+n);
if (fa_x != fa_y) {
map.put(fa_x, fa_y);
n--;
}
}
return n;
}
static int find(HashMap<Integer, Integer> map, int x) {
int parent_comp = map.get(x);
while (parent_comp != map.get(parent_comp)) {
parent_comp = map.get(parent_comp);
}
return parent_comp;
}
public static void main(String args[]){
int[][] edges = new int[4][2];
edges[0]= new int[]{0,1};
edges[1]= new int[]{1,2};
edges[2]= new int[]{3,4};
edges[3]= new int[]{0,5};
int comp = countComponents(6, edges);
System.out.println(comp);
}
}
No comments:
Post a Comment