-
Notifications
You must be signed in to change notification settings - Fork 154
/
number-of-connected-components-in-an-undirected-graph.js
168 lines (143 loc) · 3.03 KB
/
number-of-connected-components-in-an-undirected-graph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* 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.
*/
/**
* DFS Solution
*
* @param {number} n
* @param {number[][]} edges
* @return {number}
*/
export const countComponentsDFS = (n, edges) => {
const adjList = buildGraph(n, edges);
// Traverse all the nodes
const visited = new Set();
let count = 0;
for (let i = 0; i < n; i++) {
if (!visited.has(i)) {
dfs(adjList, i, visited);
count++;
}
}
return count;
};
/**
* BFS Solution
*
* @param {number} n
* @param {number[][]} edges
* @return {number}
*/
export const countComponentsBFS = (n, edges) => {
const adjList = buildGraph(n, edges);
// Traverse all the nodes
const visited = new Set();
let count = 0;
for (let i = 0; i < n; i++) {
if (!visited.has(i)) {
bfs(adjList, i, visited);
count++;
}
}
return count;
};
/**
* Union-Find Solution
*
* @param {number} n
* @param {number[][]} edges
* @return {number}
*/
export const countComponentsUnionFind = (n, edges) => {
const nums = Array(n).fill(-1);
// Step 1. union find
const find = i => {
if (nums[i] === -1) {
return i;
}
return find(nums[i]);
};
for (let i = 0; i < edges.length; i++) {
const x = find(edges[i][0]);
const y = find(edges[i][1]);
// Union
if (x !== y) {
nums[x] = y;
}
}
// Step 2. count the -1
return nums.filter(num => num === -1).length;
};
/**
* DFS tarverse the graph
*
* @param {Map} adjList
* @param {number} u
* @param {Set} visited
*/
const dfs = (adjList, u, visited) => {
visited.add(u);
adjList.get(u).forEach(v => {
if (!visited.has(v)) {
dfs(adjList, v, visited);
}
});
};
/**
* BFS traverse the graph
*
* @param {map} adjList
* @param {number} node
* @param {Set} visited
*/
const bfs = (adjList, node, visited) => {
const queue = [node];
visited.add(node);
while (queue.length > 0) {
const u = queue.shift();
adjList.get(u).forEach(v => {
if (!visited.has(v)) {
queue.push(v);
visited.add(v);
}
});
}
};
/**
* Build the graph using adjacency list
*
* @param {number} n
* @param {number[][]} edges
*/
const buildGraph = (n, edges) => {
const adjList = new Map();
for (let i = 0; i < n; i++) {
adjList.set(i, []);
}
edges.forEach(edge => {
const u = edge[0];
const v = edge[1];
adjList.get(u).push(v);
adjList.get(v).push(u);
});
return adjList;
};