/* File: RandomGridGraph.java - May 2011 */ package l25; /** * Represent a random weighted graph on the 2D grid. Various * parameters can be set to adjust the maximum edge weight, and the * extent to which vertical edges and horizontal edges differ in weight. * * @author Michael Albert * */ public class RandomGridGraph extends Graph{ static int MAX_WEIGHT = 1000000; static double VERTICAL_BIAS = 1.0; int rows; int cols; public RandomGridGraph(int rows, int cols) { this(rows, cols, MAX_WEIGHT, VERTICAL_BIAS); } public RandomGridGraph(int rows, int cols, int maxWeight) { this(rows, cols, maxWeight, VERTICAL_BIAS); } public RandomGridGraph( int rows, int cols, int maxWeight, double verticalBias ) { super(rows*cols); this.rows = rows; this.cols = cols; MAX_WEIGHT = maxWeight; VERTICAL_BIAS = verticalBias; initialise(); } public void initialise() { removeEdges(); // Vertical edges for(int r = 0; r < rows-1; r++) { for(int c = 0; c < cols; c++) { addWeightedEdge( r*cols + c, (r+1)*cols+c, (int) (VERTICAL_BIAS*getWeight()) ); } } // Horizontal edges for(int r = 0; r < rows; r++) { for(int c = 0; c < cols-1; c++) { addWeightedEdge(r*cols + c, r*cols+c+1, getWeight()); } } } static int getWeight() { return 1 + (int) (Math.random()*MAX_WEIGHT); } }