Class BarabasiAlbertGenerator
- All Implemented Interfaces:
Generator
,org.graphstream.stream.Source
public class BarabasiAlbertGenerator extends BaseGenerator
This is a very simple graph generator that generates a graph using the preferential attachment rule defined in the Barabási-Albert model: nodes are generated one by one, and each time attached by one or more edges other nodes. The other nodes are chosen using a biased random selection giving more chance to a node if it has a high degree.
Usage
The more this generator is iterated, the more nodes are generated. It can
therefore generate graphs of any size. One node is generated at each call to
nextEvents()
. At each node added at least one new edge is added. The
number of edges added at each step is given by the
getMaxLinksPerStep()
. However by default the generator creates a
number of edges per new node chosen randomly between 1 and
getMaxLinksPerStep()
. To have exactly this number of edges at each
new node, use setExactlyMaxLinksPerStep(boolean)
.
Complexity
For each new step, the algorithm act in O(n) with n the number of nodes if 1 max edge per new node is created, else the complexity is O(nm) if m max edge per new node is created.Example
Graph graph = new SingleGraph("Barabàsi-Albert"); // Between 1 and 3 new links per node added. Generator gen = new BarabasiAlbertGenerator(3); // Generate 100 nodes: gen.addSink(graph); gen.begin(); for(int i=0; i<100; i++) { gen.nextEvents(); } gen.end(); graph.display();
- Scientific Reference :
- Albert-László Barabási & Réka Albert "Emergence of scaling in random networks", Science 286: 509–512. October 1999. doi:10.1126/science.286.5439.509.
-
Nested Class Summary
-
Constructor Summary
Constructors Constructor Description BarabasiAlbertGenerator()
New generator.BarabasiAlbertGenerator(int maxLinksPerStep)
BarabasiAlbertGenerator(int maxLinksPerStep, boolean exactlyMaxLinksPerStep)
-
Method Summary
Modifier and Type Method Description void
begin()
Start the generator.void
end()
Clean degrees.int
getMaxLinksPerStep()
Maximum number of edges created when a new node is added.boolean
nextEvents()
Step of the generator.boolean
produceExactlyMaxLinkPerStep()
True if the generator produce exactlygetMaxLinksPerStep()
, else it produce a random number of links ranging between 1 andgetMaxLinksPerStep()
.void
setExactlyMaxLinksPerStep(boolean on)
Set if the generator produce exactlygetMaxLinksPerStep()
(true), else it produce a random number of links ranging between 1 andgetMaxLinksPerStep()
(false).void
setMaxLinksPerStep(int max)
Set how many edge (maximum) to create for each new node added.Methods inherited from class org.graphstream.algorithm.generator.BaseGenerator
addEdgeAttribute, addEdgeAttribute, addEdgeAttribute, addEdgeLabels, addNodeAttribute, addNodeAttribute, addNodeAttribute, addNodeLabels, isUsingInternalGraph, removeEdgeAttribute, removeNodeAttribute, setDirectedEdges, setRandomSeed, setUseInternalGraph
Methods inherited from class org.graphstream.stream.SourceBase
addAttributeSink, addElementSink, addSink, attributeSinks, clearAttributeSinks, clearElementSinks, clearSinks, elementSinks, removeAttributeSink, removeElementSink, removeSink, sendAttributeChangedEvent, sendAttributeChangedEvent, sendEdgeAdded, sendEdgeAdded, sendEdgeAttributeAdded, sendEdgeAttributeAdded, sendEdgeAttributeChanged, sendEdgeAttributeChanged, sendEdgeAttributeRemoved, sendEdgeAttributeRemoved, sendEdgeRemoved, sendEdgeRemoved, sendGraphAttributeAdded, sendGraphAttributeAdded, sendGraphAttributeChanged, sendGraphAttributeChanged, sendGraphAttributeRemoved, sendGraphAttributeRemoved, sendGraphCleared, sendGraphCleared, sendNodeAdded, sendNodeAdded, sendNodeAttributeAdded, sendNodeAttributeAdded, sendNodeAttributeChanged, sendNodeAttributeChanged, sendNodeAttributeRemoved, sendNodeAttributeRemoved, sendNodeRemoved, sendNodeRemoved, sendStepBegins, sendStepBegins
-
Constructor Details
-
BarabasiAlbertGenerator
public BarabasiAlbertGenerator()New generator. -
BarabasiAlbertGenerator
public BarabasiAlbertGenerator(int maxLinksPerStep) -
BarabasiAlbertGenerator
public BarabasiAlbertGenerator(int maxLinksPerStep, boolean exactlyMaxLinksPerStep)
-
-
Method Details
-
getMaxLinksPerStep
public int getMaxLinksPerStep()Maximum number of edges created when a new node is added.- Returns:
- The maximum number of links per step.
-
produceExactlyMaxLinkPerStep
public boolean produceExactlyMaxLinkPerStep()True if the generator produce exactlygetMaxLinksPerStep()
, else it produce a random number of links ranging between 1 andgetMaxLinksPerStep()
.- Returns:
- Does the generator generates exactly
getMaxLinksPerStep()
.
-
setMaxLinksPerStep
public void setMaxLinksPerStep(int max)Set how many edge (maximum) to create for each new node added.- Parameters:
max
- The new maximum, it must be strictly greater than zero.
-
setExactlyMaxLinksPerStep
public void setExactlyMaxLinksPerStep(boolean on)Set if the generator produce exactlygetMaxLinksPerStep()
(true), else it produce a random number of links ranging between 1 andgetMaxLinksPerStep()
(false).- Parameters:
on
- Does the generator generates exactlygetMaxLinksPerStep()
.
-
begin
public void begin()Start the generator. Two nodes connected by edge are added.- See Also:
Generator.begin()
-
nextEvents
public boolean nextEvents()Step of the generator. Add a node and try to connect it with some others. The number of links is randomly chosen between 1 and the maximum number of links per step specified insetMaxLinksPerStep(int)
. The complexity of this method is O(n) with n the number of nodes if the number of edges created per new node is 1, else it is O(nm) with m the number of edges generated per node.- Returns:
- true while there are elements to add to the graph.
- See Also:
Generator.nextEvents()
-
end
public void end()Clean degrees.- Specified by:
end
in interfaceGenerator
- Overrides:
end
in classBaseGenerator
- See Also:
Generator.end()
-