-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.java
More file actions
60 lines (51 loc) · 1.82 KB
/
Copy pathBFS.java
File metadata and controls
60 lines (51 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.anudev.ds.graphs;
import java.util.LinkedList;
import java.util.Queue;
/**
* BFS is a traversing algorithm where you should start traversing from
* a selected node (source or starting node) and traverse the graph layer
* wise thus exploring the neighbour nodes (nodes which are directly
* connected to source node). You must then move towards the next-level
* neighbour nodes. First move horizontally and visit all the nodes of
* the current layer. Move to the next layer
*/
public class BFS {
private final int[][] adjMatrix;
private final Vertex[] vertices;
private final Queue<Integer> queue;
private int vertexCount;
public BFS(int numberOfVertices) {
adjMatrix = new int[numberOfVertices][numberOfVertices];
vertices = new Vertex[numberOfVertices];
queue = new LinkedList<>();
}
public void addVertex(char label) {
vertices[vertexCount++] = new Vertex(label);
}
public void addEdge(int start, int end) {
adjMatrix[start][end] = 1;
adjMatrix[end][start] = 1;
}
public void performBFS(int index) {
queue.add(index);
vertices[index].setVisited(true);
while (!queue.isEmpty()) {
int value = queue.poll();
// add all the unvisited vertex to the queue
int unvisitedVertex = getAdjUnvisitedMatrix(value);
while (unvisitedVertex != -1) {
queue.add(unvisitedVertex);
vertices[unvisitedVertex].setVisited(true);
unvisitedVertex = getAdjUnvisitedMatrix(value);
}
}
}
private int getAdjUnvisitedMatrix(int x) {
for (int i = 0; i < vertexCount; i++) {
if (adjMatrix[x][i] == 1 && !vertices[i].isVisited()) {
return i;
}
}
return -1;
}
}