diff --git a/Java/Algorithm/Breadth-First Search (BFS)/BFS.java b/Java/Algorithm/Breadth-First Search (BFS)/BFS.java new file mode 100644 index 00000000..31e9447d --- /dev/null +++ b/Java/Algorithm/Breadth-First Search (BFS)/BFS.java @@ -0,0 +1,36 @@ +import java.util.*; + +public class BFS { + public static void bfs(Map> graph, int start) { + Queue queue = new LinkedList<>(); + Set visited = new HashSet<>(); + + queue.offer(start); + visited.add(start); + + while (!queue.isEmpty()) { + int node = queue.poll(); + System.out.print(node + " "); + + List neighbors = graph.getOrDefault(node, new ArrayList<>()); + for (int neighbor : neighbors) { + if (!visited.contains(neighbor)) { + queue.offer(neighbor); + visited.add(neighbor); + } + } + } + } + + public static void main(String[] args) { + Map> graph = new HashMap<>(); + graph.put(0, Arrays.asList(1, 2)); + graph.put(1, Arrays.asList(3, 4)); + graph.put(2, Arrays.asList(5)); + graph.put(3, new ArrayList<>()); + graph.put(4, new ArrayList<>()); + graph.put(5, new ArrayList<>()); + + bfs(graph, 0); + } +}