|
1 | 1 | /** |
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) |
3 | 9 | * |
4 | 10 | * @author William Fiset, william.alexandre.fiset@gmail.com |
5 | 11 | */ |
6 | 12 | package com.williamfiset.algorithms.graphtheory; |
7 | 13 |
|
8 | | -import java.util.*; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
9 | 16 |
|
10 | 17 | public class DepthFirstSearchAdjacencyListRecursive { |
11 | 18 |
|
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); |
20 | 24 | } |
21 | 25 |
|
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 | + } |
30 | 30 | 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); |
39 | 34 | } |
40 | | - |
41 | 35 | return count; |
42 | 36 | } |
43 | 37 |
|
44 | | - // Example usage of DFS |
45 | | - public static void main(String[] args) { |
| 38 | + // ==================== Main ==================== |
46 | 39 |
|
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); |
67 | 52 |
|
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); |
71 | 58 |
|
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"); |
75 | 61 | } |
76 | 62 |
|
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<>()); |
83 | 67 | } |
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); |
85 | 73 | } |
86 | 74 | } |
0 commit comments