最短路径程序实现:
#include<stdio.h>
#include<stdlib.h>
#define VEXNUM 6//顶点数
#define ARCNUM 10//边数
#define INT_MAX 10000//先定义最大权值;
struct Graph{
int vexs[VEXNUM+1];//顶点数组;
int a[VEXNUM+1][VEXNUM+1];//邻接矩阵;
int val[VEXNUM+1][VEXNUM+1];//权值;
};
void create(struct Graph *);//创建图;
void min_length(struct Graph *);//最短路径实现函数;
void main()
{
struct Graph *g=NULL;
g=(struct Graph *)malloc(sizeof(struct Graph));//分配空间;
create(g);
min_length(g);
}
void create(struct Graph *g)
{
int i,j,n,x,y,z;
FILE *fp;
fp=fopen("E:\\min1.txt","r");
for(n=1;n<=VEXNUM;n++)
g->vexs[n]=n;
for(i=1; i<=VEXNUM; i++)
for(j=1; j<=VEXNUM; j++){
g->a[i][j] = 0;
g->a[j][i] = g->a[i][j];
}
for(i=0; i<ARCNUM; i++){
//fscanf(fp,"%d,%d,%d",&x,&y,&z);错误输入格式
fscanf(fp,"%d%d%d",&x,&y,&z);// printf("%d %d %d\n",x,y,z);
g->a[x][y] = 1;
g->a[y][x]=g->a[x][y];
g->val[x][y]=z;
g->val[y][x]=g->val[x][y];
}
fclose(fp);
for(i=1; i<=VEXNUM; i++)
{
for(j=1; j<=VEXNUM; j++)
if(g->val[i][j]<0)
g->val[i][j]=0;
}
/*for(i=1; i<=VEXNUM; i++)
{
for(j=1; j<=VEXNUM; j++)
{
printf("%d\t",g->a[i][j]);
}
printf("\n");
}
printf("\n");printf("\n");printf("\n");
for(i=1; i<=VEXNUM; i++)
{
for(j=1; j<=VEXNUM; j++)
if(g->val[i][j]>0)
printf("%d\t",g->val[i][j]);
else{
g->val[i][j]=0;
printf("%d\t",g->val[i][j]);
}
printf("\n");
}*/ //显示邻接矩阵
}
void min_length(struct Graph *g)
{
int disk[VEXNUM+1];
int pre[VEXNUM+1];
int i,k,x,y;
int s=1;
for(i=1;i<=VEXNUM;i++){
pre[i]=0;
if(g->a[1][i]==1)
disk[i]=g->val[1][i];
else
disk[i]=INT_MAX;
}
disk[1]=0;pre[1]=1;
printf("删除结点:1\n");
while(s<VEXNUM){
int min=INT_MAX;
for(i=1;i<=VEXNUM;i++)
if(pre[i]==0&&min>disk[i])
min=disk[i];
for(i=1;i<=VEXNUM;i++)
if(pre[i]==0&&min==disk[i]){
k=i;
break;
} pre[k]=1;
printf("删除结点:%d\n",k);
for(i=1;i<=VEXNUM;i++)
{
if(pre[i]==0&&g->a[k][i]==1){
x=disk[i];y=disk[k]+g->val[i][k];
if(x>y)
disk[i]=y;
}
}
s++;
}
for(i=1;i<=VEXNUM;i++)
printf("disk[%d]=%d\n",i,disk[i]);//for(i=1;i<=VEXNUM;i++)
}
附:图的临界表存储结构程序实现:
#include <stdio.h>
#include <stdlib.h>
#define MaxVertexNum 100
typedef char VertexType; //自定义定点类型
typedef struct node //边表节点
{
int adjvex;
node* next;
}EdgeNode;
typedef struct //顶点表节点
{
VertexType vertex;
EdgeNode* firstedge;
}VertexNode;
typedef VertexNode AdjList[MaxVertexNum]; //自定义定点表节点类型
typedef struct
{
AdjList adjlist;
int n,e; //顶点数跟边数
}ALGraph; //图的结构体
void create(ALGraph*);
void main()
{
ALGraph* G= (ALGraph*)malloc(sizeof(ALGraph)); //创建图变量,并且分配空间
create(G);
for (int i=0;i< G->n;i++)
{
printf("%d->",i);
while(G->adjlist[i].firstedge!=NULL)
{
printf("%d->",G->adjlist[i].firstedge->adjvex);
G->adjlist[i].firstedge=G->adjlist[i].firstedge->next;
}
printf("\n");
}
}
void create(ALGraph* G)
{
int i,j,k,w,v;
EdgeNode *s;
printf("读入顶点数和边数");
scanf("%d,%d",&G->n,&G->e);
for (i=0;i<G->n;i++) //初始化
{
fflush(stdin); //用来清空输入缓存,以便不影响后面输入的东西
printf("建立顶点表");
G->adjlist[i].vertex=getchar();
G->adjlist[i].firstedge=NULL;
}
printf("建立边表\n");
for (k=0;k<G->e;k++)
{
printf("读入(vi-vj)的顶点对序号");
scanf("%d,%d",&i,&j);
s=(EdgeNode*)malloc(sizeof(EdgeNode));
s->adjvex=j;
s->next=G->adjlist[i].firstedge; //插入表头
G->adjlist[i].firstedge=s;
//如果是无向图,则需要创建下面操作
/* s=(EdgeNode*)malloc(sizeof(EdgeNode));
s->adjvex=i;
s->next=G->adjlist[j].firstedge;
G->adjlist[j].firstedge=s; */
}
}