| 20 | } |
| 21 | |
| 22 | void BFS(int n) { |
| 23 | boolean nodes[] = new boolean[node]; /* initialize boolean array for holding the data */ |
| 24 | int a = 0; |
| 25 | nodes[n] = true; |
| 26 | que.add(n); /* root node is added to the top of the queue */ |
| 27 | while (que.size() != 0) { |
| 28 | n = que.poll(); /* remove the top element of the queue */ |
| 29 | System.out.print(n + " "); /* print the top element of the queue */ |
| 30 | for (int i = 0; i < adj[n].size(); i++) { |
| 31 | a = adj[n].get(i); |
| 32 | if (!nodes[a]) /* only insert nodes into queue if they have not been explored already */ |
| 33 | { |
| 34 | nodes[a] = true; |
| 35 | que.add(a); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | public static void main(String args[]) { |
| 42 | Scanner sc = new Scanner(System.in); |