/* * By Chuck Connell, December 2007, for COMP 160 Algorithms at Tufts Univ. * Based on the Gale-Shapley algorithm (1962). * */ // package stablemarriage; not needed to run from .class file import java.io.FileReader; import java.io.BufferedReader; import java.io.IOException; public class StableMarriage { // public StableMarriage() { } not needed for one-class program public static void main(String[] args) throws IOException { int InputHeaderLines = 14; // # of comment lines in the input file BufferedReader InputFile = null; String InputPath; // location of input file String InputLine; // one line of input String[] InputValues; // a parsed input line int Pairs; // how many pairs of men-women are there? int [][] ManPrefers; // men's preferences for women as [man #][list of women #s] int [][] WomanPrefers; // women's preferences for men as [woman #][list of men #s] int[] ManIsEngagedTo; // who is each man engaged to? int[] WomanIsEngagedTo; // who is each woman engaged to? boolean EveryoneEngaged; // does everyone have a mate? int ThisMan; // which man is proposing now? int[] ProposalRank; // where is each man in his order of proposals? int ChosenWoman; // what woman is the current man proposing to? int i, j; // general purpose counters boolean SwitchMen; // should a woman switch from her current man to the new proposal? boolean ManFound; // after being proposed to by a man, can the woman find the man in her preference list? int ManLoopCount=0, WomanLoopCount=0; // for time analysis try { // main try block // Get the command line args. if (args.length != 1) { System.out.println("Usage: StableMarriage "); return; } InputPath = new String(args[0]); // Open the input file. InputFile = new BufferedReader( new FileReader( InputPath ) ); // Skip some header lines. for (i=0; i