/// Reads from file, given in constructor and returns ReadResults. public ReadResults Read() { using (var file = new System.IO.StreamReader(this.filename)) { var line = file.ReadLine().Split(' '); ulong vertexNumber = Convert.ToUInt64(line[0]); ulong edgeNumber = Convert.ToUInt64(line[1]); ulong robotsNumber = Convert.ToUInt64(line[2]); Graph graph = new Graph(vertexNumber); bool[] isRobotThere = new bool[vertexNumber]; for (ulong i = 0; i < edgeNumber; ++i) { line = file.ReadLine().Split(' '); if (line.Length != 2) { throw new WrongArgument("Cannot add edge, wrogh args number."); } ulong vertex1 = Convert.ToUInt64(line[0]); ulong vertex2 = Convert.ToUInt64(line[1]); if (vertex1 == vertex2) { throw new WrongArgument("Cannot add loop."); } vertex1--; vertex2--; graph.AddEdge(vertex1, vertex2); } if (robotsNumber != 0) { line = file.ReadLine().Split(' '); if (Convert.ToUInt64(line.Length) != robotsNumber) // Yes, Length is for some reason int. And it cannot be compared to ulong. // God knows why it is not uint. { throw new WrongArgument("Wrong robots number given."); } for (ulong i = 0; i < robotsNumber; ++i) { if (Convert.ToUInt64(line[i]) > vertexNumber || Convert.ToInt64(line[i]) < 1) { throw new WrongArgument("Wrong vertex number. Given value is outside array range."); } if (isRobotThere[Convert.ToUInt64(line[i]) - 1]) { throw new WrongArgument("Cannot add two robots to one vertex."); } isRobotThere[Convert.ToUInt64(line[i]) - 1] = true; } } return new ReadResults(graph, isRobotThere); } }
/// Colors connected graph in two colors. public static Color[] ColorGraph(Graph graph) { Color[] colors = new Color[graph.Vertexes.Length]; Dfs(graph.Vertexes[0], Color.firstColor, colors); return colors; }