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

BarbasiAlbertNetwork   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 17.39%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 41
ccs 4
cts 23
cp 0.1739
rs 10
c 1
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
B create_entities() 0 26 4
A initialize() 0 8 1
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