本博文源于浙江大学《数据结构》,今天学习了无权图的单源最短路径问题,整体用了类似于广度优先搜索的程序。因此代码冗长(都是队列代码太长了),只要把代码拼接成功,最后都能实现,下面给出我的测试样例:
博主想要画出这样的图:
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 ( MGraph Graph, int dist[], int path[], Vertex S )
{
Queue Q;
Vertex V;
Q = CreateQueue( Graph->Nv ); /* 创建空队列, MaxSize为外部定义的常数 */
dist[S] = 0; /* 初始化源点 */
AddQ (Q, S);
while( !IsEmptyQ(Q) ){
V = DeleteQ(Q);
for ( Vertex W = 0; W<Graph->Nv; W++ ) /* 对V的每个邻接点W->AdjV */
{
if(Graph->G[V][W] == 1)
{
Vertex AdjV = W;
if ( dist[AdjV]==-1 ) { /* 若W->AdjV未被访问过 */
dist[AdjV] = dist[V]+1; /* W->AdjV到S的距离更新 */
path[AdjV] = V; /* 将V记录在S到W->AdjV的路径上 */
AddQ(Q, AdjV);
}
}
}
} /* while结束*/
}
偷偷告诉你,博主是将邻接表改成理解矩阵的,你看Adjv就是证据,吼吼吼!完整代码(邻接矩阵版本)
//实现无权图单源最短路径邻接矩阵实现
#include<stdio.h>
#include<stdlib.h>
#define MaxVertexNum 10
#define false 0
#define true 1
#define ERROR -1
#define INFINITY 1
typedef int WeightType;
typedef int DataType;
typedef struct GNode *PtrToGNode;
typedef int bool;
typedef int Position;
typedef int ElementType;
int Visited[MaxVertexNum]={false};
struct GNode {
int Nv; //一张图的顶点数
int Ne; //一张图的边数
WeightType G[MaxVertexNum][MaxVertexNum];
DataType Data[MaxVertexNum];//存顶点的数据
};
typedef PtrToGNode MGraph;//以邻接矩阵存储的图类型
//初始化一个有VertexNum 个顶点但没有边的图
typedef int Vertex;//用顶点下标表示顶点,为整型
MGraph CreateGraph(int VertexNum)
{
Vertex V, W;
MGraph Graph;
Graph = (MGraph)malloc(sizeof(struct GNode));
Graph->Nv = VertexNum;
Graph->Ne = 0;
//注意:这里默认顶点编号从0开始,到(Graph->Nv-1)
for(V=0; V<Graph->Nv;V++)
for(W=0; W<Graph->Nv;W++)
Graph->G[V][W] = 0;
for(int i =0;i<Graph->Nv;i++)
{
Visited[i]=false;
Graph->Data[i]=i;
}
return Graph;
}
typedef struct ENode *PtrToENode;
struct ENode {
Vertex V1,V2;//有向边<V1,V2>
WeightType Weight;//权值
};
typedef PtrToENode Edge;
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];
}
}
void InsertEdge(MGraph Graph, Edge E)
{
Graph->G[E->V1][E->V2] = E->Weight;
}
MGraph BuildGraph()
{
MGraph Graph;
Edge E;
Vertex V;
int Nv,i;
scanf("%d",&Nv);
Graph = CreateGraph(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);
}
}
/*
for(V=0;V<Graph->Nv;V++)
scanf(" %c",&(Graph->Dadta[V]));
*/
return Graph;
}
void Unweighted ( MGraph Graph, int dist[], int path[], Vertex S )
{
Queue Q;
Vertex V;
Q = CreateQueue( Graph->Nv ); /* 创建空队列, MaxSize为外部定义的常数 */
dist[S] = 0; /* 初始化源点 */
AddQ (Q, S);
while( !IsEmptyQ(Q) ){
V = DeleteQ(Q);
for ( Vertex W = 0; W<Graph->Nv; W++ ) /* 对V的每个邻接点W->AdjV */
{
if(Graph->G[V][W] == 1)
{
Vertex AdjV = W;
if ( dist[AdjV]==-1 ) { /* 若W->AdjV未被访问过 */
dist[AdjV] = dist[V]+1; /* W->AdjV到S的距离更新 */
path[AdjV] = V; /* 将V记录在S到W->AdjV的路径上 */
AddQ(Q, 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()
{
MGraph 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);
int pt = 6;
int distance = 6;
PrintPath(path,pt);
printf("\nThis distiance will cost:%d",dis[distance]);
return 0;
}