Completed
Push — mongo-graph ( a5e079...6fc2a5 )
by Michael
03:53
created

BarbasiAlbertNetwork.initialize()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
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 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
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 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
48
end
49