-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphDFS.java
More file actions
43 lines (38 loc) · 1.18 KB
/
Copy pathGraphDFS.java
File metadata and controls
43 lines (38 loc) · 1.18 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.ArrayList;
public class GraphDFS {
static void addEdge(ArrayList<ArrayList<Integer>> adj, int u, int v) {
// for undirected connect u and v and then v and u
adj.get(u).add(v);
adj.get(v).add(u);
}
static void DFSRec(ArrayList<ArrayList<Integer>> adj, int s, boolean[] mark) {
mark[s] = true;
System.out.print(s + ",");
for(int u : adj.get(s)) {
if(mark[u] == false) {
DFSRec(adj, u, mark);
}
}
}
static void DFS(ArrayList<ArrayList<Integer>> adj, int V, int s) {
boolean[] mark = new boolean[V];
for(int i = 0; i < V; i++) {
mark[i] = false;
}
DFSRec(adj, s, mark);
}
public static void main(String[] args) {
int V = 7;
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
for(int i = 0; i < V; i++) {
adj.add(new ArrayList<>());
}
addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 2, 3);
addEdge(adj, 1, 3);
addEdge(adj, 1, 4);
addEdge(adj, 3, 4);
DFS(adj, V, 0);
}
}