最短路径算法C代码(D算法和F算法)

本文转自http://touch-2011.iteye.com/blog/1076031

最短路径算法C代码(D算法和F算法)_第1张图片

/* 
 * 最短路径,迪杰斯特拉算法和弗洛伊德算法(采用邻接矩阵存储) 
 * 
 */  
  
#include  
  
#define MAX_VERTEX_NUM 20  
#define INFINITE 10000  //当做无穷大  
//图的定义  
typedef struct   
{  
    int vertexNum;  
    char vertex[MAX_VERTEX_NUM];  
    int arc[MAX_VERTEX_NUM][MAX_VERTEX_NUM];  
}Graph,*PGraph;  
  
//辅助数组中的元素定义  
typedef struct  
{  
    int distance;  
    int path[MAX_VERTEX_NUM];  
}ArrayNode;  
  
  
//构造有向网  
void createdGraph(PGraph g)  
{  
    int i,j;  
    g->vertexNum=6;  
    for(i=0;ivertexNum;i++)  
        g->vertex[i]='A'+i;  
    for(i=0;ivertexNum;i++)  
        for(j=0;jvertexNum;j++)  
            g->arc[i][j]=0;  
    g->arc[0][2]=10;  
    g->arc[0][4]=30;  
    g->arc[0][5]=100;  
    g->arc[1][2]=5;  
    g->arc[2][3]=50;  
    g->arc[3][5]=10;  
    g->arc[4][3]=20;  
    g->arc[4][5]=60;  
}  
  
//迪杰斯特拉算法  
void Dijkstra(PGraph g,int from,int to)  
{  
    int i,j,index=-1;  
    int n=1;//记录已经求出的两个点之间的最短距离的个数  
    ArrayNode shortestPath[MAX_VERTEX_NUM];  
    int flag[MAX_VERTEX_NUM]={0};//标记,为1表示到这个顶点的最短距离已求出  
  
    //1.求from到各个顶点的直接距离,即初始化shortestPath数组  
    for(i=0;ivertexNum;i++){  
        if(from==i){  
            shortestPath[i].distance=0;  
            shortestPath[i].path[0]=i;  
            flag[from]=1;  
        }  
        else if(g->arc[from][i]>0){  
            shortestPath[i].path[0]=from;  
            shortestPath[i].path[1]=i;  
            shortestPath[i].distance=g->arc[from][i];  
        }else  
            shortestPath[i].distance=INFINITE;  
    }  
    //2.每次求一个最短路径  
    while(nvertexNum){  
        //选择shortestPath中距离最小的,求出from到这个顶点的最短路径  
        index=-1;  
        for(i=0;ivertexNum;i++){  
            if(i==from)  
                continue;  
            if(flag[i]==0 && index==-1 && shortestPath[i].distance!=INFINITE)  
                index=i;  
            if(flag[i]==0 && index!=-1 && shortestPath[i].distancevertexNum;i++){  
            if(i==from)  
                continue;  
            if(g->arc[index][i]>0 && g->arc[index][i]+shortestPath[index].distancearc[index][i]+shortestPath[index].distance;  
                //修改路径  
                j=0;  
                while(1){  
                    shortestPath[i].path[j]=shortestPath[index].path[j];  
                    if(shortestPath[index].path[j]==index)  
                        break;  
                    j++;  
                }  
                shortestPath[i].path[j+1]=i;  
            }  
        }  
        n++;  
    }  
    //输出from到to的最短路径及长度  
    if(shortestPath[to].distance==INFINITE){  
        printf("%c到%c没有路径\n",from+'A',to+'A');  
        return;  
    }  
    printf("%c到%c的最短路径长度是:%d\n",from+'A',to+'A',shortestPath[to].distance);  
    printf("经过的顶点:  ");  
    i=0;  
    while(1){  
        printf("%-3c",shortestPath[to].path[i]+'A');  
        if(shortestPath[to].path[i]==to)  
            break;  
        i++;  
    }  
    printf("\n");  
}  
  
//弗洛伊德算法  
void Floyd(PGraph g,int from,int to)  
{  
    int i,j,k;  
    int shortestPath[MAX_VERTEX_NUM][MAX_VERTEX_NUM];//存储最短路径的数组  
    //初始化shortestPath  
    for(i=0;ivertexNum;i++)  
        for(j=0;jvertexNum;j++){  
            if(i==j){  
                shortestPath[i][j]=0;  
                continue;  
            }  
            if(g->arc[i][j]>0)  
                shortestPath[i][j]=g->arc[i][j];  
            else  
                shortestPath[i][j]=INFINITE;  
        }  
    //将各个顶点顺次加入,并修改最短路径  
    for(k=0;kvertexNum;k++){  
        //在i,j之间加入k  
        for(i=0;ivertexNum;i++){  
            for(j=0;jvertexNum;j++){  
                if(shortestPath[i][k]+shortestPath[k][j]

你可能感兴趣的:(光网络)