查找轮图的直径、周期和边缘的程序
轮子图:轮子图是通过将单个通用顶点连接到循环的所有顶点而形成的图。特性:-
- 轮图是平面图。
- Wheel 图中总是存在一个哈密顿循环。
- 如果 n 分别为奇数和偶数,则色数为 3 和 4。
问题陈述:给定轮图中的顶点数。任务是找到:
- 车轮图中的循环数。
- 车轮图中的边数。
- 轮图的直径。
例子:
Input: vertices = 4 Output: Number of cycle = 7 Number of edge = 6 Diameter = 1 Input: vertices = 6 Output: Number of cycle = 21 Number of edge = 10 Diameter = 2
示例 #1:对于 vertices = 4 Wheel Graph,总周期为 7 :
示例 #2:对于顶点 = 5 和 7 轮图边数分别 = 8 和 12 :
示例#3:对于顶点 = 4,直径为 1 ,因为我们可以通过仅覆盖 1 条边从任何顶点到任何顶点。
计算周期、边缘和直径的公式:-
Number of Cycle = (vertices * vertices) - (3 * vertices) + 3 Number of edge = 2 * (vertices - 1) Diameter = if vertices = 4, Diameter = 1 if vertices > 4, Diameter = 2
以下是所需的实现:
C++
// C++ Program to find the diameter, // cycles and edges of a Wheel Graph #include
using namespace std; // Function that calculates the // Number of Cycle in Wheel Graph. int totalCycle(int vertices) { int result = 0; // calculates no. of Cycle. result = pow(vertices, 2) - (3 * vertices) + 3; return result; } // Function that calculates the // Number of Edges in Wheel graph. int Edges(int vertices) { int result = 0; result = 2 * (vertices - 1); return result; } // Function that calculates the // Diameter in Wheel Graph. int Diameter(int vertices) { int result = 0; // calculates Diameter. if (vertices == 4) result = 1; else result = 2; return result; } // Driver Code int main() { int vertices = 4; cout << "Number of Cycle = " << totalCycle(vertices) << endl; cout << "Number of Edges = " << Edges(vertices) << endl; cout << "Diameter = " << Diameter(vertices); return 0; } Java
//Java Program to find the diameter, // cycles and edges of a Wheel Graph import java.io.*; class GFG { // Function that calculates the // Number of Cycle in Wheel Graph. static int totalCycle(double vertices) { double result = 0; int result1 = 0; // calculates no. of Cycle. result = Math.pow(vertices, 2) - (3 * vertices) + 3; result1 = (int)(result); return result1; } // Function that calculates the // Number of Edges in Wheel graph. static int Edges(int vertices) { int result = 0; result = 2 * (vertices - 1); return result; } // Function that calculates the // Diameter in Wheel Graph. static int Diameter(int vertices) { int result = 0; // calculates Diameter. if (vertices == 4) result = 1; else result = 2; return result; } //Driver Code public static void main(String[] args) { int vertices = 4; System.out.println("Number of Cycle = " + totalCycle(vertices)); System.out.println("Number of Edges = " + Edges(vertices)); System.out.println("Diameter = " + Diameter(vertices)); } }
Python3
# Python3 Program to find the diameter, # cycles and edges of a Wheel Graph # Function that calculates the # Number of Cycle in Wheel Graph. def totalCycle(vertices): result = 0 # calculates no. of Cycle. result = (pow(vertices, 2) - (3 * vertices) + 3) return result # Function that calculates the # Number of Edges in Wheel graph. def Edges(vertices): result = 0 result = 2 * (vertices - 1) return result # Function that calculates the # Diameter in Wheel Graph. def Diameter(vertices): result = 0 # calculates Diameter. if vertices == 4: result = 1 else: result = 2 return result # Driver Code if __name__ == "__main__": vertices = 4 print("Number of Cycle =", totalCycle(vertices)) print("Number of Edges =", Edges(vertices)) print("Diameter =", Diameter(vertices)) # This code is contributed by Rituraj Jain
C#
// C# Program to find the diameter, // cycles and edges of a Wheel Graph using System; class GFG { // Function that calculates the // Number of Cycle in Wheel Graph. static int totalCycle(double vertices) { double result = 0; int result1 = 0; // calculates no. of Cycle. result = Math.Pow(vertices, 2) - (3 * vertices) + 3; result1 = (int)(result); return result1; } // Function that calculates the // Number of Edges in Wheel graph. static int Edges(int vertices) { int result = 0; result = 2 * (vertices - 1); return result; } // Function that calculates the // Diameter in Wheel Graph. static int Diameter(int vertices) { int result = 0; // calculates Diameter. if (vertices == 4) result = 1; else result = 2; return result; } // Driver Code public static void Main() { int vertices = 4; Console.WriteLine("Number of Cycle = " + totalCycle(vertices)); Console.WriteLine("Number of Edges = " + Edges(vertices)); Console.WriteLine("Diameter = " + Diameter(vertices)); } } // This code is contributed by inder_verma
PHP
输出:Number of Cycle = 7 Number of Edges = 6 Diameter = 1