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

Commit

Permalink
Create Depth First Search
Browse files Browse the repository at this point in the history
  • Loading branch information
simrankamra authored Oct 26, 2024
1 parent 5aefa58 commit 1ec79f3
Showing 1 changed file with 13 additions and 0 deletions.
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);
}
}
}

0 comments on commit 1ec79f3

Please sign in to comment.