string Solve(int n, int k, int[][] a) { bool[,] can = new bool[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) { continue; } bool good = true; for (int t = 0; t < k; t++) { if (a[i][t] >= a[j][t]) { good = false; break; } } can[i, j] = good; } } int res = n - Bipartite.FindMatch(can); return(res.ToString()); }
public void GetBipartiteMatrixTest() { // Act var result = Bipartite.GetBipartiteMatrix(u, v); // Assert Assert.IsNotNull(result); Assert.AreEqual(2, result.Length); Assert.AreEqual(0, result[0]); Assert.AreEqual(1, result[1]); }
public void Run() { // create random bipartite graph with V vertices and E edges; then add F random edges const int vv = 20; const int e = 30; const int f = 5; var graph = new Graph(vv); var vertices = new int[vv]; for (var i = 0; i < vv; i++) { vertices[i] = i; } StdRandom.Shuffle(vertices); for (var i = 0; i < e; i++) { var v = StdRandom.Uniform(vv / 2); var w = StdRandom.Uniform(vv / 2); graph.AddEdge(vertices[v], vertices[vv / 2 + w]); } // add F extra edges for (var i = 0; i < f; i++) { var v = StdRandom.Uniform(vv); var w = StdRandom.Uniform(vv); graph.AddEdge(v, w); } Console.WriteLine(graph); var b = new Bipartite(graph); if (b.IsBipartite()) { Console.WriteLine("Graph is bipartite"); for (var v = 0; v < graph.V; v++) { Console.WriteLine(v + ": " + b.Color(v)); } } else { Console.Write("Graph has an odd-length cycle: "); foreach (int x in b.OddCycle()) { Console.Write(x + " "); } Console.WriteLine(); } Console.ReadLine(); }
public void CreateBipartiteMatrixTest() { // Act var result = Bipartite.CreateBipartiteMatrix(u, v); // Assert Assert.IsNotNull(result); Assert.AreEqual(4, result.Length); Assert.AreEqual(0, result[0, 0]); Assert.AreEqual(0, result[1, 0]); Assert.AreEqual(0, result[0, 1]); Assert.AreEqual(0, result[1, 1]); }
/**//**/ public static void main(string[] strarr) { int num = Integer.parseInt(strarr[0]); int num2 = Integer.parseInt(strarr[1]); int num3 = Integer.parseInt(strarr[2]); Graph graph = new Graph(num); int[] array = new int[num]; for (int i = 0; i < num; i++) { array[i] = i; } StdRandom.shuffle(array); for (int i = 0; i < num2; i++) { int j = StdRandom.uniform(num / 2); int num4 = StdRandom.uniform(num / 2); graph.addEdge(array[j], array[num / 2 + num4]); } for (int i = 0; i < num3; i++) { int j = ByteCodeHelper.d2i(java.lang.Math.random() * (double)num); int num4 = ByteCodeHelper.d2i(java.lang.Math.random() * (double)num); graph.addEdge(j, num4); } StdOut.println(graph); Bipartite bipartite = new Bipartite(graph); if (bipartite.isBipartite()) { StdOut.println("Graph is bipartite"); for (int j = 0; j < graph.V(); j++) { StdOut.println(new StringBuilder().append(j).append(": ").append(bipartite.color(j)).toString()); } } else { StdOut.print("Graph has an odd-length cycle: "); Iterator iterator = bipartite.oddCycle().iterator(); while (iterator.hasNext()) { int num4 = ((Integer)iterator.next()).intValue(); StdOut.print(new StringBuilder().append(num4).append(" ").toString()); } StdOut.println(); } }
public void testBipartity() { Graph g = g1(); BipartiteInfo b = Bipartite.CheckBipartity(g); Assert.AreEqual(b.isBipartite, false); Graph g2 = new Graph(); for (int i = 0; i < 8; i++) { g2.AddVertex(); } g2.AddEdge(1, 2); g2.AddEdge(1, 4); g2.AddEdge(2, 3); g2.AddEdge(4, 3); g2.AddEdge(0, 2); g2.AddEdge(4, 6); g2.AddEdge(3, 5); g2.AddEdge(6, 5); g2.AddEdge(3, 7); g2.AddEdge(6, 7); BipartiteInfo b2 = Bipartite.CheckBipartity(g2); Assert.AreEqual(true, b2.isBipartite); List <int> redNodes = new List <int>() { 0, 1, 3, 6 }; List <int> blueNodes = new List <int>() { 2, 4, 5, 7 }; Assert.AreEqual(b2.redPart, redNodes); Assert.AreEqual(b2.bluePart, blueNodes); }