Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int VertexColoring(this Graph graph, out int[] colors)
- {
- if (graph.Directed)
- throw new ArgumentException();
- if (0 == graph.VerticesCount)
- {
- colors = new int[0];
- return 0;
- }
- int maxColour = 0;
- int[] vertexColors = new int[graph.VerticesCount];
- for (int i = 0; i < vertexColors.Length; i++)
- {
- vertexColors[i] = -1; //vertex not colored yet
- }
- int[] usedColors = new int[graph.VerticesCount];
- int color = 0;
- for (int v = 0; v < graph.VerticesCount; v++)
- {
- Array.Clear(usedColors, 0, usedColors.Length);
- color = 0;
- foreach (var e in graph.OutEdges(v))
- {
- if (-1 != vertexColors[e.To])
- {
- color = vertexColors[e.To];
- usedColors[color] = 1; //colour used
- }
- }
- for (int i = 0; i < usedColors.Length; i++)
- {
- if (0 == usedColors[i])
- {
- vertexColors[v] = i;
- if (i > maxColour) //for count of all colours
- {
- maxColour = i;
- }
- break;
- }
- //int minColour = 0; bool foundMinColour = false;
- //while (!foundMinColour)
- //{
- // foundMinColour = true;
- // foreach (var e in graph.OutEdges(v))
- // {
- // if (vertexColors[e.To] == minColour)
- // {
- // minColour++;
- // foundMinColour = false;
- // break;
- // }
- // }
- //}
- //if (minColour > maxColour)
- //{
- // maxColour = minColour;
- //}
- //vertexColors[v] = minColour;
- }
- }
- colors = vertexColors;
- return maxColour + 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement