-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.java
More file actions
64 lines (59 loc) · 1.59 KB
/
Copy pathBFS.java
File metadata and controls
64 lines (59 loc) · 1.59 KB
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
package algorithm.graph.BFS;
import java.util.*;
/**
* Input:
* 0
* / | \
* 1 2 3
* |
* 4
* Output: 0 1 2 3 4
* Explanation:
* 0 is connected to 1 , 2 , 3.
* 2 is connected to 4.
* so starting from 0, it will go to 1 then 2
* then 3.After this 2 to 4, thus bfs will be
* 0 1 2 3 4.
*/
class BFS {
public static void main(String[] args) {
Solution obj = new Solution();
// obj.bfsOfGraph(V, adj);
}
}
class Solution {
public ArrayList<Integer> bfsOfGraph(int V, ArrayList<ArrayList<Integer>> adj) {
// visiting guy
boolean[] vis = new boolean[V];
// we dont need to initialize visited array, it is not required
for (int i = 0; i<V; i++) {
vis[i] = false;
}
ArrayList<Integer> res = new ArrayList<>();
Queue<Integer> queue = new LinkedList<>();
//make first visited
vis[0] = true;
queue.add(0);
while (!queue.isEmpty()) {
int node = queue.peek();
res.add(node);
queue.poll();
// Iterator<Integer> i = adj.get(node).listIterator();
// while (i.hasNext()) {
// int n = i.next();
// if(!vis[n]) {
// vis[n] = true;
// queue.add(n);
// }
// }
//enhanced for loop
for (int it : adj.get(node)) {
if (!vis[it]) {
vis[it] = true;
queue.add(it);
}
}
}
return res;
}
}