(int s)
| 21 | |
| 22 | // BFS algorithm |
| 23 | void BFS(int s) { |
| 24 | |
| 25 | boolean visited[] = new boolean[V]; |
| 26 | |
| 27 | LinkedList<Integer> queue = new LinkedList(); |
| 28 | |
| 29 | visited[s] = true; |
| 30 | queue.add(s); |
| 31 | |
| 32 | while (queue.size() != 0) { |
| 33 | s = queue.poll(); |
| 34 | System.out.print(s + " "); |
| 35 | |
| 36 | Iterator<Integer> i = adj[s].listIterator(); |
| 37 | while (i.hasNext()) { |
| 38 | int n = i.next(); |
| 39 | if (!visited[n]) { |
| 40 | visited[n] = true; |
| 41 | queue.add(n); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public static void main(String args[]) { |
| 48 | Graph g = new Graph(4); |