-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDFS.java
More file actions
44 lines (38 loc) · 1.08 KB
/
Copy pathDFS.java
File metadata and controls
44 lines (38 loc) · 1.08 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
44
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class DFS {
private int V;
private List<Integer>[] graph;
public DFS(int n){
V = n;
graph = new LinkedList[10];
for(int i=0; i<graph.length; i++){
graph[i] = new LinkedList<Integer>();
}
}
public void addEdge(int u, int v){
graph[u].add(v);
}
public void DFS(int s){
boolean visited[] = new boolean[graph.length];
DFSrec(s, visited);
}
private void DFSrec(int v, boolean[] visited){
visited[v] = true;
System.out.println(v + " ");
ListIterator<Integer> iterator = graph[v].listIterator();
while(iterator.hasNext()){
int n = iterator.next();
if(!visited[n])
DFSrec(n, visited);
}
}
public static void main(String[]args){
DFS graph = new DFS(3);
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 2);
graph.DFS(0);
}
}