-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphWithDfsAndBfs.java
55 lines (41 loc) · 1.22 KB
/
GraphWithDfsAndBfs.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;
import java.util.List;
public class GraphWithDfsAndBfs {
static void bfs(List<List<Integer>> adj, int s) {
Queue<Integer> q = new LinkedList<>();
int n = adj.size();
boolean[] visited = new boolean[n];
visited[s] = true;
q.add(s);
while (!q.isEmpty()) {
int cur = q.poll();
// int cur = q.peek();
System.out.print(cur + " ");
for (int x : adj.get(cur)) {
if (!visited[x]) {
visited[x] = true;
q.add(x);
}
}
}
}
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 noVertices = 5;
for (int i = 0; i < noVertices; i++) {
adj.add(new LinkedList<>());
}
addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 1, 3);
addEdge(adj, 1, 4);
addEdge(adj, 2, 4);
System.out.println("BFS starting from 0:");
bfs(adj, 0);
}
}