module GraphMatching::Graph::Weighted::ClassMethods

no-doc

Public Instance Methods

[](*args) click to toggle source

`.[]` is the recommended, convenient constructor for weighted graphs. Each argument should be an array with three integers; the first two represent the edge, the third, the weight.

Calls superclass method
# File lib/graph_matching/graph/weighted.rb, line 41
def [](*args)
  assert_weighted_edges(args)
  weightless_edges = args.map { |e| e.slice(0..1) }
  g = super(*weightless_edges.flatten)
  g.init_weights
  args.each do |edge|
    i = edge[0] - 1
    j = edge[1] - 1
    weight = edge[2]
    g.weight[i][j] = weight
    g.weight[j][i] = weight
  end
  g
end
assert_weighted_edges(ary) click to toggle source

`assert_weighted_edges` asserts that `ary` is an array whose elements are all arrays of exactly three elements. (The first two represent the edge, the third, the weight)

# File lib/graph_matching/graph/weighted.rb, line 59
def assert_weighted_edges(ary)
  return if ary.is_a?(Array) && ary.all?(&method(:weighted_edge?))
  raise 'Invalid array of weighted edges'
end
weighted_edge?(e) click to toggle source

`weighted_edge?` returns true if `e` is an array whose first two elements are integers, and whose third element is a real number.

# File lib/graph_matching/graph/weighted.rb, line 67
def weighted_edge?(e)
  e.is_a?(Array) &&
    e.length == 3 &&
    e[0, 2].all? { |i| i.is_a?(Integer) } &&
    e[2].is_a?(Numeric)
end