import java.io.*; import rpi.goldsd.container.*; import rpi.goldsd.graph.*; public class Football { public static void main( String[] args ) { if ( args.length != 2 && args.length != 3 ) throw new IllegalArgumentException( "Team abbreviation arguments are missing?" ); boolean useStratifiedApproach = false; int width = 0; if ( args.length == 3 ) { useStratifiedApproach = true; width = Integer.parseInt( args[2] ); } System.out.println( "Constructing the Football graph ..." ); Graph G = new Graph( "Football Graph" ); int numTeams = 120; try { // Open the games.dat file and prepare for reading ... FileInputStream istream = new FileInputStream( "games.dat" ); BufferedReader in = new BufferedReader( new InputStreamReader( istream ) ); int teamCounter = numTeams; while ( teamCounter > 0 && in.ready() ) { // Parse the football team information and create a vertex ... String s = in.readLine(); if ( s.charAt( 0 ) != '*' ) { int i = 0, j, len = s.length(); while ( i < len && s.charAt( i ) != ' ' ) i++; String abbr = new String( s.substring( 0, i++ ) ); j = i; while ( i < len && s.charAt( i ) != '(' ) i++; String name = new String( s.substring( j, i++ ) ); j = i; while ( i < len && s.charAt( i ) != ')' ) i++; String nickname = new String( s.substring( j, i++ ) ); j = i; while ( i < len && s.charAt( i ) != ';' ) i++; String conference = new String( s.substring( j, i ) ); G.add( new Vertex( new FootballTeam( abbr, name, nickname, conference ) ) ); teamCounter--; } } String date = null; // Once all the teams are read in, find the first date ... while ( in.ready() ) { String s = in.readLine(); if ( s.charAt( 0 ) == '>' ) { date = parseGameDate( s ); break; } } // Parse all of the games, adding the edges to the graph. // Note that the date may change at any point in the file ... while ( in.ready() ) { String s = in.readLine(); if ( s.charAt( 0 ) != '*' ) { if ( s.charAt( 0 ) == '>' ) date = parseGameDate( s ); else { int i = 0, j, len = s.length(); while ( i < len && ! Character.isDigit( s.charAt( i ) ) ) i++; String abbr1 = new String( s.substring( 0, i ) ); j = i; while ( i < len && s.charAt( i ) != '@' && s.charAt( i ) != ',' ) i++; int score1 = ( new Integer( s.substring( j, i ) ) ).intValue(); boolean homeTeamExists = ( s.charAt( i++ ) == '@' ); j = i; while ( i < len && ! Character.isDigit( s.charAt( i ) ) ) i++; String abbr2 = new String( s.substring( j, i ) ); int score2 = ( new Integer( s.substring( i, s.length() ) ) ).intValue(); FootballTeam dummy = new FootballTeam(); dummy.abbr = new Str( abbr1 ); Vertex V1 = G.mapToVertex( dummy ); dummy.abbr = new Str( abbr2 ); Vertex V2 = G.mapToVertex( dummy ); if ( V1 != null && V2 != null ) { G.add( new DirectedEdge( V1, V2, new Game( date, score1, score2, homeTeamExists ? V2 : null ), score1 - score2 ) ); G.add( new DirectedEdge( V2, V1, new Game( date, score2, score1, homeTeamExists ? V2 : null ), score2 - score1 ) ); } } } } in.close(); } catch( IOException e ) { System.err.println( e ); System.exit(0); } G.printSummary( false ); FootballTeam dummy = new FootballTeam(); dummy.abbr = new Str( args[0] ); Vertex V1 = G.mapToVertex( dummy ); dummy.abbr = new Str( args[1] ); Vertex V2 = G.mapToVertex( dummy ); if ( V1 == null ) throw new IllegalArgumentException( args[0] + " does not exist." ); if ( V2 == null ) throw new IllegalArgumentException( args[1] + " does not exist." ); System.out.println(); System.out.print( "Finding \"longest\" path using " ); Path P = null; if ( useStratifiedApproach ) { System.out.println( "the stratified greedy approach (width " + width + ")." ); P = Algorithms.findLongestPath2( V1, V2, width ); } else { System.out.println( "the simple greedy algorithm." ); P = Algorithms.findLongestPath( V1, V2 ); } verboseOutput( P ); // System.out.println( "Sending Football Graph to Graph Draw ..." ); // Algorithms.toGraphDraw( G, "football.html" ); } private static final String parseGameDate( String s ) { String date; switch( s.charAt( 1 ) ) { case 'A': date = "Aug"; break; case 'S': date = "Sep"; break; case 'O': date = "Oct"; break; case 'N': date = "Nov"; break; case 'D': date = "Dec"; break; case 'J': date = "Jan"; break; default: date = "???"; break; } if ( s.length() == 3 ) date += " 0" + s.substring( 2, 3 ); else if ( s.length() == 4 ) date += " " + s.substring( 2, 4 ); else date += " ??"; return date; } private static final void verboseOutput( Path P ) { System.out.println(); int totalWeight = 0; java.util.Enumeration e = P.edges(); while ( e.hasMoreElements() ) { DirectedEdge E = (DirectedEdge)e.nextElement(); Game G = (Game)E.data(); System.out.print( " " + G.date + ": " ); System.out.print( E.tail() + " " + G.startVertexScore + ", " ); System.out.print( E.head() + " " + G.endVertexScore + " (" ); totalWeight += G.startVertexScore - G.endVertexScore; if ( totalWeight > 0 ) System.out.print( "+" ); System.out.println( totalWeight + ")" ); } System.out.println(); System.out.print( "Path contains " + P.length() + " edges and has weight " ); System.out.println( P.weight() + "." ); } } class FootballTeam implements Hashable { public Str abbr; public Str name; public Str nickname; public Str conference; public FootballTeam() { } public FootballTeam( String abbr, String name, String nickname, String conference ) { this.abbr = new Str( abbr ); this.name = new Str( name ); this.nickname = new Str( nickname ); this.conference = new Str( conference ); } public int hash() { return abbr.hash(); } public int hash( int tableSize ) { return abbr.hash( tableSize ); } public boolean isEqualTo( Comparable C ) { return abbr.isEqualTo( ((FootballTeam)C).abbr ); } public boolean isLessThan( Comparable C ) { return abbr.isLessThan( ((FootballTeam)C).abbr ); } public String toString() { return name + " " + nickname; } } class Game implements Hashable { private static Sequence S = new IntSequence(); private static java.util.Enumeration e = S.sequence(); public Int key; public Str date; public int startVertexScore; public int endVertexScore; public Vertex hometeam; public Game() { } public Game( String date, int startVertexScore, int endVertexScore, Vertex hometeam ) { this.key = (Int)e.nextElement(); this.date = new Str( date ); this.startVertexScore = startVertexScore; this.endVertexScore = endVertexScore; this.hometeam = hometeam; } public int hash() { return key.hash(); } public int hash( int tableSize ) { return key.hash( tableSize ); } public boolean isEqualTo( Comparable C ) { return key.isEqualTo( ((Game)C).key ); } public boolean isLessThan( Comparable C ) { return key.isLessThan( ((Game)C).key ); } public String toString() { return "<" + key + ">"; } }