import java.util.Enumeration; /** The main program. This program will find the number of nodes in the maximum pairring in a graph. The graph is loaded from the file specified on the command-line. */ public class MaximumMatching { private static String outputFilename = null; /** Starts the program. The program takes the following arguments: <pre> Usage: java MaximumMatching <DATA> [OPTIONS] A graph is read from the file DATA and the maximum matching is reported along with the nodes in the matching. The program has the following optional arguments: -l, --latex GRAPH Save the graph to the file graph-GRAPH-begin.tex as an Xy-pic matrix. This is done just after it is constructed. The graph is also saved to graph-GRAPH-end.tex when the maximum matching has been found. -h, --help Prints this message and exit with exitcode 0. </pre> @param args the command-line arguments. */ public static void main(String[] args) { try { BipartiteGraph G = new BipartiteGraph(); /* We check for arguments: */ for (int i = 0; i < args.length; i++) { if (args[i].equals("-l") || args[i].equals("--latex")) { /* LaTeX output. */ i++; outputFilename = args[i]; } else if (args[i].equals("-h") || args[i].equals("--help")) { /* Help message. */ usage(); System.exit(0); } else { /* Everything else should be a filename. */ G.loadDataFromFile(args[i]); } } /* We will now calculate the maximum matching in G: */ System.out.println(calculateMaximumMatching(G)); } catch (ArrayIndexOutOfBoundsException e) { usage(); System.exit(1); } } /** Prints a short usage message. */ public static void usage() { /* Spaces needed for alignment of the option: */ String s = " "; System.out.println("Usage: java MaximumMatching " + "<DATA> [OPTIONS]\n"); System.out.println("A graph is read from the file DATA and the " + "maximum matching is\nreported along with the " + "nodes in the matching. The program has\n" + "the following optional arguments:"); System.out.println( "\n-l, --latex GRAPH " + "Save the graph to the file graph-GRAPH-begin.tex\n" + s + "as an Xy-pic matrix. This is done just after\n" + s + "it is constructed. The graph is also saved to\n" + s + "graph-GRAPH-end.tex when the maximum\n" + s + "matching has been found." ); System.out.println("\n-h, --help " + "Prints this message and exit with exitcode 0."); } /** Calculates the maximum matching. @param G the graph that should be examined. @return a message that tells the user the number of nodes in the maximum matching along with the nodes themselves. */ public static String calculateMaximumMatching(BipartiteGraph G) { int i = 0; if (outputFilename != null) G.toLaTeX("graph-" + outputFilename + "-begin.tex"); Node n = G.runTransitionSystem(); while (n != null) { G.turnEdges(n); G.removeColors(); n = G.runTransitionSystem(); i++; } if (outputFilename != null) G.toLaTeX("graph-" + outputFilename + "-end.tex"); return "The maximum matching consists of " + i + " nodes." + "The edges are:\n" + G.getMatches(); } }