One proven and well-known algorithm to solve the SPF problem for a given weighted directed graph and a vertex in it is the Dijkstra algorithm. Dijkstra's algorithm assumes all the weights on the graph are non-negative, but, since in OSPF there is no reason to assign negative numbers, it can and in fact is used in the OSPF protocol.
To describe the algorithm, let's first explain the various symbols that will be used in its description. The directed weighted graph will be denoted G, its group of vertices will be denoted V, and its group of edges will be denoted E. An edge will be denoted as a pair of vertices. For example, (v,u) will denote and edge starting from v and ending in u. The weight associated with the edge (v,u) will be denoted w(v,u). The algorithm works by maintaining a set S of vertices for whom we already figured out the minimum cost of path from the given vertex. The denomination d[v] will state the lowest cost of route from the given vertex to vertex v we found at a certain time. The algorithm also keeps a priority queue Q of the vertices in G, in which the vertices are ordered according to their d[v] values. In addition to all of those, for each vertex v the algorithm also denoted by p[v] the predecessor of v. The p[v] value can be either NULL or a vertex, and, when the algorithm is complete, for every vertex other than the source vertex for which the algorithm is run will have a non-NULL predecessor, and determining the lowest-cost path from v to the source vertex will be easily done by running on the predecessor of v, and the predecessor of the predecessor of v, and so on, until arriving at the source vertex.
So, given a weighted graph G and a source vertex s, the algorithm is the following:
- For every vertex v in V such that v isn't s, set d[v]=infinity and p[v]=NULL. Also set p[s]=NULL, and d[s]=0.
- S is now an empty set.
- Insert into the priority queue Q all the vertices in V.
- While Q isn't empty, do:
- Mark u as the minimum item in the priority queue Q.
- Add u to S.
- For every vertex v in the adjacency of u, if d[v] > d[u] + w(u,v), then do:
The algorithm's proof shows that the algorithm ends in a finite time, and in the end, for a given vector v and the source vector s, the path of:
s->p[p[p[p[p...[p[p[p[v]]]...]]]]] -> ... -> p[p[v]] -> p[v] -> v
is a path that is in G, and it has a cost that is equal to the lowest cost of a path from s to v in G.
Thus, Dijkstra's algorithm finds the shortest paths tree from the source vertex to every other vertex in G. Here's an example of running the algorithm on a relatively straightforward directed weighted graph: