Example Analysis of js depth-first traversal
Xiaobian to share with you js depth priority traversal example analysis, I believe most people still do not know how, so share this article for your reference, I hope you read this article after a great harvest, let us go to understand it!
Search branches of the graph as deeply as possible. Conventional depth-first representation does not destroy the original data structure, but rather is represented by iVisited or color notation.
2. Visit the root node and traverse the unvisited adjacent nodes of the root node one by one.
examples
const graph = { 0: [1, 2], 1: [2], 2: [0, 3], 3: [3],};//depth-first traversal of graph const visited = new Set();const dfs = (n) => { console.log(n); visited.add(n); graph[n].forEach((c) => { if (! visited.has(c)) { dfs(c); } });}; dfs(2); // 2 0 1 3 above is "js depth-first traversal example analysis" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!