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

Deleting a Node by passing data in LInkedList

Deleting a Node by passing data in LInkedList:-

The node can be deleted by passing the data value in a function.Here we have three pointers one to traverse the list other to point current node and third one to point previous of current node.

//deleting a given keypublic Node deleteKeyNode(Node head,int key){//traverse pointer Node temp=head;//previous pointer Node prevtemp=temp;while(temp!=null){//when key is at head positionif({; System.out.println("head is deleted");return head;}//when key is at any position other than headif({;; temp=prevtemp;} prevtemp=temp;;}return head;}