001 // GraphLab Project: http://graphlab.sharif.edu 002 // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology 003 // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/ 004 package graphlab.plugins.graphgenerator.generators; 005 006 import graphlab.graph.graph.EdgeModel; 007 import graphlab.graph.graph.GraphModel; 008 import graphlab.graph.graph.VertexModel; 009 import graphlab.platform.lang.CommandAttitude; 010 import graphlab.platform.parameter.Parameter; 011 import graphlab.platform.parameter.Parametrizable; 012 import graphlab.plugins.graphgenerator.GraphGenerator; 013 import graphlab.plugins.graphgenerator.core.SimpleGeneratorInterface; 014 import graphlab.plugins.graphgenerator.core.extension.GraphGeneratorExtension; 015 016 import java.awt.*; 017 018 /** 019 * User: root 020 */ 021 @CommandAttitude(name = "generate_random_tree", abbreviation = "_g_rand_t") 022 public class RandomTreeGenerator implements GraphGeneratorExtension, Parametrizable, SimpleGeneratorInterface { 023 GraphModel g; 024 @Parameter(name = "Number of Vertices") 025 public static Integer n = 50; 026 @Parameter(name = "Height") 027 public static Integer h = 5; 028 @Parameter(name = "Maximum Degree") 029 public static Integer d = 5; 030 031 public void setWorkingGraph(GraphModel g) { 032 this.g = g; 033 } 034 035 VertexModel[] v; 036 037 public VertexModel[] getVertices() { 038 VertexModel[] ret = new VertexModel[n]; 039 for (int i = 0; i < n; i++) 040 ret[i] = new VertexModel(); 041 v = ret; 042 return ret; 043 } 044 045 public EdgeModel[] getEdges() { 046 EdgeModel[] ret = new EdgeModel[n - 1]; 047 int[][] ver = new int[2][n]; //o->deg 1->height 048 049 ver[1][0] = 0; 050 //the new one 051 for (int i = 0; i < n - 1; i++) { 052 int ran = (int) (Math.random() * (i + 1)); 053 while (!(ver[0][ran] < d && ver[1][ran] < h)) { 054 ran = (ran + 1) % (i + 1); 055 } 056 ret[i] = new EdgeModel(v[i + 1], v[ran]); 057 ver[0][i + 1]++; 058 ver[0][ran]++; 059 ver[1][i + 1] = ver[1][ran] + 1; 060 } 061 return ret; 062 } 063 064 public Point[] getVertexPositions() { 065 Point[] ret = new Point[n]; 066 int w = 100; 067 int h = 100; 068 for (int i = 0; i < n; i++) { 069 int x = (int) (Math.random() * w); 070 int y = (int) (Math.random() * h); 071 ret[i] = new Point(x, y); 072 } 073 return ret; 074 } 075 076 public String getName() { 077 return "Random Tree"; 078 } 079 080 public String getDescription() { 081 return "Generates a random tree"; 082 } 083 084 public String checkParameters() { 085 return n > Math.pow(d, h) - 1 ? "N is to small!" : null; 086 } 087 088 public GraphModel generateGraph() { 089 return GraphGenerator.getGraph(false, this); 090 } 091 092 /** 093 * generates a random tree with given parameters 094 */ 095 public static GraphModel generateRandomTree(int n, int h, int d) { 096 RandomTreeGenerator.h = h; 097 RandomTreeGenerator.d = d; 098 RandomTreeGenerator.n = n; 099 return GraphGenerator.getGraph(false, new RandomTreeGenerator()); 100 } 101 102 }