C++程序:求解轮图的直径、环数和边数


在这个问题中,我们给出一个数字,表示轮图的顶点数。我们的任务是创建一个C++程序来求解轮图的直径、环数和边数

问题描述 − 我们需要找到具有n个顶点的轮图的环数、边数和直径。

首先,让我们了解一些关于轮图的基础知识 −

一个轮图是由一个环图Cn-1添加一个新的顶点得到的。这个新的顶点称为中心点,它与Cn的所有顶点相连。

一个具有7个顶点的轮图示例。

轮图的直径是从任何一个顶点到其他任何一个顶点所需经过的边数。对于上面的轮图,直径为2

轮图的环数是给定图中可以形成的封闭环的总数。对于上面的轮图,环数为31。

轮图的边数是连接所有顶点的边的数量。对于上面的轮图,边数为12。

解决方案方法

为了解决这个问题,我们将使用图论中给出的直接公式来查找轮图的所需值。

公式如下:

轮图的直径 =

1, if vertices = 4, else 2.

轮图的环数 =

(No. of vertices )^2 - (3 * (No. of vertices -1) )

轮图的边数 =

2 * (No. of vertices - 1)

程序演示了我们解决方案的工作原理:

示例

 在线演示

#include <iostream>
#include <math.h>
using namespace std;
void calcValuesWheelGraph(int V){
   // Calculating the Diameter
   if(V == 4){
      cout<<"The Diameter of the Wheel Graph is 1 "<<endl;
   }
   else {
      cout<<"The Diameter of the Wheel Graph is 2 "<<endl;
   }
   // Calculating the no. of cycles
   cout<<"The number of cycles of the Wheel Graph is "<<(pow(V, 2) - (3 * (V-1)))<<endl;
   // Calculating the no. of Edges
   cout<<"The number of Edges of the Wheel Graph is "<<(2 * (V-1))<<endl;
}
int main(){
   int V = 9;
   calcValuesWheelGraph(V);
   return 0;
}

输出

The Diameter of the Wheel Graph is 2
The number of cycles of the Wheel Graph is 57
The number of Edges of the Wheel Graph is 16

更新于:2020年9月16日

272 次浏览

开启你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.