class FloydWarshall::Optimizer

Attributes

matrix[RW]

Public Class Methods

new(matrix) click to toggle source
# File lib/floyd_warshall.rb, line 9
def initialize(matrix)
  @matrix = matrix
end

Public Instance Methods

run() click to toggle source
# File lib/floyd_warshall.rb, line 13
def run
  matrices = [matrix]
  nodes.each_with_index do |iter, k|
    matrices[k] = matrices[k-1].clone
    nodes.each_with_index do |from, i|
      nodes.each_with_index do |to, j|
        prev = matrices[k-1]
        dist = [prev.get(from, to), prev.get(from, iter) + prev.get(iter, to)].min
        matrices[k].set(from, to, dist)
      end
    end
  end
  return matrices.last
end

Private Instance Methods

nodes() click to toggle source
# File lib/floyd_warshall.rb, line 30
def nodes
  matrix.nodes
end