-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs.java
83 lines (77 loc) · 2.05 KB
/
dfs.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.ArrayList;
import java.util.List;
class Node{
public int data;
public List<Node> edges;
private boolean visited;
public Node(int data){
this.data=data;
this.edges=new ArrayList<>();
this.visited=false;
}
public void addEdge(Node adj){
this.edges.add(adj);
// undirected graph
// adj.edges.add(this);
}
public void visit(){
this.visited=true;
}
public boolean isVisited(){
return this.visited;
}
}
class Graph{
public List<Node> nodes;
public Graph(List<Node> nodes){
this.nodes=nodes;
}
}
public class dfs{
private static List<Node> visited=new ArrayList<>();
private static void visit(Node n){
n.visit();
//visited.add(n);
System.out.println(n.data);
}
public static void depthFirstSearch(Node root){
if(root == null)
return;
visit(root);
for (Node adj : root.edges) {
if(!adj.isVisited()){
depthFirstSearch(adj);
}
}
}
public static void bfs(Node root){
List<Node> queue=new ArrayList<>();
visit(root);
queue.add(root);
while(queue.size() != 0){
Node visiting=queue.remove(0);
for (Node adj : visiting.edges) {
if(!adj.isVisited()){
visit(adj);
queue.add(adj);
}
}
}
}
public static void main(String[] args) {
List<Node> nodes= new ArrayList<Node>();
for(int i=0;i<6;i++){
nodes.add(new Node(i));
}
nodes.get(0).addEdge(nodes.get(3));
nodes.get(1).addEdge(nodes.get(2));
nodes.get(3).addEdge(nodes.get(1));
nodes.get(2).addEdge(nodes.get(3));
nodes.get(3).addEdge(nodes.get(4));
nodes.get(4).addEdge(nodes.get(2));
Graph g= new Graph(nodes);
// depthFirstSearch(nodes.get(0));
System.out.println("BFS");
bfs(nodes.get(0));
}
}