Dijkstra算法


#include <stdio.h>
#include <stdlib.h>
#define Max 999		                     /* 定义最大数 */
#define VertexNum 7	                     /* 定义顶点数 */
#define EdgeNum 9	                     /* 定义邻接边数 */

int	Graph[VertexNum][VertexNum];		/* 图形邻接数组	*/
int	Edge[EdgeNum][3] =			      /* 输入数据 */
		{ {1,2,6}, {1,3,3}, {2,4,5},
		  {3,2,2}, {3,4,3}, {3,5,4},
		  {4,6,3}, {5,4,2}, {5,6,5} };
int	Visited[VertexNum];	                /* 查找记录 */
int Distance[VertexNum];	                /* 距离总和 */
/* --------------------------------------------------- */
/* Dijkstra算法                                                */
/* --------------------------------------------------- */
void Dijkstra(int Begin)
{
	int	MinEdge;	     /* 最小边 */
	int	Vertex;		/* 最小边的顶点 */
	int	i,j;
	int	Edges;		/* 边数 */

	Edges = 1;		     /* 初始边数 */
	Visited[Begin] = 1;	/* 初始顶点 */

	for ( i=1;i<VertexNum;i++ )	
		Distance[i] = Graph[Begin][i];	/* 初始距离总和 */

	Distance[Begin] = 0;		           /* 起始点的距离为0 */
	printf("Vertice");	
	for ( i=1;i<VertexNum;i++ )
		printf("%5d",i);	           /* 输出顶点数据 */
	printf("\n");
	printf("Step %d :",Edges);
	for ( i=1;i<VertexNum;i++ )	
		printf("%5d",Distance[i]);      /* 输出距离总和数据 */
	printf("\n");
	while ( Edges < ( VertexNum - 1 ) )	/* 当边数少于顶点数时执行*/
	{
		Edges++;
		MinEdge = Max;	                /* 将最小边设到最大值 */
		for ( j=1;j<VertexNum;j++ )	/* 判断未建立的邻接顶点 */
		{
			/* 顶点未查找过且最小边加权值比距离总和大 */
			if ( Visited[j] == 0 && MinEdge > Distance[j] )
			{
				Vertex = j;	          /* 找出最小边的顶点 */
				MinEdge = Distance[j];	/* 找出最小边的距离总和*/
			}
		}
		Visited[Vertex] = 1;	          /* 将最小边的顶点设为已查找 */
		printf("Step %d :",Edges);
		for ( j=1;j<VertexNum;j++ )
		{
				/* 找出未查找顶点的最小距离总和 */
			if ( Visited[j] == 0 &&
			 Distance[Vertex] + Graph[Vertex][j] < Distance[j] )
			 {
				Distance[j] = Distance[Vertex] + Graph[Vertex][j];
			 }
			printf("%5d",Distance[j]);
		}
		printf("\n");
	}
}

/* --------------------------------------------------- */
/* 输出邻接数组数据                                               */
/* --------------------------------------------------- */
void Print_M_Graph()
{
	int	i,j;

	printf("Vertice");
	for ( i=1;i<VertexNum;i++)
		printf("%5d",i);
	printf("\n");
	for ( i=1;i<VertexNum;i++)
	{
		printf("%5d   ",i);
		for ( j=1;j<VertexNum;j++ )
			printf("%5d",Graph[i][j]);
		printf("\n");
	}
}

/* --------------------------------------------------- */
/* 以邻接数组建立图形                                             */
/* --------------------------------------------------- */
void Create_M_Graph(int Vertice1,int Vertice2,int Weight)
{

	Graph[Vertice1][Vertice2] = Weight;	/* 数组内容 */
}

/* --------------------------------------------------- */
/* 主程序                                                         */
/* --------------------------------------------------- */
void main ()
{
	int	BeginVertex = 1;		/*  起始顶点 */
	int	i,j;

	for ( i=0;i<VertexNum;i++ )	/* 清除查找记录 */
		Visited[i] = 0;

	for ( i=0;i<VertexNum;i++ )	/* 清除数组数据 */
		for ( j=0;j<VertexNum;j++)
			Graph[i][j] = Max;

	for ( i=0;i<EdgeNum;i++)	/* 调用建立邻接数组 */
		Create_M_Graph(Edge[i][0],Edge[i][1],Edge[i][2]);

	printf("##Graph##\n");
	Print_M_Graph();		/* 调用输出邻接数组数据 */

	printf("Dijkstra Algorithm : \n");
	Dijkstra(BeginVertex);		/* 调用Dijkstra */
	system("pause");
}

相关阅读:

http://hi.baidu.com/iohwwoilrrbcdld/item/9fee76319c62684a3175a147


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值