Coloring a Cycle Graph - GeeksforGeeks

20 Aug, 2021
Cycle:- cycle is a path of edges and vertices wherein a vertex is reachable from itself. or in other words, it is a Closed walk. 
Even Cycle:- In which Even number of vertices is present is known as Even Cycle. 
Odd Cycle:- In which Odd number of Vertices is present is known as Odd Cycle.Given the number of vertices in a Cyclic Graph. The task is to determine the Number of colors required to color the graph so that No two Adjacent vertices have the same color.
Approach: 
If the no. of vertices is Even then it is Even Cycle and to color such graph we require 2 colors. If the no. of vertices is Odd then it is Odd Cycle and to color such graph we require 3 colors.  
Examples:  
Input : vertices = 3
Output : No. of colors require is: 3

Input : vertices = 4
Output : No. of colors require is: 2
Example 1: Even Cycle: Number of vertices = 4 
Color required = 2  
Example 2: Odd Cycle: Number of vertices = 5  
Color required = 3  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find number of colors// required to color a cycle graph#include <bits/stdc++.h>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 codeint main(){    int vertices = 3;    cout << "No. of colors require is: " << Color(vertices);    return 0;}
// Java program to find number of colors// required to color a cycle graphimport 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
# 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 Codeif __name__=='__main__':    vertices = 3    print ("No. of colors require is:",Color(vertices)) # this code is contributed by Naman_Garg
// C# program to find number of colors// required to color a cycle graphusing 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 Codepublic static void Main (){    int vertices = 3;         Console.WriteLine("No. of colors required is: " +                                   Color(vertices));}} // This code is contributed by anuj_67
<?php// PHP program to find number of colors// required to color a cycle graph // Function that calculates Color// require to color a graph.function 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$vertices = 3;echo "No. of colors required is: " ,                  Color($vertices); // This code is contributed// by anuj_67?>
<script> // Javascript program to find number of colors// required to color a cycle graph // Function that calculates Color// require to color a graph.function Color(vertices){    var 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 codevar vertices = 3;document.write("No. of colors require is: " +               Color(vertices));                // This code is contributed by itsok </script>
No. of colors require is: 3
 
Naman_Garg
vt_m
itsok
kalrap615
Graph Coloring
graph-cycle
C++ Programs
Graph
Graph
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Passing a function as a parameter in C++
Const keyword in C++
Program to implement Singly Linked List in C++ using class
cout in C++
Dynamic _Cast in C++
Breadth First Search or BFS for a Graph
Dijkstra's shortest path algorithm | Greedy Algo-7
Depth First Search or DFS for a Graph
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5