diff --git a/Graph.java b/Graph.java index be29ca9..d002eab 100644 --- a/Graph.java +++ b/Graph.java @@ -65,6 +65,18 @@ public class Graph { return ( size == 0 ); } + /** + * Determines if this graph is a connected graph or not. + * @return boolean determining if this is connected. + */ + public boolean isConnected() { + for ( int x = 0; x < size; x++ ) { + ArrayList t = bft ( getVertex ( x ).getKey() ); + if ( t.size() != size ) return false; + } + return true; + } + /** * Convienence method for determining the number of vertecies in the graph. * @return int of the size of the graph.pat @@ -333,18 +345,17 @@ public class Graph { public ArrayList bft ( Comparable searchKey ) { GraphNode temp; ArrayList searchList = new ArrayList(); - QueueReferenceBased bfsQueue = new QueueReferenceBased(); - bfsQueue.enqueue ( vertexList.get ( findIndex ( searchKey ) ) ); + ArrayList bfsQueue = new ArrayList ( size ); + bfsQueue.add ( bfsQueue.size(), vertexList.get ( findIndex ( searchKey ) ) ); getVertex ( searchKey ).setMarked ( true ); searchList.add ( getVertex ( searchKey ) ); while ( !bfsQueue.isEmpty() ) { - temp = (GraphNode)bfsQueue.dequeue(); - + temp = (GraphNode)bfsQueue.remove ( 0 ); for ( int g = 0; g < size; g++ ) { if ( adjacent[findIndex ( temp.getKey() )][g] != Double.POSITIVE_INFINITY && !getVertex ( g ).isMarked() ) { ((GraphNode)vertexList.get ( g )).setMarked ( true ); - bfsQueue.enqueue ( vertexList.get ( g ) ); + bfsQueue.add ( bfsQueue.size(), vertexList.get ( g ) ); searchList.add ( getVertex ( g ) ); } } @@ -358,22 +369,27 @@ public class Graph { * @return int representing the diameter of the graph. */ public double diameter() { + // early check... can make it take a really long time + if ( !isConnected() ) return Double.POSITIVE_INFINITY; + System.out.println ( "Graph is connected." ); double mins[] = new double[size]; + // find all shortest paths and determine the maximum of each for ( int x = 0; x < size; x++ ) { - mins[x] = 0; - ArrayList t = bft ( getVertex ( x ).getKey() ); - if ( t.size() != size ) return Double.POSITIVE_INFINITY; - for ( int y = 1; y < t.size(); y++ ) { - mins[x] += getWeight ( ((GraphNode) t.get ( y - 1 )).getKey(), ((GraphNode) t.get ( y )).getKey() ); + mins[x] = 0.0; + Comparable key = getVertex ( x ).getKey(); + for ( int y = 0; y < size; y++ ) { + if ( x == y ) continue; + ArrayList path = shortestPath ( key, getVertex ( y ).getKey() ); + if ( path.size() > mins[x] ) mins[x] = path.size(); } } - - double minsize = mins[0]; + // find the minimum of the paths + double max = mins[0]; for ( int x = 0; x < size; x++ ) { - if ( minsize > mins[x] ) - minsize = mins[x]; + if ( mins[x] > max ) max = mins[x]; } - return minsize; + // return num edges between them, not number of nodes + return max - 1; } /** @@ -395,7 +411,7 @@ public class Graph { } else { ArrayList q = new ArrayList ( x ); - for ( int y = 0; y < x; y++ ) + for ( int y = 0; y <= x; y++ ) q.add ( p.get ( x ) ); p = q; } @@ -473,6 +489,7 @@ public class Graph { smallest = j; smallestweight = weight[j]; } + //if ( smallest == -1 ) continue; /* mark smallest vertex */ GraphNode smallnode = (GraphNode)vertexList.get ( smallest ); smallnode.setMarked ( true ); @@ -502,6 +519,9 @@ public class Graph { /* gotta add the first one */ result.add ( getVertex ( firstkey ) ); + /* unmark everything */ + clearMarks(); + /* reverse the arraylist */ path.clear(); for ( int i = result.size() - 1; i >= 0; i-- ) diff --git a/GraphDriver.java b/GraphDriver.java index 2fdc2ba..a840603 100644 --- a/GraphDriver.java +++ b/GraphDriver.java @@ -52,7 +52,9 @@ public class GraphDriver { //get all the actors in a usable form line = read.readLine(); - while ( !line.equals ( "" ) ) { + // NEW + // added check for read.ready() : no longer must have two lines ending input file + while ( read.ready() && !line.equals ( "" ) ) { actors.add ( line ); line = read.readLine(); } @@ -113,9 +115,8 @@ public class GraphDriver { command = in.readLine().trim(); } catch ( IOException exception ) { - System.out.println ( "bailing from:" ); + System.out.println ( "bailing from command:" ); exception.printStackTrace(); - break; } if ( command.equals( "path" ) ) { System.out.print ( "actors (one,two)> " ); @@ -124,15 +125,16 @@ public class GraphDriver { actors = (String [])in.readLine().trim().split ( "," ); } catch ( IOException exception ) { - System.out.println ( "bailing from:" ); + System.out.println ( "bailing from path:" ); exception.printStackTrace(); - break; } if ( actors.length != 2 ) { System.out.println ( "Enter two actors only!" ); } - ArrayList path = mygraph.shortestPath ( actors[0].trim(), actors[1].trim() ); - printPath ( path ); + else { + ArrayList path = mygraph.shortestPath ( actors[0].trim(), actors[1].trim() ); + printPath ( path ); + } } else if ( command.equals( "bfs" ) ) { System.out.print ( "actors (one,two)> " ); @@ -141,16 +143,16 @@ public class GraphDriver { actors = (String [])in.readLine().trim().split ( "," ); } catch ( IOException exception ) { - System.out.println ( "bailing from:" ); + System.out.println ( "bailing from bfs:" ); exception.printStackTrace(); - break; } if ( actors.length != 2 ) { System.out.println ( "Enter two actors only!" ); } - //put in real bfs here for two states - ArrayList path = mygraph.bfs ( actors[0].trim(), actors[1].trim() ); - printPath ( path ); + else { + ArrayList path = mygraph.bfs ( actors[0].trim(), actors[1].trim() ); + printPath ( path ); + } } else if ( command.equals ( "add" ) ) { System.out.print ( "read from file> " ); @@ -159,9 +161,8 @@ public class GraphDriver { filename = (String [])in.readLine().trim().split ( " " ); } catch ( IOException exception ) { - System.out.println ( "bailing from:" ); + System.out.println ( "bailing from add:" ); exception.printStackTrace(); - break; } for ( int q = 0; q < filename.length; q++ ) { readFile ( filename[q], q, filename.length ); @@ -174,6 +175,9 @@ public class GraphDriver { else if ( command.equals ( "quit" ) ) { break; } + else { + System.out.println ( "Unknown Command: '" + command + "'" ); + } System.out.println(); } }