VOOZH about

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

⇱ Edge Coloring of a Graph - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Edge Coloring of a Graph

Last Updated : 15 Mar, 2023

In graph theory, edge coloring of a graph is an assignment of "colors" to the edges of the graph so that no two adjacent edges have the same color with an optimal number of colors. Two edges are said to be adjacent if they are connected to the same vertex. There is no known polynomial time algorithm for edge-coloring every graph with an optimal number of colors. 

Nevertheless, a number of algorithms have been developed that relax one or more of these criteria, they only work on a subset of graphs, or they do not always use an optimal number of colors, or they do not always run in polynomial time.

Examples

Input : u1 = 1, v1 = 4 
 u2 = 1, v2 = 2
 u3 = 2, v3 = 3
 u4 = 3, v4 = 4
Output : Edge 1 is of color 1
 Edge 2 is of color 2
 Edge 3 is of color 1
 Edge 4 is of color 2

The above input shows the pair of vertices(ui, vi)
who have an edge between them. The output shows the color 
assigned to the respective edges.

👁 Image

Edge colorings are one of several different types of graph coloring problems. The above figure of a Graph shows an edge coloring of a graph by the colors green and black, in which no adjacent edge have the same color.

Below is an algorithm to solve the edge coloring problem which may not use an optimal number of colors: 

Algorithm: 

  1. Use BFS traversal to start traversing the graph.
  2. Pick any vertex and give different colors to all of the edges connected to it, and mark those edges as colored.
  3. Traverse one of it's edges.
  4. Repeat step to with a new vertex until all edges are colored.

Below is the implementation of above approach: 


Output
Edge 1 is of color 1
Edge 2 is of color 2
Edge 3 is of color 1
Edge 4 is of color 2

Complexity Analysis:

  • Time Complexity: O(N) Where N is the number of nodes in the graph.
  • Auxiliary Space: O(N)
Comment