SPFA algorithm Detailed

Source: Internet
Author: User

Program

#include <cstdio>
using namespace Std;
struct node
{int x;
int value;
int next;
};
Node e[60000];
int visited[1505],dis[1505],st[1505],queue[1000];
int main ()
{
int n,m,u,v,w,start,h,r,cur;
Freopen ("C.in", "R", stdin);
Freopen ("C.out", "w", stdout);
while (scanf ("%d%d", &n,&m)!=eof)
{
for (int i=1;i<=1500;i++)
{visited[i]=0;
Dis[i]=-1;
St[i]=-1; This initialization has implications for the while loop below
}

for (int i=1;i<=m;i++)
{
scanf ("%d%d%d\n", &u,&v,&w);
E[i].x=v; The record successor node is equivalent to creating a node in the linked list and causes the data field to be recorded first
E[i].value=w;
E[i].next=st[u]; Record the subscript of one of the edge table nodes of a vertex node, which is equivalent to the next pointer of the side table node in the linked list first points to his subsequent edge table node
St[u]=i; The pointer to the vertex points to the edge table node, which is equivalent to the insertion in the linked list, and the pointer of the head node changes
}
Start=1;
Visited[start]=1;
dis[start]=0;
h=0;
R=1;
Queue[r]=start;
while (H!=R)
{

H= (h+1)%1000;
CUR=QUEUE[H];
int tmp=st[cur];
visited[cur]=0;

while (Tmp!=-1)
{
if (Dis[e[tmp].x]>dis[cur]+e[tmp].value)
{
Dis[e[tmp].x]=dis[cur]+e[tmp].value;
if (visited[e[tmp].x]==0)
{

Visited[e[tmp].x]=1;
R= (r+1)%1000;
queue[r]=e[tmp].x;
}
}
Tmp=e[tmp].next;
}
}
printf ("%d\n", Dis[n]);
}
return 0;
}

  1. SPFA (shortest Path Faster algorithm) [graph is stored as adjacency table]
  2. is a queue implementation of the Bellman-ford algorithm, which reduces unnecessary redundancy calculations.
  3. The approximate process of the algorithm is to use a queue for maintenance. Initially, the source is added to the queue. Each time an element is taken out of the queue,
  4. and relaxes all points adjacent to him, and if an adjacent point relaxes successfully, it is enqueued. The algorithm ends until the queue is empty.
  5. It can find the shortest path from the source point to all other points in the time complexity of O (KE), and can handle negative edges.

  6. SPFA in the form and BFS very similar, the difference is the BFS in a point out of the queue will not be able to re-enter the queue, but SPFA
  7. A point may be placed in the queue again after the queue, that is, a point improved over the other points, after a period of time may be this
  8. The body is improved, so it is used again to improve the other points, so that repeated iteration down.

  9. Determine if there is a negative ring: a negative ring exists if a point enters the queue more than V (SPFA cannot process a graph with negative loops).

  10. The SPFA algorithm has two optimization algorithms SLF and LLL:
  11. Slf:small Label first policy, set the node to be added is J, team head element is I, if Dist (j) <dist (i), then J is inserted into the first team,
  12. Otherwise insert the tail of the team.
  13. Lll:large Label last policy, set the first element of the team is I, the average of all dist values in the queue is X, if dist (i) >x i insert
  14. At the end of the queue, find the next element until a certain I is found to make the dist (i) <=x, then the I-out pair for relaxation operation.
  15. Quoting online data, SLF can increase the speed of the 20%;SLF + LLL up to about 50%.
  16. In the practical application, the SPFA algorithm time efficiency is not very stable, in order to avoid the worst case occurrence, usually uses the more stable Dijkstra algorithm.
  17. */
  18. adjacency table storage with arrays, pnt[i,0] represents the number of nodes adjacent to I, PNT[I,1...K] stores the points adjacent to I
  19. int PNT[MAXN][MAXN];
  20. int MAP[MAXN][MAXN]; MAP[I,J] for the initial input of the distance from I to J, and map[i,i]=0; unknown map[i,j]=inf;
  21. int DIS[MAXN];
  22. Char VST[MAXN];
  23. int SPFA (int n,int s)
  24. {
  25. int i, PRI, end, p, t;
  26. memset (VST, 0, sizeof (VST));
  27. for (I=1; i<=n; i++)
  28. Dis[i] = INF;
  29. Dis[s] = 0;
  30. Vst[s] = 1;
  31. Q[0] = s; pri = 0; end = 1;
  32. while (PRI < end)
  33. {
  34. p = Q[pri];
  35. for (I=1; i<=pnt[p][0]; i++)
  36. {
  37. t = Pnt[p][i];
  38. Release first, and then decide if you want to join the queue
  39. if (Dis[p]+map[p][t] < dis[t])
  40. {
  41. DIS[T] = dis[p]+map[p][t];
  42. if (!vst[t])
  43. {
  44. q[end++] = t;
  45. Vst[t] = 1;
  46. }
  47. }
  48. }
  49. Vst[p] = 0;
  50. pri++;
  51. }
  52. return 1;
  53. }



SPFA algorithm Detailed

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.