-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfsdfs.java
73 lines (67 loc) · 1.56 KB
/
bfsdfs.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
import java.util.*;
public class Main
{
private int V;
private LinkedList<Integer> [] adjList;
public Main(int V)
{
this.V = V;
adjList = new LinkedList[V];
for(int i=0;i<V;++i)
{
adjList[i] = new LinkedList<>();
}
}
void addEdge(int x,int y)
{
adjList[x].add(y);
adjList[y].add(x);
}
void DFS(int v)
{
boolean [] visited = new boolean[V];
DFSUtil(v,visited);
}
void DFSUtil(int v,boolean [] visited)
{
visited[v] = true;
System.out.print(v + " ");
for (int n:adjList[v])
{
if (!visited[n])
{
DFSUtil(n,visited);
}
}
}
void BFS(int v)
{
boolean [] visited = new boolean[V];
LinkedList<Integer> queue = new LinkedList<>();
visited[v] = true;
queue.add(v);
while (!queue.isEmpty())
{
int currIndex = queue.poll();
System.out.print(currIndex+" ");
for (int n:adjList[currIndex])
{
if (!visited[n])
{
visited[n] = true;
queue.add(n);
}
}
}
}
public static void main(String[] args) {
Main graph = new Main(4);
graph.addEdge(0,1);
graph.addEdge(1,2);
graph.addEdge(2,3);
graph.addEdge(3,0);
graph.DFS(0);
System.out.println();
graph.BFS(0);
}
}