Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A really, really simple depth first search implementation.
import java.util.Arrays;
public class DepthFirstSearch {
public static void main(String[] args) {
int[][] graph = {
{ 0, 1, 2 },
{ 0, 1, 2 },
{ 0, 1, 2 },
};
boolean[] found = new boolean[ graph[0].length ];
DepthFirstSearch.DFS(graph, 0, found);
System.out.println(Arrays.toString(found));
}
public static void DFS(int[][] g, int v, boolean[] found) {
found[v] = true;
for (int w: g[v]) {
if (!found[w]) {
DFS(g, w, found);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.