📜  循环图的度

📅  最后修改于: 2021-05-31 23:06:06             🧑  作者: Mango

给定循环图中的顶点数。任务是找到循环图的度数和边数。

度:任何顶点的度定义为其上的边缘入射的次数。

循环图:在图论中,由单个循环组成的称为循环图或圆形图。具有n个顶点的循环图称为Cn

循环图的属性:

  • 它是一个连通图。
  • 循环图或循环图是由单个循环组成的图。
  • 在循环图中,顶点数等于边数。
  • 当且仅当循环图具有偶数个顶点时,循环图才是2边可着色或2顶点可着色的。
  • 当且仅当循环图的顶点数为奇数时,它才是3边可着色的或3边可着色的。
  • 在循环图中,图中每个顶点的度为2。
  • 循环图的度是顶点数的2倍。由于每个边缘被计数两次。

例子:

Input: Number of vertices = 4
Output: Degree is 8
        Edges are 4
Explanation: 
The total edges are 4 
and the Degree of the Graph is 8
as 2 edge incident on each of 
the vertices i.e on a, b, c, and d. 

Input: number of vertices = 5
Output: Degree is 10
        Edges are 5

下面是上述问题的实现:

程序1:用于4个顶点的循环图

C++
// C++ implementation of above program.
 
#include 
using namespace std;
 
// function that calculates the
// number of Edge in a cycle graph.
int getnumberOfEdges(int numberOfVertices)
{
    int numberOfEdges = 0;
 
    // The numberOfEdges of the cycle graph
    // will be same as the numberOfVertices
    numberOfEdges = numberOfVertices;
 
    // return the numberOfEdges
    return numberOfEdges;
}
 
// function that calculates the degree
int getDegree(int numberOfVertices)
{
    int degree;
 
    // The degree of the cycle graph
    // will be twice the numberOfVertices
    degree = 2 * numberOfVertices;
 
    // return the degree
    return degree;
}
 
// Driver code
int main()
{
 
    // Get the number of vertices
    int numberOfVertices = 4;
 
    // Find the numberOfEdges and degree
    // from the numberOfVertices
    // and print the result
    cout << "For numberOfVertices = "
         << numberOfVertices
         << "\nDegree = "
         << getDegree(numberOfVertices)
         << "\nNumber of Edges = "
         << getnumberOfEdges(numberOfVertices);
 
    return 0;
}


Java
// Java implementation of above program.
import java.io.*;
 
class GFG {
 
    // function that calculates the
    // number of Edge in a cycle graph.
    static int getnumberOfEdges(int numberOfVertices)
    {
        int numberOfEdges = 0;
 
        // The numberOfEdges of the cycle graph
        // will be same as the numberOfVertices
        numberOfEdges = numberOfVertices;
 
        // return the numberOfEdges
        return numberOfEdges;
    }
 
    // function that calculates the degree
    static int getDegree(int numberOfVertices)
    {
        int degree;
 
        // The degree of the cycle graph
        // will be twice the numberOfVertices
        degree = 2 * numberOfVertices;
 
        // return the degree
        return degree;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // Get the number of vertices
        int numberOfVertices = 4;
 
        // Find the numberOfEdges and degree
        // from the numberOfVertices
        // and print the result
        System.out.print("For numberOfVertices = "
                         + numberOfVertices
                         + "\nDegree = "
                         + getDegree(numberOfVertices)
                         + "\nNumber of Edges = "
                         + getnumberOfEdges(numberOfVertices));
    }
}
 
// This code is contributed by anuj_67..


Python3
# Python3 implementation of above program.
 
# function that calculates the
# number of Edge in a cycle graph.
def getnumberOfEdges(numberOfVertices) :
 
    # The numberOfEdges of the cycle graph
    # will be same as the numberOfVertices
    numberOfEdges = numberOfVertices
 
    # return the numberOfEdges
    return numberOfEdges
 
# function that calculates the degree
def getDegree(numberOfVertices) :
 
    # The degree of the cycle graph
    # will be twice the numberOfVertices
    degree = 2 * numberOfVertices
 
    # return the degree
    return degree
 
 
# Driver code    
if __name__ == "__main__" :
 
    # Get the number of vertices
    numberOfVertices = 4
 
    # Find the numberOfEdges and degree
    # from the numberOfVertices
    # and print the result
    print("For numberOfVertices =", numberOfVertices,
          "\nDegree =", getDegree(numberOfVertices),
          "\nNumber of Edges =", getnumberOfEdges(numberOfVertices))
 
 
# This code is contributed by ANKITRAI1


C#
// C# implementation of above program.
using System;
 
class GFG {
 
    // function that calculates the
    // number of Edge in a cycle graph.
    static int getnumberOfEdges(int numberOfVertices)
    {
        int numberOfEdges = 0;
 
        // The numberOfEdges of the cycle graph
        // will be same as the numberOfVertices
        numberOfEdges = numberOfVertices;
 
        // return the numberOfEdges
        return numberOfEdges;
    }
 
