using System;
public class VizingsTheorem
{
// Function to find the chromatic index
public void colorEdge(int[,] edges, int e)
{
// Initialize edge to first edge and color to color 1
int i = 0, color = 1;
// Repeat until all edges are done coloring
while (i < e)
{
// Give the selected edge a color
edges[i, 2] = color;
bool flag = false;
// Iterate through all others edges to check
for (int j = 0; j < e; j++)
{
// Ignore if same edge
if (j == i)
continue;
// Check if one vertex is similar
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]))
{
// Check if color is similar
if (edges[i, 2] == edges[j, 2])
{
// Increment the color by 1
color++;
flag = true;
break;
}
}
}
// If same color faced then repeat again
if (flag == true)
continue;
// Or else proceed to a new vertex with color 1
color = 1;
i++;
}
// Check the maximum color from all the edge colors
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]);
}
}
// Driver code
public static void Main(string[] args)
{
// Number of edges
int e = 5;
// Edge list
int[,] edges = new int[e, 3];
// Initialize all edge colors to 0
for (int i = 0; i < e; i++)
edges[i, 2] = -1;
// Edges
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;
// Run the function
VizingsTheorem c = new VizingsTheorem();
c.colorEdge(edges, e);
}
}