/** * Unit tests the {@code Bipartite} data type. * * @param args the command-line arguments */ public static void main(String[] args) { int V1 = Integer.parseInt(args[0]); int V2 = Integer.parseInt(args[1]); int E = Integer.parseInt(args[2]); int F = Integer.parseInt(args[3]); // create random bipartite graph with V1 vertices on left side, // V2 vertices on right side, and E edges; then add F random edges Graph G = GraphGenerator.bipartite(V1, V2, E); for (int i = 0; i < F; i++) { int v = StdRandom.uniform(V1 + V2); int w = StdRandom.uniform(V1 + V2); G.addEdge(v, w); } StdOut.println(G); Bipartite b = new Bipartite(G); if (b.isBipartite()) { StdOut.println("Graph is bipartite"); for (int v = 0; v < G.V(); v++) { StdOut.println(v + ": " + b.color(v)); } } else { StdOut.print("Graph has an odd-length cycle: "); for (int x : b.oddCycle()) { StdOut.print(x + " "); } StdOut.println(); } }
/** * Unit tests the {@code HopcroftKarp} data type. * Takes three command-line arguments {@code V1}, {@code V2}, and {@code E}; * creates a random bipartite graph with {@code V1} + {@code V2} vertices * and {@code E} edges; computes a maximum matching and minimum vertex cover; * and prints the results. * * @param args the command-line arguments */ public static void main(String[] args) { int V1 = Integer.parseInt(args[0]); int V2 = Integer.parseInt(args[1]); int E = Integer.parseInt(args[2]); Graph G = GraphGenerator.bipartite(V1, V2, E); if (G.V() < 1000) StdOut.println(G); BipartiteMatching matching = new BipartiteMatching(G); // print maximum matching StdOut.printf("Number of edges in max matching = %d\n", matching.size()); StdOut.printf("Number of vertices in min vertex cover = %d\n", matching.size()); StdOut.printf("Graph has a perfect matching = %b\n", matching.isPerfect()); StdOut.println(); if (G.V() >= 1000) return; StdOut.print("Max matching: "); for (int v = 0; v < G.V(); v++) { int w = matching.mate(v); if (matching.isMatched(v) && v < w) // print each edge only once StdOut.print(v + "-" + w + " "); } StdOut.println(); // print minimum vertex cover StdOut.print("Min vertex cover: "); for (int v = 0; v < G.V(); v++) if (matching.inMinVertexCover(v)) StdOut.print(v + " "); StdOut.println(); }