Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. public static int VertexColoring(this Graph graph, out int[] colors)
  2.         {
  3.             if (graph.Directed)
  4.                 throw new ArgumentException();
  5.  
  6.             if (0 == graph.VerticesCount)
  7.             {
  8.                 colors = new int[0];
  9.                 return 0;
  10.             }
  11.  
  12.             int maxColour = 0;
  13.             int[] vertexColors = new int[graph.VerticesCount];
  14.  
  15.             for (int i = 0; i < vertexColors.Length; i++)
  16.             {
  17.                 vertexColors[i] = -1;   //vertex not colored yet
  18.             }
  19.  
  20.             int[] usedColors = new int[graph.VerticesCount];
  21.             int color = 0;
  22.  
  23.             for (int v = 0; v < graph.VerticesCount; v++)
  24.             {
  25.                 Array.Clear(usedColors, 0, usedColors.Length);
  26.  
  27.                 color = 0;
  28.                 foreach (var e in graph.OutEdges(v))
  29.                 {
  30.                     if (-1 != vertexColors[e.To])
  31.                     {
  32.                         color = vertexColors[e.To];
  33.                         usedColors[color] = 1;     //colour used
  34.                     }
  35.                 }
  36.  
  37.                 for (int i = 0; i < usedColors.Length; i++)
  38.                 {
  39.                     if (0 == usedColors[i])
  40.                     {
  41.                         vertexColors[v] = i;
  42.  
  43.                         if (i > maxColour)  //for count of all colours
  44.                         {
  45.                             maxColour = i;
  46.                         }
  47.  
  48.                         break;
  49.                     }
  50.  
  51.                     //int minColour = 0; bool foundMinColour = false;
  52.  
  53.                     //while (!foundMinColour)
  54.                     //{
  55.                     //    foundMinColour = true;
  56.                     //    foreach (var e in graph.OutEdges(v))
  57.                     //    {
  58.                     //        if (vertexColors[e.To] == minColour)
  59.                     //        {
  60.                     //            minColour++;
  61.                     //            foundMinColour = false;
  62.                     //            break;
  63.                     //        }
  64.                     //    }
  65.                     //}
  66.  
  67.                     //if (minColour > maxColour)
  68.                     //{
  69.                     //    maxColour = minColour;
  70.                     //}
  71.  
  72.                     //vertexColors[v] = minColour;
  73.                 }
  74.             }
  75.  
  76.             colors = vertexColors;
  77.             return maxColour + 1;
  78.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement