Breadth-first search systematically explores the edges of G graph to discover every vertex that is reachable from starting node S. The main purpose of BFS is to calculate the distance from S to each reachable vertex. If we are given two arbitrary nodes and asked for the distance between them, then the BFS is the standard solution. The algorithm works for both directed and un-directed graphs.

# Pseudo-code and step-by-step representation from Cormen Book G = (V, E) is represented using adjacency lists. It attaches several additional attributes to each vertex in the graph. Each vertex color is stored at u.color and the predcessor of u in the attributed u.p. If u has no predcessor then u.p = NIL. The attribute u.d holds the distance from the source s to vertex u computed by the algorithm. FIFO queue Q is used to manage the set of gray vertices. • White color - not visited nodes
• Gray color - about to be visited (keeping them in FIFO queue)
• Black color - visited nodes

Intitialization with unlimited signature is needed to tell that node is impossible to reach from the starting s node. At the end of the BFS if still see some unlimited signs then there is no path from s to that node.

# Implementation in Java

There are many implementations of BFS online. Here I would like to introduce the BFS that prints the levels while performs search. Even though the source code is little long, it is quite primitive.

``````public class BfsWithLevels {

private int numOfVertices;

public BfsWithLevels(int n) {
numOfVertices = n;
for (int i = 0; i < numOfVertices; i++) {
}
}

public void addEdge(int v, int w) {
}

void BFS(int s) {
boolean visited[] = new boolean[numOfVertices];

visited[s] = true;

boolean nextLineFlag = false;
int levels = 1;

System.out.print(levels + ") ");
while (queue.size() != 0) {
int curNode = queue.poll();
System.out.print(curNode + " ");

for (int i = 0; i < curNodeNeighborsSize ; i++) {
if (!visited[neighbor]) {
visited[neighbor] = true;
nextLineFlag = true;
}
}
if (nextLineFlag) {
nextLineFlag = false;

System.out.println();
levels++;
System.out.print(levels + ") ");
}
}
}

public static void main(String args[])
{
BfsWithLevels g = new BfsWithLevels(8);