SPFA's full name is shortest Path Faster algorithm, a look at the name of 80% is the Chinese name, because the foreigner from the algorithm name will generally write their own name, very little modesty. In fact, this is the Gefandine of Southwest Jiaotong University, published in 1994, is aimed at the improvement of Bellman-ford algorithm. In this case, but more background, directly introduce the SPFA algorithm.
This is the single source shortest-circuit algorithm, high efficiency, and Dijkstra two points of the world. Time complexity analysis, for the moment I have not done, first excerpt 2009 Oi National training Team Guangdong Zhongshan Memorial Middle School Kangbino seniors in a paper analysis:
In general, the efficiency of the SPFA is very high, it can be proved that the expected complexity of SPFA is O (KM), k<2. But because the proof is not rigorous and the applicability is not broad (there is targeted data), not to repeat it here. However, the actual data will be used to verify the efficiency of the SPFA in subsequent tests.
Note: m above indicates the number of sides in the graph.
In fact, the idea of arithmetic is simple. Suppose the starting point is called S, and the end is T. Use (U,V) to indicate the length of the edge u->v. Still use D[i] to represent the shortest path of s->i. We use "slack" to constantly update the D that can be updated, and finally we cannot update the termination. The algorithm is as follows:
1 d←∞, queue q is initialized to null2d[s]=0Q.inch(S)3 while(!q.empty ())4 {5U=q. out();6 for(All sides u->v)7 if(d[v]>d[u]+(u,v))8 {9d[v]=d[u]+(u,v);Ten if(v not in team) Q.inch(v); One } A}
The algorithm pseudo-code is finished, SPFA is so concise. The actual code does not repeat.
In this more, suppose that there are n points in the diagram, and in the SPFA process found a point into the team n times, and also to join the first n+1 times, then the picture has a negative ring.
Just write it here for the time being.
"Algorithm" C + + code SPFA