Ciclo: – ciclo es un camino de aristas y vértices en el que se puede alcanzar un vértice desde sí mismo. o dicho de otro modo, es un paseo Cerrado.
Ciclo par: en el que está presente un número par de vértices, se conoce como ciclo par.
Ciclo impar: en el que el número impar de vértices está presente se conoce como ciclo impar.
Dado el número de vértices en un gráfico cíclico. La tarea es determinar la cantidad de colores necesarios para colorear el gráfico de modo que no haya dos vértices adyacentes que tengan el mismo color.
Acercarse:
Si el nro. de vértices es par entonces es ciclo par y para colorear dicho gráfico necesitamos 2 colores.
Si el nro. de vértices es impar, entonces es ciclo impar y para colorear dicho gráfico necesitamos 3 colores.
Ejemplos:
Input : vertices = 3 Output : No. of colors require is: 3 Input : vertices = 4 Output : No. of colors require is: 2
Ejemplo 1: Ciclo Par: Número de vértices = 4
Color requerido = 2
Ejemplo 2: Ciclo Impar: Número de vértices = 5
Color requerido = 3
C++
// 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 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
<?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 ?>
Javascript
<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 code var vertices = 3; document.write("No. of colors require is: " + Color(vertices)); // This code is contributed by itsok </script>
No. of colors require is: 3
Complejidad de tiempo: O(1)
Complejidad espacial: O(1)
Publicación traducida automáticamente
Artículo escrito por Naman_Garg y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA