Skip to content

Commit eba25c9

Browse files
williamfisetclaude
andcommitted
Refactor DFS recursive: simplify, add docs, clean up example
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 99d199a commit eba25c9

File tree

1 file changed

+50
-62
lines changed

1 file changed

+50
-62
lines changed
Lines changed: 50 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,74 @@
11
/**
2-
* An implementation of a recursive approach to DFS Time Complexity: O(V + E)
2+
* Depth-First Search — Adjacency List (Recursive)
3+
*
4+
* <p>Performs a recursive DFS traversal on a directed graph represented as an
5+
* adjacency list, counting the number of reachable nodes from a given source.
6+
*
7+
* <p>Time: O(V + E)
8+
* <p>Space: O(V)
39
*
410
* @author William Fiset, william.alexandre.fiset@gmail.com
511
*/
612
package com.williamfiset.algorithms.graphtheory;
713

8-
import java.util.*;
14+
import java.util.ArrayList;
15+
import java.util.List;
916

1017
public class DepthFirstSearchAdjacencyListRecursive {
1118

12-
static class Edge {
13-
int from, to, cost;
14-
15-
public Edge(int from, int to, int cost) {
16-
this.from = from;
17-
this.to = to;
18-
this.cost = cost;
19-
}
19+
/**
20+
* Returns the number of nodes reachable from {@code start} (including itself).
21+
*/
22+
static int dfs(int start, List<List<Integer>> graph) {
23+
return dfs(start, new boolean[graph.size()], graph);
2024
}
2125

22-
// Perform a depth first search on the graph counting
23-
// the number of nodes traversed starting at some position
24-
static long dfs(int at, boolean[] visited, Map<Integer, List<Edge>> graph) {
25-
26-
// We have already visited this node
27-
if (visited[at]) return 0L;
28-
29-
// Visit this node
26+
private static int dfs(int at, boolean[] visited, List<List<Integer>> graph) {
27+
if (visited[at]) {
28+
return 0;
29+
}
3030
visited[at] = true;
31-
long count = 1;
32-
33-
// Visit all edges adjacent to where we're at
34-
List<Edge> edges = graph.get(at);
35-
if (edges != null) {
36-
for (Edge edge : edges) {
37-
count += dfs(edge.to, visited, graph);
38-
}
31+
int count = 1;
32+
for (int to : graph.get(at)) {
33+
count += dfs(to, visited, graph);
3934
}
40-
4135
return count;
4236
}
4337

44-
// Example usage of DFS
45-
public static void main(String[] args) {
38+
// ==================== Main ====================
4639

47-
// Create a fully connected graph
48-
// (0)
49-
// / \
50-
// 5 / \ 4
51-
// / \
52-
// 10 < -2 >
53-
// +->(2)<------(1) (4)
54-
// +--- \ /
55-
// \ /
56-
// 1 \ / 6
57-
// > <
58-
// (3)
59-
int numNodes = 5;
60-
Map<Integer, List<Edge>> graph = new HashMap<>();
61-
addDirectedEdge(graph, 0, 1, 4);
62-
addDirectedEdge(graph, 0, 2, 5);
63-
addDirectedEdge(graph, 1, 2, -2);
64-
addDirectedEdge(graph, 1, 3, 6);
65-
addDirectedEdge(graph, 2, 3, 1);
66-
addDirectedEdge(graph, 2, 2, 10); // Self loop
40+
//
41+
// 0 ---> 1 ---> 3
42+
// | |
43+
// v v
44+
// 2 4 ---> 5 6
45+
//
46+
// DFS from 0: 6 nodes
47+
// DFS from 6: 1 node
48+
//
49+
public static void main(String[] args) {
50+
int n = 7;
51+
List<List<Integer>> graph = createGraph(n);
6752

68-
long nodeCount = dfs(0, new boolean[numNodes], graph);
69-
System.out.println("DFS node count starting at node 0: " + nodeCount);
70-
if (nodeCount != 4) System.err.println("Error with DFS");
53+
addDirectedEdge(graph, 0, 1);
54+
addDirectedEdge(graph, 0, 2);
55+
addDirectedEdge(graph, 1, 3);
56+
addDirectedEdge(graph, 1, 4);
57+
addDirectedEdge(graph, 4, 5);
7158

72-
nodeCount = dfs(4, new boolean[numNodes], graph);
73-
System.out.println("DFS node count starting at node 4: " + nodeCount);
74-
if (nodeCount != 1) System.err.println("Error with DFS");
59+
System.out.println("DFS from 0: " + dfs(0, graph) + " nodes");
60+
System.out.println("DFS from 6: " + dfs(6, graph) + " nodes");
7561
}
7662

77-
// Helper method to setup graph
78-
private static void addDirectedEdge(Map<Integer, List<Edge>> graph, int from, int to, int cost) {
79-
List<Edge> list = graph.get(from);
80-
if (list == null) {
81-
list = new ArrayList<Edge>();
82-
graph.put(from, list);
63+
private static List<List<Integer>> createGraph(int n) {
64+
List<List<Integer>> graph = new ArrayList<>(n);
65+
for (int i = 0; i < n; i++) {
66+
graph.add(new ArrayList<>());
8367
}
84-
list.add(new Edge(from, to, cost));
68+
return graph;
69+
}
70+
71+
private static void addDirectedEdge(List<List<Integer>> graph, int from, int to) {
72+
graph.get(from).add(to);
8573
}
8674
}

0 commit comments

Comments
 (0)