-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph_BreadthFirstSearch.java
More file actions
57 lines (46 loc) · 1.63 KB
/
Graph_BreadthFirstSearch.java
File metadata and controls
57 lines (46 loc) · 1.63 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
package datastructures;
import java.util.*;
public class Graph_BreadthFirstSearch {
private Map<Integer, List<Integer>> adjList;
// Constructor
public Graph_BreadthFirstSearch() {
adjList = new HashMap<>();
}
// Add edge to the graph
public void addEdge(int src, int dest) {
adjList.computeIfAbsent(src, k -> new ArrayList<>()).add(dest);
adjList.computeIfAbsent(dest, k -> new ArrayList<>()).add(src); // If undirected graph
}
// Perform BFS
public void bfs(int start) {
Set<Integer> visited = new HashSet<>();
Queue<Integer> queue = new LinkedList<>();
// Start with the initial node
visited.add(start);
queue.add(start);
while (!queue.isEmpty()) {
int node = queue.poll();
System.out.print(node + " ");
// Visit all adjacent nodes
for (int neighbor : adjList.getOrDefault(node, new ArrayList<>())) {
if (!visited.contains(neighbor)) {
visited.add(neighbor);
queue.add(neighbor);
}
}
}
}
// Main method to test the graph and BFS implementation
public static void main(String[] args) {
Graph_BreadthFirstSearch graph = new Graph_BreadthFirstSearch();
// Add edges to the graph
graph.addEdge(1, 2);
graph.addEdge(1, 3);
graph.addEdge(2, 4);
graph.addEdge(2, 5);
graph.addEdge(3, 6);
graph.addEdge(3, 7);
System.out.println("BFS Traversal:");
graph.bfs(1);
}
}