Dijkstra algorithm (Digestala algorithm)

Source: Internet
Author: User

Algorithm Description:

The Dijkstra (Dijkstra) algorithm is a typical shortest path routing algorithm used to calculate the shortest path of a node to all other nodes. The main feature is to extend from the center of the starting point to the outer layer until it expands to the end point. The Dijkstra algorithm can get the optimal solution of the shortest path, but it is inefficient because it traverses many nodes.

Dijkstra algorithm is a very representative of the shortest path algorithm, in many professional courses as the basic content of the detailed introduction, such as data structure, graph theory, operations research and so on.

The basic idea is to set up the vertex set S and continue to make greedy choices to augment this set. A vertex belongs to the set S when and only if the shortest path length from the source to the vertex is known. Initially, S contains only the source. Set U is a vertex of G, the path from source to u and only through the vertices of s in the middle is called a special path from source to u, and the shortest special path length corresponding to each vertex is recorded with array Dist. The Dijkstra algorithm removes the vertex u with the shortest special path length from the V-s each time, adds u to S, and makes the necessary modifications to the array dist. Once s contains all the vertices in V, the Dist records the shortest path length from source to all other vertices.

Code implementation:

#include <stdio.h> #include <stdlib.h> #define MAXVERTEX 20#define INF 65535typedef Char vertextype;typedef  int edgetype;typedef int pathmatirx[maxvertex];typedef int shortpathtable[maxvertex];typedef struct MGraph{VertexType    Vertex[maxvertex];    Edgetype Edge[maxvertex][maxvertex];    int Numvex; int Numedge;}    Mgraph;void createmgraph (mgraph *g) {int i = 0,j = 0,k = 0,w = 0;    printf ("Please enter the number of vertices in the graph and the number of edges, separated by commas: \ n");    scanf ("%d,%d", &g->numvex,&g->numedge);            for (i = 0;i < g->numvex;i++) {for (j = 0;j < g->numvex;j++) {if (i = = j)            {G->edge[i][j] = 0;            } else {g->edge[i][j] = INF;        }}} for (k = 0;k < g->numedge;k++) {printf ("Please enter the subscript I and J for Edge VI-VJ, and the weight w: \ n");        scanf ("%d,%d,%d", &i,&j,&w);        G-&GT;EDGE[I][J] = W;    G->edge[j][i] = g->edge[i][j]; } printf ("\ n "); for (i = 0;i < g->numvex;i++) {for (j = 0;j < g->numvex;j++) {printf ("%d", g->ed        GE[I][J]);    } printf ("\ n");    The}}//dijkstra algorithm implements void Shortestpathbydijkstra (Mgraph *g,int v0,pathmatirx *p,shortpathtable *D) {int i,j,k,w,min;    int Final[maxvertex];        for (i = 0;i < g->numvex;i++) {Final[i] = 0; (*d)        [I] = g->edge[v0][i]; (*p)    [I] = 0;    } Final[v0] = 1; (*d)    [V0] = 0;        for (i = 1;i <g->numvex;i++) {min = INF;                for (j = 0;j < g->numvex; j + +) {if (final[j] = = 0 && min > (*d) [j]) {                min = (*d) [j];            K = J;        }} Final[k] = 1;            for (w = 0;w < g->numvex;w++) {if (final[w] = = 0 && (min + g->edge[k][w] < (*D) [W]))                {(*D) [W] = min + g->edge[k][w]; (*p)            [W] = k;    }        }    }for (i = 0;i < g->numvex;i++) {printf ("%d", (*p) [i]);    }}int Main () {int k = 0;    struct Mgraph G;    Pathmatirx P;    Shortpathtable D;    Createmgraph (&AMP;G);    printf ("The shortest path V0 to v* obtained by the Dijkstra algorithm is: \ n");    Shortestpathbydijkstra (&AMP;G,K,&AMP;P,&AMP;D); return 0;}

  

Dijkstra algorithm (Digestala 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.