AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
AStarShortestPath.java
Go to the documentation of this file.
1package aud.example.graph;
2
3import java.util.Comparator;
4
5import aud.PriorityQueue;
6import aud.HashMap;
8
10public class AStarShortestPath extends Traversal {
11
13 MyNode s1_ = null;
14
18 protected Comparator<MyNode> compareNodes =
19 new Comparator<MyNode>() {
20 @Override public int compare(MyNode a,MyNode b) {
21 double d=a.f-b.f ; // compare f-values
22 return (d<0.0) ? -1 : (d>0.0 ? +1 : 0);
23 }
24 };
25
28 super(g);
29 s1_=s1;
30
31 }
33 this(g,null);
34 }
35
37 protected double h(MyNode node) {
38 double[] a=s1_.getPosition(), b=node.getPosition();
39 double dx=b[0]-a[0], dy=b[1]-a[1];
40 return Math.sqrt(dx*dx+dy*dy);
41 }
42
43 @Override public String name() { return "A*"; }
44
46 @Override public void start(MyNode s0) {
47 if (s1_==null)
48 throw new RuntimeException("A* requires destination");
49
50 initialize(); // reset all node attributes
51
52 int n=g_.getNumNodes();
53 int count=0;
56 s0.d=0.0;
57 s0.f=h(s0);
58
59 showMark(s0);
60
61 open.push(s0);
62
63 while (!open.is_empty()) {
64 if (verbose>0)
65 System.out.println("open="+open.toString());
66
67 MyNode s=open.pop();
68
69 assert(!Double.isInfinite(s.d));
70 closed.insert(s,s.d);
71 showVisit(s);
72
73 if (s==s1_) {
74 if (verbose>0)
75 System.out.println("reached destination '"+s.getLabel()+"'");
76
77 ((SimpleDecorator) g_.getDecorator()).unmarkNode(s);
78 ((SimpleDecorator) g_.getDecorator()).highlightNode(null);
79 ((SimpleDecorator) g_.getDecorator()).highlightEdge(null);
80 s.color="red"; // destination
81
82 break;
83 }
84
85 for (MyEdge e : g_.getOutEdges(s)) {
86
87 if (verbose>0)
88 System.err.println(e);
89
90 MyNode t=(MyNode) e.destination();
91 if (t==s)
92 t=(MyNode) e.source(); // undirected graph
93
94 if (!closed.contains(t)) {
95
96 double dt=s.d+(e.hasWeight() ? e.getWeight() : 1.0);
97 double pr=dt+h(t);
98
99 boolean push=Double.isInfinite(t.d);
100
101 if (dt<t.d) {
102 t.ord=count++;
103 t.p=s;
104 t.d=dt;
105 t.f=pr;
106
107 showMark(t);
108
109 if (push)
110 open.push(t);
111 else
112 open.lower(t);
113 }
114
115 if (verbose>0)
116 System.out.println("open="+open.toString());
117 }
118 }
119 }
120
121 // backtracking: output from destination to source
122
123 ((SimpleDecorator) g_.getDecorator()).unmarkAllEdges();
124 System.out.println("backtracked path: ");
125
126 MyNode node=s1_;
127
128 while (node!=s0) {
129 System.out.print(node.toString()+" ");
130 if (node!=s1_) {
131 node.color="red";
132 ((SimpleDecorator) g_.getDecorator()).unmarkNode(node);
133 }
134 g_.getEdge(node.p,node).color="red";
135 g_.getEdge(node.p,node).penwidth=4.0;
136
137 node=node.p;
138 }
139 node.color="red";
140 ((SimpleDecorator) g_.getDecorator()).unmarkNode(node);
141 System.out.println(s0);
142 }
143
145 public void start(MyNode s0,MyNode s1) {
146 s1_=s1;
147 start(s0);
148 }
149}
Implementation of an unordered map based on a hash table.
Definition: HashMap.java:31
Value insert(Key key, Value value)
insert key-value pair
Definition: HashMap.java:260
boolean contains(Key key)
Is key contained in map?
Definition: HashMap.java:295
Priority queue based on binary min-heap.
boolean is_empty()
Is PQ empty?
void push(T x)
Add entry to PQ.
T pop()
Pop minimal element from PQ.
void lower(T x)
update heap after lowering priority of x
implementation of the A* algorithm
void start(MyNode s0, MyNode s1)
find shortest path from s0 to s1
double h(MyNode node)
heuristic that guides search
AStarShortestPath(MyGraph g, MyNode s1)
create instance with destination s1
void start(MyNode s0)
find shortest path to destination
Comparator< MyNode > compareNodes
comparator for {#link aud.PriorityQueue}: compares f-values (in contrast to {#link DijkstraShortestPa...
edge with all possible attributes that we require ;-)
Definition: MyEdge.java:6
graph based on aud.graph.GraphAM
Definition: MyGraph.java:11
GraphvizDecorator getDecorator()
Definition: MyGraph.java:34
node with all possible attributes that we require ;-)
Definition: MyNode.java:6
double f
priority for A*-algorithm
Definition: MyNode.java:24
MyNode p
node from which node was reached (defines spanning tree)
Definition: MyNode.java:16
String color
color as string
Definition: MyNode.java:27
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
Edge getEdge(Node source, Node destination)
Definition: GraphAM.java:80
String getLabel()
get text description
Definition: SimpleNode.java:12
double[] getPosition()
helper for drawing the graph: return {x,y} as array or null
Definition: SimpleNode.java:22
Example for a simple decorator.
utilities (not related to AuD lecture)
Definition: Colormap.java:1
AuD lecture: Data structures, algorithms, examples.
Definition: A234Tree.java:1