# C++ Program to Implement the Vizing’s Theorem

Vizing’s theorem state that the chromatic index of simple graph can be either maxdegree or maxdegree+1. Here, chromatic index means maximum color needed for the edge coloring of the graph.

This is a C++ program to implement the Vizing’s Theorem.

## Algorithm

Begin
Take the number of vertices and edges as input.
Take the vertex pair for the edges.
function EdgeColor() : Color the graph edges.
1) Assign color to current edge as c i.e. 1 initially.
2) If the same color is occupied by any of the adjacent edges,
then discard this color and go to flag again and try with the next color.
End

## Example

#include<iostream>
using namespace std;
void EdgeColor(int ed[], int e) {
int i, c, j;
for(i = 0; i < e; i++) {
c = 1; //Assign color to current edge 1 initially.
// If the same color is occupied by any of the adjacent edges,
// then discard this color and go to flag again and try next color.
flag:
ed[i] = c;
for(j = 0; j < e; j++) {
if(j == i)
continue;
if(ed[j] == ed[i] || ed[j] == ed[i] || ed[j] == ed[i] || ed[j] == ed[i]) {
if(ed[j] == ed[i]) {
c++;
goto flag;
}
}
}
}
}
int main() {
int i, n, e, j, max = -1;
cout<<"Enter the number of vertices of the graph: ";
cin>>n;
cout<<"Enter the number of edges of the graph: ";
cin>>e;
int ed[e], deg[n+1] = {0};
for(i = 0; i < e; i++) {
cout<<"\nEnter the vertex pair for edge "<<i+1;
cout<<"\nN(1): ";
cin>>ed[i];
cout<<"N(2): ";
cin>>ed[i];
//calculate the degree of each vertex
ed[i] = -1;
deg[ed[i]]++;
deg[ed[i]]++;
}
//find out the maximum degree.
for(i = 1; i <= n; i++) {
if(max < deg[i])
max = deg[i];
}
EdgeColor(ed , e);
cout<<"\nAccording to Vizing's theorem this graph can use maximum of "<<max+1<<" colors to generate a valid edge coloring.\n\n";
for(i = 0; i < e; i++)
cout<<"\nThe color of the edge between vertex N(1):"<<ed[i]<<" and N(2):"<<ed[i]<<" is: color"<<ed[i]<<".";
}

## Output

Enter the number of vertices of the graph: 4
Enter the number of edges of the graph: 3

Enter the vertex pair for edge 1
N(1): 1
N(2): 2

Enter the vertex pair for edge 2
N(1): 3
N(2): 2

Enter the vertex pair for edge 3
N(1): 4
N(2): 1

According to Vizing's theorem this graph can use maximum of 3 colors to generate a valid edge coloring.

The color of the edge between vertex N(1):1 and N(2):2 is: color1.
The color of the edge between vertex N(1):3 and N(2):2 is: color2.
The color of the edge between vertex N(1):4 and N(2):1 is: color2.

Updated on: 30-Jul-2019

108 Views 