Bill Gates and Scott McNealy were playing a friendly game of frisbee on the Gate's estate on the shore of Lake Washington. At one point, Bill accidentally sends the frisbee over Scott's head, and the frisbee lands in the lake. Scott walks out onto the surface of the lake and retrieves the frisbee.
The next day the newspapers report:
GATE'S THROW EXCEEDS EXPECTATIONS Sun CEO Unable to Swim
G
|
3|
| 1 4 1 9
A---B---C---D---E
| | | |
9| 2| 0| |
| | | |
+---F---+ |
| 6 |
+-----------+
/**
* Compute the shortest paths from a node to every other node in
* the graph.
*
* pre: The graph is initialized.
* pre: The root is in the graph.
* pre: The graph is nonempty.
* post: An array of distance/prevnode pairs is returned.
* post: The underlying graph is unaffected.
*/
Dictionary shortestPaths(Node root) {
Dictionary SP = new Dictionary(); // The cost/previous pairs
Dictionary Est = new Dictionary(); // Estimated " "
Node next; // The next node to add to SP
Node neighbor; // Neighbors of next;
int distance; // The distance to the current
// node.
int newdist; // Potential new distance to
// neighbor
// Initialize Est
Est.put(root, new Pair(0,null));
for(Iterator nodes = nodes(); nodes.hasMoreElements; ) {
next = nodes.nextElement();
if (!next.equals(root)) {
Est.add(next, new Pair(MAXPATH+1,null));
}
} // for
// Remove nodes from Est until it is empty.
while(!Est.empty()) {
(next,distance) = smallest(Est);
if (distance == MAXPATH+1) {
// Whatever you want to do if there is no path to some nodes
}
for(Iterator neighbors = next.neighbors();
neighbors.hasMoreElements; ) {
// Get a neighbor
neighbor = neighbors.nextElement();
// Compute the distance to that neighbor if we pass
// through the current node.
newdist = distance + weight(next,neighbor);
// If that's better than the best known distance, update
// the best known distance
if (newdist < Est.get(neighbor).distance) {
Est.put(neighbor, new Pair(newdist, next));
} // If we've found a better path
} // for each neighbor
} // while there are nodes left in Est.
// Done
return SP:
} // shortestPaths
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
Source text last modified Mon Dec 8 09:16:08 1997.
This page generated on Mon Dec 8 10:25:25 1997 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu