-
Notifications
You must be signed in to change notification settings - Fork 14
/
DFS.js
32 lines (27 loc) · 892 Bytes
/
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
const { createGraph } = require("./index");
const { maxVertices, adjacencyList } = createGraph([
[1, 0],
[2, 0],
[3, 1],
[3, 2],
]);
console.log(dfs(0));
function dfs(vertex) {
// initialize the visited array with false.
let visited = new Array(maxVertices).fill(false);
dfsUtil(vertex, visited);
// a recursive utility function to explore all adjacent vertices of the 'vertex'
function dfsUtil(vertex, visited) {
// mark the current vertex as visited and print it
visited[vertex] = true;
console.log(vertex);
// get all neighbours of the current vertex
const neighbours = adjacencyList.get(vertex);
// now explore each and every vertex recursively until all vertices are visited.
for (const neighbour of neighbours) {
if (!visited[neighbour]) {
dfsUtil(neighbour, visited);
}
}
}
}