public int CountConflictsAndUpdateCandidates(Dictionary <int, Color> solution, HashSet <int> candidates = null) { var conflicts = 0; //Since it's undirect graph go just over upper triangle of adjacency matrix for (int i = 0; i < graph.GetNumberOfVertices(); i++) { for (int j = i + 1; j < graph.GetNumberOfVertices(); j++) { if (!IsBadEdge(i, j, solution)) { continue; } if (candidates != null) { candidates.Add(i); candidates.Add(j); } conflicts++; } } return(conflicts); }
public UndirectedGraph Crossover(UndirectedGraph parent1, UndirectedGraph parent2) { var random = new Random(); var crosspoint = random.Next(0, parent1.GetNumberOfVertices() - 1); var graph = new UndirectedGraph(parent1.GetNumberOfVertices()); for (int i = 0; i < parent1.GetNumberOfVertices(); i++) { if (i < crosspoint) { graph.SetColor(i, parent1.GetColor(i).Value); } else { graph.SetColor(i, parent2.GetColor(i).Value); } } return(graph); }
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 int Fitness(UndirectedGraph graph) { var usedColors = new List <Color>(); for (int i = 0; i < graph.GetNumberOfVertices(); i++) { if (!usedColors.Contains((Color)graph.GetColor(i))) { usedColors.Add((Color)graph.GetColor(i)); } } return(usedColors.Count); }
public Dictionary <int, Color> GenerateGreedySolutionDict(UndirectedGraph graph) { var greedy = new GraphColoringGreedy(graph); greedy.Color(); var res = new Dictionary <int, Color>(); for (int i = 0; i < graph.GetNumberOfVertices(); i++) { res.Add(i, (Color)graph.GetColor(i)); } return(res); }
public UndirectedGraph GenerateRandomInstance() { var random = new Random(); var graph = new UndirectedGraph(100); var numberOfVertices = graph.GetNumberOfVertices(); var maxEdges = numberOfVertices * (numberOfVertices - 1) / 2; for (int i = 0; i < 5000; i++) { graph.AddEdge(random.Next(1, numberOfVertices), random.Next(1, numberOfVertices)); } return(graph); }
public void SaveToFile(UndirectedGraph graph, string filePath) { if (string.IsNullOrWhiteSpace(filePath)) { filePath = "results.txt"; } using (StreamWriter sw = new StreamWriter(filePath)) { sw.WriteLine(graph.GetNumberOfVertices()); foreach (var item in graph.GetEdgesList()) { sw.WriteLine($"{item.Item1} {item.Item2}"); } } }
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; } } }