A brief understanding of Dijkstra (Dijkstra) algorithm and C language implementation (source code)

Source: Internet
Author: User

Dijkstra (Dijkstra) algorithm: To find the shortest path algorithm, data structure in the course of learning content.

1. Understand

Algorithm idea:: Set g= (V,e) is a weighted graph, the graph of the vertex set V into two groups, the first set of the shortest path of the vertex set (in s), the initial s only one source point, each subsequent to a shortest path, will be added to the set S, until all the vertices are added to the S, The algorithm ends, and the second set is the vertex set (denoted by u) of the remaining indeterminate shortest paths, and the second set of vertices is added to s in order of ascending shortest path length. During the join process, the shortest path length that is always maintained from the source Point V to the vertices in S is not longer than the shortest path length from the source point V to any vertex in U. In addition, each vertex corresponds to a distance, the distance from the vertex in S is the shortest path length from V to the vertex, and the distance from the vertex in U is the current shortest path length from V to the vertex that includes only the vertex in S as the middle vertex.

The implementation steps of the algorithm:

(1) Initially, S contains only the source point, that is, the distance of S=,v is 0. U contains other vertices except V, where the vertex u distance is the right on the edge (if V and u have edges) or) (if u is not the out edge adjacency Point of V).

(2) Select from U a distance v the smallest vertex K, the K, join the S (the selected distance is the shortest path length V to k).

(3) The distance of each vertex in the U is modified with K as the new consideration, and if the distance from the source point v to the vertex u (through vertex K) is shorter than the original distance (without the vertex K), the distance value of the vertex U is modified, the distance of the vertex k of the modified distance value and the right of the edge.

(4) Repeat steps (2) and (3) until all vertices are contained in S.

Concrete implementation of the thinking:

Minlen (i) =min{minlen (i), Minlen (j) +a[j][i]}

Wherein, I is the target point, J is a point before I, traverse all other points for J, if not adjacent, then A[j][i]=max.

This implements the following code.

2. The initial realization of C language

#include <stdio.h>
#include <stdlib.h>

#define N 7
#define MAX 0X0FFFFFFF

typedef struct _NODE
{
int way[n-1];
int Len;
}node;

void Addway (node *nd,int n,int type)//type=1 means finding a new minimum path, type=0 means finding the smallest path
{
int i=0;
while (nd->way[i]!=0&&i<n-1)
{
i++;
}
if (i>=n-1)
{
printf ("The node ' s way[] is full.");
}
Else
{
if (0==type)
nd->way[i]=n;
if (1==type)
nd->way[i-1]=n;
}

}

void Dkstra (node nd[],int (*a) [N])
{
int i=0,j=0;

for (i=1;i<n;i++)
{
int tag=0;
for (j=0;j<n;j++)
{
if (Nd[i].len<max&&nd[j].len+a[j][i]<max)
tag=1;
Nd[i].len<= (Nd[j].len+a[j][i])? (Nd[i].len=nd[i].len):(nd[i].len= (Nd[j].len+a[j][i]), Addway (&nd[i],j+1,tag));
tag=0;
}
}

}

int main ()
{
int i=0,j=0,x=0,y=0,le=0;
int a[n][n];
for (i=0;i<n;i++)//assignment default path length Max
{
for (j=0;j<n;j++)
{
A[i][j]=max;
}
}
for (; x!=-1;)//Manual weighting value
{
scanf ("%d,%d,%d", &x,&y,&le);
A[x-1][y-1]=le;
A[y-1][x-1]=le;
}
a[0][0]=0;
Node nd[n]={0};
for (i=0;i<n;i++)
{
Nd[i].len=a[0][i];
}

Dkstra (Nd,a);
for (i=0;i<n;i++)
{
j=0;
printf ("Node%d: \ n Best way:1", i+1);
while (nd[i].way[j]!=0)
{
printf (",");
printf ("%d", nd[i].way[j]);

j + +;
}
printf (",");
printf ("%d", i+1);
printf ("\nminlength:%d\n", Nd[i].len);
}
return 0;
}

Test Case:

Test results:

Problem, every I point depends on the J point, using the above code, when the first few points of the Minlen, will be wrong?

Test Case: Test Result:

found that minlen[2] is incorrect. The reason for this is that when looking for minlen[2], minlen[6] is still max, so no real shortest path can be traversed. This time we cycle again, because the following shortest path has been found, at this time can not find the real shortest path?

Code Modification Section:

void Dkstra (node nd[],int (*a) [N])
{
int i=0,j=0;

for (i=1;i<n;i++)
{
int tag=0;
for (j=0;j<n;j++)
{
if (Nd[i].len<max&&nd[j].len+a[j][i]<max)
tag=1;
Nd[i].len<= (Nd[j].len+a[j][i])? (Nd[i].len=nd[i].len):(nd[i].len= (Nd[j].len+a[j][i]), Addway (&nd[i],j+1,tag));
tag=0;
}
}

for (i=1;i<n;i++)
{
int tag=0;
for (j=0;j<n;j++)
{
if (Nd[i].len<max&&nd[j].len+a[j][i]<max)
tag=1;
Nd[i].len<= (Nd[j].len+a[j][i])? (Nd[i].len=nd[i].len):(nd[i].len= (Nd[j].len+a[j][i]), Addway (&nd[i],j+1,tag));
tag=0;
}
}
}

The test case is still the diagram above and the test results are as follows:

At this point, the minimum path data is basically correct, but the test is not much, and the code is not sure. And because of Addway (), the node information of the correct path cannot be saved. These questions await further refinement.

A brief understanding of Dijkstra (Dijkstra) algorithm and C language implementation (source code)

Related Article

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.