堆优化的Dijkstra算法

时间:2022-12-31 23:40:10
//最小堆实现Dijkstra算法
#include<stdio.h>
#include<stdlib.h>
#define MAXVEX 100 //最大顶点数
#define MAXSIZE 20
#define OK 1
#define ERROR 0
typedef char VertexType; //顶点
typedef int EdgeType; //权值
#define INFINITY 65535 /*用65535来代表∞*/
#define UNVISITED -1 //标记未访问
#define VISITED 1 //标记未访问

//Dist的存储结构
typedef struct
{
int index; //顶点的索引值
int length; //当前最短路径长度
int pre; //路径最后经过的顶点
}Dist;

typedef struct
{
int from; //边的始点
int to; //边的终点
EdgeType weight; //权重
}Edge; //边的结构

//图的结构
typedef struct
{
int numVertex; //顶点个数
int numEdge; //边的个数
VertexType vexs[MAXVEX]; /*顶点表*/
int Indegree[MAXVEX]; //顶点入度
int Mark[MAXVEX]; //标记是否被访问过
EdgeType arc[MAXVEX][MAXVEX]; //边表
Dist D[MAXVEX];
}Graph;

typedef int Status;
typedef Edge ElemType; //定义为Edge类型



//最小堆的存储结构
typedef struct
{
ElemType heapArray[MAXSIZE];
int length;
}MinHeap;

//返回依附于顶点的第一条边
Edge FirstEdge(Graph * G,int oneVertex);

//返回与preEdge有相同顶点的下一条边
Edge NextEdge(Graph * G,Edge preEdge);

//判断是否为边
bool IsEdge(Edge oneEdge);



//初始化堆数组
Status Init_heapArray(Graph * G,MinHeap * M,int s)
{
for(Edge e=FirstEdge(G,s);IsEdge(e);e=NextEdge(G,e))
{
M->heapArray[M->length]=e;
M->length++;
}
return OK;
}



//对最小堆初始化
Status Init_MinHeap(Graph * G,MinHeap * M,int s)
{
M->length=0;
Init_heapArray(G,M,s);
return OK;

}



int MinHeap_Leftchild(int pos) //返回左孩子的下标
{
return 2*pos+1;
}


int MinHeap_Rightchild(int pos) //返回右孩子的下标
{
return 2*pos+2;
}


int MinHeap_Parent(int pos) //返回双亲的下标
{
return (pos-1)/2;
}



void MinHeap_SiftDown(MinHeap * M,int left)
{
int i=left; //标识父结点
int j=MinHeap_Leftchild(i); //用于记录关键值较小的子结点
ElemType temp=M->heapArray[i]; //保存父结点
while(j<M->length) //过筛
{
if((j<M->length-1)&&(M->heapArray[j].weight>M->heapArray[j+1].weight)) //若有右子结点,且小于左子结点
{
j++; //j指向右子结点
}
if(temp.weight>M->heapArray[j].weight) //如果父结点大于子结点的值则交换位置
{
M->heapArray[i]=M->heapArray[j];
i=j;
j=MinHeap_Leftchild(j);
}
else //堆序性满足时则跳出
{
break;
}
}
M->heapArray[i]=temp;
}


void MinHeap_SiftUp(MinHeap * M,int position) //从position开始向上调整
{
int temppos=position;
ElemType temp=M->heapArray[temppos]; //记录当前元素
while((temppos>0) && (M->heapArray[MinHeap_Parent(temppos)].weight>temp.weight)) //temppos>0,结束于根结点
{
M->heapArray[temppos]=M->heapArray[MinHeap_Parent(temppos)];
temppos=MinHeap_Parent(temppos);
}
M->heapArray[temppos]=temp;
}


void Swap(MinHeap * M,int data1,int data2)
{
ElemType temp;
temp=M->heapArray[data1];
M->heapArray[data1]=M->heapArray[data2];
M->heapArray[data2]=temp;
}


//建立最小堆
void Create_MinHeap(MinHeap * M)
{
for(int i=M->length/2-1;i>=0;i--)
{
MinHeap_SiftDown(M,i);
}
}



//插入元素
Status MinHeap_Insert(MinHeap * M,ElemType NewElem)
{
if(M->length==MAXSIZE)
{
return ERROR;
}
M->heapArray[M->length]=NewElem;
MinHeap_SiftUp(M,M->length);
M->length++;
return OK;
}



//删除最小堆的最小值
Status MinHeap_Delete(MinHeap * M,ElemType * MinElem)
{
if(M->length==0)
{
printf("不能删除,堆已空!\n");
return ERROR;
}
else
{

Swap(M,0,--M->length);
if(M->length>1)
{
MinHeap_SiftDown(M,0);
}
*MinElem=M->heapArray[M->length];
return OK;
}
}




