Vizing’s Theorem
Last Updated :
13 Apr, 2023
In graph theory, Vizing’s theorem states that every simple undirected graph may be edge colored using a number of colors that is at most one larger than the maximum degree ‘d’ of the graph. In simple meaning this theorem states that the chromatic index of the simple graph can be either ‘d’ or ‘d’ +1. The minimum number of colors needed for the edge coloring of the graph is called chromatic index.
There are 5 vertices in the above graph G. Highest Degree is 4, but we need 5 colors, so that no edge shares the same color with any edge of the adjacent vertices, as you can see in the above graph. Therefore the required number of valid colors for this graph is equal to 5, which is ( ‘highest degree’ + 1 ).
Note: c1, c2, c3, c4 and c5 in the above diagram implies distinct colors.
Examples :
Input :
v = 3, e = 3
{{ 1, 2, -1 },
{ 2, 3, -1 },
{ 3, 1, -1 }};
Output :
3 colors needs to generate a valid edge coloring :
color between v(1): 1 and v(2): 2 is: color 1
color between v(1): 2 and v(2): 3 is: color 2
color between v(1): 3 and v(2): 1 is: color 3
Algorithm:
- After initializing the number of edges, assign the vertex pair of every edge
- Color the graph edges according to the theorem
- Assign a color and then check its validity
- Check if any two adjacent edges have the same color, then increment the Color, goto flag and try next color
- Repeat till all the edges get it’s color according to the theorem
- Once done print the color of all the edges between the vertices
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void colorEdge( int edges[][3], int e)
{
int color;
for ( int i = 0; i < e; i++) {
color = 1;
flag:
edges[i][2] = color;
for ( int j = 0; j < e; j++) {
if (j == i)
continue ;
if ((edges[i][0] == edges[j][0])
|| (edges[i][1] == edges[j][0])
|| (edges[i][0] == edges[j][1])
|| (edges[i][1] == edges[j][1])) {
if (edges[i][2] == edges[j][2]) {
color++;
goto flag;
}
}
}
}
int maxColor = -1;
for ( int i = 0; i < e; i++) {
maxColor = max(maxColor, edges[i][2]);
}
cout << maxColor
<< " colors needs to generate a valid edge "
"coloring:"
<< endl;
for ( int i = 0; i < e; i++) {
cout << "color between v(1): " << edges[i][0]
<< " and v(2): " << edges[i][1]
<< " is: color " << edges[i][2] << endl;
}
}
int main()
{
int e = 5;
int edges[e][3] = { { 1, 2, -1 },
{ 2, 3, -1 },
{ 3, 4, -1 },
{ 4, 1, -1 },
{ 1, 3, -1 } };
colorEdge(edges, e);
return 0;
}
|
Java
import java.util.*;
public class VizingsTheorem {
public void colorEdge( int [][] edges, int e)
{
int i = 0 , color = 1 ;
while (i < e) {
edges[i][ 2 ] = color;
boolean flag = false ;
for ( int j = 0 ; j < e; j++) {
if (j == i)
continue ;
if ((edges[i][ 0 ] == edges[j][ 0 ])
|| (edges[i][ 1 ] == edges[j][ 0 ])
|| (edges[i][ 0 ] == edges[j][ 1 ])
|| (edges[i][ 1 ] == edges[j][ 1 ])) {
if (edges[i][ 2 ] == edges[j][ 2 ]) {
color++;
flag = true ;
break ;
}
}
}
if (flag == true ) {
continue ;
}
color = 1 ;
i++;
}
int maxColor = - 1 ;
for (i = 0 ; i < e; i++)
{
maxColor = Math.max(maxColor, edges[i][ 2 ]);
}
System.out.println(
maxColor
+ " colors needs to generate"
+ " a valid edge coloring:" );
for (i = 0 ; i < e; i++)
{
System.out.println(
"color between v(1): " + edges[i][ 0 ]
+ " and v(2): " + edges[i][ 1 ]
+ " is: color " + edges[i][ 2 ]);
}
}
public static void main(String[] args)
{
int e = 5 ;
int [][] edges = new int [e][ 3 ];
for ( int i = 0 ; i < e; i++) {
edges[i][ 2 ] = - 1 ;
}
edges[ 0 ][ 0 ] = 1 ;
edges[ 0 ][ 1 ] = 2 ;
edges[ 1 ][ 0 ] = 2 ;
edges[ 1 ][ 1 ] = 3 ;
edges[ 2 ][ 0 ] = 3 ;
edges[ 2 ][ 1 ] = 4 ;
edges[ 3 ][ 0 ] = 4 ;
edges[ 3 ][ 1 ] = 1 ;
edges[ 4 ][ 0 ] = 1 ;
edges[ 4 ][ 1 ] = 3 ;
VizingsTheorem c = new VizingsTheorem();
c.colorEdge(edges, e);
}
}
|
Python3
def colorEdge(edges, e):
i = 0
color = 1
while (i < e):
edges[i][ 2 ] = color
flag = False
for j in range (e):
if (j = = i):
continue
if ((edges[i][ 0 ] = = edges[j][ 0 ]) or
(edges[i][ 1 ] = = edges[j][ 0 ]) or
(edges[i][ 0 ] = = edges[j][ 1 ]) or
(edges[i][ 1 ] = = edges[j][ 1 ])):
if (edges[i][ 2 ] = = edges[j][ 2 ]):
color + = 1
flag = True
break
if (flag = = True ):
continue
color = 1
i + = 1
maxColor = - 1
for i in range (e):
maxColor = max (maxColor, edges[i][ 2 ])
print ( str (maxColor) + " colors needs to generate a valid edge coloring:" )
for i in range (e):
print ( "color between v(1): " + str (edges[i][ 0 ]) + " and v(2): "
+ str (edges[i][ 1 ]) + " is: color " + str (edges[i][ 2 ]))
if __name__ = = "__main__" :
e = 5
edges = [[ 0 for _ in range ( 3 )] for _ in range (e)]
for i in range (e):
edges[i][ 2 ] = - 1
edges[ 0 ][ 0 ] = 1
edges[ 0 ][ 1 ] = 2
edges[ 1 ][ 0 ] = 2
edges[ 1 ][ 1 ] = 3
edges[ 2 ][ 0 ] = 3
edges[ 2 ][ 1 ] = 4
edges[ 3 ][ 0 ] = 4
edges[ 3 ][ 1 ] = 1
edges[ 4 ][ 0 ] = 1
edges[ 4 ][ 1 ] = 3
colorEdge(edges, e)
|
C#
using System;
public class VizingsTheorem
{
public void colorEdge( int [,] edges, int e)
{
int i = 0, color = 1;
while (i < e)
{
edges[i, 2] = color;
bool flag = false ;
for ( int j = 0; j < e; j++)
{
if (j == i)
continue ;
if ((edges[i, 0] == edges[j, 0])
|| (edges[i, 1] == edges[j, 0])
|| (edges[i, 0] == edges[j, 1])
|| (edges[i, 1] == edges[j, 1]))
{
if (edges[i, 2] == edges[j, 2])
{
color++;
flag = true ;
break ;
}
}
}
if (flag == true )
continue ;
color = 1;
i++;
}
int maxColor = -1;
for (i = 0; i < e; i++)
{
maxColor = Math.Max(maxColor, edges[i, 2]);
}
Console.WriteLine(maxColor + " colors needs to generate a valid edge coloring:" );
for (i = 0; i < e; i++)
{
Console.WriteLine( "color between v(1): " + edges[i, 0] + " and v(2): " + edges[i, 1] + " is: color " + edges[i, 2]);
}
}
public static void Main( string [] args)
{
int e = 5;
int [,] edges = new int [e, 3];
for ( int i = 0; i < e; i++)
edges[i, 2] = -1;
edges[0, 0] = 1;
edges[0, 1] = 2;
edges[1, 0] = 2;
edges[1, 1] = 3;
edges[2, 0] = 3;
edges[2, 1] = 4;
edges[3, 0] = 4;
edges[3, 1] = 1;
edges[4, 0] = 1;
edges[4, 1] = 3;
VizingsTheorem c = new VizingsTheorem();
c.colorEdge(edges, e);
}
}
|
Javascript
<script>
function colorEdge(edges, e)
{
var i = 0, color = 1;
while (i < e) {
edges[i][2] = color;
var flag = false ;
for ( var j = 0; j < e; j++) {
if (j == i)
continue ;
if ((edges[i][0] == edges[j][0])
|| (edges[i][1] == edges[j][0])
|| (edges[i][0] == edges[j][1])
|| (edges[i][1] == edges[j][1])) {
if (edges[i][2] == edges[j][2]) {
color++;
flag = true ;
break ;
}
}
}
if (flag == true ) {
continue ;
}
color = 1;
i++;
}
var maxColor = -1;
for (i = 0; i < e; i++)
{
maxColor = Math.max(maxColor, edges[i][2]);
}
document.write(
maxColor
+ " colors needs to generate"
+ " a valid edge coloring:<br>" );
for (i = 0; i < e; i++)
{
document.write(
"color between v(1): " + edges[i][0]
+ " and v(2): " + edges[i][1]
+ " is: color " + edges[i][2] + "<br>" );
}
}
var e = 5;
var edges = Array.from(Array(e), ()=>Array(3));
for ( var i = 0; i < e; i++) {
edges[i][2] = -1;
}
edges[0][0] = 1;
edges[0][1] = 2;
edges[1][0] = 2;
edges[1][1] = 3;
edges[2][0] = 3;
edges[2][1] = 4;
edges[3][0] = 4;
edges[3][1] = 1;
edges[4][0] = 1;
edges[4][1] = 3;
colorEdge(edges, e);
</script>
|
Output
3 colors needs to generate a valid edge coloring:
color between v(1): 1 and v(2): 2 is: color 1
color between v(1): 2 and v(2): 3 is: color 2
color between v(1): 3 and v(2): 4 is: color 1
color between v(1): 4 and v(2): 1 is: color 2
color between v(1): 1 and v(2): 3 is: color 3
Time Complexity: O(e2)
Auxiliary Space: O(1)
As constant extra space is used.
Share your thoughts in the comments
Please Login to comment...