* Return DFS(Depth First Search) List Using Iteration
(start)
| 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 |
no test coverage detected