📜  着色循环图

📅  最后修改于: 2021-06-01 02:25:45             🧑  作者: Mango

循环:-循环是一条边和顶点的路径,其中顶点可从其自身到达。换句话说,这是一次封闭式步行。

偶数周期:-出现偶数个顶点的方法称为偶数周期。

奇数周期:-出现奇数个顶点的过程称为奇数周期。
给定循环图中的顶点数。任务是确定为图形着色所需的颜色数,以使没有两个相邻的顶点具有相同的颜色。

方法:

例子:

Input : vertices = 3
Output : No. of colors require is: 3

Input : verices = 4
Output : No. of colors require is: 2

示例1:偶数周期:顶点数= 4

所需颜色= 2

示例2:奇周期:顶点数= 5

所需颜色= 3

C++
// CPP program to find number of colors
// required to color a cycle graph
#include 
using namespace std;
 
// Function that calculates Color
// require to color a graph.
int Color(int vertices)
{
    int result = 0;
 
    // Check if number of vertices
    // is odd or even.
    // If number of vertices is even
    // then color require is 2 otherwise 3
    if (vertices % 2 == 0)
        result = 2;
    else
        result = 3;
 
    return result;
}
 
// Driver code
int main()
{
    int vertices = 3;
    cout << "No. of colors require is: " << Color(vertices);
    return 0;
}


Java
// Java program to find number of colors
// required to color a cycle graph
import java.io.*;
 
class GFG {
   
    // Function that calculates Color
    // require to color a graph. 
    static int Color(int vertices)
    {
        int result = 0;
   
        // Check if number of vertices
        // is odd or even.
        // If number of vertices is even
        // then color require is 2 otherwise 3
        if (vertices % 2 == 0)
            result = 2;
        else
            result = 3;
       
        return result;
    } 
       
    // Driver program to test above function
    public static void main (String[] args)
    {
        int vertices = 3;
         
        System.out.println("No. of colors require is: " + Color(vertices));
           
    }
}
 
// this code is contributed by Naman_Garg


Python3
# Naive Python3 Program to
# find the number of colors
# required to color a cycle graph 
   
# Function to find Color required.
def Color(vertices): 
   
    result = 0
   
    # Check if number of vertices
    # is odd or even.
    # If number of vertices is even
    # then color require is 2 otherwise 3
    if (vertices % 2 == 0):
        result = 2
    else:
        result = 3
   
    return result
   
# Driver Code
if __name__=='__main__':
    vertices = 3
    print ("No. of colors require is:",Color(vertices))
 
# this code is contributed by Naman_Garg


C#
// C# program to find number of colors
// required to color a cycle graph
using System;
 
class GFG
{
 
// Function that calculates Color
// require to color a graph.
static int Color(int vertices)
{
    int result = 0;
 
    // Check if number of vertices
    // is odd or even.
    // If number of vertices is even
    // then color require is 2 otherwise 3
    if (vertices % 2 == 0)
        result = 2;
    else
        result = 3;
 
    return result;
}
 
// Driver Code
public static void Main ()
{
    int vertices = 3;
     
    Console.WriteLine("No. of colors required is: " +
                                   Color(vertices));
}
}
 
// This code is contributed by anuj_67


PHP


Javascript


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