-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS.java
More file actions
29 lines (22 loc) · 702 Bytes
/
Copy pathDFS.java
File metadata and controls
29 lines (22 loc) · 702 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package algorithm.graph.DFS;
import java.util.ArrayList;
public class DFS {
static void dfsUtils( int start, boolean[] vis,
ArrayList<ArrayList<Integer>> adj,
ArrayList<Integer> res) {
vis[start] = true;
res.add(start);
for(int it : adj.get(start)) {
if(!vis[it]) {
dfsUtils(it, vis, adj, res);
}
}
}
public ArrayList<Integer> dfsOfGraph(int V, ArrayList<ArrayList<Integer>> adj) {
// Code here
boolean[] vis = new boolean[V];
ArrayList<Integer> res = new ArrayList<>();
dfsUtils(0, vis, adj, res);
return res;
}
}