MCPcopy
hub / github.com/TheAlgorithms/JavaScript / DFSIterative

Method DFSIterative

Data-Structures/Graph/Graph3.js:59–81  ·  view source on GitHub ↗

* Return DFS(Depth First Search) List Using Iteration

(start)

Source from the content-addressed store, hash-verified

57 * Return DFS(Depth First Search) List Using Iteration
58 */
59 DFSIterative(start) {
60 if (!start) return null
61
62 const stack = [start]
63 const visited = {}
64 visited[start] = true
65
66 const result = []
67 let currentVertex
68
69 while (stack.length) {
70 currentVertex = stack.pop()
71 result.push(currentVertex)
72
73 this.adjacencyObject[currentVertex].forEach((neighbor) => {
74 if (!visited[neighbor]) {
75 visited[neighbor] = true
76 stack.push(neighbor)
77 }
78 })
79 }
80 return result
81 }
82
83 BFS(start) {
84 if (!start) return null

Callers 1

Graph3.test.jsFile · 0.45

Calls 2

popMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected