Program to find the diameter, cycles and edges of a Wheel Graph

02 Dec, 2019
Wheel Graph: A Wheel graph is a graph formed by connecting a single universal vertex to all vertices of a cycle. Properties:-
Wheel graphs are Planar graphs.
There is always a Hamiltonian cycle in the Wheel graph.
Chromatic Number is 3 and 4, if n is odd and even respectively.
Problem Statement: Given the Number of Vertices in a Wheel Graph. The task is to find:
The Number of Cycles in the Wheel Graph.Number of edges in Wheel Graph.The diameter of a Wheel Graph.Examples: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

Recommended: Please try your approach on {IDE} first, before moving on to the solution.Example #1: For vertices = 4 Wheel Graph, total cycle is 7:Example #2: For vertices = 5 and 7 Wheel Graph Number of edges = 8 and 12 respectively:Example #3:For vertices = 4, the Diameter is 1 as We can go from any vertices to any vertices by covering only 1 edge.Formula to calculate the cycles, edges and diameter:-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
Below is the required implementation:C++JavaPython3C#PHPC++// C++ Program to find the diameter, // cycles and edges of a Wheel Graph#include <bits/stdc++.h>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 Codeint 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 Graphimport 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 JainC#// C# Program to find the diameter, // cycles and edges of a Wheel Graphusing 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 Codepublic 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_vermaPHP<?php// PHP Program to find the diameter, // cycles and edges of a Wheel Graph  // Function that calculates the// Number of Cycle in Wheel Graph.function 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.function Edges($vertices){    $result = 0;      $result = 2 * ($vertices - 1);      return $result;}  // Function that calculates the// Diameter in Wheel Graph.function Diameter($vertices){    $result = 0;      // calculates Diameter.    if ($vertices == 4)        $result = 1;    else        $result = 2;      return $result;}  // Driver Code$vertices = 4;  echo "Number of Cycle = " ,       totalCycle($vertices), "\n";echo "Number of Edges = " ,       Edges($vertices), "\n" ;echo "Diameter = " , Diameter($vertices);  // This code is contributed by inder_verma?>Output:Number of Cycle = 7
Number of Edges = 6
Diameter = 1
My Personal Notes
arrow_drop_upSave
The Number of Cycles in the Wheel Graph.
Number of edges in Wheel Graph.
The diameter of a Wheel Graph.
Examples: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

Recommended: Please try your approach on {IDE} first, before moving on to the solution.Example #1: For vertices = 4 Wheel Graph, total cycle is 7:Example #2: For vertices = 5 and 7 Wheel Graph Number of edges = 8 and 12 respectively:Example #3:For vertices = 4, the Diameter is 1 as We can go from any vertices to any vertices by covering only 1 edge.Formula to calculate the cycles, edges and diameter:-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
Below is the required implementation:C++JavaPython3C#PHPC++// C++ Program to find the diameter, // cycles and edges of a Wheel Graph#include <bits/stdc++.h>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 Codeint 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 Graphimport 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 JainC#// C# Program to find the diameter, // cycles and edges of a Wheel Graphusing 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 Codepublic 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_vermaPHP<?php// PHP Program to find the diameter, // cycles and edges of a Wheel Graph  // Function that calculates the// Number of Cycle in Wheel Graph.function 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.function Edges($vertices){    $result = 0;      $result = 2 * ($vertices - 1);      return $result;}  // Function that calculates the// Diameter in Wheel Graph.function Diameter($vertices){    $result = 0;      // calculates Diameter.    if ($vertices == 4)        $result = 1;    else        $result = 2;      return $result;}  // Driver Code$vertices = 4;  echo "Number of Cycle = " ,       totalCycle($vertices), "\n";echo "Number of Edges = " ,       Edges($vertices), "\n" ;echo "Diameter = " , Diameter($vertices);  // This code is contributed by inder_verma?>Output:Number of Cycle = 7
Number of Edges = 6
Diameter = 1
My Personal Notes
arrow_drop_upSave
Examples:
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


Example #1: For vertices = 4 Wheel Graph, total cycle is 7:
Example #2: For vertices = 5 and 7 Wheel Graph Number of edges = 8 and 12 respectively:
Example #3:For vertices = 4, the Diameter is 1 as We can go from any vertices to any vertices by covering only 1 edge.
Formula to calculate the cycles, edges and diameter:-
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

Below is the required implementation:
C++
Java
Python3
C#
PHP
// C++ Program to find the diameter, // cycles and edges of a Wheel Graph#include <bits/stdc++.h>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 Codeint 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 Program to find the diameter, // cycles and edges of a Wheel Graphimport 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 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# Program to find the diameter, // cycles and edges of a Wheel Graphusing 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 Codepublic 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// PHP Program to find the diameter, // cycles and edges of a Wheel Graph  // Function that calculates the// Number of Cycle in Wheel Graph.function 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.function Edges($vertices){    $result = 0;      $result = 2 * ($vertices - 1);      return $result;}  // Function that calculates the// Diameter in Wheel Graph.function Diameter($vertices){    $result = 0;      // calculates Diameter.    if ($vertices == 4)        $result = 1;    else        $result = 2;      return $result;}  // Driver Code$vertices = 4;  echo "Number of Cycle = " ,       totalCycle($vertices), "\n";echo "Number of Edges = " ,       Edges($vertices), "\n" ;echo "Diameter = " , Diameter($vertices);  // This code is contributed by inder_verma?>
Number of Cycle = 7
Number of Edges = 6
Diameter = 1

Naman_Garg
inderDuMCA
rituraj_jain
Akanksha_Rai
Data Structures-Graph
Graph
School Programming
Graph
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
What is Data Structure: Types, Classifications and Applications
Check if a given graph is tree or not
Minimum steps to reach target by a Knight | Set 1
Total number of Spanning Trees in a Graph
C++ Classes and Objects
Python Dictionary
Object Oriented Programming in C++
Inheritance in C++
Arrays in C/C++