|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.graphstream.algorithm.AStar
public class AStar
An implementation of the A* algorithm.
A* computes the shortest path from a node to another in a graph. It can eventually fail if the two nodes are in two distinct connected components.
In this A* implementation, the various costs (often called g, h and f) are
given by a AStar.Costs
class. This class
must provide a way to compute :
By default the AStar.Costs
implementation
used uses a heuristic that returns 0 for any heuristic. This makes A* an
equivalent of the Dijkstra algorithm, but also makes it far less efficient.
The basic usage is to create an instance of A*, then to ask it to compute from a shortest path from one target to one destination, and finally to ask for that path:
AStart astar = new AStar(graph); astar.compute("A", "Z"); // with A and Z node identifiers in the graph. Path path = astar.getShortestPath();
The advantage of A* is that it can consider any cost function to drive the
search. You can create your own cost functions implementing the
#Costs
interface.
You can also test the default "distance" cost function on a graph that has
"x" and "y" values. You specify the Cost function before calling the
compute(String,String)
method:
AStart astar = new AStar(graph); astar.setCosts(new DistanceCosts()); astar.compute("A", "Z"); Path path = astar.getShortestPath();
This algorithm uses the std-algo-1.0 algorithm's standard.
Nested Class Summary | |
---|---|
static interface |
AStar.Costs
The definition of an heuristic. |
static class |
AStar.DefaultCosts
An implementation of the Costs interface that provide a default heuristic. |
static class |
AStar.DistanceCosts
An implementation of the Costs interface that assume that the weight of edges is an Euclidian distance in 2D or 3D. |
Constructor Summary | |
---|---|
AStar()
New A* algorithm. |
|
AStar(org.graphstream.graph.Graph graph)
New A* algorithm on a given graph. |
|
AStar(org.graphstream.graph.Graph graph,
String src,
String trg)
New A* algorithm on the given graph. |
Method Summary | |
---|---|
org.graphstream.graph.Path |
buildPath(org.graphstream.algorithm.AStar.AStarNode target)
Build the shortest path from the target/destination node, following the parent links. |
void |
compute()
Run the algorithm. |
void |
compute(String source,
String target)
Call compute() after having called setSource(String)
and setTarget(String) . |
org.graphstream.graph.Path |
getShortestPath()
The computed path, or null if nor result was found. |
void |
init(org.graphstream.graph.Graph graph)
Initialization of the algorithm. |
boolean |
noPathFound()
After having called compute() or
compute(String, String) , if the getShortestPath()
returns null, or this method return true, there is no path from the given
source node to the given target node. |
void |
setCosts(AStar.Costs costs)
Specify how various costs are computed. |
void |
setSource(String nodeName)
Change the source node. |
void |
setTarget(String nodeName)
Change the target node. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public AStar()
public AStar(org.graphstream.graph.Graph graph)
graph
- The graph where the algorithm will compute paths.public AStar(org.graphstream.graph.Graph graph, String src, String trg)
graph
- The graph where the algorithm will compute paths.src
- The start node.trg
- The destination node.Method Detail |
---|
public void setSource(String nodeName)
nodeName
- Identifier of the source node.public void setTarget(String nodeName)
nodeName
- Identifier of the target node.public void setCosts(AStar.Costs costs)
costs
- The cost method to use.public void init(org.graphstream.graph.Graph graph)
Algorithm
Algorithm.compute()
method to initialize or reset the algorithm according
to the new given graph.
init
in interface Algorithm
graph
- The graph this algorithm is using.public void compute()
Algorithm
Algorithm.init(Graph)
method has to be called
before computing.
compute
in interface Algorithm
Algorithm.init(Graph)
public org.graphstream.graph.Path getShortestPath()
public boolean noPathFound()
compute()
or
compute(String, String)
, if the getShortestPath()
returns null, or this method return true, there is no path from the given
source node to the given target node. In other words, the graph as
several connected components.
public org.graphstream.graph.Path buildPath(org.graphstream.algorithm.AStar.AStarNode target)
target
- The destination node.
public void compute(String source, String target)
compute()
after having called setSource(String)
and setTarget(String)
.
source
- Identifier of the source node.target
- Identifier of the target node.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |