From 1ec79f33dfb952872f0127e9198a4a384244b776 Mon Sep 17 00:00:00 2001 From: Simran <65457292+simrankamra@users.noreply.github.com> Date: Sat, 26 Oct 2024 17:21:34 +0530 Subject: [PATCH] Create Depth First Search --- .../Depth First Search" | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 "Beginner Level \360\237\223\201/Depth First Search" diff --git "a/Beginner Level \360\237\223\201/Depth First Search" "b/Beginner Level \360\237\223\201/Depth First Search" new file mode 100644 index 00000000..4f08ddd4 --- /dev/null +++ "b/Beginner Level \360\237\223\201/Depth First Search" @@ -0,0 +1,13 @@ +//Depth First Search (DFS) +import java.util.*; + +public class DFS { + public static void dfs(int node, Map> graph, Set visited) { + if (visited.contains(node)) return; + visited.add(node); + System.out.print(node + " "); + for (int neighbor : graph.getOrDefault(node, Collections.emptyList())) { + dfs(neighbor, graph, visited); + } + } +}