-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS.js
48 lines (40 loc) · 1.09 KB
/
DFS.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
//! Depth First Search
let nodes = [
[0, 2], [2, 1], [1, 3], [3, 4], [4, 2]
];
const n = 5; // number of nodes
function DFS_traversal(node,adjList,visited,component ) {
component.push(node);
visited[node] = true;
// Recursive call for all connected nodes, which are not visited
for(el of adjList[node]){
if(!visited[el]){
DFS_traversal(el,adjList,visited,component)
}
}
}
function printNodes(nodes, n) {
let adjList = [];
let visited = [];
let ans = [];
// Initializing adjList and Visited
for (let i = 0; i < n; i++) {
adjList[i] = [];
visited[i] = false;
}
// Creating Adjacency List
for (let node of nodes) {
adjList[node[0]].push(node[1])
adjList[node[1]].push(node[0])
}
// traversing all nodes, covering the case of Discontinued Graph
for (let i = 0; i < n; i++) {
if (!visited[i]) {
let component = [];
DFS_traversal(i, adjList, visited, component)
ans.push(component)
}
}
console.log(ans)
}
printNodes(nodes,n)