Skip to content

Instantly share code, notes, and snippets.

@pori
Created August 25, 2014 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pori/ff26e48f6d7a7205766a to your computer and use it in GitHub Desktop.
Save pori/ff26e48f6d7a7205766a to your computer and use it in GitHub Desktop.
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