Implementing Graph Traversing Algorithms in Java

Graph traversal is the problem of visiting all the nodes in a graph in a particular manner, updating and/or checking their values along the way. There are two methods for graph traversal, they are as follows.

  1. Depth-first search: DFS visits the child nodes before visiting the sibling nodes; that is, it traverses the depth of any particular path before exploring its breadth.
  2. Breadth-first search: FS visits the parent nodes before visiting the child nodes.


Here is java implementation of both methods:

package graphtraversal;
import java.io.*;
class Quec
{
public int front,rear,n,que[];
public Quec()
{
n=20;
que=new int[20];
front=0;
rear=-1;
}
public boolean isFull()
{
if(rear==n-1)
return true;
else
return false;
}
public void enque(int x)
{
if(isFull()==true)
System.out.println(“No Space”);
else
{
rear++;
que[rear]=x;
}
}
public void display()
{
if(isEmpty()==true)
System.out.println(“Queue is empty”);
else
{
for(int i=front;i<=rear;i++) { System.out.print(que[i]+" "); } } } public boolean isEmpty() { if(rear

Leave a Reply

Your email address will not be published. Required fields are marked *