    // function that calculates the degree
    static int getDegree(int numberOfVertices)
    {
        int degree;
 
        // The degree of the cycle graph
        // will be twice the numberOfVertices
        degree = 2 * numberOfVertices;
 
        // return the degree
        return degree;
    }
 
    // Driver code
    public static void Main()
    {
        // Get the number of vertices
        int numberOfVertices = 4;
 
        // Find the numberOfEdges and degree
        // from the numberOfVertices
        // and print the result
        Console.WriteLine("For numberOfVertices = "
                          + numberOfVertices
                          + "\nDegree = "
                          + getDegree(numberOfVertices)
                          + "\nNumber of Edges = "
                          + getnumberOfEdges(numberOfVertices));
    }
}
 
// This code is contributed by anuj_67..


PHP


Javascript


C++
// C++ implementation of above program.
 
#include 
using namespace std;
 
// function that calculates the
// number of Edge in a cycle graph.
int getnumberOfEdges(int numberOfVertices)
{
    int numberOfEdges = 0;
 
    // The numberOfEdges of the cycle graph
    // will be same as the numberOfVertices
    numberOfEdges = numberOfVertices;
 
    // return the numberOfEdges
    return numberOfEdges;
}
 
// function that calculates the degree
int getDegree(int numberOfVertices)
{
    int degree;
 
    // The degree of the cycle graph
    // will be twice the numberOfVertices
    degree = 2 * numberOfVertices;
 
    // return the degree
    return degree;
}
 
// Driver code
int main()
{
 
    // Get the number of vertices
    int numberOfVertices = 6;
 
    // Find the numberOfEdges and degree
    // from the numberOfVertices
    // and print the result
    cout << "For numberOfVertices = "
         << numberOfVertices
         << "\nDegree = "
         << getDegree(numberOfVertices)
         << "\nNumber of Edges = "
         << getnumberOfEdges(numberOfVertices);
 
    return 0;
}


Java
// Java implementation of above program.
class GfG {
 
    // function that calculates the
    // number of Edge in a cycle graph.
    static int getnumberOfEdges(int numberOfVertices)
    {
        int numberOfEdges = 0;
 
        // The numberOfEdges of the cycle graph
        // will be same as the numberOfVertices
        numberOfEdges = numberOfVertices;
 
        // return the numberOfEdges
        return numberOfEdges;
    }
 
    // function that calculates the degree
    static int getDegree(int numberOfVertices)
    {
        int degree;
 
        // The degree of the cycle graph
        // will be twice the numberOfVertices
        degree = 2 * numberOfVertices;
 
        // return the degree
        return degree;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        // Get the number of vertices
        int numberOfVertices = 6;
 
        // Find the numberOfEdges and degree
        // from the numberOfVertices
        // and print the result
        System.out.println("For numberOfVertices = "
                           + numberOfVertices + "\nDegree = "
                           + getDegree(numberOfVertices)
                           + "\nNumber of Edges = "
                           + getnumberOfEdges(numberOfVertices));
    }
}
 
// This code contributed by Rajput-Ji


Python3
# Python 3 implementation of above program
 
# function that calculates the
# number of Edge in a cycle graph.
def getnumberOfEdges(numberOfVertices):
 
    numberOfEdges = 0
 
    # The numberOfEdges of the cycle graph
    # will be same as the numberOfVertices
    numberOfEdges = numberOfVertices
 
    # return the numberOfEdges
    return numberOfEdges
 
# function that calculates the degree
def getDegree(numberOfVertices):
 
    # The degree of the cycle graph
    # will be twice the numberOfVertices
    degree = 2 * numberOfVertices
 
    # return the degree
    return degree
 
# Driver code
if __name__ == "__main__":
     
    # Get the number of vertices
    numberOfVertices = 6
 
    # Find the numberOfEdges and degree
    # from the numberOfVertices
    # and print the result
    print("For numberOfVertices = ",
           numberOfVertices, "\nDegree = ",
               getDegree(numberOfVertices),
          "\nNumber of Edges = ",
        getnumberOfEdges(numberOfVertices))
 
# This code is contributed by ChitraNayal


C#
// C# implementation of above program.
class GfG {
 
    // function that calculates the
    // number of Edge in a cycle graph.
    static int getnumberOfEdges(int numberOfVertices)
    {
        int numberOfEdges = 0;
 
        // The numberOfEdges of the cycle graph
        // will be same as the numberOfVertices
        numberOfEdges = numberOfVertices;
 
        // return the numberOfEdges
        return numberOfEdges;
    }
 
    // function that calculates the degree
    static int getDegree(int numberOfVertices)
    {
        int degree;
 
        // The degree of the cycle graph
        // will be twice the numberOfVertices
        degree = 2 * numberOfVertices;
 
        // return the degree
        return degree;
    }
 
