AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
BreadthFirstSearch.java
Go to the documentation of this file.
1package aud.example.graph;
2
3import aud.Queue;
4
6public class BreadthFirstSearch extends Traversal {
7
9 super(g);
10 }
11
12 @Override public String name() { return "BFS"; };
13
14 @Override public void start(MyNode s0) {
15 initialize(); // reset all node attributes
16
17 int count=0;
19 s0.ord=count++;
20 s0.d=0.0;
21
22 showMark(s0);
23
24 open.enqueue(s0);
25
26 while (!open.is_empty()) {
27 if (verbose>0)
28 System.out.println("open="+open.toString());
29
30 MyNode s=open.dequeue();
31
32 showVisit(s);
33
34 for (MyEdge e : g_.getOutEdges(s)) {
35
36 if (verbose>0)
37 System.err.println(e);
38
39 MyNode t=(MyNode) e.destination();
40 if (t==s)
41 t=(MyNode) e.source(); // undirected graph
42
43 if (t.ord<0) { // <=> not marked
44 t.ord=count++;
45 t.p=s;
46 t.d=s.d+(e.hasWeight() ? e.getWeight() : 1.0);
47
48 showMark(t);
49
50 open.enqueue(t);
51 }
52 }
53 }
54 }
55
56}
Implementation of AbstractQueue as a (dynamically resized) circular buffer based on array.
Definition: Queue.java:11
void enqueue(T x)
Enqueue element at end of queue.
Definition: Queue.java:62
boolean is_empty()
Is queue empty?
Definition: Queue.java:35
String toString()
Get string representation.
Definition: Queue.java:152
T dequeue()
Remove front element from queue.
Definition: Queue.java:50
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
AuD lecture: Data structures, algorithms, examples.
Definition: A234Tree.java:1