R1 |
R4 |
R2 |
R5 |
N3 |
N1 |
N5 |
R6 |
N2 |
R3 |
8 |
4 |
2 |
3 |
9 |
5 |
5 |
7 |
2 |
5 |
N4 |
2 |
8 |
R1 |
R4 |
R2 |
R5 |
N3 |
N1 |
N5 |
R6 |
N2 |
R3 |
4 |
2 |
3 |
9 |
5 |
5 |
7 |
2 |
5 |
N4 |
2 |
8 |
4 |
開始 路由器 | 目的路由器 或目的網絡 | 最短路徑經過的路由器或網絡序列 | 最短路徑值 |
R1 | R2 | R1-N1-R2 | 5 |
R1 | R3 | R1-N1-R3 | 5 |
R1 | R4 | R1-R4 | 8 |
R1 | R5 | R1-N1-R2-R5 | 9 |
R1 | R6 | R1-R4-N3-R6 | 10 |
R1 | N1 | R1-N1 | 5 |
R1 | N2 | R1-N1-R3-N2 | 7 |
R1 | N3 | R1-R4-N3 | 10 |
R1 | N4 | R1-N1-R2-R5-N4 | 11 |
R1 | N5 | R1-R4-N3-R6-N5 | 15 |
目的網絡 | 下一跳(路由器) |
N1 | -- |
N2 | R3 |
N3 | R4 |
N4 | R2 |
N5 | R4 |
#include <stdio.h>
#include <math.h>
#include <string.h>
#define FALSE 0
#define TRUE 1
#define MAX 11
#define INFINITY 50
typedef struct{
int vexnum;
int arcs[MAX][MAX];
int arcnum;
}mgrath;
int mode[MAX][MAX]={
{0,50,50,8,50,50,5,50,50,50,50},
{50,0,50,50,4,50,7,50,50,50,50},
{50,50,0,50,50,50,3,2,50,50,50},
{8,50,50,0,50,50,50,50,2,50,50},
{50,4,50,50,0,50,50,50,5,2,50},
{50,50,50,50,50,0,50,50,9,50,5},
{0,0,0,50,50,50,50,50,50,50,50},
{50,50,0,50,50,50,50,50,50,50,50},
{50,50,50,0,0,0,50,50,50,50,50},
{50,50,50,50,0,50,50,50,50,50,50},
{50,50,50,50,50,0,50,50,50,50,50}
};
typedef int pathmatrix[MAX][MAX];
typedef int shortpath[MAX];
typedef char ch[MAX][MAX];
char emax[][3]={"r1","r2","r3","r4","r5","r6","n1","n2","n3","n4","n5"};
int createmgrath(mgrath &G,ch &H)
{
int i,j,k=0,l=0;
G.vexnum=G.arcnum=11;
puts("input G.vexnum G.arcnum");
printf("%d,%d \n",G.vexnum,G.arcnum);
for(i=0;i<11;i++)
{
strcpy(H[i],emax[i]);
}
for(i=0;i<G.vexnum;i++)
for(j=0;j<G.vexnum;j++)
G.arcs[i][j]=mode[i][j];
return 1;
}
void shortestpath_DIJ(mgrath G,int v0,pathmatrix &p,shortpath &D)
{
int v,w,i,min,j;
shortpath final;
for(v=0;v<G.vexnum;++v)
{
final[v]=FALSE;
D[v]=G.arcs[v0][v];
for(w=0;w<G.vexnum;++w) p[v][w]=FALSE; // 設空路徑
if(D[v]<INFINITY)
{
p[v][v0]=TRUE;
p[v][v]=TRUE;
}
}
D[v0]=0; final[v0]=TRUE; // 初始化,v0頂點屬於S集
//--- 開始主循環,每次求得v0到某個v頂點的最短路徑,並加v到S集 ---
for(i=0;i<G.vexnum;++i) // 其餘G.vexnum-1個頂點
{
min=INFINITY;
for(w=0;w<G.vexnum;++w)
{
if(!final[w])
if(D[w]<min)
{
v=w;
min=D[w];
}
}
final[v]=TRUE;
for(w=0;w<G.vexnum;++w)
{
if(!final[w]&&(min+G.arcs[v][w]<D[w]))
{
D[w]=min+G.arcs[v][w];
for(j=0;j<G.vexnum;j++)
p[w][j]=p[v][j];
p[w][w]=TRUE;
}
}
}
}
void main()
{
int k,i,j,q,l,h,n;
int f,m;
mgrath A;
ch B,C;
pathmatrix N,D;
shortpath M;
createmgrath(A,B);
for( unsigned int w=0; w<MAX; ++w )
{
for( unsigned int j=0; j<MAX; ++j )
C[w][j] = 0;
}
puts("begin from which node : ");
printf("if you want input r1 please input 0 \n");
printf("if you want input r2 please input 1 \n");
printf("if you want input r3 please input 2 \n");
printf("if you want input r4 please input 3 \n");
printf("if you want input r5 please input 4 \n");
printf("if you want input r6 please input 5 \n");
printf("if you want input n1 please input 7 \n");
printf("if you want input n2 please input 8 \n");
printf("if you want input n3 please input 9 \n");
printf("if you want input n4 please input 10 \n");
scanf("%d",&k);
shortestpath_DIJ(A,k,N,M);
for(i=0;i<A.vexnum;i++)
{
for(j=0;j<A.vexnum;j++)
D[i][j]=N[i][j];
}
printf("\n");
printf(" 起點 目的網絡 最短路徑經過的路由器或網絡序列 最短路徑值 \n");
for(l=0,f=0;l<11;l++,f=0)
{
if(B[l][0]=='n'){
q=k;
printf(" %s %s ",B[k],B[l]);
for(n=13,h=0;h<11;h++)
{
for(j=0;j<11;j++)
if(D[l][j]==TRUE && A.arcs[q][j]<50)
{
D[l][j]=FALSE;
printf("%s-",B[j]);
n--;
q=j;
if(f==0 && B[j][0]=='r' && B[j]!=B[k])
{
for(m=0;m<11;m++)
C[l][m]=B[j][m];
f=1;
}break;
}
}
for(;n>0;n--)
printf(" ");
printf("%d \n",M[l]);
}
}
printf(" \n \n目的網絡 起點%s的路由表的下一跳\n",B[k],B[k]);
for(l=0;l<11;l++)
{
if(B[l][0]=='n')
{
if(B[l][0]==NULL)
printf("NULL");
else{
printf(" %s ",B[l]);
printf(" %s \n",C[l]);
}
}
}
}