-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.java
More file actions
59 lines (49 loc) · 1.51 KB
/
BFS.java
File metadata and controls
59 lines (49 loc) · 1.51 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
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class BFS {
static class Edge{
int source;
int destination;
public Edge(int source, int destination) {
this.source = source;
this.destination = destination;
}
}
public static void CreateGraph(ArrayList<Edge> graph[]){
for(int i = 0;i< graph.length;i++){
graph[i] = new ArrayList<Edge>();
}
graph[0].add(new Edge(0,2 ));
graph[1].add(new Edge(1,2));
graph[1].add(new Edge(1,7));
graph[2].add(new Edge(2,8));
graph[2].add(new Edge(2,2));
graph[2].add(new Edge(2,1));
graph[3].add(new Edge(3,2));
graph[3].add(new Edge(3,5));
}
public static void bfs(ArrayList<Edge> graph[], int V){
Queue<Integer> q = new LinkedList<>();
boolean visited[] = new boolean[V];
q.add(0);
while(!q.isEmpty()){
int curr = q.remove();
if(visited[curr] == false){
System.out.println(curr+"");
visited[curr] = true;
for(int i = 0;i<graph[curr].size();i++){
Edge e = graph[curr].get(i);
q.add(e.destination);
}
}
}
}
public static void main(String[] args) {
int V = 9;
ArrayList<Edge> graph[] = new ArrayList[V];
CreateGraph(graph);
bfs(graph,V);
System.out.println();
}
}