Class: Carmenere::SingleLinkage::Algorithm

Inherits:
Algorithm
  • Object
show all
Defined in:
lib/carmenere/singlelinkage/algorithm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_matrixObject (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

#nodesObject (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_clustersObject



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

#runObject

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