Synthia
Generic and flexible data structure generator
BarabasiAlbert.java
Go to the documentation of this file.
1 /*
2  Synthia, a data structure generator
3  Copyright (C) 2019-2020 Laboratoire d'informatique formelle
4  Université du Québec à Chicoutimi, Canada
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 package examples.graphs;
20 
21 import ca.uqac.lif.synthia.Picker;
22 import ca.uqac.lif.synthia.Reactive;
28 import ca.uqac.lif.synthia.tree.Node;
29 import ca.uqac.lif.synthia.util.AsInt;
30 import ca.uqac.lif.synthia.util.Tick;
31 import static examples.util.Utilities.colorGradient;
32 
33 /**
34  * Generates a graph following the
35  * <a href="https://en.wikipedia.org/wiki/Barab%C3%A1si%E2%80%93Albert_model">Barabási–Albert
36  * model</a>. Such a graph is generated by first picking a number of vertices.
37  * For each new vertex <i>n</i>' added to the graph, an edge is added to
38  * an existing node <i>n</i> with probability <i>d<sub>n</sub></i>/<i>D</i>,
39  * where <i>d<sub>n</sub></i> is the current degree of <i>n</i> and <i>D</i> is
40  * the sum of the degrees of all pre-existing vertices.
41  * <p>
42  * The resulting graph has the property of being <em>scale-free</em>: it
43  * contains few vertices with high degree, and many vertices with low degree.
44  * An example of a graph generated by this program is the following:
45  * <p>
46  * <img src="./doc-files/graphs/Graph.png" alt="Graph" />
47  * <p>
48  * In the source code, this graph is merely printed to the console in the DOT
49  * format of the <a href="https://graphviz.org">Graphviz</a> graph rendering
50  * tool.
51  * @author Sylvain Hallé
52  * @ingroup Examples
53  */
54 public class BarabasiAlbert<T> extends GraphPicker<T>
55 {
56  public static void main(String[] args)
57  {
59  new IntegerNodePicker(new AsInt(new Tick(0, 1))), new RandomInteger(15, 100), new RandomBoolean());
60  Node<Integer> root = generator.pick();
61 
62  /* Draw. We cannot show the graph, but the following instructions render it as
63  a Graphviz input file. We tweak the default settings for a better display. */
65  public String getLabel(Node<Integer> n) { return ""; }
66  public String getColor(Node<Integer> n) { return colorGradient((float) n.getChildren().size() / generator.getMaxDegree()); }
67  }.setNodeString("[style=\"filled\",width=0.1,height=0.1,shape=\"circle\"]");
68  gr.printToDot(System.out, root);
69  }
70 
72 
74  {
75  super(node_picker, size);
76  m_coin = coin;
77  }
78 
79  @Override
80  public Node<T> pick()
81  {
82  /* The graph starts with two connected nodes. */
83  Node<T> start_node1 = m_nodePicker.pick();
84  Node<T> start_node2 = m_nodePicker.pick();
85  connect(start_node1, start_node2);
86  m_nodes.add(start_node1);
87  m_nodes.add(start_node2);
88 
89  /* Add nodes until the desired size is reached. */
90  int sum_kj = 2, size = m_size.pick();
91  while (m_nodes.size() < size)
92  {
93  Node<T> new_n = m_nodePicker.pick();
94  m_nodes.add(new_n);
95  int added = 0;
96  for (Node<T> n : m_nodes)
97  {
98  int k_i = n.getChildren().size();
99  m_coin.tell((float) k_i / (float) sum_kj);
100  if (m_coin.pick())
101  {
102  connect(new_n, n);
103  added++;
104  }
105  }
106  sum_kj += 2 * added;
107  }
108  return start_node1;
109  }
110 
111  @Override
112  public Picker<Node<T>> duplicate(boolean with_state)
113  {
114  // Don't care for this example.
115  return null;
116  }
117 }
ca.uqac.lif.synthia.tree.GraphRenderer.printToDot
void printToDot(PrintStream ps, Node< T > n)
Definition: GraphRenderer.java:53
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
examples.graphs.BarabasiAlbert.BarabasiAlbert
BarabasiAlbert(Picker< Node< T >> node_picker, Picker< Integer > size, Reactive< Float, Boolean > coin)
Definition: BarabasiAlbert.java:73
examples.util.Utilities.colorGradient
static String colorGradient(float fraction)
Creates a simple 5-color gradient.
Definition: Utilities.java:123
ca.uqac.lif.synthia.util.AsInt
Utility picker that converts an input into an integer.
Definition: AsInt.java:28
examples.graphs.BarabasiAlbert.m_coin
Reactive< Float, Boolean > m_coin
Definition: BarabasiAlbert.java:71
ca.uqac.lif.synthia.tree.GraphPicker
Definition: GraphPicker.java:8
ca.uqac.lif.synthia.Reactive
Interface implemented by pickers whose picking of objects can be altered by external information.
Definition: Reactive.java:43
ca.uqac.lif.synthia.util
Miscellaneous pickers performing various functions.
Definition: ArrayPicker.java:19
examples.graphs.BarabasiAlbert.main
static void main(String[] args)
Definition: BarabasiAlbert.java:56
examples.graphs.BarabasiAlbert.duplicate
Picker< Node< T > > duplicate(boolean with_state)
Creates a copy of the picker.
Definition: BarabasiAlbert.java:112
examples.graphs.BarabasiAlbert
Generates a graph following the Barabási–Albert model.
Definition: BarabasiAlbert.java:54
ca.uqac.lif.synthia.tree.GraphPicker.m_nodePicker
Picker< Node< T > > m_nodePicker
Definition: GraphPicker.java:10
ca.uqac
examples.util
Definition: package-info.java:1
ca.uqac.lif.synthia.random
Pickers that produce pseudo-random objects such as numbers.
Definition: AffineTransform.java:19
ca.uqac.lif.synthia.tree.GraphPicker.m_nodes
Set< Node< T > > m_nodes
Definition: GraphPicker.java:14
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif.synthia.tree.IntegerNodePicker
Definition: IntegerNodePicker.java:5
ca.uqac.lif.synthia.tree.Node.getChildren
List< Node< T > > getChildren()
Gets the children of this node.
Definition: Node.java:69
ca.uqac.lif.synthia.tree
Pickers for the generation of trees made of nodes with labels.
Definition: ColoredNodePicker.java:19
examples.graphs.BarabasiAlbert.pick
Node< T > pick()
Picks an object.
Definition: BarabasiAlbert.java:80
examples.util.Utilities
Object providing a few utility methods to simplify the examples in this project.
Definition: Utilities.java:30
ca.uqac.lif.synthia.tree.GraphRenderer
Renders a tree of labeled nodes as a Graphviz input file.
Definition: GraphRenderer.java:33
ca.uqac.lif
ca.uqac.lif.synthia.tree.GraphPicker.connect
void connect(Node< T > n1, Node< T > n2)
Connects two nodes in a graph.
Definition: GraphPicker.java:29
ca.uqac.lif.synthia.random.RandomInteger
Picks an integer uniformly in an interval.
Definition: RandomInteger.java:31
ca.uqac.lif.synthia.util.Tick
Generates a sequence of monotonically increasing numerical values.
Definition: Tick.java:51
ca
ca.uqac.lif.synthia.Picker.pick
T pick()
Picks an object.
ca.uqac.lif.synthia.tree.GraphPicker.getMaxDegree
int getMaxDegree()
Gets the maximum out degree in a set of nodes in the last graph generated.
Definition: GraphPicker.java:50
ca.uqac.lif.synthia.Reactive.tell
void tell(U u)
Notifies a picker of some external information.
examples
ca.uqac.lif.synthia.tree.Node
Simple implementation of a labeled nodel.
Definition: Node.java:31
ca.uqac.lif.synthia.tree.GraphPicker.m_size
Picker< Integer > m_size
Definition: GraphPicker.java:12
ca.uqac.lif.synthia.random.RandomBoolean
Picks a Boolean value.
Definition: RandomBoolean.java:34