public bool Traverse()
        {
            if (adjacencyList == null)
            {
                throw new ArgumentException("Missing initialization arguments");
            }

            markedVertices = new bool[adjacencyList.Count + 1];
            List<int> adjacentWithRootColor = new List<int>();
            StoredColors storedColors = new StoredColors();
            int head = 1;
            traversalQueue.Enqueue(head);
            markedVertices[head] = true;

            short headColor = verticesColoring[head];
            adjacentWithRootColor.Add(head);

            while (traversalQueue.Any())
            {
                int node = traversalQueue.Dequeue();
                Console.WriteLine(node);

                foreach (int tail in adjacencyList[node])
                {
                    if (markedVertices[tail])
                        continue;

                    // if tail has diff color add it to counter
                    if (verticesColoring[tail] != headColor)
                    {
                        storedColors.Add(verticesColoring[tail]);
                        markedVertices[tail] = true; // it's probably redundant, but in time pressure conditions I'd like to have it there. TODO check
                        continue;
                    }

                    // if tail has same color, then store it for recoloring and continue traversal
                    adjacentWithRootColor.Add(tail);
                    markedVertices[tail] = true;
                    traversalQueue.Enqueue(tail);
                }
            }

            // short winnerColor = storedColors.First(x => x.Value == storedColors.Values.Max()).Key;

            var winnerColor = storedColors.FirstOrDefault(x => x.Value == storedColors.Values.Max());
            if (winnerColor.Equals(default(KeyValuePair<short, int>)))
            {
                return true;
            }

            Console.WriteLine("And the winner color is: {0}", winnerColor.Key);

            // recolor vertices
            foreach (int vertice in adjacentWithRootColor)
            {
                verticesColoring[vertice] = winnerColor.Key;
            }

            return false;
        }
        public void Should_Be_Able_To_Store_Value()
        {
            short expected = 1;

            StoredColors colors = new StoredColors();
            colors.Add(100);

            Assert.AreEqual(expected, colors.Count);
        }
        public void Should_Be_Able_To_Store_Different_Values()
        {
            short expectedSize = 4;
            short expectedCount = 4;

            StoredColors colors = new StoredColors();
            colors.Add(100);
            colors.Add(200);
            colors.Add(300);
            colors.Add(400);

            Assert.AreEqual(expectedSize, colors.Count);
        }
        public void Should_Be_Able_To_Store_Same_Value()
        {
            short expectedSize = 1;
            short expectedCount = 4;

            StoredColors colors = new StoredColors();
            colors.Add(100);
            colors.Add(100);
            colors.Add(100);
            colors.Add(100);

            Assert.AreEqual(expectedSize, colors.Count);
            Assert.AreEqual(expectedCount, colors[100]);
        }
Пример #5
0
 public static void SetPreviousAndPopColors(ushort times = 1)
 {
     while (times-- > 0)
     {
         if (StoredColors.Count > 0)
         {
             var(fore, back)         = StoredColors.Pop();
             Console.ForegroundColor = fore;
             Console.BackgroundColor = back;
         }
         else
         {
             Console.ForegroundColor = PreForeground;
             Console.BackgroundColor = PreBackground;
             break;
         }
     }
 }
 public void Should_Not_Be_Able_To_Use_Add()
 {
     StoredColors colors = new StoredColors();
     colors.Add(100, 100);
 }
Пример #7
0
 public static void SetAndPushColors(ConsoleColor foreground, ConsoleColor background)
 {
     StoredColors.Push(Tuple.Create(Console.ForegroundColor, Console.BackgroundColor));
     Console.ForegroundColor = foreground;
     Console.BackgroundColor = background;
 }