java - Random Tour Generation Issues -
i trying generate random tour graph. using adjacency-list method.
there problem vertices. when add vertex particular list, vertex gets added lists in graph. not understand why! here's code:
public static void main(string[] args) { defaultdata(6); } public static void defaultdata(int n) { integer costs[] = { 26, 95, 38, 74, 80, 73, 73, 92, 22, 97, 13, 81, 41, 17, 4, 2, 47, 54, 21, 68, 78, 4, 77, 3, 66, 55, 99, 42, 62, 39, 8, 36, 53, 74, 26, 8, 42, 66, 30, 58, 69, 14, 49, 39, 85, 98, 72, 3, 18, 99, 96, 66, 64, 36, 17, 44, 70, 0, 8, 14, 62, 41, 84, 59, 94, 27, 5, 27, 96, 10, 15, 52, 43, 20, 2, 86, 45, 43, 32, 17, 49, 92, 9, 15, 6, 49, 72, 7, 51, 21, 2, 26, 63, 82, 98, 48, 21, 96, 16 }; arraylist<arraylist<integer>> costgraph = new arraylist<>(); arraylist<arraylist<integer>> completegraph = new arraylist<>(); random rand = new random(system.currenttimemillis()); int costindex = 0; (int = 0; <= n; i++) { arraylist<integer> cost = new arraylist<>(); arraylist<integer> edge = new arraylist<>(); (int j = 0; j <= n; j++) { if (i == j) { continue; } edge.add(j); cost.add(costs[costindex]); costindex++; } completegraph.add(edge); costgraph.add(cost); } system.out.println(completegraph); arraylist<arraylist<integer>> dummygraph = (arraylist<arraylist<integer>>)completegraph.clone(); arraylist<arraylist<integer>> randomtour = new arraylist<>(); arraylist<integer> dummylist = new arraylist<>(); (int = 0; <= n; i++) { randomtour.add(dummylist); } system.out.println(dummygraph); int edgecount = 0; integer row = rand.nextint(n); integer start = row; while(edgecount <= n-1){ //dummylist = dummygraph.get(row); // keep bounds of random equal // new reduced size of lists in graph integer col = dummygraph.get(row).get(rand.nextint(n-edgecount)); randomtour.get(row).add(col); system.out.println(row); system.out.println(randomtour); edgecount++; for(int k = 0; k < n; k++) dummygraph.get(k).remove(row); row = col; } randomtour.get(row).add(start); system.out.println(randomtour); }
i grateful timely response. in advance!
you don't want this:
for (int = 0; <= n; i++) { randomtour.add(dummylist); }
it keeps adding same reference lots of times, arraylist
s in arraylist
same object.
instead want this:
for (int = 0; <= n; i++) { randomtour.add(new arraylist<integer>()); }
that way arraylist
instances in arraylist
different.
i hope answer timely enough!
Comments
Post a Comment