public static void Test() { Graph g1 = new Graph(4); g1.addEdge(0, 1); g1.addEdge(0, 2); g1.addEdge(1, 2); g1.addEdge(2, 0); g1.addEdge(2, 3); g1.addEdge(3, 3); DFS(g1, 2); // 2 -> 0 -> 1 -> 3 }
private static void DFS(Graph g, int v, bool[] visited) { visited[v] = true; Console.Write(v + " "); foreach (int node in g.adj[v]) { if (!visited[node]) DFS(g, node, visited); } }
// single source DFS search public static void DFS(Graph g, int v) { DFS(g, v, new bool[g.V]); }