Back in 2000, I posted a
Graph Search unit that contained the data structure describing graphs
(nodes and edges). It has "depth first" and "breadth
first" search procedures that look for ways to get from some starting
node to some goal node. I recently modified the structure to allow
weights to be associated with the edges which connect nodes.
A new procedure implements "Dijkstra's Shortest Path Search algorithm, a
clever and relatively efficient way to search for shortest paths between
nodes. The algorithm finds the shortest paths from a specified
start node to any other reachable node (or all reachable nodes) for graphs
with positive weights (or costs, or distances) between nodes.
Edsger Wybe Dijkstra, programmer, professor, and mathematician,
invented and published his Shortest Path algorithm early in his career, around
1960 as best I can determine. He died in 2002, but you can find many of
his writings and learn more about his somewhat eccentric character by
searching on his full name. I would like to have known
him.
The immediate motivation was problem number 83 in the Euler Project
programming challenges at
ProjectEuler.net,
a great site if you think you are a pretty good programmer. There are
other sections of the site with lots of good math problems that do not require
programming skills. In any event, Problem 83 asks for
the shortest path from top left to bottom right of an 80x80 matrix of positive
integers. Moves can be made up, down, left or right which is what makes
the problem hard. The demo program included here does not solve that
problem, but could be adapted to do so, if you figure out how to represent the
matrix as a graph.
The demo randomly assigns weights to the same graph used in the 10 node
graph in the original Graph Search demo program and calls the Dijkstra search
procedure to find the shortest path. I have included a
"verbose" feature that will let you see algorithm working step by
step. It works so well that I found several ways to improve the
program when I first ran "verbosely". I think I'll
add the same feature to Depth and Breadth first procedures one of these
days.