# Vizing’s Theorem

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 maximum number of color 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 vertices and the number of edges, assign the vertex pair of every edge
- Calculate the maximum degree among all the vertices
- 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++ program to illustrate ` `// Vizing's Theorem ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to print the color of the edge ` `void` `colorEdge(` `int` `edg[][3], ` `int` `e) ` `{ ` ` ` `int` `color; ` ` ` ` ` `// Assign a color to every edge 'i'. ` ` ` `for` `(` `int` `i = 0; i < e; i++) { ` ` ` `color = 1; ` ` ` `flag: ` ` ` `// Assign a color and ` ` ` `// then check its validity. ` ` ` `edg[i][2] = color; ` ` ` `for` `(` `int` `j = 0; j < e; j++) { ` ` ` `if` `(j == i) ` ` ` `continue` `; ` ` ` ` ` `// If the color of edges ` ` ` `// is adjacent to edge i ` ` ` `if` `((edg[i][0] == edg[j][0])|| ` ` ` `(edg[i][1] == edg[j][0]) || ` ` ` `(edg[i][0] == edg[j][1]) || ` ` ` `(edg[i][1] == edg[j][1])) { ` ` ` ` ` `// If the color matches ` ` ` `if` `(edg[i][2] == edg[j][2]) { ` ` ` ` ` `// Increment the color, ` ` ` `// denotes the change in color. ` ` ` `color++; ` ` ` ` ` `// goes back, and assigns ` ` ` `// the next color. ` ` ` `goto` `flag; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `} ` `} ` ` ` `// Function to assign Degree ` `void` `degree(` `int` `v, ` `int` `e, ` `int` `edg[][3]) ` `{ ` ` ` `int` `maximum = -1; ` ` ` `int` `Deg[v + 1] = { 0 }; ` ` ` ` ` `for` `(` `int` `i = 0; i < e; i++) { ` ` ` `Deg[edg[i][0]]++; ` ` ` `Deg[edg[i][1]]++; ` ` ` `} ` ` ` ` ` `// To find the maximum degree. ` ` ` `for` `(` `int` `i = 1; i <= v; i++) { ` ` ` `if` `(maximum < Deg[i]) ` ` ` `maximum = Deg[i]; ` ` ` `} ` ` ` ` ` `// color the edges of the graph. ` ` ` `colorEdge(edg, e); ` ` ` `cout << maximum + 1 << ` `" colors needs to generate"` ` ` `<< ` `" a valid edge coloring :\n"` `; ` ` ` ` ` `for` `(` `int` `i = 0; i < e; i++) ` ` ` `cout << ` `"\ncolor between v(1): "` `<< edg[i][0] << ` `" and v(2): "` ` ` `<< edg[i][1] << ` `" is: color "` `<< edg[i][2]; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `// initialize the number ` ` ` `// of vertexes of the graph ` ` ` `int` `v = 4; ` ` ` `// initialize the number ` ` ` `// of edges of the graph ` ` ` `int` `e = 5; ` ` ` ` ` `// initialize the vertex ` ` ` `// pair of every edge in a graph ` ` ` `int` `edg[e][3] = { { 1, 2, -1 }, ` ` ` `{ 2, 3, -1 }, ` ` ` `{ 3, 4, -1 }, ` ` ` `{ 4, 1, -1 }, ` ` ` `{ 1, 3, -1 } }; ` ` ` ` ` `degree(v, e, edg); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

4 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

## Recommended Posts:

- Check whether an array can be made strictly decreasing by modifying at most one element
- Construct the Rooted tree by using start and finish time of its DFS traversal
- Rearrange numbers in an array such that no two adjacent numbers are same
- Merge two BSTs with constant extra space
- Largest sub-string where all the characters appear at least K times
- Longest prefix in a string with highest frequency
- Convert given integer X to the form 2^N - 1
- Data Structures and Algorithms Online Courses : Free and Paid
- Sorting Algorithms Visualization : Bubble Sort
- Complexity Analysis of Binary Search
- Find maximum path sum in a 2D matrix when exactly two left moves are allowed
- Factorial of an Array of integers
- Find the number of Chicks in a Zoo at Nth day
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.