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.