|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.waldura.tw.DijkstraEngine
public class DijkstraEngine
An implementation of Dijkstra's shortest path algorithm. It computes the shortest path (in distance) to all cities in the map. The output of the algorithm is the shortest distance from the start city to every other city, and the shortest path from the start city to every other.
Upon calling
execute(City, City)
,
the results of the algorithm are made available by calling
getPredecessor(City)
and
getShortestDistance(City)
.
To get the shortest path between the city destination and
the source city after running the algorithm, one would do:
ArrayList<City> l = new ArrayList<City>(); for (City city = destination; city != null; city = engine.getPredecessor(city)) { l.add(city); } Collections.reverse(l); return l;
execute(City, City)
Field Summary | |
---|---|
static int |
INFINITE_DISTANCE
Infinity value for distances. |
private static int |
INITIAL_CAPACITY
Some value to initialize the priority queue with. |
private RoutesMap |
map
The graph. |
private java.util.Map<City,City> |
predecessors
Predecessors list: maps a city to its predecessor in the spanning tree of shortest paths. |
private java.util.Set<City> |
settledNodes
The set of cities for which the shortest distance to the source has been found. |
private java.util.Comparator<City> |
shortestDistanceComparator
This comparator orders cities according to their shortest distances, in ascending fashion. |
private java.util.Map<City,java.lang.Integer> |
shortestDistances
The currently known shortest distance for all cities. |
private java.util.PriorityQueue<City> |
unsettledNodes
The working set of cities, kept ordered by shortest distance. |
Constructor Summary | |
---|---|
DijkstraEngine(RoutesMap map)
Constructor. |
Method Summary | |
---|---|
void |
execute(City start,
City destination)
Run Dijkstra's shortest path algorithm on the map. |
City |
getPredecessor(City city)
|
int |
getShortestDistance(City city)
|
private void |
init(City start)
Initialize all data structures used by the algorithm. |
private boolean |
isSettled(City v)
Test a node. |
private void |
relaxNeighbors(City u)
Compute new shortest distance for neighboring nodes and update if a shorter distance is found. |
private void |
setPredecessor(City a,
City b)
|
private void |
setShortestDistance(City city,
int distance)
Set the new shortest distance for the given node, and re-balance the queue according to new shortest distances. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int INFINITE_DISTANCE
private static final int INITIAL_CAPACITY
private final java.util.Comparator<City> shortestDistanceComparator
private final RoutesMap map
private final java.util.PriorityQueue<City> unsettledNodes
private final java.util.Set<City> settledNodes
private final java.util.Map<City,java.lang.Integer> shortestDistances
private final java.util.Map<City,City> predecessors
Constructor Detail |
---|
public DijkstraEngine(RoutesMap map)
Method Detail |
---|
private void init(City start)
start
- the source nodepublic void execute(City start, City destination)
getPredecessor(City)
and
getShortestDistance(City)
upon completion of this method.
start
- the starting citydestination
- the destination city. If this argument is null
, the algorithm is
run on the entire graph, instead of being stopped as soon as the destination is reached.private void relaxNeighbors(City u)
u
- the nodeprivate boolean isSettled(City v)
v
- the node to consider
public int getShortestDistance(City city)
INFINITE_DISTANCE
if there is no route to the destination.private void setShortestDistance(City city, int distance)
city
- the node to setdistance
- new shortest distance valuepublic City getPredecessor(City city)
null
if there is no route to the destination.private void setPredecessor(City a, City b)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |