public bool IsBadEdge(int i, UndirectedGraph graph) { var color = graph.GetColor(i); if (graph.GetNeighbourColors(i).Contains(color)) { return(true); } return(false); }
public bool ContainsBadEdge(UndirectedGraph graph) { for (int i = 0; i < graph.GetNumberOfVertices(); i++) { var color = graph.GetColor(i); if (graph.GetNeighbourColors(i).Contains(color)) { return(true); } } return(false); }
public void Mutate(UndirectedGraph graph) { var random = new Random(); for (int i = 0; i < graph.GetNumberOfVertices(); i++) { var neightboursColors = graph.GetNeighbourColors(i); var color = graph.GetColor(i); if (neightboursColors.Contains(color)) { var colors = Enum.GetValues(typeof(Color)).Cast <Color>().ToList(); foreach (var neighbourColor in neightboursColors) { colors.Remove((Color)neighbourColor); } colors.OrderBy(x => random.Next(0, colors.Count - 1)); graph.SetColor(i, colors.FirstOrDefault()); } } }
public int Color() { var numberOfVertices = _graph.GetNumberOfVertices(); var usedColors = new List <Color>(); var result = 0; for (int i = 0; i < numberOfVertices; i++) { var usedNeighbourColors = _graph.GetNeighbourColors(i); var firstAvailableColor = GetFirstAvailableColor(usedNeighbourColors); _graph.SetColor(i, firstAvailableColor); if (!usedColors.Contains(firstAvailableColor)) { usedColors.Add(firstAvailableColor); result++; } } return(result); }
public void ColorRandomly(UndirectedGraph graph) { var random = new Random(); for (int i = 0; i < graph.GetNumberOfVertices(); i++) { var neightboursColors = graph.GetNeighbourColors(i); while (true) { var colors = Enum.GetValues(typeof(Color)).Cast <Color>().ToList(); foreach (var neighbourColor in neightboursColors) { colors.Remove((Color)neighbourColor); } if (colors.Count == 0) { throw new Exception("Not enough colors"); } colors.OrderBy(x => random.Next(0, colors.Count - 1)); graph.SetColor(i, colors.FirstOrDefault()); break; } } }