-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBFSindex1.java
More file actions
35 lines (30 loc) · 779 Bytes
/
BFSindex1.java
File metadata and controls
35 lines (30 loc) · 779 Bytes
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
class Solution{
public ArrayList<Integer> bfsOfGraph(int V, ArrayList<ArrayList<Integer>>adj)
{
ArrayList<Integer> bfs = new ArrayList<>();
boolean visited[] = new boolean[V+1];
for(int i=1; i<=V; i++)
{
if(visited[i] == false)
{
Queue<Integer> q = new LinkedList<>();
q.add(i);
visited[i] = true;
while(!q.isEmpty())
{
Integer node = q.poll();
bfs.add(node);
for(Integer it : adj.get(node))
{
if(visited[it] == false)
{
visited[it] = true;
q.add(it);
}
}
}
}
}
return bfs;
}
}