-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphDFS_Disconnected.java
More file actions
36 lines (30 loc) · 969 Bytes
/
GraphDFS_Disconnected.java
File metadata and controls
36 lines (30 loc) · 969 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
30
31
32
33
34
35
36
import java.util.ArrayList;
public class GraphDFS_Disconnected {
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;
}
for(int i = 0; i < V; i++) {
if(mark[i] == false) {
DFSRec(adj, i, mark);
}
}
}
public static void main(String[] args) {
}
}