/* * TSP.java */ package edu.ucsb.cs.jicos.examples.tsp; import edu.ucsb.cs.jicos.applications.utilities.graph.*; import java.util.Random; /** * @author Peter Cappello * @version 1.0 */ public class TSP implements java.io.Serializable { /* * I think it is faster overall to have a complete matrix, even in the * symmetric case: I believe that it takes more time to compute the min & * max of computing a distance element index (e.g., * distance[min(i,j)][max(i,j)] ), which needs to be done many many times, * than it does to double the time to serialize/send the distance matrix * to all hosts initially. */ int[][] distance; private final static int MAX_EDGE = 10; public TSP ( Graph graph ) { distance = graph.getCosts(); } public TSP( int nodes, long seed, int maxEdgeWeight ) { Random random = new Random( seed ); distance = new int[ nodes ][ nodes ]; for ( int i = 0; i < nodes; i++ ) { System.out.print(" Row " + i + ": "); for ( int j = 0; j < i; j++ ) { /* make a pseudo-random, uniformly distributed distance in * [0, maxEdgeWeight) */ distance[i][j] = (int) ( maxEdgeWeight * random.nextFloat() ); distance[j][i] = distance[i][j]; System.out.print(" " + distance[i][j]); } System.out.println(" "); } } }