"Algorithm" C + + code SPFA

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.