-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFSAdjacencyMatrix.java
More file actions
56 lines (42 loc) · 1.27 KB
/
Copy pathBFSAdjacencyMatrix.java
File metadata and controls
56 lines (42 loc) · 1.27 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
import java.util.*;
class Main {
static void addEdge(int[][] adj, int u, int v) {
adj[u][v]=1;
adj[v][u]=1;
}
public static void main(String[] args) {
System.out.println("Start small. Ship something.");
int V=8;
int [][] adjMatrix = new int[V][V];
// for(int i =0;i<V;i++){
// for(int j=0;j<V;j++){
// adjMatrix[i][j]=0;
// }
// }
addEdge(adjMatrix,1,2);
addEdge(adjMatrix,1,4);
addEdge(adjMatrix,1,5);
addEdge(adjMatrix,2,3);
addEdge(adjMatrix,2,6);
addEdge(adjMatrix,2,7);
//BFS
boolean [] visited = new boolean[V];
List<Integer> vertices = new ArrayList<>();
Queue<Integer> q = new LinkedList<>();
q.add(1);
visited[1]=true;
while(!q.isEmpty()){
int v = q.poll();
vertices.add(v);
for(int i =0;i<V;i++){
if(adjMatrix[v][i]==1){
if(!visited[i]){
q.add(i);
visited[i]=true;
}
}
}
}
System.out.println("Graph Inorder : " + vertices);
}
}