There are two ways to represent a graph. Using Neighbours list Using Adjacency Matrix

Using Neighbours list

In this, you can have List<Node> as neighbours in Node class as below.

``````import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

{

private Queue<Node> queue;
static ArrayList<Node> nodes=new ArrayList<Node>();
static class Node
{
int data;
boolean visited;
List<Node> neighbours;

Node(int data)
{
this.data=data;
this.neighbours=new ArrayList<>();

}
{
}
public List<Node> getNeighbours() {
return neighbours;
}
public void setNeighbours(List<Node> neighbours) {
this.neighbours = neighbours;
}
}

{
}

public void bfs(Node node)
{
node.visited=true;
while (!queue.isEmpty())
{

Node element=queue.remove();
System.out.print(element.data + "t");
List<Node> neighbours=element.getNeighbours();
for (int i = 0; i < neighbours.size(); i++) {
Node n=neighbours.get(i);
if(n!=null && !n.visited)
{
n.visited=true;

}
}

}
}

public static void main(String arg[])
{

Node node40 =new Node(40);
Node node10 =new Node(10);
Node node20 =new Node(20);
Node node30 =new Node(30);
Node node60 =new Node(60);
Node node50 =new Node(50);
Node node70 =new Node(70);

System.out.println("The BFS traversal of the graph is ");
bfsExample.bfs(node40);

}
}
``````

When you run above program, you will get below output:

``````The BFS traversal of the graph is
40 10 20 30 60 50 70``````

Adjacency_matrix is used to find the connection between two nodes.
if adjacency_matrix[i][j]==1, then nodes at index i and index j are connected

``````import java.util.ArrayList;
import java.util.Queue;

{

private Queue<Node> queue;
static ArrayList<Node> nodes=new ArrayList<Node>();
static class Node
{
int data;
boolean visited;

Node(int data)
{
this.data=data;

}
}

{
}

// find neighbors of node using adjacency matrix
// if adjacency_matrix[i][j]==1, then nodes at index i and index j are connected
{
int nodeIndex=-1;

ArrayList<Node> neighbours=new ArrayList<Node>();
for (int i = 0; i < nodes.size(); i++) {
if(nodes.get(i).equals(x))
{
nodeIndex=i;
break;
}
}

if(nodeIndex!=-1)
{
for (int j = 0; j < adjacency_matrix[nodeIndex].length; j++) {
{
}
}
}
return neighbours;
}

public void bfs(int adjacency_matrix[][], Node node)
{
node.visited=true;
while (!queue.isEmpty())
{

Node element=queue.remove();
System.out.print(element.data + "t");
for (int i = 0; i < neighbours.size(); i++) {
Node n=neighbours.get(i);
if(n!=null && !n.visited)
{
n.visited=true;

}
}

}
}

public static void main(String arg[])
{

Node node40 =new Node(40);
Node node10 =new Node(10);
Node node20 =new Node(20);
Node node30 =new Node(30);
Node node60 =new Node(60);
Node node50 =new Node(50);
Node node70 =new Node(70);

{0,1,1,0,0,0,0}, // Node 1: 40
{0,0,0,1,0,0,0}, // Node 2 :10
{0,1,0,1,1,1,0}, // Node 3: 20
{0,0,0,0,1,0,0}, // Node 4: 30
{0,0,0,0,0,0,1}, // Node 5: 60
{0,0,0,0,0,0,1}, // Node 6: 50
{0,0,0,0,0,0,0}, // Node 7: 70
};
System.out.println("The BFS traversal of the graph is ");

}
}
``````
When you run the above program, you will get the below output:
``````The BFS traversal of the graph is
40 10 20 30 60 50 70``````

You may also like

Kubernetes Microservices
Python AI/ML
Spring Framework Spring Boot
Core Java Java Coding Question
Maven AWS