-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectedGraph.java
More file actions
1054 lines (835 loc) · 27.5 KB
/
DirectedGraph.java
File metadata and controls
1054 lines (835 loc) · 27.5 KB
1
import java.io.*;import java.util.Random;import java.net.*;public class DirectedGraph { private float DistMatrix[][]; public float linkLength[][]; FloatStack Speed[]; private float avgSpeed, varianceSpeed; private int vertices= -1, edges = 0; //edges is total number of links private int nooflinks[], endnodenumbers[][]; //nooflinks: Number of links connected to the given node private int xcoordinate[], ycoordinate[]; public int speed1, speed2; private int nodeNumbers[]; public URL url; /*//////////////////////////////////////////////////////////////// FORMAT OF INPUT FILE!!!! vertices Node1:nodenumbers, xcoordinate, ycoordinate, nooflinks,endnodenumbers1,endnodenumbers2,... Node2: .... note: vertices: Number of nodes xcoordinate,ycoordinate:coordinates of each node in the landuse coordinates nooflinks: Number of links connected to the given node endnodenumbers: the node number other than the given node of each given link *////////////////////////////////////////////////////////////////// //private float DistanceMat[][]; // distance between nodes /////// Constructor 1 //////// public DirectedGraph() { } ///////// Constructor 2 ///////// public DirectedGraph(URL url, String filename, int speed1, int speed2,Demo demo) throws IOException { this.url =url; this.speed1 = speed1; this.speed2 = speed2; ReadANumber read = new ReadANumber(); int integer = 0; String message = ""; InputStream fin = null; try { fin = new URL(url, filename).openStream(); } catch(FileNotFoundException e) { System.out.println("From DirectedGraph class: Exception Occured!!!!"); return; } //System.out.println("Before Reading from the file"); vertices = read.readint(fin); //System.out.println(vertices); // * NetworkDynamics.writeMe(vertices, fout); // * fout.write(13); //System.out.print((int)'1'+"\t"+(int)'2'+"\t"+(int)'3'+"\t"+(int)'9'+"\t"+(int)'0'+"\t"+(int)'.'+"\t"); //System.out.println("\n///////////////////////Vertices: " + vertices+"///////////////////////");//// Initializing the variables; nooflinks = new int[vertices]; endnodenumbers = new int[vertices][]; DistMatrix = new float[vertices][]; linkLength = new float[vertices][]; xcoordinate = new int[vertices]; ycoordinate = new int[vertices]; nodeNumbers = new int[vertices]; //DistanceMat = new float[vertices][vertices]; Speed = new FloatStack[vertices]; //float dummy; for(int i=0; i<vertices; i++) { if(read.end == -1) break; nodeNumbers[i] = read.readint(fin); //System.out.print(nodeNumbers[i]+" "); if(read.end == -1) break; xcoordinate[i] = read.readint(fin); //System.out.print(xcoordinate[i] +" "); // * NetworkDynamics.writeMe( xcoordinate[i], fout); // * fout.write(32); if(read.end == -1) break; ycoordinate[i] = read.readint(fin); //System.out.print(ycoordinate[i] +" "); // * NetworkDynamics.writeMe( ycoordinate[i], fout); // * fout.write(32); if(read.end == -1) break; nooflinks[i] = read.readint(fin); //System.out.print(nooflinks[i] +" "); // * NetworkDynamics.writeMe( nooflinks[i] , fout); // * fout.write(32); if(read.end == -1) break; DistMatrix[i] = new float[nooflinks[i]]; linkLength[i] = new float[nooflinks[i]]; //DistanceMat[i] = new float[nooflinks[i]]; Speed[i] = new FloatStack(nooflinks[i]); endnodenumbers[i] = new int[nooflinks[i]]; for(int j =0; j<nooflinks[i]; j++) { endnodenumbers[i][j] = read.readint(fin); //System.out.print(endnodenumbers[i][j] +" "); // * NetworkDynamics.writeMe( endnodenumbers[i][j], fout); // * fout.write(32); if(read.end == -1) break; DistMatrix[i][j] =0; linkLength[i][j] = read.readfloat(fin); //System.out.print(linkLength[i][j] +" "); // * NetworkDynamics.writeMe( 4, fout); // * if ( !( j == nooflinks[i] -1) ) // * fout.write(32); //dummy = read.readfloat(fin); if(read.end == -1) break; } // * fout.write(13); } for(int i=0; i<vertices; i++) edges +=nooflinks[i]; //System.out.println("Number of edges: " + edges); //System.out.println(); for(int i=0; i< vertices; i++) for(int j=0; j< nooflinks[i]; j++) endnodenumbers[i][j] = returnPosition( endnodenumbers[i][j] ) + 1; fin.close () ;// resetSpeed(url);// speedStatistics(); Random r = new Random(); int bits, val; int pop = speed2-speed1+1; int networksize=-speed1; InputStream speeds = null; float speedvector[]=new float[edges]; String speedfilename="RandomSpeed-"+networksize+".txt"; if(networksize==99)speedfilename="RandomSpeed-river.txt"; if(speed1>=0){ for(int i=0; i<vertices;i++) { for(int j=0; j<nooflinks[i]; j++) { do { bits = r.nextInt(); val = bits%pop; } while (bits - val + (pop-1) < 0); Speed[i].push(speed1+val); } } } else{ //changeOriginalSpeeds(-speed1,url); //System.out.println(edges); //System.out.print("\n"+"networksize="+networksize); //System.out.print("url="+url+"\n"); /* try { speeds = new URL(url, speedfilename).openStream(); //speeds = new URL("http://www.tc.umn.edu/~xiex0055/SONG(1.0)(Update)/"+filename).openStream(); //fin = new FileInputStream(filename); } catch(IOException e) { System.out.println("Exception Occured!!!!"); } */ try { speeds = new URL(url, speedfilename).openStream(); } catch(FileNotFoundException e) { System.out.println("From DirectedGraph class: Exception Occured!!!!"); return; } //System.out.println("edges="+edges); int temp=0; try{ temp=speeds.read(); }catch (IOException e){System.out.println("not read!!!!");} int i=0; while(i<edges){ String speed = ""; //System.out.println("temp="+(char)temp); //while(temp!=-1 && temp != 13 && temp != (int)' ') while((temp>=(int)'0' && temp<=(int)'9' )|| temp==(int)'.' ) { speed = speed+(char)temp; try{ temp=speeds.read(); }catch (IOException e){System.out.println("not read!!!!");} ////// 32 ---- space, 13 ----- new Ltempne } //System.out.print(speed+"\t"); try { if(speed != null) { speedvector[i] = (float)Double.valueOf(speed).doubleValue(); i++; } } catch(NumberFormatException e) { //System.out.println("NumberFormatException while reading an float."); System.out.print("!"); } //System.out.println(speedvector[i]); try{ temp=speeds.read(); }catch (IOException e){System.out.println("not read!!!!");} } int pointer=0; for(int m=0;m<vertices ;m++) for(int n=0;n<NoofLinks( m+1);n++){ Speed[m].push(speedvector[pointer]); pointer++; } try{ speeds.close() ; }catch (IOException e){System.out.println("Cannot close the file!!!!");} } } // //////END OF CONSTRUCTOR2 public DirectedGraph(URL url, String filename, int speed1, int speed2) throws IOException { this.url =url; this.speed1 = speed1; this.speed2 = speed2; ReadANumber read = new ReadANumber(); int integer = 0; String message = ""; InputStream fin = null; try { fin = new URL(url, filename).openStream(); } catch(FileNotFoundException e) { System.out.println("From DirectedGraph class: Exception Occured!!!!"); return; } //System.out.println("Before Reading from the file"); vertices = read.readint(fin); //System.out.println(vertices); // * NetworkDynamics.writeMe(vertices, fout); // * fout.write(13); //System.out.print((int)'1'+"\t"+(int)'2'+"\t"+(int)'3'+"\t"+(int)'9'+"\t"+(int)'0'+"\t"+(int)'.'+"\t"); //System.out.println("\n///////////////////////Vertices: " + vertices+"///////////////////////");//// Initializing the variables; nooflinks = new int[vertices]; endnodenumbers = new int[vertices][]; DistMatrix = new float[vertices][]; linkLength = new float[vertices][]; xcoordinate = new int[vertices]; ycoordinate = new int[vertices]; nodeNumbers = new int[vertices]; //DistanceMat = new float[vertices][vertices]; Speed = new FloatStack[vertices]; //float dummy; for(int i=0; i<vertices; i++) { if(read.end == -1) break; nodeNumbers[i] = read.readint(fin); //System.out.print(nodeNumbers[i]+" "); if(read.end == -1) break; xcoordinate[i] = read.readint(fin); //System.out.print(xcoordinate[i] +" "); // * NetworkDynamics.writeMe( xcoordinate[i], fout); // * fout.write(32); if(read.end == -1) break; ycoordinate[i] = read.readint(fin); //System.out.print(ycoordinate[i] +" "); // * NetworkDynamics.writeMe( ycoordinate[i], fout); // * fout.write(32); if(read.end == -1) break; nooflinks[i] = read.readint(fin); //System.out.print(nooflinks[i] +" "); // * NetworkDynamics.writeMe( nooflinks[i] , fout); // * fout.write(32); if(read.end == -1) break; DistMatrix[i] = new float[nooflinks[i]]; linkLength[i] = new float[nooflinks[i]]; //DistanceMat[i] = new float[nooflinks[i]]; Speed[i] = new FloatStack(nooflinks[i]); endnodenumbers[i] = new int[nooflinks[i]]; for(int j =0; j<nooflinks[i]; j++) { endnodenumbers[i][j] = read.readint(fin); //System.out.print(endnodenumbers[i][j] +" "); // * NetworkDynamics.writeMe( endnodenumbers[i][j], fout); // * fout.write(32); if(read.end == -1) break; DistMatrix[i][j] =0; linkLength[i][j] = read.readfloat(fin); //System.out.print(linkLength[i][j] +" "); // * NetworkDynamics.writeMe( 4, fout); // * if ( !( j == nooflinks[i] -1) ) // * fout.write(32); //dummy = read.readfloat(fin); if(read.end == -1) break; } // * fout.write(13); } for(int i=0; i<vertices; i++) edges +=nooflinks[i]; //System.out.println("Number of edges: " + edges); //System.out.println(); for(int i=0; i< vertices; i++) for(int j=0; j< nooflinks[i]; j++) endnodenumbers[i][j] = returnPosition( endnodenumbers[i][j] ) + 1; fin.close () ;// resetSpeed(url);// speedStatistics(); Random r = new Random(); int bits, val; int pop = speed2-speed1+1; int networksize=-speed1; InputStream speeds = null; float speedvector[]=new float[edges]; String speedfilename="RandomSpeed-"+networksize+".txt"; if(networksize==99)speedfilename="RandomSpeed-river.txt"; if(speed1>=0){ for(int i=0; i<vertices;i++) { for(int j=0; j<nooflinks[i]; j++) { do { bits = r.nextInt(); val = bits%pop; } while (bits - val + (pop-1) < 0); Speed[i].push(speed1+val); } } } else{ //changeOriginalSpeeds(-speed1,url); //System.out.println(edges); //System.out.print("\n"+"networksize="+networksize); //System.out.print("url="+url+"\n"); /* try { speeds = new URL(url, speedfilename).openStream(); //speeds = new URL("http://www.tc.umn.edu/~xiex0055/SONG(1.0)(Update)/"+filename).openStream(); //fin = new FileInputStream(filename); } catch(IOException e) { System.out.println("Exception Occured!!!!"); } */ try { speeds = new URL(url, speedfilename).openStream(); } catch(FileNotFoundException e) { System.out.println("From DirectedGraph class: Exception Occured!!!!"); return; } //System.out.println("edges="+edges); int temp=0; try{ temp=speeds.read(); }catch (IOException e){System.out.println("not read!!!!");} int i=0; while(i<edges){ String speed = ""; //System.out.println("temp="+(char)temp); while((temp>=(int)'0' && temp<=(int)'9' )|| temp==(int)'.' ) // while(temp!=-1 && temp != 13 &&temp != (int)' ') { speed = speed+(char)temp; try{ temp=speeds.read(); }catch (IOException e){System.out.println("not read!!!!");} ////// 32 ---- space, 13 ----- new Ltempne } //System.out.println(speed); try { if(speed != null) { speedvector[i] = Float.valueOf(speed).floatValue(); i++; } } catch(NumberFormatException e) { System.out.println("!"); } //System.out.println(speedvector[i]); try{ temp=speeds.read(); }catch (IOException e){System.out.println("not read!!!!");} } int pointer=0; for(int m=0;m<vertices ;m++) for(int n=0;n<NoofLinks( m+1);n++){ Speed[m].push(speedvector[pointer]); pointer++; } try{ speeds.close() ; }catch (IOException e){System.out.println("Cannot close the file!!!!");} } System.out.print("\n"+vertices+"\n"); for (int i=0;i<vertices;i++){ System.out.print(XCoordinate(i+1)+"\t"+YCoordinate(i+1)+"\n"); } System.out.print(edges+"\n"); int linkindex=0; for (int i=0;i<vertices;i++){ for (int k=0;k<NoofLinks(i+1);k++){ linkindex++; int node1=i+1; int node2=EndNodeNumbers(i+1, k+1); double length=0.2*Math.sqrt(Math.pow(XCoordinate(node1)-XCoordinate(node2), 2)+Math.pow(YCoordinate(node1)-YCoordinate(node2), 2)); float speed, capacity; if (length<2){ speed=50; capacity=2400; } else{ speed=100; capacity=4800; } System.out.print(linkindex+"\t"+node1+"\t"+node2+"\t0\t"+(float)length+"\t"+speed+"\t"+capacity+"\t0\t0\t"+(float)(length/speed)+"\t"+(float)(length/speed)+"\n"); } } //System.out.print(linkindex+"\n"+vertices+"\n"+"11\n"); } ////////END OF CONSTRUCTOR3 // ////// a class used for reading numbers from the file. this class is written in order to reduce the redundancy of the code// ////// either an int or a float can be read class ReadANumber{ public int end; ReadANumber() { end = 0; } int readint(InputStream f) throws IOException { String msg = ""; int i; do { i=f.read(); //if(i != -1 && i != 13 && i!=32 && i!=9 ) if(i>47 && i<58 || i==(int)'.') ////// 32 ---- space, 13 ----- new Line msg += (char)i; //System.out.print("\tmsg="+msg); } while(i>47 && i<58 ||i==(int)'.'); end = i; try { if(msg != null) { i = Integer.parseInt(msg); return( i ); } else return ( 0 ); } catch(NumberFormatException e) { System.out.println("NumberFormatException while reading an integer."); return ( 0 ); } } float readfloat(InputStream f) throws IOException { String msg = ""; int i; do { i = f.read(); //System.out.print("\ti="+i); //if(i != -1 && i != 13 && i != 32 && i!=9 && i!=10) if(i>47 && i<58 || i==(int)'.') msg += (char)i; } while(i>47 && i<58 || i==(int)'.'); end = i; try { if(msg != null) { return( Float.valueOf(msg).floatValue() ); } else return ( 0 ); } catch(NumberFormatException e) { System.out.println("NumberFormatException while reading a float value."); return (0); } } }// /////////////////////// End of ReadANumber class ////// return a value between 0 1 2 3 ..... (vertices -1) public int returnPosition(int nodeNumber) { for(int i=0; i< vertices; i++) if( nodeNumbers[i] == nodeNumber) return (i); System.out.println("Error!!!-------- " + nodeNumber); return(0); } public void changeSpeeds(int speed1, int speed2,URL url) { this.speed1 = speed1; this.speed2 = speed2; resetSpeed(url);// speedStatistics(); } public void resetSpeed(URL url) { Random r = new Random(); int bits, val; int pop = speed2-speed1+1; int networksize=-speed1; InputStream speeds = null; float speedvector[]=new float[edges]; String speedfilename="RandomSpeed-"+networksize+".txt"; if(networksize==99)speedfilename="RandomSpeed-river.txt"; if(speed1>=0){ for(int i=0; i<vertices;i++) { for(int j=0; j<nooflinks[i]; j++) { do { bits = r.nextInt(); val = bits%pop; } while (bits - val + (pop-1) < 0); Speed[i].push(speed1+val); } } } else{ //changeOriginalSpeeds(-speed1,url); //System.out.println(edges); //System.out.print("\n"+"networksize="+networksize); //System.out.print("url="+url+"\n"); try { speeds = new URL(url, speedfilename).openStream(); //speeds = new URL("http://www.tc.umn.edu/~xiex0055/SONG(1.0)(Update)/"+filename).openStream(); //fin = new FileInputStream(filename); } catch(IOException e) { System.out.println("Exception Occured!!!!"); } //System.out.println("edges="+edges); int temp=0; try{ temp=speeds.read(); }catch (IOException e){System.out.println("not read!!!!");} int i=0; while(i<edges){ String speed = ""; //System.out.println("temp="+(char)temp); while((temp>=(int)'0' && temp<=(int)'9' )|| temp==(int)'.' ) //while(temp!=-1 && temp != 13 &&temp != (int)' ') { speed = speed+(char)temp; try{ temp=speeds.read(); }catch (IOException e){System.out.println("not read!!!!");} ////// 32 ---- space, 13 ----- new Ltempne } try { if(speed != null) { speedvector[i] = Float.valueOf(speed).floatValue(); i++; } } catch(NumberFormatException e) { System.out.println("!"); } //System.out.println(speedvector[i]); try{ temp=speeds.read(); }catch (IOException e){System.out.println("not read!!!!");} } int pointer=0; for(int m=0;m<vertices ;m++) for(int n=0;n<NoofLinks( m+1);n++){ Speed[m].push(speedvector[pointer]); pointer++; } try{ speeds.close() ; }catch (IOException e){System.out.println("Cannot close the file!!!!");} } } //////// even though the following functions look unnecessary and option of "private" can be used in declaring variables//////// these functions eliminate the accidental manipulation of data public int Vertices() { return vertices; } public int Edges() { return edges; } public int NoofLinks(int NodeNumber) { ////// NodeNumber (1, 2, 3, ........ vertices) if(NodeNumber > vertices || NodeNumber <1) { System.out.println("Invalid argument in NoofLinks function in graph class."); return ( 0 ); } else return(nooflinks[NodeNumber-1]); } ///// FirstNN = {1,2,3....Vertices} , SecondNN = {1,2.....nooflinks[FirstNN]} public void change_DistMatrix(int FirstNN, int SecondNN, float new_value) { if ( (FirstNN>=1 && FirstNN<=vertices) && (SecondNN>=1 && SecondNN <= nooflinks[FirstNN-1]) ) DistMatrix[FirstNN-1][SecondNN-1] = new_value; else System.out.println("From Graph class: Invalid attempt to change an element in DistMatrix[][]"); } /// i,j = {1,2....Vertices} public float Read_Distance(int i, int j) { if(Connected(i, j)) //return ( (float) Math.sqrt( Math.pow( xcoordinate[i-1]-xcoordinate[j-1], 2)+ // Math.pow( ycoordinate[i-1]-ycoordinate[j-1], 2 )) ); return ( (float) linkLength[i-1][ Position_EndNode(i,j) -1] ); else { System.out.println("From DirectedGraph class: Error!!!! Nodes are not connected. Returned zero."); return 0; } /* if( (i>=1 && i<=vertices) && (j>=1 && j<= vertices) ) return(DistanceMat[i-1][j-1]); else { System.out.println("From Graph class: Invalid argument passed to Read_Distance method"); return(0); }*/ } //// EndNodeNumbers argument are first argument: node number (1,2,3,....... vertices) /// second argument: (1,2,3...... nooflinks[first argument -1] = NoofLinks(first argument)) public int EndNodeNumbers(int NodeNumber, int jthLinkedNode) { if ( ( NodeNumber<=vertices && NodeNumber>=1 ) && ( jthLinkedNode <= nooflinks[NodeNumber-1] && jthLinkedNode>=1 ) ) return( endnodenumbers[NodeNumber-1][jthLinkedNode-1] ); else { System.out.println("Invlid argument passed to the function EndNodeNumbers in Graph class."); return( 0 ); } } ////// FirstNN, SecondNN = {1,2,3......vertices} position ={1,2,3....nooflinks[FirstNN-1]} public int Position_EndNode(int FirstNN, int SecondNN) { int position =0; if(Connected(FirstNN, SecondNN) ) { for(int i=0; i<nooflinks[FirstNN-1]; i++) { if( endnodenumbers[FirstNN-1][i] == SecondNN) position = i+1; } } else System.out.println("From Graph class, Position_EndNode: Invalid nodes!!! Nodes are not connected returning a zero value "); return position; } //////////// FirstNN (1,2,3, .......vertices), SecondNP (1,2,3,.....nooflinks[FirstNN-1]) public float DistMatrix(int FirstNN, int SecondNP) { if ( (FirstNN>=1 && FirstNN<= vertices) && (SecondNP >=1 && SecondNP <= nooflinks[FirstNN-1]) ) return ( DistMatrix[FirstNN-1][SecondNP-1]); else { System.out.println("From Graph class: Invalid argument sent to DistMatrix Method"); return 0; } /*if( ( FirstNN>vertices || FirstNN<1 ) || ( SecondNN>vertices || SecondNN<1 ) ) { System.out.println("Invalid argument passed to the function Distance Matrix in DirectedGraph class"); return (0); } else return( DistMatrix[FirstNN-1][SecondNN-1] ); */ } ///// FirstNN = {1,2,3....vertices} SecondNP = {1,2,3...nooflinks[FirstNN-1]} public float Speed(int FirstNN, int SecondNP) { if ( (FirstNN >=1 && FirstNN <= vertices) && (SecondNP>=1 && SecondNP <=nooflinks[FirstNN-1]) ) return (Speed[FirstNN-1].access(SecondNP-1) ); else { System.out.println("From graph class: Invalid argument passed to Speed Method"); return 0; } } //////// FirstNN, SecondNN = {1,2,3.....vertices} public void change_Speed(int FirstNN, int SecondNN, float value) { int position; if ( Connected(FirstNN, SecondNN) ) Speed[FirstNN-1].replace(Position_EndNode(FirstNN, SecondNN)-1, value); else System.out.println("From Graph class: Nodes passed to change_Speed method are not connected!!!"); } ///////////// NN (1,2,3,4 ........vertices) public int XCoordinate(int NN) { if( NN>vertices || NN<1 ) { System.out.println("Invalid argument passes to the function XCoordinate in DirectedGraph calss"); return (0); } else return(xcoordinate[NN-1]); } ///////////// NN (1,2,3,4 ........vertices) public int YCoordinate(int NN) { if( NN>vertices || NN<1 ) { System.out.println("Invalid argument passes to the function XCoordinate in DirectedGraph calss"); return (0); } else return(ycoordinate[NN-1]); } /////////// FirstNN = {1,2,3...vertices}, SecondNN = {node thats connected to FirstNN} = {1,2,3...vertices} public boolean Connected(int FirstNN, int SecondNN) { boolean conntd = false; /// true if Second NN is connected to FirstNN (FNN -----> SNN) for(int i=0; i<nooflinks[FirstNN-1]; i++) { if( endnodenumbers[FirstNN-1][i] == SecondNN) conntd = true; } return conntd; } public boolean mutuallyConnected(int FirstNN, int SecondNN) { if(Connected(FirstNN, SecondNN) ) { if(Connected(SecondNN, FirstNN)) return true; else return false; } else { System.out.println("From mutuallyConnected Method in DirectedGraph class: The nodes are not connected!!! "); return false; } } /////// reference to xcoordinates public int[] getXCoord() { int a[] = new int[vertices]; for(int i=0; i<vertices; i++) a[i] = xcoordinate[i]; return a; } public int[] getYCoord() { int a[] = new int[vertices]; for(int i=0; i<vertices; i++) a[i] = ycoordinate[i]; return a; } public int[] getNoOfLinks() { int a[] = new int[vertices]; for(int i=0; i<vertices; i++) a[i] = nooflinks[i]; return a; } public int[][] getEndNodeNumbers() { int a[][] = new int[vertices][]; for(int i=0; i<vertices; i++) for(int j =0; j<nooflinks[i];j++) a[i][j] = endnodenumbers[i][j]; return a; } public void speedStatistics() { float sum = 0; for(int i=0; i<vertices; i++) for(int j=0; j<nooflinks[i]; j++) sum+=Speed[i].access(j); avgSpeed = sum/ edges;; sum = 0; for(int i=0; i<vertices; i++) { for(int j=0; j<nooflinks[i]; j++) { float f = (Speed[i].access(j) - avgSpeed ); sum += (float) (f*f); } } varianceSpeed = (float) ( Math.sqrt ( sum/(edges - 1) ) ); } public float avgSpeed() { float sum = 0; for(int i=0; i<vertices; i++) for(int j=0; j<nooflinks[i]; j++) sum+=Speed[i].access(j); avgSpeed = sum/ edges; return avgSpeed; } public float varianceSpeed() { float sum= 0; for(int i=0; i<vertices; i++) { for(int j=0; j<nooflinks[i]; j++) { float f = (Speed[i].access(j) - avgSpeed ); sum += (float) (f*f); } } varianceSpeed = (float) ( Math.sqrt ( sum/(edges - 1) ) ); return varianceSpeed; } void changeData() { //System.out.println(edges); InputStream origin = null; PrintWriter update=null; String filename="Grid50source.txt"; try { origin = new URL(url, filename).openStream(); //fin = new FileInputStream(filename); } catch(IOException e) { System.out.println("Exception Occured!!!!"); } try { update=new PrintWriter(new FileOutputStream("Grid.txt")); } catch(IOException e) { System.out.print("Error opening the files!"); System.exit(0); } //System.out.println("edges="+edges); int temp=0; try{ temp=origin.read(); }catch (IOException e){System.out.println("not read!!!!");} int i=0; while(i<29601){ String msg = ""; System.out.println("temp="+(char)temp); while(temp>(int)'0' && temp<(int)'9') { msg = msg+(char)temp; try{ temp=origin.read(); }catch (IOException e){System.out.println("not read!!!!");} ////// 32 ---- space, 13 ----- new Ltempne } //System.out.println(speed); try { if(msg != null) { update.print(Integer.parseInt(msg)+"\t"); i++; } } catch(NumberFormatException e) { System.out.println("NumberFormatException while reading an int."); } //System.out.println(speedvector[i]); try{ temp=origin.read(); }catch (IOException e){System.out.println("not read!!!!");} } try{ origin.close() ; }catch (IOException e){System.out.println("Cannot close the file!!!!");} update.close(); } void changeOriginalSpeeds(int networksize, URL url) { } /////////////////////////////////// public void printDMatrix() { System.out.println("Distance Matrix for the give graph; "); for(int i=0; i<vertices; i++) { for(int j=0; j<nooflinks[i]; j++) System.out.print(DistMatrix[i][j] + " "); System.out.println(); } System.out.println(); } public void printSpeed() { System.out.println("Speed Matrix for the give graph; "); for(int i=0; i<vertices; i++) { for(int j=0; j<nooflinks[i]; j++) System.out.print(Speed[i].access(j) + " "); System.out.println(); } System.out.println(); } public void printNoofLinks() { System.out.print("No of Nodes from each vertices: "); for(int i=0; i<vertices; i++) System.out.print(nooflinks[i] + " "); System.out.println(); System.out.println(); } public void printEndNodeNumbers() { System.out.println("End Node Numbers Matrix:"); for(int i=0; i<vertices; i++) { for(int j=0;j<nooflinks[i]; j++) System.out.print(endnodenumbers[i][j] + " "); System.out.println(); } System.out.println(); } public void printCoordinates() { System.out.println("Coordinates of the network nodes:"); for(int i=0; i<vertices; i++) System.out.println("Node Number; " + (i+1) + " ( " + xcoordinate[i] +", " + ycoordinate[i] + ")" ); } }