Class: Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/rgraph/graph.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(csv) ⇒ Graph



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rgraph/graph.rb', line 14

def initialize(csv)
  @nodes = []
  @links = []
  @distance = nil
  @infinity = 100_000

  csv = CSV.new(File.open(csv), headers: true) if csv.is_a?(String)

  csv.each do |row|
    #last because CSV#delete returns [column,value]
    source_id = row.delete('source').last
    target_id = row.delete('target').last
    type = row.delete('type').last || 'undirected'
    weight = row.delete('weight').last || 1

    source = get_node_by_id(source_id) || Node.new(id: source_id)
    target = get_node_by_id(target_id) || Node.new(id: target_id)

    maybe_add_to_nodes(source, target)

    @links << Link.new(source: source, target: target, weight: weight, type: type)
  end
end

Instance Attribute Details

#infinityObject

Returns the value of attribute infinity.



7
8
9
# File 'lib/rgraph/graph.rb', line 7

def infinity
  @infinity
end

Returns the value of attribute links.



7
8
9
# File 'lib/rgraph/graph.rb', line 7

def links
  @links
end

#nodesObject

Returns the value of attribute nodes.



7
8
9
# File 'lib/rgraph/graph.rb', line 7

def nodes
  @nodes
end

Class Method Details

.new_from_string(string) ⇒ Object



9
10
11
12
# File 'lib/rgraph/graph.rb', line 9

def self.new_from_string(string)
  csv = CSV.new(string, headers: true)
  new(csv)
end

Instance Method Details

#average_degreeObject



50
51
52
# File 'lib/rgraph/graph.rb', line 50

def average_degree
  degrees.inject(:+) / @nodes.size.to_f
end

#between(i, args = {}) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/rgraph/graph.rb', line 150

def between(i, args = {})
  giant = args[:giant] || false
  shortest_paths(multiples: true, giant: giant)

  n = @shortest_paths.select{|c| c[1..-2].include?(i)}.size.to_f
  m = @shortest_paths.size.to_f
  n / m
end

#betweenness(args = {}) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rgraph/graph.rb', line 159

def betweenness(args = {})
  cumulative = args[:cumulative] || false
  giant = args[:giant] || false

  #If we ask cumulative, normalized must be true
  normalized = args[:normalized] || cumulative || false

  bts = 0.upto(@nodes.size - 1).map { |i| between(i, args) }

  if normalized
    max = bts.max
    min = bts.min

    bts.map!{|bt| (bt - min) / (max - min)}
  end

  if cumulative
    bts.uniq.sort.map{ |bt1| [bts.select{|bt2| bt2 >= bt1}.count, bt1] }
  else
    bts
  end
end

#closenessObject



194
195
196
# File 'lib/rgraph/graph.rb', line 194

def closeness
  farness.map{|f| 1.0 / f }
end

#clusteringObject



206
207
208
# File 'lib/rgraph/graph.rb', line 206

def clustering
  nodes.map{ |node| single_clustering(node) }.inject(:+) / nodes.size.to_f
end

#componentsObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/rgraph/graph.rb', line 221

def components
  distances unless @distance

  @components = []

  @distance.each do |d|
    same = []
    d.each_with_index { |dist, i| same << @nodes[i] if dist != @infinity}

    #its a new component
    if @components.select{ |c| c.include?(same.first) }.size == 0
      @components << same
    end
  end
  @components
end

#cumulative_degreeObject



54
55
56
57
# File 'lib/rgraph/graph.rb', line 54

def cumulative_degree
  out = (0..(degrees.max - 1)).map { |i| degrees.select{|degree| degree > i}.count }
  out.map{|a| a / out.max.to_f}
end

#degreesObject



46
47
48
# File 'lib/rgraph/graph.rb', line 46

def degrees
  @nodes.map{|node| node.degree}
end

#diameterObject



182
183
184
185
186
# File 'lib/rgraph/graph.rb', line 182

def diameter
  distances unless @distance

  (distances.flatten - [@infinity]).max
end

#distancesObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rgraph/graph.rb', line 73

