|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object | +--graph.Dijkstra
A class to find shortest paths from a single start point to any end point. The user interface is nicely set up so that it is efficient regardless of whether the user wants the distance to lots of end points, or just to a single end point near the start point.
| Field Summary | |
container.Attribute |
attrBestEdgeIn
Backpointer attribute. |
container.Attribute |
attrDist
Distance attribute. |
protected container.Attribute |
attrEntry
Entry attribute. |
protected graph.Digraph |
g
The graph we are searching. |
protected pqueue.AdaptablePriorityQueue |
pq
A priority queue of vertices that still have to be relaxed, keyed by their attrDist. |
protected graph.Vertex |
vStart
The start vertex. |
protected graph.Weighter |
weighter
An object that can compute the weight function on edges. |
| Constructor Summary | |
Dijkstra(graph.Digraph g,
graph.Weighter weighter,
java.lang.Object keyStart)
Convenience version of the constructor where the graph is keyed and the start vertex is specified by a key. |
|
Dijkstra(graph.Digraph g,
graph.Weighter weighter,
graph.Vertex vStart)
Constructor that sets up a shortest-path problem. |
|
| Method Summary | |
protected void |
clearAttributes(graph.Vertex v)
Clears this instance's attributes from v and all the vertices that can be reached from it by directed paths from v. |
protected void |
computePathTo(graph.Vertex vEnd)
Run Dijkstra's algorithm until we know the shortest path from vertex vStart to vertex vEnd, or have explored the whole graph without finding such a path. |
void |
finalize()
The finalizer for this Dijkstra instance. |
private double |
getDist(graph.Vertex v)
Value of v's attrDist attribute, as a double. |
graph.Path |
getPathTo(java.lang.Object keyEnd)
Convenience version of getPathTo() where the end vertex is specified by a key. |
graph.Path |
getPathTo(graph.Vertex vEnd)
Run Dijkstra's algorithm until we know the shortest path from vertex vStart to vertex vEnd. |
static void |
main(java.lang.String[] args)
|
protected graph.Path |
pathTo(graph.Vertex v)
Get the current best path from vStart to v, according to the backpointers. |
private void |
recordNewPathTo(graph.Vertex v,
double d,
graph.DirectedEdge e)
We'll call this method when we find a new, shorter path to v. |
private graph.Vertex |
removeMinVertex()
Remove and return the Vertex at the front of the priority queue. |
| Methods inherited from class java.lang.Object |
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
protected graph.Digraph g
protected graph.Weighter weighter
protected graph.Vertex vStart
protected pqueue.AdaptablePriorityQueue pq
public container.Attribute attrDist
This attribute is public, enabling the user to find its value at any vertex.
public container.Attribute attrBestEdgeIn
This attribute is public, enabling the user to find its value at any vertex.
protected container.Attribute attrEntry
| Constructor Detail |
public Dijkstra(graph.Digraph g,
graph.Weighter weighter,
graph.Vertex vStart)
public Dijkstra(graph.Digraph g,
graph.Weighter weighter,
java.lang.Object keyStart)
java.lang.ClassCastException - if the graph is not an instance of KeyedDigraph.
NoSuchVertexException - if there is no vertex keyed by keyStart.| Method Detail |
public graph.Path getPathTo(graph.Vertex vEnd)
throws NegativeWeightException
The user may call us again later with another vertex vEnd, in which case we will pick up where we left off and continue running Dijkstra's algorithm, if necessary, until we know the shortest path to THAT vertex!
To find the length of the path, the user can look at v.get(d.attrDist), where d is this Dijkstra instance. Alternatively, the user can just sum the weights of the returned path's edges.
NegativeWeightException - if negative edge weights are
discovered in the graph.
public graph.Path getPathTo(java.lang.Object keyEnd)
throws NegativeWeightException
java.lang.ClassCastException - if the graph is not an instance of KeyedDigraph.
NoSuchVertexException - if there is no vertex keyed by keyEnd.
NegativeWeightException - if negative edge weights are
discovered in the graph.
protected void computePathTo(graph.Vertex vEnd)
throws NegativeWeightException
pathTo() can then be used to find the shortest path to vEnd, or the shortest KNOWN path (so far) to another vertex.
NegativeWeightException - if negative edge weights are
discovered in the graph.protected graph.Path pathTo(graph.Vertex v)
private graph.Vertex removeMinVertex()
private double getDist(graph.Vertex v)
private void recordNewPathTo(graph.Vertex v,
double d,
graph.DirectedEdge e)
In addition to recording the new path in v's attributes, we ensure that v is on the priority queue, so that the change in its attrDist will be propagated to its neighbors. This may mean inserting or reprioritizing v. Note: This method doesn't have to care whether v has previously been popped off the queue. However, it is interesting to remember that in Dijkstra's algorithm, a vertex's distance will never improve further once it has been popped, so it never goes back on on the priority queue. (This is also true for the A* algorithm if we use a "monotonic" heuristic function, which is usually the case and is the case in this assignment.)
public void finalize()
A finalizer is the opposite of a constructor. When and if a Dijkstra instance is eventually garbage-collected (long after we're done with it), Java will automatically call the special finalize() method if it exists. Since this particular finalize() method is public, the user is also welcome to call it sooner when done with the Dijkstra instance, instead of waiting for the garbage collector to show up. In C++, it is necessary to have destructors for most objects because there is no garbage collection in C++; memory must be released explicitly. But in Java, it is quite rare to need a finalizer.
finalize in class java.lang.Objectprotected void clearAttributes(graph.Vertex v)
public static void main(java.lang.String[] args)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||