-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphBfs.java
64 lines (46 loc) · 1.44 KB
/
GraphBfs.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
56
57
58
59
60
61
62
63
64
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class GraphBfs {
public static void bfsDisconnected(List<List<Integer>> adj) {
boolean[] visited = new boolean[adj.size()]; // Not visited
for (int i = 0; i < adj.size(); i++) {
if (!visited[i]) {
bfs(adj, i, visited);
}
}
}
static void bfs(List<List<Integer>> graph, int v, boolean visited[]) {
Queue<Integer> queue = new LinkedList<>();
// boolean[] visited = new boolean[graph.size()];
visited[v] = true;
queue.add(v);
while (!queue.isEmpty()) {
int cur = queue.poll();
System.out.print(cur + " ");
for (int i : graph.get(cur)) {
if (!visited[i]) {
visited[i] = true;
queue.add(i);
}
}
}
}
static void addEdge(List<List<Integer>> adj, int u, int v) {
adj.get(u).add(v);
adj.get(v).add(u);
}
public static void main(String[] args) {
List<List<Integer>> adj = new LinkedList<>();
int V = 6;
for (int i = 0; i < V; i++) {
adj.add(new LinkedList<>());
}
addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 3, 4);
addEdge(adj, 4, 5);
// Perform BFS traversal for the entire graph
bfsDisconnected(adj);
}
}