Reprint: SPFA Algorithm Learning

Source: Internet
Author: User

Reprint Address: http://www.cnblogs.com/scau20110726/archive/2012/11/18/2776124.html

Roughly speaking the principle of SPFA algorithm, SPFA algorithm is 1994 XI ' an Jiaotong university Gefandine put forward

is an algorithm to find the shortest single source

The main variables to be used in the algorithm

int n; Represents n points, numbering from 1 to n

int s,t; S is the source point and T is the end point

int d[n]; D[i] Indicates the shortest path of the source point S to I

int p[n]; Record path (or record precursor)

Queue <int> q; A queue, with the STL implementation, of course, there is a hand in the queue, does not matter

BOOL Vis[n]; Vis[i]=1 indicates that the point I vis[i]=0 in the queue is not in the queue

Almost all of the shortest-path algorithms can be divided into two steps.

1. Initialization

2. Slack operation

Initialization: The D array is all assigned the INF (infinity), the P array is all assigned s (that is, the source point), or the assignment is 1, indicating that the precursor has not been known

Then d[s]=0; Indicates that the source point does not need to find the shortest path, or that the short circuit is 0. queue the source point;

(also remember that the entire algorithm has vertices queued to remember to mark the VIS array, there are vertices out of the team remember to eliminate the mark)

Queue + slack operation

Read the team head point U, and the team head point u out of the team (remember to eliminate the mark), the point U connected to all point v relaxation operation, if you can update the estimated value (even if d[v] smaller), then update, in addition, if the point V is not in the queue, then the point v is queued (remember Mark), if already in Then you don't have to be in the queue

In this loop, until the team is empty, the shortest-single source solution is completed.

SPFA can handle the negative weight side

Theorem: As long as the shortest path exists, the above SPFA algorithm must be able to find the minimum value.

Prove:

Each time the point is put into the tail, it is achieved by relaxation operation. In other words, each time the optimization will have a point V of the shortest path estimate d[v] becomes smaller. So the execution of the algorithm will make D smaller. Since we assume that there is no negative weight loop in the diagram, each node has a shortest path value. Therefore, the algorithm does not execute indefinitely, as the D value gradually becomes smaller, until the shortest path value is reached, the algorithm ends, and the shortest path estimate is the shortest path value of the corresponding node. (Certificate of Completion)

The expected time complexity O (ke), where k is the average number of incoming teams for all vertices, can prove that K is generally less than or equal to 2.

To determine whether there is a negative ring:

A negative ring exists if a point enters the queue more than n times (SPFA cannot process a graph with negative loops)

SPFA two kinds of writing, BFS and dfs,bfs discriminant negative ring instability, equivalent to limit depth search, but set good words still no problem, Dfs words judgement negative ring quickly

int Spfa_bfs (Ints) {Queue <Int>Q memset (D,0x3fsizeof(d)); d[s]=0; Memset (c,0,sizeof(c)); memset (Vis,0,sizeof(VIS)); Q.push (s); vis[s]=1; c[s]=1;//Vertex queue vis to do mark, in addition to count the number of vertices of the queueint ok=1;while (!Q.empty ()) {IntX X=q.front (); Q.pop (); vis[x]=0;//Team head element out of the team and eliminate the markForint k=f[x]; k!=0; K=NNEXT[K])//Traverse the adjacency table of vertex x{int y=V[K];if (D[x]+w[k) < D[y]) {D[y]=d[x]+w[k]; // relax if (!vis[y]) // Vertex y not in the team  {Vis[y]=1; // Mark c[y]++; // statistics Q.push (y); // queue if (C[Y]>NN) Span style= "color: #008000;" >// exceeds the maximum number of queued times, indicating a negative ring return ok= 0return OK;       

int Spfa_dfs (Intu) {vis[u]=1;Forint K=f[u]; k!=0; k=e[k].next) {int v=e[k].v,w=E[K].W; if (d[u]+w < D[v]) {d[v]=d[u]+w; if (! Vis[v]) {if (Spfa_dfs (v)) return 1else return 1;} vis[u ]=0return 0    

Reprint: SPFA Algorithm Learning

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.