diff --git a/Graphs/dfs.js b/Graphs/dfs.js new file mode 100644 index 0000000..c5615c5 --- /dev/null +++ b/Graphs/dfs.js @@ -0,0 +1,27 @@ +const graph = [ + [0,1,0,1], + [1,0,1,1], + [0,1,0,1], + [1,1,1,0] +] + +const visited = new Array(graph.length).fill(false); +const stack = []; + +const dfs = (node) => { + stack.push(node); + while (stack.length > 0) { + node = stack.pop(); + if (visited[node] === false) { + visited[node] = true; + console.log(`visited node ${node}`) + for (let j = 0; j < graph[node].length; j++) { + if (graph[node][j] === 1){ + stack.push(j); + } + } + } + } +} + +dfs(2); \ No newline at end of file