0% found this document useful (0 votes)
31 views6 pages

Breadth First Search

The document describes an implementation of breadth-first search (BFS) on a graph data structure. It defines a Vertex class to represent nodes, includes methods to add vertices and edges to an adjacency matrix, and implements a BFS algorithm that uses a queue to traverse the graph in breadth-first order, printing each visited vertex.

Uploaded by

zawmyomin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views6 pages

Breadth First Search

The document describes an implementation of breadth-first search (BFS) on a graph data structure. It defines a Vertex class to represent nodes, includes methods to add vertices and edges to an adjacency matrix, and implements a BFS algorithm that uses a queue to traverse the graph in breadth-first order, printing each visited vertex.

Uploaded by

zawmyomin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

BreadthFirstSearch

import java.util.LinkedList;
import java.util.Queue;
class Vertex {
char label;
boolean visited;
public Vertex(char label) {
this.label = label;
visited = false;
}
}
public class BreadthFirstSearch {
private static final int MAX = 9;
private Vertex[] lstVertices;
private int[][] adjMatrix;
private int vertexCount;
public BreadthFirstSearch() {
lstVertices = new Vertex[MAX];
adjMatrix = new int[MAX][MAX];
vertexCount = 0;
}
private void addVertex(char label) {
Vertex vertex = new Vertex(label);
lstVertices[vertexCount++] = vertex;
}
private void addEdge(int start, int end) {
adjMatrix[start][end] = 1;
adjMatrix[end][start] = 1;
}
private void displayVertex(int vertexIndex) {
System.out.print(lstVertices[vertexIndex].label + " ");
}
private int getAdjUnvisitedVertex(int vertexIndex) {
for (int i = 0; i < vertexCount; i++) {
if (adjMatrix[vertexIndex][i] == 1 && !lstVertices[i].visited)
return i;
}
return -1;
}
private void breadthFirstSearch() {
lstVertices[0].visited = true;
displayVertex(0);
Queue<Integer> queue = new LinkedList<>();
queue.add(0);
while (!queue.isEmpty()) {
int tempVertex = queue.poll();
int unvisitedVertex;
while ((unvisitedVertex = getAdjUnvisitedVertex(tempVertex)) != -1)
{
lstVertices[unvisitedVertex].visited = true;
displayVertex(unvisitedVertex);
queue.add(unvisitedVertex);
}
}
// Reset the visited flag
for (int i = 0; i < vertexCount; i++) {
lstVertices[i].visited = false;
}
}
public static void main(String[] args) {
BreadthFirstSearch graph = new BreadthFirstSearch();
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++)
graph.adjMatrix[i][j] = 0;
}
graph.addVertex('A'); //0
graph.addVertex('B'); //1
graph.addVertex('C'); //2
graph.addVertex('D'); //3
graph.addVertex('E'); //4
graph.addVertex('F'); //5
graph.addVertex('G'); //6
graph.addVertex('H'); //7
graph.addVertex('I'); //8
graph.addEdge(0, 1); //A - B
graph.addEdge(0, 2); //A - C
graph.addEdge(0, 3); //A - D
graph.addEdge(0, 4); //A - E
graph.addEdge(1, 5); //B - F
graph.addEdge(3, 6); //D - G
graph.addEdge(5, 7); //F - H
graph.addEdge(6, 8); //G - I
System.out.print("Breadth First Search: ");
graph.breadthFirstSearch();
}
}
DepthFirstSearch
public class DepthFirstSearch {
private static final int MAX = 9;
private static class Vertex {
char label;
boolean visited;
}
private static int[] stack = new int[MAX];
private static int top = -1;
private static Vertex[] lstVertices = new Vertex[MAX];
private static int[][] adjMatrix = new int[MAX][MAX];
private static int vertexCount = 0;
private static void push(int item) {
stack[++top] = item;
}
private static int pop() {
return stack[top--];
}
private static int peek() {
return stack[top];
}
private static boolean isStackEmpty() {
return top == -1;
}
private static void addVertex(char label) {
Vertex vertex = new Vertex();
vertex.label = label;
vertex.visited = false;
lstVertices[vertexCount++] = vertex;
}
private static void addEdge(int start, int end) {
adjMatrix[start][end] = 1;
adjMatrix[end][start] = 1;
}
private static void displayVertex(int vertexIndex) {
System.out.print(lstVertices[vertexIndex].label + " ");
}
private static int getAdjUnvisitedVertex(int vertexIndex) {
for (int i = 0; i < vertexCount; i++) {
if (adjMatrix[vertexIndex][i] == 1 && !lstVertices[i].visited) {
return i;
}
}
return -1;
}
private static void depthFirstSearch() {
lstVertices[0].visited = true;
displayVertex(0);
push(0);
while (!isStackEmpty()) {
int unvisitedVertex = getAdjUnvisitedVertex(peek());
if (unvisitedVertex == -1) {
pop();
} else {
lstVertices[unvisitedVertex].visited = true;
displayVertex(unvisitedVertex);
push(unvisitedVertex);
}
}
for (int i = 0; i < vertexCount; i++) {
lstVertices[i].visited = false;
}
}
public static void main(String[] args) {
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++) {
adjMatrix[i][j] = 0;
}
}
addVertex('A'); //0
addVertex('B'); //1
addVertex('C'); //2
addVertex('D'); //3
addVertex('E'); //4
addVertex('F'); //5
addVertex('G'); //6
addVertex('H'); //7
addVertex('I'); //8
addEdge(0, 1); //A - B
addEdge(0, 2); //A - C
addEdge(0, 3); //A - D
addEdge(0, 4); //A - E
addEdge(1, 5); //B - F
addEdge(3, 6); //D - G
addEdge(5, 7); //F - H
addEdge(6, 8); //G - I
System.out.print("Depth First Search: ");
depthFirstSearch();
}
}

You might also like