dijkstra算法C语言实现

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define NUM 7

typedef struct MGraph		/* 邻接表存储结构 */
{
     int edges[NUM][NUM];
     int n,e;
} MGraph;

MGraph *build_mgraph();
void Dijkstra(MGraph *mgraph, int v);
void Dispath(int dis[],int pre[],int visited[], int n, int v);
void Ppath(int path[], int i, int v);

int main(void)
{
     MGraph *mgraph;

     printf("\n*************************************************************\n");
     printf("该图的矩阵表示为:\n");
     mgraph=build_mgraph();
     printf("\n*************************************************************\n");
     printf("dijkstra 算法:\n");
     Dijkstra(mgraph,0);
     printf("\n*************************************************************\n");
     
     return 0;
}

MGraph *build_mgraph()
{
     int i,j;
     int num_e=0;
     MGraph *mgraph=(MGraph *)malloc(sizeof(MGraph));
     int matrix[NUM][NUM]={{0,4,6,6,INT_MAX,INT_MAX,INT_MAX},
			   {INT_MAX,0,1,INT_MAX,7,INT_MAX,INT_MAX},
			   {INT_MAX,INT_MAX,0,INT_MAX,6,4,INT_MAX},
			   {INT_MAX,INT_MAX,2,0,INT_MAX,5,INT_MAX},
			   {INT_MAX,INT_MAX,INT_MAX,INT_MAX,0,INT_MAX,6},
			   {INT_MAX,INT_MAX,INT_MAX,INT_MAX,1,0,8},
			   {INT_MAX,INT_MAX,INT_MAX,INT_MAX,INT_MAX,INT_MAX,0}};
     for(i=0;i<NUM;i++)
     {
	  for(j=0;j<NUM;j++)
	  {
	       mgraph->edges[i][j]=matrix[i][j];
	       if(matrix[i][j]!=0 && matrix[i][j]!=INT_MAX)
		    num_e++;
	  }
     }
     mgraph->n=NUM;
     mgraph->e=num_e;

     printf("node=%d;edges=%d\n",mgraph->n,mgraph->e);
     for(i=0;i<NUM;i++)
     {
	  for(j=0;j<NUM;j++)
	  {
	       if(mgraph->edges[i][j]!=INT_MAX)
		    printf("%3d",mgraph->edges[i][j]);
	       else
		    printf("%3c",'&');
	  }
	  printf("\n");
     }

     return mgraph;
}

void Dijkstra(MGraph *mgraph, int v)
{
     int i,j;
     int no;
     int min_dis;
     int dis[NUM];
     int visited[NUM]={0};
     int pre[NUM];

     visited[v]=1;
     for(i=0;i<mgraph->n;i++)
     {
	  dis[i]=mgraph->edges[v][i];
	  pre[i]=v;
     }
     
     for(i=0;i<mgraph->n;i++)
     {
	  min_dis=INT_MAX;
	  for(j=0;j<mgraph->n;j++)
	  {
	       if(!visited[j] && dis[j]<min_dis)
	       {
		    min_dis=dis[j];
		    no=j;
	       }
	  }

	  visited[no]=1;
	  for(j=0;j<mgraph->n;j++)
	  {
	       if(!visited[j] && mgraph->edges[no][j]!=INT_MAX && dis[no]+mgraph->edges[no][j] < dis[j])
	       {
		    dis[j]=dis[no]+mgraph->edges[no][j];
		    pre[j]=no;
	       }
	  }
     }
     
     Dispath(dis,pre,visited,mgraph->n,v);
}

void Dispath(int dis[],int pre[],int visited[], int n, int v)
{
     int i;
     for(i=0;i<n;i++)
     {
	  if(visited[i])
	  {
	       printf("从%d到%d的最短路径长度为:%d\t路径为:",v,i,dis[i]);
	       printf("%d, ",v);
	       Ppath(pre,i,v);
	       printf("%d\n",i);
	  }
	  else
	       printf("no path\n");
     }
}

void Ppath(int pre[], int i, int v)
{
     int k;
     k=pre[i];
     if(k==v)
	  return;
     Ppath(pre,k,v);
     printf("%d, ",k);
}

    原文作者:Dijkstra算法
    原文地址: https://blog.csdn.net/xiaowang627/article/details/51588843
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