Skip to content
This repository has been archived by the owner on Nov 3, 2024. It is now read-only.

Depth First Search #445

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Beginner Level πŸ“/Depth First Search
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//Depth First Search (DFS)
import java.util.*;

public class DFS {
public static void dfs(int node, Map<Integer, List<Integer>> graph, Set<Integer> 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);
}
}
}