2
* Copyright 2008-2010 Gephi
3
* Authors : Cezary Bartosiak
4
* Website : http://www.gephi.org
6
* This file is part of Gephi.
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.
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.
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/>.
21
package org.gephi.io.complexgenerator.plugin;
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;
38
* Generates an undirected not necessarily connected graph.
40
* http://www.math-inst.hu/~p_erdos/1960-10.pdf
41
* http://www.inf.uni-konstanz.de/algo/publications/bb-eglrn-05.pdf
45
* m <= n * (n - 1) / 2
49
* @author Cezary Bartosiak
51
@ServiceProvider(service = Generator.class)
52
public class ErdosRenyiGnm implements Generator {
53
private boolean cancel = false;
54
private ProgressTicket progressTicket;
59
public void generate(ContainerLoader container) {
60
Progress.start(progressTicket, n + n * n + m);
61
Random random = new Random();
62
container.setEdgeDefault(EdgeDefault.UNDIRECTED);
64
NodeDraft[] nodes = new NodeDraft[n];
67
for (int i = 0; i < n && !cancel; ++i) {
68
NodeDraft node = container.factory().newNodeDraft();
69
node.setLabel("Node " + i);
71
container.addNode(node);
72
Progress.progress(progressTicket);
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]);
83
Progress.progress(progressTicket);
87
for (int i = 0; i < m; ++i) {
88
EdgeDraft e = edges.get(random.nextInt(edges.size()));
93
Progress.finish(progressTicket);
94
progressTicket = null;
105
public void setn(int n) {
109
public void setm(int m) {
113
public String getName() {
114
return "Erdos-Renyi G(n, m) model";
117
public GeneratorUI getUI() {
118
return Lookup.getDefault().lookup(ErdosRenyiGnmUI.class);
121
public boolean cancel() {
126
public void setProgressTicket(ProgressTicket progressTicket) {
127
this.progressTicket = progressTicket;