* Prints the Depth first traversal of the graph from source. * @param {number} source The source vertex to start DFS.
(source, visited = new Set(), output = (value) => console.log(value))
| 58 | * @param {number} source The source vertex to start DFS. |
| 59 | */ |
| 60 | dfs(source, visited = new Set(), output = (value) => console.log(value)) { |
| 61 | if (visited.has(source)) { |
| 62 | // visited |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | output(`Visited node ${source}`) |
| 67 | visited.add(source) |
| 68 | for (const neighbour of this.adjacencyMap[source]) { |
| 69 | this.dfs(neighbour, visited, output) |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | const example = () => { |