    // Driver code
    static void Main()
    {
 
        // Get the number of vertices
        int numberOfVertices = 6;
 
        // Find the numberOfEdges and degree
        // from the numberOfVertices
        // and print the result
        System.Console.WriteLine("For numberOfVertices = "
                                 + numberOfVertices + "\nDegree = "
                                 + getDegree(numberOfVertices)
                                 + "\nNumber of Edges = "
                                 + getnumberOfEdges(numberOfVertices));
    }
}
 
// This code contributed by mits


PHP


输出:
For numberOfVertices = 4
Degree = 8
Number of Edges = 4

程序2:对于6个顶点的循环图

C++

// C++ implementation of above program.
 
#include 
using namespace std;
 
// function that calculates the
// number of Edge in a cycle graph.
int getnumberOfEdges(int numberOfVertices)
{
    int numberOfEdges = 0;
 
    // The numberOfEdges of the cycle graph
    // will be same as the numberOfVertices
    numberOfEdges = numberOfVertices;
 
    // return the numberOfEdges
    return numberOfEdges;
}
 
// function that calculates the degree
int getDegree(int numberOfVertices)
{
    int degree;
 
    // The degree of the cycle graph
    // will be twice the numberOfVertices
    degree = 2 * numberOfVertices;
 
    // return the degree
    return degree;
}
 
// Driver code
int main()
{
 
    // Get the number of vertices
    int numberOfVertices = 6;
 
    // Find the numberOfEdges and degree
    // from the numberOfVertices
    // and print the result
    cout << "For numberOfVertices = "
         << numberOfVertices
         << "\nDegree = "
         << getDegree(numberOfVertices)
         << "\nNumber of Edges = "
         << getnumberOfEdges(numberOfVertices);
 
    return 0;
}

Java

// Java implementation of above program.
class GfG {
 
    // function that calculates the
    // number of Edge in a cycle graph.
    static int getnumberOfEdges(int numberOfVertices)
    {
        int numberOfEdges = 0;
 
        // The numberOfEdges of the cycle graph
        // will be same as the numberOfVertices
        numberOfEdges = numberOfVertices;
 
        // return the numberOfEdges
        return numberOfEdges;
    }
 
    // function that calculates the degree
    static int getDegree(int numberOfVertices)
    {
        int degree;
 
        // The degree of the cycle graph
        // will be twice the numberOfVertices
        degree = 2 * numberOfVertices;
 
        // return the degree
        return degree;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        // Get the number of vertices
        int numberOfVertices = 6;
 
        // Find the numberOfEdges and degree
        // from the numberOfVertices
        // and print the result
        System.out.println("For numberOfVertices = "
                           + numberOfVertices + "\nDegree = "
                           + getDegree(numberOfVertices)
                           + "\nNumber of Edges = "
                           + getnumberOfEdges(numberOfVertices));
    }
}
 
// This code contributed by Rajput-Ji

Python3

# Python 3 implementation of above program
 
# function that calculates the
# number of Edge in a cycle graph.
def getnumberOfEdges(numberOfVertices):
 
    numberOfEdges = 0
 
    # The numberOfEdges of the cycle graph
    # will be same as the numberOfVertices
    numberOfEdges = numberOfVertices
 
    # return the numberOfEdges
    return numberOfEdges
 
# function that calculates the degree
def getDegree(numberOfVertices):
 
    # The degree of the cycle graph
    # will be twice the numberOfVertices
    degree = 2 * numberOfVertices
 
    # return the degree
    return degree
 
# Driver code
if __name__ == "__main__":
     
    # Get the number of vertices
    numberOfVertices = 6
 
    # Find the numberOfEdges and degree
    # from the numberOfVertices
    # and print the result
    print("For numberOfVertices = ",
           numberOfVertices, "\nDegree = ",
               getDegree(numberOfVertices),
          "\nNumber of Edges = ",
        getnumberOfEdges(numberOfVertices))
 
# This code is contributed by ChitraNayal

C#

// C# implementation of above program.
class GfG {
 
    // function that calculates the
    // number of Edge in a cycle graph.
    static int getnumberOfEdges(int numberOfVertices)
    {
        int numberOfEdges = 0;
 
        // The numberOfEdges of the cycle graph
        // will be same as the numberOfVertices
        numberOfEdges = numberOfVertices;
 
        // return the numberOfEdges
        return numberOfEdges;
    }
 
    // function that calculates the degree
    static int getDegree(int numberOfVertices)
    {
        int degree;
 
        // The degree of the cycle graph
        // will be twice the numberOfVertices
        degree = 2 * numberOfVertices;
 
        // return the degree
        return degree;
    }
 
    // Driver code
    static void Main()
    {
 
        // Get the number of vertices
        int numberOfVertices = 6;
 
        // Find the numberOfEdges and degree
        // from the numberOfVertices
        // and print the result
        System.Console.WriteLine("For numberOfVertices = "
                                 + numberOfVertices + "\nDegree = "
                                 + getDegree(numberOfVertices)
                                 + "\nNumber of Edges = "
                                 + getnumberOfEdges(numberOfVertices));
    }
}
 
// This code contributed by mits

的PHP


输出:
For numberOfVertices = 6
Degree = 12
Number of Edges = 6
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”