
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Edge Coloring for Line Graph of an Input Graph in C++
Edge coloring is a method of coloring the edges of a graph, such that no two edges having a common vertex is colored with same color. A line Graph is a special type of graph which help us to assume an edge-coloring problem into a vertex-coloring problem. In other words, using a line graph makes edge coloring easier. In this article, we will discuss how to perform edge coloring of a graph using line graph and greedy coloring approach.
What is Line Graph?
Line graph is a special graph that is created from another graph. In the line graph:
- Each vertex represents an edge of the original graph.
- Two vertices are connected if their edges in the original graph are adjacent, meaning they share a common vertex.
By converting the graph into its line graph, we can easily use the normal vertex coloring techniques to solve the edge coloring problem. The image below shows an example of line graph of a graph.

Edge Coloring using Line Graph
In edge coloring using line graph, we first convert the graph into its line graph. After that, we apply vertex coloring to the line graph. Each color assigned to the vertex of the line graph represents a color assigned to the corresponding edge in the original graph. In this way, we make sure that adjacent edges in the original graph get different colors.
Steps to Perform Edge Coloring
The following steps explain the logic used for edge coloring using line graph:
- Step 1: Input the original graph and create a list of edges with unique labels.
- Step 2: Create the line graph by connecting two edge-nodes if their corresponding edges in original graph are adjacent.
- Step 3: Use greedy coloring to assign colors to each vertex in the line graph. This ensures that no two connected edge-nodes (i.e., adjacent edges) have same color.
- Step 4: Map back the vertex color of the line graph to the corresponding edge in the original graph.
- Step 5: Display the edge colors of the original graph.
C++ Program to Perform Edge Coloring
The code below implements above algorithm in C++ language. It takes graph as input and displays color assigned to each edge of the graph.
#include <iostream> #include <vector> #include <map> #include <set> using namespace std; typedef pair<int, int> Edge; bool areAdjacent(Edge a, Edge b) { return a.first == b.first || a.first == b.second || a.second == b.first || a.second == b.second; } int main() { // Hardcoded graph edges vector<Edge> edges = { {0, 1}, {0, 2}, {1, 3}, {2, 3}, {3, 4} }; // Optional: normalize edges so smaller vertex comes first for (auto& e : edges) { if (e.first > e.second) swap(e.first, e.second); } int E = edges.size(); // Build line graph vector<set<int>> lineGraph(E); for (int i = 0; i < E; i++) { for (int j = i + 1; j < E; j++) { if (areAdjacent(edges[i], edges[j])) { lineGraph[i].insert(j); lineGraph[j].insert(i); } } } // Greedy coloring vector<int> colors(E, -1); for (int i = 0; i < E; i++) { set<int> used; for (int neighbor : lineGraph[i]) { if (colors[neighbor] != -1) used.insert(colors[neighbor]); } int c = 0; while (used.count(c)) c++; colors[i] = c; } // Display results cout << "Edge coloring result:" << endl; for (int i = 0; i < E; i++) { cout << "Edge (" << edges[i].first << ", " << edges[i].second << ") -> Color " << colors[i] << endl; } return 0; }
The output of the above code will be:
Edge coloring result: Edge (0, 1) -> Color 0 Edge (0, 2) -> Color 1 Edge (1, 3) -> Color 1 Edge (2, 3) -> Color 0 Edge (3, 4) -> Color 2