Class: Louvian::Community

Inherits:
Object
  • Object
show all
Defined in:
lib/louvian/community.rb

Constant Summary collapse

@@count =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adj_list, level) ⇒ Community

Returns a new instance of Community.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/louvian/community.rb', line 4

def initialize adj_list, level
  #puts "Adj list is "
  @id = @@count
  @@count+=1

  # TODO NO NEED TO SORT
  @nodes_ids = adj_list.keys.sort
  @level = level

  # sum of links weights inside the community
  #@in = adj_list.select {|k,v| nodes_ids.include? k}.inject(0) {|r,(k,v)| r+v.count}

  @in = 0
  adj_list.each do |node, neighbors|
    @in += neighbors.select {|node| @nodes_ids.include? node}.values.inject(0,:+)
  end

  # sum of links weights inside the community
  @tot = 0
  adj_list.each do |node, links|
    @tot += links.values.inject(0, :+)
  end

end

Instance Attribute Details

#idObject

Returns the value of attribute id.



2
3
4
# File 'lib/louvian/community.rb', line 2

def id
  @id
end

#inObject

Returns the value of attribute in.



2
3
4
# File 'lib/louvian/community.rb', line 2

def in
  @in
end

#nodes_idsObject

Returns the value of attribute nodes_ids.



2
3
4
# File 'lib/louvian/community.rb', line 2

def nodes_ids
  @nodes_ids
end

#totObject

Returns the value of attribute tot.



2
3
4
# File 'lib/louvian/community.rb', line 2

def tot
  @tot
end

Class Method Details

.resetObject



29
30
31
# File 'lib/louvian/community.rb', line 29

def self.reset
  @@count = 0
end

Instance Method Details

#insert(node, node_adj, links_from_community) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/louvian/community.rb', line 33

def insert node, node_adj, links_from_community
  links_to_comm = node_adj.select {|n| @nodes_ids.include? n}.values.inject(0,:+)

  @nodes_ids << node
  # Copied from the cpp code
  @in += links_to_comm + links_from_community + (node_adj[node] || 0)
  @tot += node_adj.values.inject(0,:+)

end

#remove(node, node_adj, links_from_community) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/louvian/community.rb', line 43

def remove node, node_adj, links_from_community
  @nodes_ids.delete node
  links_to_comm = node_adj.select {|n| @nodes_ids.include? n}.values.inject(0,:+)

  #puts "linksto t-com  #{links_to_comm}"
  # Copied from the cpp code
  @in -= (links_to_comm + links_from_community + (node_adj[node] || 0))
  @tot -= node_adj.values.inject(0,:+)

end