MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / BFS

Method BFS

Java/BfsTraversal.java:22–39  ·  view source on GitHub ↗
(int n)

Source from the content-addressed store, hash-verified

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);

Callers 1

mainMethod · 0.95

Calls 4

addMethod · 0.45
sizeMethod · 0.45
printMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected