| 27 | } |
| 28 | |
| 29 | const depthFirstSearchVisit = (vertex, color, adjList, callback) => { |
| 30 | color[vertex] = Colors.GREY; // Mark as discovered |
| 31 | if (callback) { |
| 32 | callback(vertex); |
| 33 | } |
| 34 | const neighbors = adjList.get(vertex); |
| 35 | for (let i = 0; i < neighbors.length; i++) { |
| 36 | const neighbor = neighbors[i]; |
| 37 | if (color[neighbor] === Colors.WHITE) { // If unvisited - recursive call |
| 38 | depthFirstSearchVisit(neighbor, color, adjList, callback); |
| 39 | } |
| 40 | } |
| 41 | color[vertex] = Colors.BLACK; // Mark as explored |
| 42 | } |
| 43 | |
| 44 | const enhancedDepthFirstSearch = (graph, callback) => { |
| 45 | const vertices = graph.vertices; |