(c语言)小白实现无权图单源最短路径(邻接表实现附测试用例)

本博文源于浙江大学《数据结构》,今天学习了无权图的单源最短路径问题,整体用了类似于广度优先搜索的程序。因此代码冗长(都是队列代码太长了),只要把代码拼接成功,最后都能实现,下面给出我的测试样例:
博主想要画出这样的图:
在这里插入图片描述

7是顶点个数,12是边的个数。模仿mooc里的那张图,大家都懂!

7 12
2 0
2 5
0 1
0 3
1 4
1 3
3 2
3 5
3 6
3 4
4 6
6 5

没有权重,边已经指定好了,核心代码是这些,其他都是零碎的东西,文末完整有代码

void Unweighted ( LGraph Graph, int dist[], int path[], Vertex S )
{
    Queue Q;
    Vertex V;
    PtrToAdjVNode W;
     
    Q = CreateQueue( Graph->Nv ); /* 创建空队列, MaxSize为外部定义的常数 */
    dist[S] = 0; /* 初始化源点 */
    AddQ (Q, S);
 
    while( !IsEmptyQ(Q) ){
        V = DeleteQ(Q);
        for ( W=Graph->G[V].FirstEdge; W; W=W->Next ) /* 对V的每个邻接点W->AdjV */
            if ( dist[W->AdjV]==-1 ) { /* 若W->AdjV未被访问过 */
                dist[W->AdjV] = dist[V]+1; /* W->AdjV到S的距离更新 */
                path[W->AdjV] = V; /* 将V记录在S到W->AdjV的路径上 */
                AddQ(Q, W->AdjV);
            }
    } /* while结束*/
}

下面是完整的代码(邻接表的哟)!

//无权图的单源最短路径实现,邻接表
#include<stdio.h>
#include<stdlib.h>
#define MaxVertexNum 10
#define false 0
#define true 1
#define ERROR -1
typedef int Vertex;
typedef int WeightType;
typedef char DataType;
typedef int ElementType;
typedef struct ENode *PtrToENode;
int Visited[MaxVertexNum]={false};

struct ENode {
	Vertex V1, V2;
	WeightType Weight;
};
typedef PtrToENode Edge;

typedef struct AdjVNode *PtrToAdjVNode;


struct AdjVNode {
	Vertex AdjV; // 邻接点的下标
	WeightType Weight;//边的权重
	PtrToAdjVNode Next;
};

typedef struct Vnode{
	PtrToAdjVNode FirstEdge;
	DataType Data;//存顶点得数据
}AdjList[MaxVertexNum];

typedef struct GNode *PtrToGNode;
struct GNode {
	int Nv;//顶点数
	int Ne;//边数
	AdjList G;//邻接表
};
typedef PtrToGNode LGraph;


struct QNode{
	ElementType *Data;
	int rear;
	int front;
	int MaxSize;
};
typedef struct QNode *Queue;

Queue CreateQueue(int MaxSize){
	Queue Q = (Queue)malloc(sizeof(struct QNode));
	Q->Data = (ElementType *) malloc(MaxSize * sizeof(ElementType));
	Q->front = Q->rear = 0;
	Q->MaxSize = MaxSize;
	return Q;
	
}
int IsFullQ(Queue Q){
	
	return ((Q->rear+1)%Q->MaxSize == Q->front);
}
void AddQ(Queue Q,ElementType item){
	if(IsFullQ(Q)){
		printf("Queue is full\n");
		return ;
	}
	Q->rear = (Q->rear+1) %Q->MaxSize;
	Q->Data[Q->rear] = item;
}
int IsEmptyQ(Queue Q){
	return (Q->front == Q->rear);
}
ElementType DeleteQ(Queue Q){
	if(IsEmptyQ(Q))
	{
		printf("Queue is empty\n");
		return ERROR;
		
	}else{
		Q->front = (Q->front+1)%Q->MaxSize;
		return Q->Data[Q->front];
	}
}

