Class: Carmenere::SingleLinkage::Algorithm
- Defined in:
- lib/carmenere/singlelinkage/algorithm.rb
Instance Attribute Summary collapse
-
#distance_matrix ⇒ Object
readonly
Returns the value of attribute distance_matrix.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
Instance Method Summary collapse
- #closest_clusters ⇒ Object
-
#initialize(k, nodes) ⇒ Algorithm
constructor
A new instance of Algorithm.
-
#run ⇒ Object
If step is true, then yields a copy of the array of clusters.
Constructor Details
#initialize(k, nodes) ⇒ Algorithm
Returns a new instance of Algorithm.
9 10 11 |
# File 'lib/carmenere/singlelinkage/algorithm.rb', line 9 def initialize k, nodes super k, nodes end |
Instance Attribute Details
#distance_matrix ⇒ Object (readonly)
Returns the value of attribute distance_matrix.
7 8 9 |
# File 'lib/carmenere/singlelinkage/algorithm.rb', line 7 def distance_matrix @distance_matrix end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
5 6 7 |
# File 'lib/carmenere/singlelinkage/algorithm.rb', line 5 def nodes @nodes end |
Instance Method Details
#closest_clusters ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/carmenere/singlelinkage/algorithm.rb', line 25 def closest_clusters @clusters.reduce(nil) do |m, i| @clusters.reduce(m) do |m, j| d = i.distance j m = if i != j and (m.nil? or m[2] > d) [i, j, d] else m end end end end |
#run ⇒ Object
If step is true, then yields a copy of the array of clusters
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/carmenere/singlelinkage/algorithm.rb', line 14 def run (nodes.size - @k).times do a, b = self.closest_clusters @clusters.delete a @clusters.delete b @clusters.add Cluster.new(a | b) yield @clusters.to_a if block_given? end @clusters.to_a end |