您的位置:首页 > 理论基础 > 数据结构算法

数据结构——邻接矩阵表示的图的Floyd算法

2010-08-10 19:55 351 查看
#include <iostream>
#include <iomanip>
using namespace std;

#define MAX_VERTEX_NUM    10        //最大顶点个数
#define TRUE 1
#define FALSE 0
#define INFINITY 32767 /* 用整型最大值代替∞ */

typedef char VERTYPE;
typedef struct
{
VERTYPE    vexs[MAX_VERTEX_NUM];    //顶点向量
int        arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM];                    //邻接矩阵
int        vexnum,arcnum;            //图的当前顶点数和弧数
}mgraph, * MGraph;

typedef int DistancMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];        //存放路径长度
typedef int PathMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM][MAX_VERTEX_NUM];
//存放路径,P[0][1][]表示顶点0到顶点1的路径,经过哪个点P[0][1][i]就是TRUE。

void init_mgraph(MGraph &g)    //初始化图
{
g=(MGraph)malloc(sizeof(mgraph));
g->vexnum=0;
g->arcnum=0;
for(int i=0;i<MAX_VERTEX_NUM;i++)
g->vexs[i]=0;
for(i=0;i<MAX_VERTEX_NUM;i++)
for(int j=0;j<MAX_VERTEX_NUM;j++)
g->arcs[i][j]=INFINITY;
}

void add_vexs(MGraph &g)    //增加顶点
{
cout<<"请输入顶点的个数:"<<endl;
cin>>g->vexnum;
cout<<"请输入顶点的值"<<endl;
for(int i=0;i<g->vexnum;i++)
{
cin>>g->vexs[i];
}
}
void add_arcs(MGraph &g)    //增加边
{
cout<<"请输入边的个数:"<<endl;
cin>>g->arcnum;
VERTYPE ch1,ch2;
int row,col,weight;

for(int i=0;i<g->arcnum;i++)
{
cin>>ch1>>ch2>>weight;
for(int j=0;j<g->vexnum;j++)
{
if(g->vexs[j]==ch1)
{
row=j;
}
if(g->vexs[j]==ch2)
{
col=j;
}
}
g->arcs[row][col]=weight;    //有向带权图只需把1改为weight
}
}

void creat_mgraph(MGraph &g) //创建图
{
add_vexs(g);    //增加顶点
add_arcs(g);    //增加边
}

void print_mgraph(MGraph &g) //打印图
{
for(int i=0;i<g->vexnum;i++)
cout<<"   "<<g->vexs[i]<<"  ";
cout<<endl;
for(i=0;i<g->vexnum;i++)
{
cout<<g->vexs[i]<<" ";
for(int j=0;j<g->vexnum;j++)
{
cout<<setw(5)<<g->arcs[i][j]<<"  ";
}
cout<<endl;
}
}

void ShortestPath_FLOYD(MGraph &g, PathMatrix &P, DistancMatrix &D)
{
//用Floyd算法求有向网G中各顶点对v和w之间的最短路径P[v][w]及其带权长度D[v][w]。
//若P[v][w][u]为TRUE,则u是从v到w当前求得最短路径上的顶点。
int v,w,u,i;
for(v=0; v<g->vexnum; ++v)
for(w=0; w<g->vexnum; ++w)
{
D[v][w] = g->arcs[v][w];
for(u=0; u<g->vexnum; ++u)    //初始化
P[v][w][u] = FALSE;
if(D[v][w] < INFINITY)        //从v到w有直接路径
{
P[v][w][v] = TRUE;        //起点
P[v][w][w] = TRUE;        //终点
}//if
}//for

for(u=0; u<g->vexnum; ++u)
for(v=0; v<g->vexnum; ++v)
for(w=0; w<g->vexnum; ++w)
{
if(u==v || v==w || w==u)
continue;
if(D[v][u] + D[u][w] < D[v][w])        //从v经u到w的一条路径更短
{
D[v][w] = D[v][u] + D[u][w];
for(i=0; i<g->vexnum; ++i)
P[v][w][i] = P[v][u][i] || P[u][w][i];
}//if
}
}

void print_PathMatrix(MGraph &g, PathMatrix &P) //打印路径矩阵
{
cout<<"        ";
for(int i=0;i<g->vexnum;i++)
cout<<g->vexs[i]<<"  ";
cout<<endl;

for(i=0;i<g->vexnum;i++)
{
for(int j=0;j<g->vexnum;j++)
{
cout<<i<<"-->"<<j<<":  ";
for(int k=0;k<g->vexnum;k++)
cout<<P[i][j][k]<<"  ";
cout<<endl;
}
cout<<endl;
}
}

void print_DistancMatrix(MGraph &g, DistancMatrix &D) //打印距离矩阵
{
for(int i=0;i<g->vexnum;i++)
cout<<"   "<<g->vexs[i]<<"  ";
cout<<endl;
for(i=0;i<g->vexnum;i++)
{
cout<<g->vexs[i]<<" ";
for(int j=0;j<g->vexnum;j++)
{
cout<<setw(5)<<D[i][j]<<" ";
}
cout<<endl;
}
}

int main()
{
MGraph G;
init_mgraph(G);        //初始化图
creat_mgraph(G);    //创建图
print_mgraph(G);    //打印图

DistancMatrix D;
PathMatrix P;
ShortestPath_FLOYD(G,P,D);

print_DistancMatrix(G,D);    //打印距离
print_PathMatrix(G,P);        //打印路径

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: