Removed Boshart's Reference Queue implementation for an ArrayList. Also fixed a few bugs with the interface and diameter methods.

master
mercury 2003-12-09 04:10:16 +00:00
parent c3e90001e0
commit 94bf0bc87e
2 changed files with 54 additions and 30 deletions

View File

@ -65,6 +65,18 @@ public class Graph {
return ( size == 0 ); 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. * Convienence method for determining the number of vertecies in the graph.
* @return int of the size of the graph.pat * @return int of the size of the graph.pat
@ -333,18 +345,17 @@ public class Graph {
public ArrayList bft ( Comparable searchKey ) { public ArrayList bft ( Comparable searchKey ) {
GraphNode temp; GraphNode temp;
ArrayList searchList = new ArrayList(); ArrayList searchList = new ArrayList();
QueueReferenceBased bfsQueue = new QueueReferenceBased(); ArrayList bfsQueue = new ArrayList ( size );
bfsQueue.enqueue ( vertexList.get ( findIndex ( searchKey ) ) ); bfsQueue.add ( bfsQueue.size(), vertexList.get ( findIndex ( searchKey ) ) );
getVertex ( searchKey ).setMarked ( true ); getVertex ( searchKey ).setMarked ( true );
searchList.add ( getVertex ( searchKey ) ); searchList.add ( getVertex ( searchKey ) );
while ( !bfsQueue.isEmpty() ) { while ( !bfsQueue.isEmpty() ) {
temp = (GraphNode)bfsQueue.dequeue(); temp = (GraphNode)bfsQueue.remove ( 0 );
for ( int g = 0; g < size; g++ ) { for ( int g = 0; g < size; g++ ) {
if ( adjacent[findIndex ( temp.getKey() )][g] != Double.POSITIVE_INFINITY && !getVertex ( g ).isMarked() ) { if ( adjacent[findIndex ( temp.getKey() )][g] != Double.POSITIVE_INFINITY && !getVertex ( g ).isMarked() ) {
((GraphNode)vertexList.get ( g )).setMarked ( true ); ((GraphNode)vertexList.get ( g )).setMarked ( true );
bfsQueue.enqueue ( vertexList.get ( g ) ); bfsQueue.add ( bfsQueue.size(), vertexList.get ( g ) );
searchList.add ( getVertex ( g ) ); searchList.add ( getVertex ( g ) );
} }
} }
@ -358,22 +369,27 @@ public class Graph {
* @return int representing the diameter of the graph. * @return int representing the diameter of the graph.
*/ */
public double diameter() { 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]; double mins[] = new double[size];
// find all shortest paths and determine the maximum of each
for ( int x = 0; x < size; x++ ) { for ( int x = 0; x < size; x++ ) {
mins[x] = 0; mins[x] = 0.0;
ArrayList t = bft ( getVertex ( x ).getKey() ); Comparable key = getVertex ( x ).getKey();
if ( t.size() != size ) return Double.POSITIVE_INFINITY; for ( int y = 0; y < size; y++ ) {
for ( int y = 1; y < t.size(); y++ ) { if ( x == y ) continue;
mins[x] += getWeight ( ((GraphNode) t.get ( y - 1 )).getKey(), ((GraphNode) t.get ( y )).getKey() ); ArrayList path = shortestPath ( key, getVertex ( y ).getKey() );
if ( path.size() > mins[x] ) mins[x] = path.size();
} }
} }
// find the minimum of the paths
double minsize = mins[0]; double max = mins[0];
for ( int x = 0; x < size; x++ ) { for ( int x = 0; x < size; x++ ) {
if ( minsize > mins[x] ) if ( mins[x] > max ) max = mins[x];
minsize = mins[x];
} }
return minsize; // return num edges between them, not number of nodes
return max - 1;
} }
/** /**
@ -395,7 +411,7 @@ public class Graph {
} }
else { else {
ArrayList q = new ArrayList ( x ); ArrayList q = new ArrayList ( x );
for ( int y = 0; y < x; y++ ) for ( int y = 0; y <= x; y++ )
q.add ( p.get ( x ) ); q.add ( p.get ( x ) );
p = q; p = q;
} }
@ -473,6 +489,7 @@ public class Graph {
smallest = j; smallest = j;
smallestweight = weight[j]; smallestweight = weight[j];
} }
//if ( smallest == -1 ) continue;
/* mark smallest vertex */ /* mark smallest vertex */
GraphNode smallnode = (GraphNode)vertexList.get ( smallest ); GraphNode smallnode = (GraphNode)vertexList.get ( smallest );
smallnode.setMarked ( true ); smallnode.setMarked ( true );
@ -502,6 +519,9 @@ public class Graph {
/* gotta add the first one */ /* gotta add the first one */
result.add ( getVertex ( firstkey ) ); result.add ( getVertex ( firstkey ) );
/* unmark everything */
clearMarks();
/* reverse the arraylist */ /* reverse the arraylist */
path.clear(); path.clear();
for ( int i = result.size() - 1; i >= 0; i-- ) for ( int i = result.size() - 1; i >= 0; i-- )

View File

@ -52,7 +52,9 @@ public class GraphDriver {
//get all the actors in a usable form //get all the actors in a usable form
line = read.readLine(); 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 ); actors.add ( line );
line = read.readLine(); line = read.readLine();
} }
@ -113,9 +115,8 @@ public class GraphDriver {
command = in.readLine().trim(); command = in.readLine().trim();
} }
catch ( IOException exception ) { catch ( IOException exception ) {
System.out.println ( "bailing from:" ); System.out.println ( "bailing from command:" );
exception.printStackTrace(); exception.printStackTrace();
break;
} }
if ( command.equals( "path" ) ) { if ( command.equals( "path" ) ) {
System.out.print ( "actors (one,two)> " ); System.out.print ( "actors (one,two)> " );
@ -124,15 +125,16 @@ public class GraphDriver {
actors = (String [])in.readLine().trim().split ( "," ); actors = (String [])in.readLine().trim().split ( "," );
} }
catch ( IOException exception ) { catch ( IOException exception ) {
System.out.println ( "bailing from:" ); System.out.println ( "bailing from path:" );
exception.printStackTrace(); exception.printStackTrace();
break;
} }
if ( actors.length != 2 ) { if ( actors.length != 2 ) {
System.out.println ( "Enter two actors only!" ); System.out.println ( "Enter two actors only!" );
} }
ArrayList path = mygraph.shortestPath ( actors[0].trim(), actors[1].trim() ); else {
printPath ( path ); ArrayList path = mygraph.shortestPath ( actors[0].trim(), actors[1].trim() );
printPath ( path );
}
} }
else if ( command.equals( "bfs" ) ) { else if ( command.equals( "bfs" ) ) {
System.out.print ( "actors (one,two)> " ); System.out.print ( "actors (one,two)> " );
@ -141,16 +143,16 @@ public class GraphDriver {
actors = (String [])in.readLine().trim().split ( "," ); actors = (String [])in.readLine().trim().split ( "," );
} }
catch ( IOException exception ) { catch ( IOException exception ) {
System.out.println ( "bailing from:" ); System.out.println ( "bailing from bfs:" );
exception.printStackTrace(); exception.printStackTrace();
break;
} }
if ( actors.length != 2 ) { if ( actors.length != 2 ) {
System.out.println ( "Enter two actors only!" ); System.out.println ( "Enter two actors only!" );
} }
//put in real bfs here for two states else {
ArrayList path = mygraph.bfs ( actors[0].trim(), actors[1].trim() ); ArrayList path = mygraph.bfs ( actors[0].trim(), actors[1].trim() );
printPath ( path ); printPath ( path );
}
} }
else if ( command.equals ( "add" ) ) { else if ( command.equals ( "add" ) ) {
System.out.print ( "read from file> " ); System.out.print ( "read from file> " );
@ -159,9 +161,8 @@ public class GraphDriver {
filename = (String [])in.readLine().trim().split ( " " ); filename = (String [])in.readLine().trim().split ( " " );
} }
catch ( IOException exception ) { catch ( IOException exception ) {
System.out.println ( "bailing from:" ); System.out.println ( "bailing from add:" );
exception.printStackTrace(); exception.printStackTrace();
break;
} }
for ( int q = 0; q < filename.length; q++ ) { for ( int q = 0; q < filename.length; q++ ) {
readFile ( filename[q], q, filename.length ); readFile ( filename[q], q, filename.length );
@ -174,6 +175,9 @@ public class GraphDriver {
else if ( command.equals ( "quit" ) ) { else if ( command.equals ( "quit" ) ) {
break; break;
} }
else {
System.out.println ( "Unknown Command: '" + command + "'" );
}
System.out.println(); System.out.println();
} }
} }