~cezary-bartosiak/gephi/spread-simulator

« back to all changes in this revision

Viewing changes to ComplexGeneratorPlugin/src/org/gephi/io/complexgenerator/plugin/ErdosRenyiGnm.java

  • Committer: Cezary Bartosiak
  • Date: 2011-02-21 07:47:23 UTC
  • mfrom: (2044.4.7 generators)
  • Revision ID: cezary.bartosiak@gmail.com-20110221074723-ul2u6nkg1evjvh0a
* merged with generators

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2008-2010 Gephi
 
3
 * Authors : Cezary Bartosiak
 
4
 * Website : http://www.gephi.org
 
5
 * 
 
6
 * This file is part of Gephi.
 
7
 *
 
8
 * Gephi is free software: you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published by
 
10
 * the Free Software Foundation, either version 3 of the License, or
 
11
 * (at your option) any later version.
 
12
 *
 
13
 * Gephi is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with Gephi.  If not, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
package org.gephi.io.complexgenerator.plugin;
 
22
 
 
23
import java.util.ArrayList;
 
24
import java.util.List;
 
25
import java.util.Random;
 
26
import org.gephi.io.generator.spi.Generator;
 
27
import org.gephi.io.generator.spi.GeneratorUI;
 
28
import org.gephi.io.importer.api.ContainerLoader;
 
29
import org.gephi.io.importer.api.EdgeDefault;
 
30
import org.gephi.io.importer.api.EdgeDraft;
 
31
import org.gephi.io.importer.api.NodeDraft;
 
32
import org.gephi.utils.progress.Progress;
 
33
import org.gephi.utils.progress.ProgressTicket;
 
34
import org.openide.util.Lookup;
 
35
import org.openide.util.lookup.ServiceProvider;
 
36
 
 
37
/**
 
38
 * Generates an undirected not necessarily connected graph.
 
39
 *
 
40
 * http://www.math-inst.hu/~p_erdos/1960-10.pdf
 
41
 * http://www.inf.uni-konstanz.de/algo/publications/bb-eglrn-05.pdf
 
42
 *
 
43
 * n > 0
 
44
 * m >= 0
 
45
 * m <= n * (n - 1) / 2
 
46
 *
 
47
 * O(n^2)
 
48
 *
 
49
 * @author Cezary Bartosiak
 
50
 */
 
51
@ServiceProvider(service = Generator.class)
 
52
public class ErdosRenyiGnm implements Generator {
 
53
        private boolean cancel = false;
 
54
        private ProgressTicket progressTicket;
 
55
 
 
56
        private int n = 50;
 
57
        private int m = 50;
 
58
 
 
59
        public void generate(ContainerLoader container) {
 
60
                Progress.start(progressTicket, n + n * n + m);
 
61
                Random random = new Random();
 
62
                container.setEdgeDefault(EdgeDefault.UNDIRECTED);
 
63
 
 
64
                NodeDraft[] nodes = new NodeDraft[n];
 
65
 
 
66
                // Creating n nodes
 
67
                for (int i = 0; i < n && !cancel; ++i) {
 
68
                        NodeDraft node = container.factory().newNodeDraft();
 
69
                        node.setLabel("Node " + i);
 
70
                        nodes[i] = node;
 
71
                        container.addNode(node);
 
72
                        Progress.progress(progressTicket);
 
73
                }
 
74
 
 
75
                // Creating a list of n^2 edges
 
76
                List<EdgeDraft> edges = new ArrayList<EdgeDraft>();
 
77
                for (int i = 0; i < n && !cancel; ++i)
 
78
                        for (int j = i + 1; j < n && !cancel; ++j) {
 
79
                                EdgeDraft edge = container.factory().newEdgeDraft();
 
80
                                edge.setSource(nodes[i]);
 
81
                                edge.setTarget(nodes[j]);
 
82
                                edges.add(edge);
 
83
                                Progress.progress(progressTicket);
 
84
                        }
 
85
 
 
86
                // Drawing m edges
 
87
                for (int i = 0; i < m; ++i) {
 
88
                        EdgeDraft e = edges.get(random.nextInt(edges.size()));
 
89
                        edges.remove(e);
 
90
                        container.addEdge(e);
 
91
                }
 
92
 
 
93
                Progress.finish(progressTicket);
 
94
                progressTicket = null;
 
95
        }
 
96
 
 
97
        public int getn() {
 
98
                return n;
 
99
        }
 
100
 
 
101
        public int getm() {
 
102
                return m;
 
103
        }
 
104
 
 
105
        public void setn(int n) {
 
106
                this.n = n;
 
107
        }
 
108
 
 
109
        public void setm(int m) {
 
110
                this.m = m;
 
111
        }
 
112
 
 
113
        public String getName() {
 
114
                return "Erdos-Renyi G(n, m) model";
 
115
        }
 
116
 
 
117
        public GeneratorUI getUI() {
 
118
                return Lookup.getDefault().lookup(ErdosRenyiGnmUI.class);
 
119
        }
 
120
 
 
121
        public boolean cancel() {
 
122
                cancel = true;
 
123
                return true;
 
124
        }
 
125
 
 
126
        public void setProgressTicket(ProgressTicket progressTicket) {
 
127
                this.progressTicket = progressTicket;
 
128
        }
 
129
}