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

Adding Node at a given position in Linked List

Adding Node at a given position in Linked List

publicvoidaddNodeAtPosition(Node head,int position,int data){ Node temp=head;//create a node Node node=new Node(data);int c=1;//while counter is not equal to the positionwhile(c!=position){; c++;}//point node to the next of current node;//point next of current to the node to insert;}To Know the basic structure of the linked list click here