Skip to main content

Breadth First Search Implementation in Java

Breadth First Search Implementation in Java

For Graph Class Design and DFS implementation please refer to below link:-



1. Pick a vertex.
2. Get Adjacent Vertex.
3. Insert in queue ( at tail)
4. Remove vertex from queue( head)
5. Do until no vertex left in a queue.


void searchBFS()
//this is how we define queue using LinkedList
  Queue<Character> queue=new LinkedList<Character>();
                 //element at head (Function in main graph class please use above link)
  ArrayList adjvertex=getNeighbours(queue.peek());
              //element removed at head
  for(int i=0;i<adjvertex.size();i++)
               //element added at tail  
   queue.add((Character) adjvertex.get(i));



Popular posts from this blog

Solved: The index 1 is out of range.

This error usually comes when we try to insert data in a query where there is no index defined for it. Example :- String strQuery=“select * from location where city_id=? ”; The question mark will be the the first index if we want to insert data so if we call a function like- oPreparedStatement = oConnection.prepareStatement(strQuery); oPreparedStatement.setString(1,235); here we are sending 235 as a first parameter so it will work fine but as soon as we write something after it like oPreparedStatement.setString(2,”kanpur”) then it will throw “The index 2 is out of range” since there is no place to send this value in a query hence it will throw the same error. Here index defines the parameter for which there is no place in the query. To rectify this we need to write query like- String strQuery=“select * from location where city_id=? And city_name=? ”; then it will work fine. The cases in which these errors can occur is- 1)Query is co