private static int IsGraphBipartiteness(GraphArrayOfList graph) { color = new int[graph.Vertexes.Length]; for (int i = 0; i < graph.Vertexes.Length; i++) { if (color[i] != 0) { continue; } color[i] = 1; if (!DFS(graph, i)) { return -1; } } return 1; }
private static bool DFS(GraphArrayOfList graph, int v) { foreach(int n in graph.Vertexes[v]) { if (color[n] == 0) { color[n] = 3 - color[v]; if (!DFS(graph, n)) { return false; } } else { if (color[n] + color[v] != 3) { return false; } } } return true; }
public GraphsArrayOfList(string fileName) { string filePath = Utils.GetFullFilePath(fileName); try { using (StreamReader reader = new StreamReader(filePath)) { string line; int iLine = 0; int iGraph = -1; int iEdge = 0; bool curLineIsGraphArgs = false; while ((line = reader.ReadLine()) != null) { switch (iLine) { case 0: _count = int.Parse(line); _GraphsArrayOfList = new GraphArrayOfList[_count]; break; default: if (line == "") { iGraph++; curLineIsGraphArgs = true; break; } if (curLineIsGraphArgs) { curLineIsGraphArgs = false; string[] args = line.Split(' '); int countVerticles = int.Parse(args[0]); int countEdges = int.Parse(args[1]); _GraphsArrayOfList[iGraph] = new GraphArrayOfList(); _GraphsArrayOfList[iGraph].Vertexes = new List<int>[countVerticles]; for(int i = 0; i < _GraphsArrayOfList[iGraph].Vertexes.Length; i++) { _GraphsArrayOfList[iGraph].Vertexes[i] = new List<int>(); } iEdge = 0; } else { string[] edge = line.Split(' '); int v0 = int.Parse(edge[0]); int v1 = int.Parse(edge[1]); _GraphsArrayOfList[iGraph].Vertexes[v0 - 1].Add(v1 - 1); _GraphsArrayOfList[iGraph].Vertexes[v1 - 1].Add(v0 - 1); iEdge++; } break; } iLine++; } } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } }