* Return DFS (Depth First Search) List Using Recursive Method
(start)
| 32 | * Return DFS (Depth First Search) List Using Recursive Method |
| 33 | */ |
| 34 | DFS(start) { |
| 35 | if (!start) return null |
| 36 | |
| 37 | const result = [] |
| 38 | const visited = {} |
| 39 | const adjacencyObject = this.adjacencyObject |
| 40 | |
| 41 | function dfs(vertex) { |
| 42 | if (!vertex) return null |
| 43 | visited[vertex] = true |
| 44 | result.push(vertex) |
| 45 | adjacencyObject[vertex].forEach((neighbor) => { |
| 46 | if (!visited[neighbor]) { |
| 47 | dfs(neighbor) |
| 48 | } |
| 49 | }) |
| 50 | } |
| 51 | |
| 52 | dfs(start) |
| 53 | return result |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Return DFS(Depth First Search) List Using Iteration |