//初始化图
void InitGraph(Graph * G,int numVert,int numEd ) //传入顶点个数,边数
{
G->numVertex=numVert;
G->numEdge=numEd;
for(int i=0;i<numVert;i++)
{
G->Mark[i]=UNVISITED;
G->Indegree[i]=0;
for(int j=0;j<numVert;j++)
{
G->arc[i][j]=INFINITY;
if(i==j)
{
G->arc[i][j]=0;
}
}
}
return ;
}

//初始化Dist结构
void InitDist(Graph * G,int s) //s是源点
{
for(int i=0;i<G->numVertex;i++)
{
G->D[i].index=i;
G->D[i].length=INFINITY;
G->D[i].pre=s;
}
G->D[s].length=0;
}



//判断是否为边
bool IsEdge(Edge oneEdge)
{
if(oneEdge.weight>0 && oneEdge.weight!=INFINITY && oneEdge.to>=0)
{
return true;
}
else
{
return false;
}
}




//建立有向图的邻接矩阵
void CreatGraph(Graph * G)
{
int i,j,k,w;
printf("请输入%d个顶点元素:\n",G->numVertex);
for(i=0;i<G->numVertex;i++)
{
scanf(" %c",&G->vexs[i]);
}
for(k=0;k<G->numEdge;k++)
{
printf("请输入边(Vi,Vj)的下标Vi,Vj,和权重w:\n");
scanf("%d%d%d",&i,&j,&w);
G->Indegree[j]++;
G->arc[i][j]=w;
}
}



//返回顶点个数
int VerticesNum(Graph * G)
{
return G->numVertex;
}


//返回依附于顶点的第一条边
Edge FirstEdge(Graph * G,int oneVertex)
{
Edge firstEdge;
firstEdge.from=oneVertex;
for(int i=0;i<G->numVertex;i++)
{
if(G->arc[oneVertex][i]!=0 && G->arc[oneVertex][i]!=INFINITY)
{
firstEdge.to=i;
firstEdge.weight=G->arc[oneVertex][i];
break;
}

}
return firstEdge;
}


//返回oneEdge的终点
int ToVertex(Edge oneEdge)
{
return oneEdge.to;
}


//返回与preEdge有相同顶点的下一条边
Edge NextEdge(Graph * G,Edge preEdge)
{
Edge myEdge;
myEdge.from=preEdge.from; //边的始点与preEdge的始点相同
if(preEdge.to<G->numVertex) //如果preEdge.to+1>=G->numVertex;将不存在下一条边
for(int i=preEdge.to+1;i<G->numVertex;i++) //找下一个arc[oneVertex][i]
{ //不为0的i
if(G->arc[preEdge.from][i]!=0 && G->arc[preEdge.from][i]!=INFINITY)
{
myEdge.to=i;
myEdge.weight=G->arc[preEdge.from][i];
break;
}
}
return myEdge;
}


//将顶点的指向边插入
void Insert(Graph * G,MinHeap * M,int oneVertex)
{
for(Edge e=FirstEdge(G,oneVertex);IsEdge(e);e=NextEdge(G,e))
{
MinHeap_Insert(M,e);
}
}

void print_Dist(Graph * G);



void Dijkstra(Graph * G,MinHeap * M,int s)
{
InitDist(G,s); //初始化Dist数组
Init_MinHeap(G,M,s);//初始化最小堆
for(int i=0;i<G->numVertex;i++)
{
bool FOUND=false;
Edge d;
MinHeap_Delete(M,&d);
int v=d.from;
int nv=d.to;
G->Mark[nv]=VISITED; //标记该顶点的标记位置为VISITED
//加入v以后需要重新刷新D中v的邻接点的最短路径
int count=0;
for(Edge e=FirstEdge(G,v);IsEdge(e);e=NextEdge(G,e))
{
count++;
if(G->D[ToVertex(e)].length>(G->D[v].length+e.weight))
{
G->D[ToVertex(e)].length=G->D[v].length+e.weight;
G->D[ToVertex(e)].pre=v;
}
}
Insert(G,M,nv);
}
}




//输出Dist[]数组
void print_Dist(Graph * G)
{
for(int i=0;i<G->numVertex;i++)
{
printf("元素:%c index:%d length:%d pre:%d\n",G->vexs[i],G->D[i].index,G->D[i].length,G->D[i].pre);
}
printf("\n");
}




int main()
{
Graph G;
MinHeap M;
int numVert,numEd;
printf("请输入顶点数和边数:\n");
scanf("%d%d",&numVert,&numEd);
InitGraph(&G,numVert,numEd );
CreatGraph(&G);
Dijkstra(&G,&M,0); //源点设置为0
print_Dist(&G);
return 0;
}

堆优化的Dijkstra算法
堆优化的Dijkstra算法