-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathDirected graph BFS and DFS.java
123 lines (116 loc) · 2.99 KB
/
Directed graph BFS and 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//Program for Implementation of Breadth First Search and Depth First Search of Directed Graph.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main
{
public static int V=0,E=0;//vertices and edges
public static ArrayList<ArrayList<Integer> >graph;//Graph DS
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void addEdge(int iniV,int finV){
if(iniV>=0&&finV>=0&&iniV<V&&finV<V){
graph.get(iniV).add(finV);
}
}
public static void bfs(int src,boolean[] visited){
if(src<0||src>=V||visited[src])
return;
visited[src]=true;
Queue<Integer> q=new LinkedList<Integer>();
q.add(src);
while(!q.isEmpty()){
int currV=q.poll();
System.out.print(currV+"->");
for(int i=0;i<graph.get(currV).size();i++){
if(!visited[graph.get(currV).get(i)]){
q.add(graph.get(currV).get(i));
visited[graph.get(currV).get(i)]=true;
}
}
}
System.out.println("null");
}
public static void dfs(int src,boolean[] visited){
if(src<0||src>=V||visited[src])
return;
visited[src]=true;
System.out.print(src+"->");
for(int i=0;i<graph.get(src).size();i++){
if(!visited[graph.get(src).get(i)])
dfs(graph.get(src).get(i),visited);
}
}
public static void main(String[] args)
{
FastReader scan=new FastReader();
V=scan.nextInt();//v=vertices ...0 based indexing
E=scan.nextInt();//e=edges;
graph=new ArrayList<ArrayList<Integer>>();//graph
for(int i=0;i<V;i++)
graph.add(new ArrayList<Integer>());
for(int i=1;i<=E;i++){
addEdge(scan.nextInt(),scan.nextInt());//add edge between two vertices
}
int choice=scan.nextInt();// 0) DFS , 1) BFS
int src=scan.nextInt();//enter source of traversal
if(choice==0){
System.out.println("BFS of directed graph starts here:");
bfs(src,new boolean[V]);
}
else{
System.out.println("DFS of directed graph starts here:");
dfs(src,new boolean[V]);
System.out.println("null");
}
}
}