-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumberOfConnectedComponents.js
68 lines (55 loc) · 1.66 KB
/
numberOfConnectedComponents.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
// You have a graph of n nodes. You are given an integer n and an array edges where edges[i] = [ai, bi] indicates that there is an edge between ai and bi in the graph.
// Return the number of connected components in the graph.
// Example 1:
// Input: n = 5, edges = [[0,1],[1,2],[3,4]]
// Output: 2
// Example 2:
// Input: n = 5, edges = [[0,1],[1,2],[2,3],[3,4]]
// Output: 1
/**
* @param {number} n
* @param {number[][]} edges
* @return {number}
*/
// Input: Number and an edge list
// Output: Number of components that exist in the graph
// Strategy:
// Convert the number and edges to a graph by transforming into an adjacency list
// Undirected graph so we need to push edges to both nodes
// Iterate through every node of the graph and do a traversal (dfs for this approach)
// After each traversal increment our count as one traversal is a full traversal of the current node
var countComponents = function (n, edges) {
const graph = {};
for (let i = 0; i < n; i++) {
graph[i] = [];
}
for (let edge of edges) {
const [a, b] = edge;
graph[a].push(b);
graph[b].push(a);
}
const visited = new Set();
let componentCount = 0;
for (let node in graph) {
const result = traverse(graph, node, visited);
if (result === true) componentCount += 1;
}
return componentCount;
};
const traverse = (graph, current, visited) => {
if (visited.has(String(current))) return false;
visited.add(String(current));
for (let neighbor of graph[current]) {
traverse(graph, neighbor, visited);
}
return true;
};
console.log(
countComponents(5, [
[0, 1],
[1, 2],
[3, 4],
])
);
// Input: n = 5, edges = [[0,1],[1,2],[3,4]]
// Output: 2