-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS_.java
34 lines (27 loc) · 842 Bytes
/
BFS_.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
import java.util.*;
import java.io.*;
class Solution{
public ArrayList<Integer> BfsOfGraph(int v, ArrayList<ArrayList<Integer>> adj){
ArrayList<Integer> bfs=new ArrayList<Integer>();
boolean vis[]=new boolean[v+1];
for(int i=1;i<=v;i++){
if(vis[i]==false){
Queue<Integer> q=new LinkedList<>();
q.add(i);
vis[i]=true;
while(!q.isEmpty())
{
Integer node =q.poll();
bfs.add(node);
for(Integer it: adj.get(node)){
if(vis[it]==false){
vis[it] = true;
q.add(it);
}
}
}
}
}
return bfs;
}
}