001 package graphlab.samples.extensions;// 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 Lesser General Public License (LGPL): http://www.gnu.org/licenses/ 004 005 import graphlab.graph.graph.EdgeModel; 006 import graphlab.graph.graph.GraphModel; 007 import graphlab.graph.graph.GraphPoint; 008 import graphlab.graph.graph.VertexModel; 009 import graphlab.platform.lang.BoundedInteger; 010 import graphlab.platform.parameter.Parameter; 011 import graphlab.platform.parameter.Parametrizable; 012 import graphlab.plugins.graphgenerator.core.PositionGenerators; 013 import graphlab.plugins.graphgenerator.core.extension.GraphGeneratorExtension; 014 015 import java.awt.*; 016 017 public class BinaryTreeGenerator implements GraphGeneratorExtension, Parametrizable { 018 019 //the depth should be positive, and also if it is very large the 020 //generated graph is too large to generate. 021 @Parameter(description = "depth of the tree") 022 public BoundedInteger depth = new BoundedInteger(3, 15, 1); 023 private int d; 024 private int n; //num of vertices 025 026 027 public String checkParameters() { 028 d = depth.getValue(); 029 n = (int) (Math.pow(2, d + 1) - 1); 030 return null; //the parameters are well defined. 031 } 032 033 public String getName() { 034 return "binary tree"; 035 } 036 037 public String getDescription() { 038 return "generates a binary tree"; 039 } 040 041 public GraphPoint[] getVertexPositions() { 042 Point[] r = new Point[n]; 043 r[0] = new Point(0, 0); 044 int last = 1; 045 for (int h = 1; h <= d; h++) { 046 int nh = (int) Math.pow(2, h); //num of vertices at height h. 047 Point p[] = PositionGenerators.circle(30 * h * h, 0, 0, nh); 048 System.arraycopy(p, 0, r, last, nh); 049 last += nh; 050 } 051 GraphPoint ret[] = new GraphPoint[n]; 052 for (int i = 0; i < n; i++) { 053 ret[i] = new GraphPoint(r[i].x, r[i].y); 054 } 055 return ret; 056 } 057 058 public GraphModel generateGraph() { 059 GraphModel g = new GraphModel(false); 060 VertexModel[] v = new VertexModel[n]; 061 EdgeModel[] e = new EdgeModel[n - 1]; 062 //generating vertices 063 for (int i = 0; i < n; i++) 064 v[i] = new VertexModel(); 065 //generating edges 066 for (int i = 0; i < n - 1; i++) 067 e[i] = new EdgeModel(v[i + 1], v[i / 2]); 068 069 g.insertVertices(v); 070 g.insertEdges(e); 071 072 //generating and setting vertex positions 073 GraphPoint[] pos = getVertexPositions(); 074 for (int i = 0; i < n; i++) 075 v[i].setLocation(pos[i]); 076 return g; 077 } 078 }