-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFSIterativeAdjMatrix.java
55 lines (48 loc) · 1.47 KB
/
BFSIterativeAdjMatrix.java
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
import java.util.LinkedList;
import java.util.Queue;
public class BFSIterativeAdjMatrix {
int v;
int[][] adjMatrix;
public BFSIterativeAdjMatrix(int v) {
this.v = v;
adjMatrix = new int[v][v];
}
public void addEdge(int src, int dest) {
adjMatrix[src][dest] = 1;
adjMatrix[dest][src] = 1;
}
public void populateGraph(int[][] edges) {
for (int[] edge : edges) {
this.addEdge(edge[0], edge[1]);
}
}
public void bfs() {
boolean[] visited = new boolean[v];
for (int i = 0; i < v; i++) {
if (!visited[i]) {
bfsUtil(visited, i);
}
}
}
private void bfsUtil(boolean[] visited, int source) {
visited[source] = true;
Queue<Integer> queue = new LinkedList<>();
queue.offer(source);
while(!queue.isEmpty()) {
int node = queue.poll();
System.out.print(node + " ");
for (int i = 0; i < v; ++i) {
if (adjMatrix[node][i] == 1 && !visited[i]) {
queue.offer(i);
visited[i] = true;
}
}
}
}
public static void main(String[] args) {
BFSIterativeAdjMatrix graph = new BFSIterativeAdjMatrix(10);
int[][] edges = {{0, 1}, {0, 2}, {1, 3}, {1, 4}, {4, 5}, {2, 5}, {2, 6}, {2, 7}, {8, 9}};
graph.populateGraph(edges);
graph.bfs(); // 0 1 2 3 4 5 6 7 8 9
}
}