(int source)
| 29 | |
| 30 | |
| 31 | public void DFS(int source) { |
| 32 | //if the node is already visited, then backtrack. |
| 33 | if(this.visited[source] == true) { |
| 34 | return; |
| 35 | } |
| 36 | //mark the source vertex as visited |
| 37 | System.out.print(source+" "); |
| 38 | this.visited[source] = true; |
| 39 | |
| 40 | //traverse through all the neighbours and backtrack if already visited. |
| 41 | LinkedList<Integer> neighbours = this.adj[source]; |
| 42 | |
| 43 | for(int next : neighbours) { |
| 44 | DFS(next); |
| 45 | } |
| 46 | |
| 47 | } |
| 48 | |
| 49 | } |
| 50 |