001
002 package algo;
003
004 import java.io.Serializable;
005
006 /**
007 * A Graph for an TSP Problem.
008 * @author Heinz Kredel.
009 */
010 public class Graph implements Serializable {
011
012 protected double[][] g;
013 final static double INFINITY = Double.POSITIVE_INFINITY;
014 // = unconnected
015
016 /**
017 * Constructs an unconnected graph of given size.
018 * @param n size of the graph.
019 */
020 public Graph(int n) {
021 g = new double[n][n];
022 for (int i = 0; i < n; i++ ) {
023 connect(i,i,0.0);
024 for (int j = i+1; j < n; j++ ) {
025 connect(i,j,INFINITY);
026 connect(j,i,INFINITY);
027 }
028 }
029 }
030
031
032 /**
033 * Connect two nodes by defining a distance.
034 * @param i node number.
035 * @param j node number.
036 * @param d distance between the two nodes.
037 */
038 public void connect(int i, int j, double d) {
039 g[i][j] = d;
040 }
041
042
043 /**
044 * Get the distance between two nodes.
045 * @param i node number.
046 * @param j node number.
047 * @return distance between the two nodes.
048 */
049 public double distance(int i, int j) {
050 return g[i][j];
051 }
052
053
054 /**
055 * @return size of the graph.
056 */
057 public int size() {
058 return g.length;
059 }
060
061 }