def distances
  @path = matrix(@nodes.size, [nil])

  @distance = idistance
  @distance.each_index do |k|
    @distance.each_index do |i|
      @distance.each_index do |j|
        new_dist = @distance[i][k] + @distance[k][j]
        if @distance[i][j] > new_dist
          @distance[i][j] = new_dist
          @path[i][j] = [k]
        end
        if @distance[i][j] == new_dist && !(@path[i][j].include? k) && ! [i,j].include?(k)
          @path[i][j] << k
        end
      end
    end
  end
  @distance
end


42
43
44
# File 'lib/rgraph/graph.rb', line 42

def each_link(&block)
  @links.each(&block)
end

#each_node(&block) ⇒ Object



38
39
40
# File 'lib/rgraph/graph.rb', line 38

def each_node(&block)
  @nodes.each(&block)
end

#farnessObject



188
189
190
191
192
# File 'lib/rgraph/graph.rb', line 188

def farness
  distances unless @distance

  @distance.map{ |line| line.select{|l| l != @infinity }.inject(:+) }
end

#giant_componentObject



238
239
240
241
242
243
244
245
246
247
# File 'lib/rgraph/graph.rb', line 238

def giant_component
  components unless @components

  @giant_component = []
  @components.each do |c|
    @giant_component = c if c.size > @giant_component.size
  end

  @giant_component
end

#idistanceObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rgraph/graph.rb', line 59

def idistance
  dist = matrix(@nodes.size, 0)

  sorted = @nodes.sort { |a,b| a.id <=> b.id}

  sorted.each_with_index do |n1, i|
    sorted.each_with_index do |n2, j|
      next if i == j
      dist[i][j] = n1.has_neighbour?(n2) ? link_between(n1, n2).weight.to_i : @infinity
    end
  end
  dist
end

#mdistanceObject



94
95
96
97
98
99
# File 'lib/rgraph/graph.rb', line 94

def mdistance
  distances unless @distance

  fdistance = @distance.flatten.select{|d| d != @infinity}
  fdistance.inject(:+) / fdistance.count.to_f
end

#page_rank(d = 0.85, e = 0.01) ⇒ Object



210
211
212
213
214
215
216
217
218
219
# File 'lib/rgraph/graph.rb', line 210

def page_rank(d = 0.85, e = 0.01)
  n = @nodes.count

  #Initial values
  @page_rank = Array.new(n, 1 / n.to_f)

  while true
    return @page_rank if @page_rank.inject(:+) - step_page_rank(d, e).inject(:+) < e
  end
end

#shortest_path(i, j, multiples = false, giant = false) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rgraph/graph.rb', line 121

def shortest_path(i, j, multiples = false, giant = false)
  nodes = giant ? @giant_component : @nodes

  return [] if @distance[i][j] == @infinity

  path = @path[i][j].count == 1 || multiples ? @path[i][j] : [@path[i][j].first]
  out = []

  path.each do |k|

    case k
    when @infinity
      out << []
    when nil
      out << [i,j]
    else
      i_k = shortest_path(i, k)
      k_j = shortest_path(k, j)

      i_k.each do |ik|
        k_j.each do |kj|
          out << ik[0..-2] + [k] + kj[1..-1]
        end
      end
    end
  end
  out
end

#shortest_paths(args = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rgraph/graph.rb', line 101

def shortest_paths(args = {})
  multiples = args[:multiples] || false
  giant = args[:giant] || false

  @shortest_paths = []

  distances
  giant_component

  nodes = giant ? @giant_component : @nodes

  0.upto(nodes.size - 1).each do |i|
    (i + 1).upto(nodes.size - 1).each do |j|
      @shortest_paths += shortest_path(i, j, multiples, giant)
    end
  end

  @shortest_paths
end

#single_clustering(node) ⇒ Object



198
199
200
201
202
203
204
# File 'lib/rgraph/graph.rb', line 198

def single_clustering(node)
  possible = possible_connections(node)
  return 0 if possible == 0

  existent = node.neighbours.combination(2).select{ |t| t[0].neighbours.include?(t[1]) }.count
  existent / possible.to_f
end