SPFA algorithm-Single source Shortest path algorithm

Source: Internet
Author: User

1. Introduction:

  SPFA algorithm: Single source shortest path algorithm, an efficient shortest path algorithm!

2. Ideas

  (1) initialization

    1 > Source point path is 0:d[s]=0, where S is the source point

    2 > Initialize d[n] to infinity, i.e. d[i], source point s to I for Infinity INF

3>p[n] initialized to the source point s or-1, indicating no precursor

  (2) Queue + slack

     1> Read the team head point U, and the team head point u out of the team (remember to eliminate the mark);

2> will be relaxed with all point v connected to the point U, and if the estimated value can be updated (even if d[v] becomes smaller), then update;

3> In addition, if Point V is not in the queue, then the point v should be enqueued (remember Mark);

4> if it's already in the queue, then you don't have to go.

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

voidSPFA (intSintt)//s is the source point and T is the end point {inti,j,top,front,rear;  for(i=1; i<=n;i++) Dist[i]=inf;//Initialize memset (Vis,0,sizeof(VIS)); Dist[s]=0;//source point path is 0 Vis[s]=1;//visited front=rear=0; Q[rear++]=s;//no precursor . while(front<rear) {Top=q[front++]; Vis[top]=0;  for(i=first[top];i!=-1; i=Edge[i].next) {J=edge1[i].to; if(dist[j]>dist[top]+EDGE[I].W)//slack {DIST[J]=dist[top]+EDGE[I].W; if(!Vis[j]) {Vis[j]=1; Q[rear++]=J; }            }        }    }}

SPFA algorithm-Single source Shortest path algorithm

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.