An example of an algorithm-dijkstra (finding the shortest path to each vertex from the source point)

Source: Internet
Author: User

Algorithmic thinking

1. In a diagram, divide all vertices into two sets P,q (P is the shortest path set, Q is the set to be selected), and the DIS array is used to save the source point to the shortest path of each vertex (to itself 0).
2. Initialize the P-set, which is to add the source point to the collection, and mark the tag (mark[y]=1 in the code), then the Q-set is the remaining vertex composition.
3. Find such a vertex in the Q collection: the shortest path from the source point to the vertex (recorded as u), adds the point to the P collection, lists all the edges starting with u (the end point is V), and determines whether the path from the source to each v vertex (because there are multiple edges with a starting point of u) will be smaller, update dis[v] The value.
4. Repeat the 3 step until the Q set is empty, the algorithm ends, and the DIS array stores the shortest path from the source point to each vertex.

The head of the word looks big, or an example of it

Now there is a diagram of this:

We can store it in a two-dimensional array, which can be seen as:

The code implementation is like this:

#include <stdio.h>#include <string.h>//Macro#define N#define INF 999999//define global variablesintE[n][n];//Storage graph two-dimensional arrayintDis[n];//dis[i] Indicates the distance from the source point to Iintmark[Ten];//flag array, mark[i]==0 means no access, not to mentionintN,m;the number of vertices, and the number of edges of the graph/** The minimum path value of the source point (Vertex y) to each vertex, always remember the function of this algorithm OH * /voidDijkstra (intY) {intI,j,k;//Cyclic control variables    intMinintU/ * Initialize related data * /    //memset (mark,0,sizeof (Mark));    / * Get the initial value of dis * /     for(i=1; i<=n;i++) {dis[i]=e[y][i]; } mark[y]=1;//Record source point already in set P    / * This loop is meaningful Ah, experience the next ^_^ * *      for(k=1; k<=n-1; k++) {/* From the source point to the minimum value of a point, record the point information (value, location) */min = INF;//Initialize minimum value         for(i=1; i<=n;i++) {/ * If the point is in the set Q, and the value is small * /             if(mark[i]==0&& Min>dis[i]) {/ * Record Pity Dorado information * /min = dis[i];            U = i; }} mark[u]=1;//Record the point in the set P        / * Extend this point * /         for(i=1; i<=n;i++) {/ * If there is an out-side from this point, the starting point of the edge is the U endpoint is i * /             if(E[u][i] < INF) {/ * If you use the U vertex to reduce the distance from the source point to the vertex I, then the value of the new Dis[i] * /                if(Dis[i] > Dis[u]+e[u][i])                {Dis[i] = Dis[u]+e[u][i]; }             }        }    }//Output The minimum path from the source point to each point     for(i=1; i<=n;i++) {printf("%d", Dis[i]); }}intMain () {intA,b,c,y;/ * Number of vertices read in, number of edges * /     scanf("%d%d", &n,&m);/* Initialization diagram, each vertex to its own distance is 0, the other first initialized to a non-reachable state */      for(intI=1; i<=n;i++) { for(intj=1; j<=n;j++) {if(I==J) {e[i][j]=0; }Else{E[i][j]=inf; }        }    }//Read into each side of the information, start, end, weight value     for(intI=1; i<=m;i++) {scanf("%d%d%d", &a,&b,&c);    E[a][b]=c; }/ * Read into source point * /     scanf("%d", &y); Dijkstra (y); }/***** test Data 6 91 2 11 3 122 3 92 4 33 5 54 3 44 5 134 6 155 6 41**** test Results 0 1 8 4

What do you think.... This algorithm does not know .... Welcome to the wrong point!

An example of an algorithm-dijkstra (finding the shortest path to each vertex from the source point)

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.