Total Complexity | 5 |
Total Lines | 41 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | # frozen_string_literal: true |
||
6 | 1 | class BarbasiAlbertNetwork < Network |
|
7 | 1 | def initialize(params = {}) |
|
8 | 6 | super params |
|
9 | |||
10 | # We assume for now that m0 = m = 2 |
||
11 | |||
12 | 6 | create_entities |
|
13 | 6 | add_foreign_keys |
|
14 | end |
||
15 | |||
16 | 1 | private |
|
17 | |||
18 | # Create all the entities in the graph and their connections |
||
19 | # @return [void] |
||
20 | 1 | def create_entities |
|
|
|||
21 | # Add the initial one or two entities |
||
22 | 6 | @entities = [create_entity(0)] |
|
23 | 6 | return if @nodes_nb == 1 |
|
24 | |||
25 | 6 | @entities << create_entity(1) |
|
26 | 6 | add_link 0, 1 |
|
27 | 6 | return if @nodes_nb == 2 |
|
28 | |||
29 | 6 | @entities << create_entity(2) |
|
30 | 6 | add_link 2, 0 |
|
31 | 6 | add_link 2, 1 |
|
32 | 6 | return if @nodes_nb == 3 |
|
33 | |||
34 | # Add and connect more entities as needed |
||
35 | 6 | 3.upto(@nodes_nb - 1).each do |node| |
|
36 | 42 | @entities << create_entity(node) |
|
37 | 42 | pick = Pickup.new(0.upto(node - 1).to_a, |
|
38 | 174 | key_func: ->(n) { n }, |
|
39 | 426 | weight_func: ->(n) { @neighbours[n].size }, |
|
40 | uniq: true) |
||
41 | 42 | pick.pick(2).each do |node2| |
|
42 | 84 | add_link node, node2 |
|
43 | end |
||
44 | end |
||
45 | end |
||
46 | end |
||
47 | end |
||
49 |