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    
005    package graphlab.plugins.graphgenerator.generators;
006    
007    import graphlab.graph.graph.EdgeModel;
008    import graphlab.graph.graph.GraphModel;
009    import graphlab.graph.graph.VertexModel;
010    import graphlab.platform.lang.CommandAttitude;
011    import graphlab.platform.parameter.Parameter;
012    import graphlab.platform.parameter.Parametrizable;
013    import graphlab.plugins.graphgenerator.GraphGenerator;
014    import graphlab.plugins.graphgenerator.core.PositionGenerators;
015    import graphlab.plugins.graphgenerator.core.SimpleGeneratorInterface;
016    import graphlab.plugins.graphgenerator.core.extension.GraphGeneratorExtension;
017    
018    import java.awt.*;
019    
020    /**
021     * @author Mohammad Ali Rostami
022     * @email mamliam@gmail.com
023     */
024    
025    @CommandAttitude(name = "generate_generalized_peterson", abbreviation = "_g_p", description = "generalized peterson")
026    public class GeneralizedPetersonGenerator implements GraphGeneratorExtension, Parametrizable, SimpleGeneratorInterface {
027        private GraphModel g;
028    
029        @Parameter(name = "n")
030        public static Integer n = 5;
031    
032        @Parameter(name = "k")
033        public static Integer k = 2;
034        private VertexModel[] v;
035    
036        public String getName() {
037            return "Generalized Peterson";
038        }
039    
040        public String getDescription() {
041            return "Generalized Peterson";
042        }
043    
044        public void setWorkingGraph(GraphModel g) {
045            this.g = g;
046        }
047    
048        public VertexModel[] getVertices() {
049            VertexModel[] ret = new VertexModel[2 * n];
050            for (int i = 0; i < 2 * n; i++)
051                ret[i] = new VertexModel();
052            this.v = ret;
053            return ret;
054        }
055    
056        public EdgeModel[] getEdges() {
057            EdgeModel[] ret = new EdgeModel[3 * n];
058            int counter = 0;
059            for (int i = 0; i < n; i++) {
060                ret[counter] = new EdgeModel(v[(i % n)], v[((i + 1) % n)]);
061                counter++;
062                ret[counter] = new EdgeModel(v[(i % n)], v[((i + n) % (2 * n))]);
063                counter++;
064                if (i + n + k > 2 * n - 1)
065                    ret[counter] = new EdgeModel(v[((i + n) % (2 * n))], v[((i + n + k) % (2 * n)) + n]);
066                else
067                    ret[counter] = new EdgeModel(v[((i + n) % (2 * n))], v[((i + n + k) % (2 * n))]);
068                counter++;
069            }
070            return ret;
071        }
072    
073        public Point[] getVertexPositions() {
074            Point[] p = new Point[2 * n];
075            Point[] p1 = PositionGenerators.circle(300, 200, 200, n);
076            Point[] p2 = PositionGenerators.circle(600, 200, 200, n);
077            for (int i = 0; i < n; i++) {
078                p[i] = p2[i];
079                p[i + n] = p1[i];
080            }
081            return p;
082        }
083    
084        public String checkParameters() {
085            if (k > n / 2)
086                return "K should be smaller than n/2";
087            else
088                return null;
089        }
090    
091        public GraphModel generateGraph() {
092            return GraphGenerator.getGraph(false, this);
093        }
094    
095        /**
096         * generates a Generalized Peterson Graph with given parameters
097         */
098        public static GraphModel generateGeneralizedPeterson(int n, int k) {
099            GeneralizedPetersonGenerator.n = n;
100            GeneralizedPetersonGenerator.k = k;
101            return GraphGenerator.getGraph(false, new GeneralizedPetersonGenerator());
102        }
103    
104    }