| 1 |
package fun; |
| 2 |
|
| 3 |
import java.util.Arrays; |
| 4 |
import java.util.Random; |
| 5 |
|
| 6 |
import fileIO.TextStreamWriter; |
| 7 |
import shared.PreParser; |
| 8 |
import shared.Shared; |
| 9 |
import structures.ByteBuilder; |
| 10 |
|
| 11 |
public class MakeAdjacencyList { |
| 12 |
|
| 13 |
public static void main(String[] args){ |
| 14 |
{ |
| 15 |
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); |
| 16 |
args=pp.args; |
| 17 |
} |
| 18 |
|
| 19 |
parse(args); |
| 20 |
int[][] matrix=genMatrix(); |
| 21 |
writeMatrix(matrix); |
| 22 |
} |
| 23 |
|
| 24 |
public static void parse(String[] args){ |
| 25 |
|
| 26 |
for(int i=0; i<args.length; i++){ |
| 27 |
String arg=args[i]; |
| 28 |
String[] split=arg.split("="); |
| 29 |
String a=split[0].toLowerCase(); |
| 30 |
String b=split.length>1 ? split[1] : null; |
| 31 |
|
| 32 |
if(a.equals("out") || a.equals("out1")){ |
| 33 |
out=b; |
| 34 |
}else if(a.equals("nodes") || a.equals("n")){ |
| 35 |
nodes=Integer.parseInt(b); |
| 36 |
}else if(a.equals("minlen") || a.equals("min")){ |
| 37 |
minlen=Integer.parseInt(b); |
| 38 |
}else if(a.equals("maxlen") || a.equals("max")){ |
| 39 |
maxlen=Integer.parseInt(b); |
| 40 |
}else if(a.equals("prob")){ |
| 41 |
prob=Float.parseFloat(b); |
| 42 |
}else if(a.equals("seed")){ |
| 43 |
seed=Long.parseLong(b); |
| 44 |
}else{ |
| 45 |
throw new RuntimeException("Unknown parameter "+arg); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
public static int[][] genMatrix(){ |
| 52 |
|
| 53 |
final Random randy=Shared.threadLocalRandom(seed); |
| 54 |
final int[][] matrix=new int[nodes][nodes]; |
| 55 |
final int range=maxlen-minlen+1; |
| 56 |
for(int[] array : matrix){ |
| 57 |
Arrays.fill(array, -1); |
| 58 |
} |
| 59 |
|
| 60 |
for(int i=0; i<nodes; i++){ |
| 61 |
for(int j=i+1; j<nodes; j++){ |
| 62 |
if(randy.nextFloat()<prob){ |
| 63 |
int dist=minlen+(range<1 ? 0 : randy.nextInt(range)); |
| 64 |
matrix[i][j]=matrix[j][i]=dist; |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
return matrix; |
| 69 |
} |
| 70 |
|
| 71 |
public static void writeMatrix(int[][] matrix){ |
| 72 |
TextStreamWriter tsw=new TextStreamWriter(out, false, false, false); |
| 73 |
tsw.start(); |
| 74 |
for(int i=0; i<nodes; i++){ |
| 75 |
for(int j=i; j<nodes; j++){ |
| 76 |
int dist=matrix[i][j]; |
| 77 |
if(dist>=0){ |
| 78 |
tsw.print(toString(i)+"\t"+toString(j)+"\t"+dist+"\n"); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
tsw.poisonAndWait(); |
| 83 |
} |
| 84 |
|
| 85 |
public static String toString(int number){ |
| 86 |
ByteBuilder sb=new ByteBuilder(); |
| 87 |
while(number>0){ |
| 88 |
int x='A'+number%26; |
| 89 |
sb.append((char)x); |
| 90 |
number=number/26; |
| 91 |
} |
| 92 |
return (sb.length()<1 ? "A" : sb.reverseInPlace().toString()); |
| 93 |
} |
| 94 |
|
| 95 |
public static int nodes=10; |
| 96 |
public static int minlen=5; |
| 97 |
public static int maxlen=25; |
| 98 |
public static float prob=0.3f; |
| 99 |
public static long seed=-1; |
| 100 |
public static String out="stdout.txt"; |
| 101 |
|
| 102 |
} |