VOOZH about

URL: https://www.geeksforgeeks.org/dsa/coloring-a-cycle-graph/

⇱ Coloring a Cycle Graph - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Coloring a Cycle Graph

Last Updated : 2 Sep, 2022

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 

👁 Image

Color required = 2  

👁 Image

Example 2: Odd Cycle: Number of vertices = 5  

👁 Image

Color required = 3  

👁 Image

Implementation:


Output
No. of colors require is: 3

Complexity Analysis:

  • Time Complexity: O(1)
  • Space Complexity: O(1)
Comment