Created
August 25, 2014 15:46
-
-
Save pori/ff26e48f6d7a7205766a to your computer and use it in GitHub Desktop.
A really, really simple depth first search implementation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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