lib/rgraph/graph.rb in rgraph-0.0.14 vs lib/rgraph/graph.rb in rgraph-0.0.15

- old
+ new

@@ -98,24 +98,30 @@ fdistance.inject(:+) / fdistance.count.to_f end def shortest_paths(args = {}) multiples = args[:multiples] || false + giant = args[:giant] || false + @shortest_paths = [] distances + giant_component - 0.upto(@nodes.size - 1).each do |i| - (i + 1).upto(@nodes.size - 1).each do |j| - @shortest_paths += shortest_path(i, j, multiples) + 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 - def shortest_path(i, j, multiples = false) + 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 = [] @@ -139,25 +145,27 @@ end end out end - def between(i) - shortest_paths(multiples: true) + 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 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) } + bts = 0.upto(@nodes.size - 1).map { |i| between(i, args) } if normalized max = bts.max min = bts.min @@ -223,9 +231,20 @@ if @components.select{ |c| c.include?(same.first) }.size == 0 @components << same end end @components + end + + 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 private def step_page_rank(d = 0.85, e = 0.01)