LGraph CreateGraph(int VertexNum)
{
	Vertex V;
	LGraph Graph;
	
	Graph = (LGraph)malloc(sizeof(struct GNode));
	Graph->Nv = VertexNum;
	Graph->Ne = 0;
	
	for(V = 0;V<Graph->Nv;V++){
		Graph->G[V].FirstEdge = NULL;
		Graph->G[V].Data = V;
	}
	
	return Graph;
}

void InsertEdge(LGraph Graph, Edge E)
{
	PtrToAdjVNode NewNode;
	
	NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
	NewNode->AdjV = E->V2;
	NewNode->Weight = E->Weight;
	//将v2插入v1的表头
	NewNode->Next = Graph->G[E->V1].FirstEdge;
	Graph->G[E->V1].FirstEdge = NewNode;
	
	// NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
	// NewNode->AdjV = E->V1;
	// NewNode->Weight = E->Weight;
	// //将v2插入v1的表头
	// NewNode->Next = Graph->G[E->V2].FirstEdge;
	// Graph->G[E->V2].FirstEdge = NewNode;	
}

LGraph BuildGraph()
{
	LGraph Graph;
	Edge E;
	Vertex V;
	int Nv,i;
	scanf("%d",&Nv);//读入顶点个数
	Graph = CreateGraph(Nv);//初始化有Nv个顶点但没有边的图
	
	scanf("%d",&(Graph->Ne));
	if(Graph->Ne != 0)
	{
		E = (Edge)malloc(sizeof(struct ENode));
		for(i=0;i<Graph->Ne;i++)
		{
			
			scanf("%d %d",&E->V1,&E->V2);
			E->Weight = 1;
			InsertEdge(Graph, E);
			
		}
	}
	

	
	return Graph;
}

void Unweighted ( LGraph Graph, int dist[], int path[], Vertex S )
{
    Queue Q;
    Vertex V;
    PtrToAdjVNode W;
     
    Q = CreateQueue( Graph->Nv ); /* 创建空队列, MaxSize为外部定义的常数 */
    dist[S] = 0; /* 初始化源点 */
    AddQ (Q, S);
 
    while( !IsEmptyQ(Q) ){
        V = DeleteQ(Q);
        for ( W=Graph->G[V].FirstEdge; W; W=W->Next ) /* 对V的每个邻接点W->AdjV */
            if ( dist[W->AdjV]==-1 ) { /* 若W->AdjV未被访问过 */
                dist[W->AdjV] = dist[V]+1; /* W->AdjV到S的距离更新 */
                path[W->AdjV] = V; /* 将V记录在S到W->AdjV的路径上 */
                AddQ(Q, W->AdjV);
            }
    } /* while结束*/
}
void PrintPath(int path[],int pt)
{
	
	int Collect[MaxVertexNum];
	for(int i=0;i<MaxVertexNum;i++)
	{
		Collect[i] = 0;
	}
	printf("\n");
	int cnt = 0;//逆序输出数组--计数
	int tmp = pt;//遍历路径path--临时变量
	while(tmp != -1)
	{
		
		if(path[tmp] != -1){
			
			Collect[cnt++] = path[tmp];
		}
		tmp = path[tmp];
		
		
	}
	while(cnt--!=0)
	{
		printf("v%d->",Collect[cnt]);	
	}
	printf("v%d",pt);
}

int  main()
{
	LGraph Graph = BuildGraph();
	int Nv = Graph->Nv;
	int dis[Nv];
	int path[Nv];

	for(int i =0;i<Nv;i++)
	{
			dis[i] = -1;
			path[i] = -1;
	}
	
	Unweighted(Graph,dis,path,2);
	//遍历路径path
	//查看到达v6的路径整体路线,并且打印出所需要花费的价格
	int pt = 6;
	int distance = 6;
	PrintPath(path,pt);
	
	printf("\nThis distiance will cost:%d",dis[distance]);
	
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值