C language implements Dixtra's algorithm

#Using the c language to implement Dikstra
's algorithm. There are mature algorithms for finding the shortest path, such as Dikstra's algorithm. The idea is to get the shortest distance of each vertex from near to far according to the distance.

  首先,构建结构体,其代码如下:
struct Dijkstra {
    
    
	int w[Maxsize][Maxsize];                     //权重距离矩阵
	int l[Maxsize];                              //依次求得各点到起点的最短距离
	int s;                                       //顶点个数
}path;

Then, we will sort the distance matrix input from the keyboard and output:

void Dijkstra_mat()
{
    
    
	for (int i = 0; i < path.s; i++)
	{
    
    
		for (int j = 0; j < path.s; j++)
		{
    
    
			printf("%-6d", path.w[i][j]);
		}
		printf("\n");
	}
	printf("********************\n");
}

We know that the core of Dixter's algorithm is to update the assignment matrix to find the shortest path. Therefore, we need to update the shortest path. The code is as follows:

int min(int a, int b)                            //更新赋值路
{
    
    
	if (a <= b)
		return a;
	if (a > b)
		return b;
}

In the C language, using the core part of Dijkstra's algorithm and calling the above functions, the available code is:

void Dijkstra()
{
    
    
	int u = 0, v;          //u为起点,v为距u最短点
	for (int i = 0; i < path.s; i++)
	{
    
    
		for (int v = 0; v < path.s; v++)
		{
    
    
			path.l[v] = min(path.l[v], path.l[u] + path.w[u][v]);
		}
		u = the_mins(u);
		i++;
	}
	for (int j = 0; j < path.s; j++)
	{
    
    
		printf("到第%d个地方的距离为%4d\n", j + 1, path.l[j]);
	}
}

The role of the main function is to assign values ​​to the distance matrix and call other functions to finally get the complete Dikstra algorithm:

int main()
{
    
    
	int path_sum = 0;                                //最初的路径为0 
	printf("请输入顶点个数:");
	scanf_s("%d", &path.s);
	printf("请输入距离矩阵:");
	for (int i = 0; i < path.s; i++)                  //距离赋权矩阵赋值
	{
    
    
		for (int j = 0; j < path.s; j++)
		{
    
    
			scanf_s("%d", &path.w[i][j]);
			if (path.w[i][j] == -1)
				path.w[i][j] = INF;
			printf("   ");
		}
		printf("\n");
	}
	for (int i = 0; i < path.s; i++)
	{
    
    
		if (i == 0)
			path.l[i] = 0;
		else
			path.l[i] = INF;
	}
	printf("得到的距离矩阵为:\n");
	Dijkstra_mat();
	Dijkstra();
}

Obtained complete code:

#include<stdio.h>

#define INF 9999
#define Maxsize 50

struct Dijkstra {
    
    
	int w[Maxsize][Maxsize];                     //权重距离矩阵
	int l[Maxsize];                              //依次求得各点到起点的最短距离
	int s;                                       //顶点个数
}path;

int min(int a, int b)                            //更新赋值路
{
    
    
	if (a <= b)
		return a;
	if (a > b)
		return b;
}

void Dijkstra_mat()
{
    
    
	for (int i = 0; i < path.s; i++)
	{
    
    
		for (int j = 0; j < path.s; j++)
		{
    
    
			printf("%-6d", path.w[i][j]);
		}
		printf("\n");
	}
	printf("********************\n");
}
int the_mins(int u)                        //以距离上一个点最短的点为起点
{
    
    
	int v = u, j = 0;
	int min, step;
	for (int i = 0; i < path.s; i++)
	{
    
    
		if (path.l[i] > path.l[u])
		{
    
    
			j++;
			if (j == 1)
			{
    
    
				step = path.l[i];
				min = path.l[i];
				v = i;
			}
			if (path.l[i] < step)
			{
    
    
				min = path.l[i];
				v = i;
			}
		}
	}
	return v;
}

void Dijkstra()
{
    
    
	int u = 0, v;          //u为起点,v为距u最短点
	for (int i = 0; i < path.s; i++)
	{
    
    
		for (int v = 0; v < path.s; v++)
		{
    
    
			path.l[v] = min(path.l[v], path.l[u] + path.w[u][v]);
		}
		u = the_mins(u);
		i++;
	}
	for (int j = 0; j < path.s; j++)
	{
    
    
		printf("到第%d个地方的距离为%4d\n", j + 1, path.l[j]);
	}
}

int main()
{
    
    
	int path_sum = 0;                                //最初的路径为0 
	printf("请输入顶点个数:");
	scanf_s("%d", &path.s);
	printf("请输入距离矩阵:");
	for (int i = 0; i < path.s; i++)                  //距离赋权矩阵赋值
	{
    
    
		for (int j = 0; j < path.s; j++)
		{
    
    
			scanf_s("%d", &path.w[i][j]);
			if (path.w[i][j] == -1)
				path.w[i][j] = INF;
			printf("   ");
		}
		printf("\n");
	}
	for (int i = 0; i < path.s; i++)
	{
    
    
		if (i == 0)
			path.l[i] = 0;
		else
			path.l[i] = INF;
	}
	printf("得到的距离矩阵为:\n");
	Dijkstra_mat();
	Dijkstra();
}

For example, we can use the above code to find the shortest distance in the figure below
Insert picture description here

Calling this function, we get
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41516983/article/details/115272181