Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RecursiveDFSImpl |
|
| 1.8;1,8 |
1 | /* | |
2 | This file is part of Combean. | |
3 | ||
4 | Combean is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation; either version 2 of the License, or | |
7 | (at your option) any later version. | |
8 | ||
9 | Combean is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with Combean; if not, write to the Free Software | |
16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | */ | |
18 | /* | |
19 | * Created on 14.01.2005 | |
20 | * | |
21 | */ | |
22 | package net.sourceforge.combean.graph.alg.traversal; | |
23 | ||
24 | import net.sourceforge.combean.interfaces.graph.Edge; | |
25 | import net.sourceforge.combean.interfaces.graph.EdgeIterator; | |
26 | import net.sourceforge.combean.interfaces.graph.Node; | |
27 | import net.sourceforge.combean.interfaces.graph.alg.traverse.DFSVisitor; | |
28 | import net.sourceforge.combean.interfaces.graph.alg.traverse.DepthFirstSearch; | |
29 | import net.sourceforge.combean.interfaces.graph.alg.traverse.TraversalVisitor; | |
30 | ||
31 | /** | |
32 | * A recursive implementation of the Depth First Search algorithm. | |
33 | * | |
34 | * @author schickin | |
35 | * | |
36 | */ | |
37 | public class RecursiveDFSImpl extends AbstractGraphTraversalAlg | |
38 | implements DepthFirstSearch { | |
39 | ||
40 | 45 | private DFSVisitor dfsVisitor = null; |
41 | ||
42 | /** | |
43 | * constructor | |
44 | */ | |
45 | public RecursiveDFSImpl() { | |
46 | 45 | super(); |
47 | 45 | } |
48 | ||
49 | /** | |
50 | * @see net.sourceforge.combean.interfaces.graph.alg.traverse.DepthFirstSearch#getDFSVisitor() | |
51 | */ | |
52 | public DFSVisitor getDFSVisitor() { | |
53 | 0 | return this.dfsVisitor; |
54 | } | |
55 | ||
56 | /** | |
57 | * @see net.sourceforge.combean.interfaces.graph.alg.traverse.DepthFirstSearch#setDFSVisitor(net.sourceforge.combean.interfaces.graph.alg.traverse.DFSVisitor) | |
58 | */ | |
59 | public void setDFSVisitor(DFSVisitor visitor) { | |
60 | 24 | setVisitor(visitor); |
61 | 24 | this.dfsVisitor = visitor; |
62 | 24 | } |
63 | ||
64 | /** | |
65 | * @see net.sourceforge.combean.interfaces.graph.alg.traverse.GraphTraversalAlg#setVisitor(net.sourceforge.combean.interfaces.graph.alg.traverse.TraversalVisitor) | |
66 | */ | |
67 | public void setVisitor(TraversalVisitor visitor) { | |
68 | 54 | super.setVisitor(visitor); |
69 | 54 | this.dfsVisitor = null; |
70 | 54 | } |
71 | ||
72 | /** | |
73 | * Explore the component of v with a recursive DFS. | |
74 | * | |
75 | * @param v current node of the recursive DFS. | |
76 | */ | |
77 | protected void runTraversalWithSingleStartNode(Node v) { | |
78 | 336 | getVisitor().visitNode(v); |
79 | 336 | EdgeIterator it = getNeighborIterator(v); |
80 | 681 | while (it.hasNext()) { |
81 | 345 | Edge e = it.next(); |
82 | 345 | Node w = getNeighborhood().getOtherNode(e, v); |
83 | 345 | boolean notYetVisited = getVisitedNodes().add(w); |
84 | 345 | if (notYetVisited) { |
85 | 249 | getVisitor().openNeighbor(e, v); |
86 | 249 | runTraversalWithSingleStartNode(w); |
87 | 249 | if (this.dfsVisitor != null) { |
88 | 132 | this.dfsVisitor.leaveNeighbor(e, v); |
89 | } | |
90 | } | |
91 | else { | |
92 | 96 | getVisitor().reopenNeighbor(e, v); |
93 | } | |
94 | 345 | } |
95 | 336 | if (this.dfsVisitor != null) { |
96 | 156 | this.dfsVisitor.leaveNode(v); |
97 | } | |
98 | 336 | } |
99 | } |