Module: GraphMatching::Graph::Weighted::ClassMethods
- Defined in:
- lib/graph_matching/graph/weighted.rb
Overview
no-doc
Instance Method Summary collapse
-
#[](*args) ⇒ Object
‘.[]` is the recommended, convenient constructor for weighted graphs.
-
#assert_weighted_edges(ary) ⇒ Object
‘assert_weighted_edges` asserts that `ary` is an array whose elements are all arrays of exactly three elements.
-
#weighted_edge?(e) ⇒ Boolean
‘weighted_edge?` returns true if `e` is an array whose first two elements are integers, and whose third element is a real number.
Instance Method Details
#[](*args) ⇒ Object
‘.[]` 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.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# 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) ⇒ Object
‘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)
59 60 61 62 |
# 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) ⇒ Boolean
‘weighted_edge?` returns true if `e` is an array whose first two elements are integers, and whose third element is a real number.
67 68 69 70 71 72 |
# 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 |