AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
DepthFirstSearch.java
Go to the documentation of this file.
1package aud.example.graph;
2
15public class DepthFirstSearch extends Traversal {
16
18 super(g);
19 }
20
21 @Override public String name() { return "recursive DFS"; }
22
23 private int count_ = 0;
24
25 @Override public void start(MyNode s0) {
26
27 initialize(); // reset all node attributes
28
29 count_ = 0;
30 s0.d=0.0;
31
32 dfs(s0);
33 }
34
35 protected void dfs(MyNode s) {
36 s.ord=count_++; // marked
37 showMark(s);
38 showVisit(s);
39
40 if (verbose>0)
41 System.out.println("s="+s);
42
43 for (MyEdge e : g_.getOutEdges(s)) {
44
45 if (verbose>1)
46 System.err.println(e);
47
48 MyNode t=(MyNode) e.destination();
49 if (t==s)
50 t=(MyNode) e.source(); // undirected graph
51
52 if (t.ord<0) { // <=> not marked
53 t.d=s.d+(e.hasWeight() ? e.getWeight() : 1.0);
54 t.p=s;
55 dfs(t);
56 }
57 }
58 }
59}
Standard recursive implementation of depth first search.
String name()
get traversal name
void start(MyNode s0)
start traversal at node s0
edge with all possible attributes that we require ;-)
Definition: MyEdge.java:6
graph based on aud.graph.GraphAM
Definition: MyGraph.java:11
node with all possible attributes that we require ;-)
Definition: MyNode.java:6
MyNode p
node from which node was reached (defines spanning tree)
Definition: MyNode.java:16
double d
distance to start node (sum of weighs or edge count if no weights defined)
Definition: MyNode.java:21
int ord
time when node is (first marked/put into front)
Definition: MyNode.java:13
interface for traversals of MyGraph
Definition: Traversal.java:6
int verbose
set verbosity (extra output if >0)
Definition: Traversal.java:15
void initialize()
initialize graph for traversal (reset all attributes), provided for convenience to be called by start
Definition: Traversal.java:34
void showMark(MyNode node)
callback to give visual feedback on marking a node
Definition: Traversal.java:44
Vector< Edge > getOutEdges(Node node)
Definition: GraphAM.java:98