C++ Program to Implement the Vizing’s Theorem



In this article, we will explain the Vizing's theorem and implement it in C++ to color a graph using the Greedy Coloring Algorithm.

What is Vizing's Theorem?

Vizing's theorem states that for any graph, the minimum number of colors needed to color the edges (chromatic index) is either equal to the maximum degree G of the graph or one more than maximum degree G + 1.

Vizing's Theorem

The degree of a vertex is the number of edges connected to it. The maximum degree G refer to highest degree for any vertex in the graph. It is important to note that while coloring the edges of a graph, no two edges close to the same vertex can have the same color.

For example, consider the following graph:

// Graph with 3 vertices
   0
  / \ 
 1 - 2

Maximum degree G = 2
Chromatic index = G + 1 = 3

Total 3 colors needed to color the edges

The theorem doesn't tell us when to use G or G + 1. In general,

  • Use G: For easier graphs like bipartite graphs, trees, even cycles
  • Use G + 1: For harder graphs like odd cycles, complete graphs (kn), and some non-bipartite graphs

Greedy Coloring Algorithm to Implement Vizing's Theorem

In greedy coloring algorithm, we will try to color the edges of the graph using the minimum number of colors. The algorithm works as follows:

  • Use a struct Edge to define the edges of the graph with their source(u), destination(v), and color
  • Create a vector of edges to store all edges of the graph
  • Build the adjacency list from the vector of edges by adding each edge's vertices to each other's adjacency list.
  • Now, calculate the maximum degree G of the graph by finding the maximum size of the adjacency list
  • Set maximum number of colors as G + 1
  • Iterate over the edges and assign colors using a greedy approach
  • For each edge, check the colors of adjacent edges and assign the first available color
  • Print the colored edges

C++ Code to Implement Vizing's Theorem

In the code below, we implement the greedy coloring algorithm to color the edges of a graph according to Vizing's theorem:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Edge {
    int u, v, color;
};

int maxDegree(const vector<vector<int>>& adj) {
    int maxDeg = 0;
    for (const auto& neighbors : adj)
        maxDeg = max(maxDeg, (int)neighbors.size());
    return maxDeg;
}

int main() {
    int V = 4;  // number of vertices
    vector<vector<int>> adj(V);  // adjacency list

    // Sample edges: a square
    vector<Edge> edges = {
        {0, 1, -1},
        {1, 2, -1},
        {2, 3, -1},
        {3, 0, -1},
        {0, 2, -1} // diagonal
    };

    // Build adjacency for degree calculation
    for (const auto& e : edges) {
        adj[e.u].push_back(e.v);
        adj[e.v].push_back(e.u);
    }

    int maxDeg = maxDegree(adj);
    int maxColors = maxDeg + 1;

    // Assign colors to edges greedily
    for (int i = 0; i < edges.size(); ++i) {
        vector<bool> used(maxColors, false);
        for (int j = 0; j < edges.size(); ++j) {
            if (i != j) {
                // If adjacent (sharing a vertex), mark its color as used
                if (edges[i].u == edges[j].u || edges[i].u == edges[j].v ||
                    edges[i].v == edges[j].u || edges[i].v == edges[j].v) {
                    if (edges[j].color != -1)
                        used[edges[j].color] = true;
                }
            }
        }
        // Assign the first unused color
        for (int c = 0; c < maxColors; ++c) {
            if (!used[c]) {
                edges[i].color = c;
                break;
            }
        }
    }

    // Output results
    cout << "Edge Coloring (each edge: u - v : color):" << endl;
    int colorUsed = 0;
    for (const auto& e : edges) {
        cout << e.u << " - " << e.v << " : Color " << e.color << endl;
        colorUsed = max(colorUsed, e.color + 1);
    }

    cout << "\nColors used: " << colorUsed << endl;
    cout << "Maximum degree: " << maxDeg << endl;
    cout << "Vizing's bound: " << maxDeg << " or " << maxDeg + 1 << endl;

    return 0;
}

The output of the above code will be:

Edge Coloring (each edge: u - v : color):
0 - 1 : Color 0
1 - 2 : Color 1
2 - 3 : Color 0
3 - 0 : Color 1
0 - 2 : Color 2
Colors used: 3
Maximum degree: 3
Vizing's bound: 3 or 4

Time and Space Complexity

Time Complexity: The time complexity of this algorithm is O(E^2), where E is the number of edges in the graph.

Space Complexity: The space complexity is O(V + E), where V is the number of vertices and E is the number of edges, due to the storage of the adjacency list and edge list.

Farhan Muhamed
Farhan Muhamed

No Code Developer, Vibe Coder

Updated on: 2025-06-16T18:14:31+05:30

240 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements