File tree Expand file tree Collapse file tree 1 file changed +12
-5
lines changed
src/java/sequential/graph/dfs Expand file tree Collapse file tree 1 file changed +12
-5
lines changed Original file line number Diff line number Diff line change @@ -12,23 +12,30 @@ class RecursiveDepthFirstTraversal<T> {
12
12
private Map <T , List <T >> graph ;
13
13
private Set <T > explored ;
14
14
15
- public Collection <T > dfs (Map <T , List <T >> graph , T root ) {
15
+ public synchronized Collection <T > dfs (Map <T , List <T >> graph , T root ) {
16
16
this .graph = graph ;
17
17
explored = new LinkedHashSet <>();
18
18
19
- explore (root );
20
-
21
- return explored ;
19
+ var traversed = explore (root );
20
+
21
+ clear ();
22
+ return traversed ;
22
23
}
23
24
24
- private void explore (T node ) {
25
+ private Collection < T > explore (T node ) {
25
26
explored .add (node );
26
27
27
28
var successors = graph .getOrDefault (node , List .of ());
28
29
for (var succ : successors )
29
30
if (!explored .contains (succ )) explore (succ );
31
+
32
+ return explored ;
30
33
}
31
34
35
+ private void clear () {
36
+ explored = null ;
37
+ graph = null ;
38
+ }
32
39
33
40
public static void main (String [] args ) {
34
41
var graph = Map .of (
You can’t perform that action at this time.
0 commit comments