AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
aud.example.graph.AStarShortestPath Class Reference

implementation of the A* algorithm More...

+ Inheritance diagram for aud.example.graph.AStarShortestPath:
+ Collaboration diagram for aud.example.graph.AStarShortestPath:

Public Member Functions

 AStarShortestPath (MyGraph g, MyNode s1)
 create instance with destination s1
More...
 
 AStarShortestPath (MyGraph g)
 
String name ()
 get traversal name More...
 
void start (MyNode s0)
 find shortest path to destination More...
 
void start (MyNode s0, MyNode s1)
 find shortest path from s0 to s1
More...
 
- Public Member Functions inherited from aud.example.graph.Traversal
 Traversal (MyGraph g)
 initiate traversal of g
More...
 
abstract String name ()
 get traversal name More...
 
abstract void start (MyNode s0)
 start traversal at node s0 More...
 
void showMark (MyNode node)
 callback to give visual feedback on marking a node More...
 

Protected Member Functions

double h (MyNode node)
 heuristic that guides search More...
 
- Protected Member Functions inherited from aud.example.graph.Traversal
void initialize ()
 initialize graph for traversal (reset all attributes), provided for convenience to be called by start More...
 

Protected Attributes

Comparator< MyNodecompareNodes
 comparator for {#link aud.PriorityQueue}: compares f-values (in contrast to {#link DijkstraShortestPath} More...
 
- Protected Attributes inherited from aud.example.graph.Traversal
MyGraph g_ = null
 
int time_ = 0
 

Additional Inherited Members

- Public Attributes inherited from aud.example.graph.Traversal
SingleStepper singlestepper = null
 may halt if single stepper was set More...
 
int nsteps = 1
 halt every nsteps steps in time_
More...
 
int verbose = 0
 set verbosity (extra output if >0) More...
 

Detailed Description

implementation of the A* algorithm

Definition at line 10 of file AStarShortestPath.java.

Constructor & Destructor Documentation

◆ AStarShortestPath() [1/2]

create instance with destination s1

Definition at line 27 of file AStarShortestPath.java.

27 {
28 super(g);
29 s1_=s1;
30
31 }

◆ AStarShortestPath() [2/2]

Definition at line 32 of file AStarShortestPath.java.

32 {
33 this(g,null);
34 }

Member Function Documentation

◆ h()

double aud.example.graph.AStarShortestPath.h ( MyNode  node)
protected

heuristic that guides search

Definition at line 37 of file AStarShortestPath.java.

37 {
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 }
double[] getPosition()
helper for drawing the graph: return {x,y} as array or null
Definition: SimpleNode.java:22

◆ name()

String aud.example.graph.AStarShortestPath.name ( )

get traversal name

Reimplemented from aud.example.graph.Traversal.

Definition at line 43 of file AStarShortestPath.java.

43{ return "A*"; }

◆ start() [1/2]

void aud.example.graph.AStarShortestPath.start ( MyNode  s0)

find shortest path to destination

Reimplemented from aud.example.graph.Traversal.

Definition at line 46 of file AStarShortestPath.java.

46 {
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;
54 PriorityQueue<MyNode> open=new PriorityQueue<MyNode>(n,compareNodes);
55 HashMap<MyNode,Double> closed=new HashMap<MyNode,Double>(n);
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 }
double h(MyNode node)
heuristic that guides search
Comparator< MyNode > compareNodes
comparator for {#link aud.PriorityQueue}: compares f-values (in contrast to {#link DijkstraShortestPa...
GraphvizDecorator getDecorator()
Definition: MyGraph.java:34
String color
color as string
Definition: MyNode.java:27
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

◆ start() [2/2]

void aud.example.graph.AStarShortestPath.start ( MyNode  s0,
MyNode  s1 
)

find shortest path from s0 to s1

Definition at line 145 of file AStarShortestPath.java.

145 {
146 s1_=s1;
147 start(s0);
148 }
void start(MyNode s0)
find shortest path to destination

Member Data Documentation

◆ compareNodes

Comparator<MyNode> aud.example.graph.AStarShortestPath.compareNodes
protected
Initial value:
=
new Comparator<MyNode>() {
@Override public int compare(MyNode a,MyNode b) {
double d=a.f-b.f ;
return (d<0.0) ? -1 : (d>0.0 ? +1 : 0);
}
}

comparator for {#link aud.PriorityQueue}: compares f-values (in contrast to {#link DijkstraShortestPath}

Definition at line 18 of file AStarShortestPath.java.


The documentation for this class was generated from the following file: