Advertisement
Guest User

caiobm

a guest
Jan 23rd, 2009
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. /*
  2.  * MatGraph is a class that implements the interface Graph<Double,Double>.
  3.  * It was not intended to make a good oriented-object design.
  4.  * Only for test.
  5.  *
  6.  * Creates an adjacency matrix graph from a file with the follow example format:
  7.  *
  8.  *  
  9.     8
  10.     0 * * * * * * *
  11.     30 0 * * * * * *
  12.     100 80 0 * * * * *
  13.     * * 120 0 * * * *
  14.     * * * 150 0 25 * *
  15.     * * * 100 * 0 90 140
  16.     * * * * * * 0 100
  17.     170 * * * * * * 0
  18.    
  19.     The first line must contains the number "n" of vertices. The other "n" lines must
  20.     have "n" elements. If there is no conexion between two vertices a single char "*" must be typed.
  21.  */
  22.  
  23. /**
  24.  *
  25.  * @author Caio Bomfim Martins
  26.  * @version 1.0
  27.  */
  28. public class MatGraph implements Graph<Double,Double> {
  29.    
  30.     private double[][] cost;
  31.    
  32.     public MatGraph(String fileName) throws java.io.IOException {
  33.         java.io.BufferedReader br = new java.io.BufferedReader(
  34.                            new java.io.FileReader(new java.io.File(fileName)));
  35.        
  36.         int length = Integer.parseInt(br.readLine());
  37.        
  38.         cost = new double[length][length];
  39.        
  40.         String[] line;
  41.        
  42.         for ( int i = 0; i < cost.length; i++ ) {
  43.             line = br.readLine().split(" ");
  44.            
  45.             for ( int j = 0; j < cost.length; j++ ) {
  46.                 if (line[j].compareTo("*") == 0) {
  47.                     cost[i][j] = Float.MAX_VALUE;
  48.                    
  49.                 } else {
  50.                     cost[i][j] = Float.parseFloat(line[j]);
  51.                   }              
  52.             }
  53.         }
  54.        
  55.         br.close();
  56.     }
  57.    
  58.     public Double get(int i, int j) {
  59.         return Double.valueOf(cost[i][j]);
  60.     }
  61.    
  62.     public int size() {
  63.         return cost.length;
  64.     }
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement