-
Notifications
You must be signed in to change notification settings - Fork 14
/
bfs.java
58 lines (48 loc) · 1.63 KB
/
bfs.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
import java.util.LinkedList;
import java.util.Queue;
class Graph {
private int V; // Number of vertices
private LinkedList<Integer>[] adjacencyList; // Adjacency list representation
public Graph(int v) {
V = v;
adjacencyList = new LinkedList[v];
for (int i = 0; i < v; i++) {
adjacencyList[i] = new LinkedList<>();
}
}
// Add an edge to the graph
public void addEdge(int v, int w) {
adjacencyList[v].add(w);
}
// Perform Breadth-First Search starting from a given vertex
public void bfs(int startVertex) {
boolean[] visited = new boolean[V]; // Mark all vertices as not visited
Queue<Integer> queue = new LinkedList<>();
visited[startVertex] = true;
queue.add(startVertex);
while (!queue.isEmpty()) {
startVertex = queue.poll();
System.out.print(startVertex + " ");
for (Integer neighbor : adjacencyList[startVertex]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
queue.add(neighbor);
}
}
}
}
}
public class BFSExample {
public static void main(String[] args) {
// Create a graph with 6 vertices
Graph graph = new Graph(6);
// Add edges to the graph
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 3);
graph.addEdge(2, 4);
graph.addEdge(2, 5);
System.out.println("Breadth-First Traversal (starting from vertex 0):");
graph.bfs(0);
}
}