import java.util.ArrayList; import java.util.Random; import java.util.StringTokenizer; import java.io.*; public class GAUtils { private static Random randg = new Random(); public static int[] picklist2perm(int[] pl){ int[] perm = new int[pl.length]; ArrayList tmp = new ArrayList(pl.length); for(int i=0; i < pl.length; i++) tmp.add(i,new Integer(i)); for(int i=0; i < pl.length; i++) perm[i] = ((Integer)tmp.remove(pl[i])).intValue(); return perm; } public static int[] perm2picklist(int[] pr){ int[] pl = new int[pr.length]; ArrayList tmp = new ArrayList(pr.length); for(int i=0; i < pr.length; i++) tmp.add(i,new Integer(i)); for(int i=0; i < pr.length; i++){ int index = tmp.indexOf(new Integer(pr[i])); tmp.remove(index); pl[i] = index; } return pl; } public static int[] randperm(int n){ // return a random permutation of size n // that is an array containing a random permutation of values 0, 1, ..., n-1 int[] perm = new int[n]; for(int i=0; i < n; i++){ perm[i] = i; } for(int i=0; i < n-1; i++){ int j = randg.nextInt(n-i) + i; // sawp perm[i] and perm[j] int temp = perm[i]; perm[i] = perm[j]; perm[j] = temp; } return perm; } public static float[][] readMatrix(String file_name){ // Read the graph description from file_name, and return the // adjacency matrix of the graph. // Each line of file_name is a comment (started with //) or // otherwise specify an edge of the graph, such that the first // number on this line is the starting vertex, the second is the // ending vertex and the third is the weight of this edge. // EACH LINE SPECIFIES JUST ONE EDGE! (NOT TWO AS IN UNDIRECTED GRAPHS) float[][] matrix; // the adjacency matrix BufferedReader inp; try{ inp = new BufferedReader(new FileReader(file_name)); String s; ArrayList line = new ArrayList(); while( (s = inp.readLine()) != null ){ float[] tmp = new float[3]; StringTokenizer st = new StringTokenizer(s,", \t",false); if( ! st.hasMoreTokens() ) continue; String token = st.nextToken(); if( token.equals("//") ) continue; for(int i=0; i < 2; i++){ tmp[i] = Integer.parseInt(token); token = st.nextToken(); } tmp[2] = Float.parseFloat(token); line.add(tmp); } int max_index = 0; // number of graph vertices == max_index + 1 for(int i=0; i < line.size(); i++){ float[] tmp = (float[])(line.get(i)); if( tmp[0] > max_index ) max_index = (int) tmp[0]; if( tmp[1] > max_index ) max_index = (int) tmp[1]; } matrix = new float[max_index+1][max_index+1]; for(int i=0; i <= max_index; i++) for(int j=0; j <= max_index; j++) matrix[i][j] = 0.0f; for(int i=0; i < line.size(); i++){ float[] tmp = (float[])(line.get(i)); matrix[(int)tmp[0]][(int)tmp[1]] = tmp[2]; } }catch(Exception ex){ System.err.println("error in "+file_name); return null; } try{ inp.close(); }catch(Exception ex){ System.err.println(ex); } return matrix; } public static String array2string(float[] n){ String s = "["; for(int i=0; i < n.length - 1; i++) s += n[i]+", "; s += n[ n.length - 1 ]+"]"; return s; } public static void main(String[] args) { int[] a = new int[]{ 2,4,3,1,0,5 }; int[] b = GAUtils.perm2picklist(a); float[][] matrix = GAUtils.readMatrix("graph.txt"); for(int i=0; i < matrix.length; i++){ System.out.println(GAUtils.array2string(matrix[i])); } } }