Completed
Push — mongo-graph ( 87a116...a5e079 )
by Michael
03:26
created

BarbasiAlbertNetwork.initialize()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.4218

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 8
ccs 1
cts 4
cp 0.25
crap 1.4218
rs 9.4285
c 0
b 0
f 0
1
# frozen_string_literal: true
2
3 1
module NoSE
4 1
  module Random
5
    # Generates a random graph using the Barbasi-Albert model
6 1
    class BarbasiAlbertNetwork < Network
7 1
      def initialize(params = {})
8
        super params
9
10
        # We assume for now that m0 = m = 2
11
12
        create_entities
13
        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
0 ignored issues
show
Coding Style introduced by
The Assignment, Branch, Condition size for create_entities is considered too high. [27.24/20]. The ABC size is based on assignments, branches (method calls), and conditions.
Loading history...
21
        # Add the initial one or two entities
22
        @entities = [create_entity(0)]
23
        return if @nodes_nb == 1
24
25
        @entities << create_entity(1)
26
        add_link 0, 1
27
        return if @nodes_nb == 2
28
29
        @entities << create_entity(2)
30
        add_link 2, 0
31
        add_link 2, 1
32
        return if @nodes_nb == 3
33
34
        # Add and connect more entities as needed
35
        3.upto(@nodes_nb - 1).each do |node|
36
          @entities << create_entity(node)
37
          pick = Pickup.new(0.upto(node - 1).to_a,
38
                            key_func: ->(n) { n },
39
                            weight_func: ->(n) { @neighbours[n].size },
40
                            uniq: true)
41
          pick.pick(2).each do |node2|
42
            add_link node, node2
43
          end
44
        end
45
      end
46
    end
47
  end
48
end
49