北邮通信网编程作业.docx
《北邮通信网编程作业.docx》由会员分享,可在线阅读,更多相关《北邮通信网编程作业.docx(14页珍藏版)》请在冰豆网上搜索。
![北邮通信网编程作业.docx](https://file1.bdocx.com/fileroot1/2023-1/26/58cdc462-f6d7-4e8e-a317-d5663eb60c5d/58cdc462-f6d7-4e8e-a317-d5663eb60c5d1.gif)
北邮通信网编程作业
通信网理论基础实践课
1.Floyd算法
1.#include
2.usingnamespacestd;
3.voidshowMatrix(float**x,inty)//用于float型矩阵显示
4.{
5.for(intm=0;m6.{
7.for(intk=0;k8.{
9.cout<10.if(k==y-1)
11.{
12.cout<<"\n";
13.}
14.}
15.}
16.
17.}
18.voidshowMatrix(int**x,inty)//用于int型矩阵显示
19.{
20.for(intm=0;m21.{
22.for(intk=0;k23.{
24.cout<25.if(k==y-1)
26.{
27.cout<<"\n";
28.}
29.}
30.}
31.
32.}
33.
34.voidmain()
35.{
36.intV;//V为图中顶点数
37.cout<<"请输入图中矩阵顶点个数:
";
38.cin>>V;
39.cout<<"生成的图中顶点个数为:
"<40.
41.float**w;//w为一个二维float数组,储存权值
42.w=newfloat*[V];//创建行指针
43.for(inti=0;i44.{
45.w[i]=newfloat[V];
46.}
47.
48.ints=0;//用于检测矩阵是否输入正确
49.do
50.{
51.cout<<"\n"<<"请输入距离矩阵(无边用100代替):
"<<"\n";
52.for(intm=0;m53.{
54.for(intk=0;k55.{
56.cin>>w[m][k];
57.}
58.}
59.cout<<"\n"<<"距离矩阵如下:
"<<"\n";
60.showMatrix(w,V);
61.
62.cout<<"\n"<<"上述距离矩阵输入正确按1,重新输入按0"<<"\n";
63.cin>>s;
64.
65.}while(s==0);//正确输入初始化距离矩阵
66.
67.int**r;//r为一个二维int数组,储存路由
68.r=newint*[V];//创建行指针
69.for(inti=0;i70.{
71.r[i]=newint[V];
72.}
73.for(intm=0;m74.{
75.for(intk=0;k76.{
77.if(w[m][k]==0)
78.r[m][k]=0;
79.else
80.r[m][k]=k+1;
81.}
82.}
83.cout<<"\n"<<"初始化的路由矩阵为"<<"\n";
84.showMatrix(r,V);
85.
86.
87.//初始化完成,下面开始进行Floyd算法迭代
88.for(intl=0;l89.{
90.for(intm=0;m91.{
92.for(intk=0;k93.{
94.if(w[m][l]+w[l][k]95.{
96.w[m][k]=w[m][l]+w[l][k];
97.r[m][k]=r[m][l];
98.}
99.
100.}
101.}
102.cout<<"\n"<<"第"<103.showMatrix(w,V);
104.cout<<"\n"<<"第"<105.showMatrix(r,V);
106.
107.}
108.cout<<"\n"<<"Floyd迭代完成。
"<<"\n";
109.
110.intmidpoint=0;//代表中点的顶点标号
111.intcentral=0;//代表中心的顶点标号
112.float*max=newfloat[V];//代表每个端点i的max{w[i][j]}
113.float*sum=newfloat[V];//代表每个端点i的w[i][j]之和
114.for(intm=0;m115.{
116.sum[m]=0;
117.max[m]=0;
118.}
119.
120.for(intm=0;m121.{
122.for(intk=0;k123.{
124.sum[m]=sum[m]+w[m][k];//记录以m为端点的边权值之和
125.if(w[m][k]>max[m])
126.max[m]=w[m][k];//记录以m为端点的边的最大权值
127.}
128.
129.}
130.cout<<"各端点的最大边权值分别为:
"<<"\n";
131.for(intm=0;m132.{
133.cout<134.}
135.cout<<"\n"<<"各端点的边权值之和分别为:
"<<"\n";
136.for(intm=0;m137.{
138.cout<139.}
140.
141.floatminSum=sum[0];//以第一个顶点为最小值进行初始化
142.floatminMax=max[0];//以第一个顶点为最小值进行初始化
143.for(intm=1;m144.{
145.if(sum[m]146.{
147.minSum=sum[m];
148.midpoint=m;
149.}
150.if(max[m]151.{
152.minMax=max[m];
153.central=m;
154.}
155.}
156.cout<<"\n"<<"图的中心为V"<157.
158.cout<<"\n"<<"进行最短路径规划输入1,不进行输入0:
";
159.cin>>s;//用于判断是否进行路径规划
160.while(s==1)
161.{
162.intstart,end,middle;
163.cout<<"请输入路径起点:
";
164.cin>>start;
165.cout<<"请输入路径终点:
";
166.cin>>end;
167.cout<<"\n"<<"从V"<"<<"\n"
168.<<"V"<169.do
170.{
171.middle=r[start-1][end-1];
172.cout<<"->"<<"V"<173.start=middle;
174.}while(middle!
=end);
175.cout<<"\n"<<"继续进行最短路径规划输入1,不进行输入0:
";
176.cin>>s;//用于判断是否进行路径规划
177.}
178.delete[]w,r,sum,max;
179.}
实验测试结果如下:
2.图的连通性判断
1.#include
2.usingnamespacestd;
3.
4.voidDFS(int**c,intm,intn,int*b)
5.//c为矩阵,m为遍历起点,n为顶点个数,b为顶点遍历情况
6.{
7.for(intx=0;x8.{
9.if(c[m][x]==1)
10.{
11.if(b[x]==0)
12.{
13.cout<<"->V"<14.b[x]=1;//端点设为已遍历
15.DFS(c,x,n,b);
16.}
17.}
18.}
19.}
20.
21.voidmain()
22.{
23.intV;
24.cout<<"请输入顶点个数:
";
25.cin>>V;
26.int**C;
27.C=newint*[V];//二维动态数组存储邻接矩阵
28.for(inti=0;i29.{
30.C[i]=newint[V];
31.}
32.ints=0;
33.do
34.{
35.cout<<"\n"<<"请输入邻接矩阵(无边为0,有边为1):
"<<"\n";
36.for(intm=0;m37.{
38.for(intk=0;k39.{
40.cin>>C[m][k];
41.}
42.}
43.cout<<"\n"<<"邻接矩阵如下:
"<<"\n";
44.for(intm=0;m45.{
46.for(intk=0;k47.{
48.cout<49.if(k==V-1)
50.{
51.cout<<"\n";
52.}
53.}
54.}
55.
56.cout<<"\n"<<"上述邻接矩阵输入正确按1,重新输入按0"<<"\n";
57.cin>>s;
58.
59.}while(s==0);//正确输入初始化距离矩阵
60.
61.int*a=newint[V];//初始化各顶点遍历情况
62.for(inti=0;i63.{
64.a[i]=0;
65.}
66.
67.intU=0;//从第U个节点开始遍历
68.cout<<"\n请输入从开始遍历的顶点号:
";
69.cin>>U;
70.inti=U-1;//第U个节点对应a[U-1]
71.a[i]=1;//标记第U个节点为已遍历
72.cout<<"\n遍历的次序为:
\nV"<
73.
74.DFS(C,i,V,a);
75.
76.cout<<"\n"<<"以第一个顶点为起点进行遍历,各节点遍历情况为:
(被遍历为1,不被遍历为0)"
77.<<"\n";
78.intsum=0;
79.for(intx=0;x80.{
81.cout<82.sum=sum+a[x];
83.}
84.if(sum!
=V)
85.cout<<"\n"<<"\n存在不被遍历的点,该图不连通"<<"\n";
86.else
87.cout<<"\n"<<"所有点均被遍历,该图为连通图"<<"\n\n";
88.
89.delete[]C,a;
90.}
实验测试结果如下: