-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcHierarchical.cls
2238 lines (1952 loc) · 71.7 KB
/
cHierarchical.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "cHierarchical"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
'Main Reference:
'1. "Modern Hierarchical, agglomerative clustering algorithms", Daniel Mullner (2011)
'2. "dendsort: modular leaf ordering methods for dendogram representaions in R", Ryo Sakai (2014)
'3. "Fast optimal leaf ordering for hierarchical clustering", Ziv Bar-Joseph (2001)
Private pn_raw As Long 'size of original data
Private pZ() As Long 'First column is index of internal node, 2nd and 3rd columns are its children
Private pZ_height() As Double 'height of internal node
Private plabel As Variant 'label of leaf node
Private pleaf_order() As Long 'pleaf_order(i)=k means that leaf k is placed at the i-th position
Private psizes() As Long 'sizes of leaf+internal node
Private pxy_plot() As Double 'layout position of leaf+internal node
Private pparents() As Long 'parents of leaf+internal node
Private plink_type As String
Private pswapped_Z() As Long
Private pswapped_Z_height() As Double
Private pDistance() As Double
Sub Reset()
Erase pZ, pZ_height, plabel, pleaf_order, psizes, pxy_plot
Erase pparents, pDistance, pswapped_Z, pswapped_Z_height
End Sub
Public Property Get z() As Long()
z = pZ
End Property
Public Property Get Z_height() As Double()
Z_height = pZ_height
End Property
Public Property Get tree_height() As Double
tree_height = pZ_height(pn_raw - 1)
End Property
Public Property Get leaf_order() As Long()
leaf_order = pleaf_order
End Property
Public Property Get sizes() As Long()
sizes = psizes
End Property
Public Property Get Size() As Long
Size = pn_raw
End Property
Public Property Get xy_plot() As Double()
xy_plot = pxy_plot
End Property
Public Property Get link_type() As String
link_type = plink_type
End Property
Public Property Get leaves_list(i As Long) As Long()
Dim child_list() As Long
If i > pn_raw Then
Call Find_Children(i, child_list, 1)
Else
ReDim child_list(1 To 1)
child_list(1) = i
End If
leaves_list = child_list
End Property
'==========================================================
'Create dendrogram for distance matrix x(), specific
'alogirthm is chosen for each linkage method.
'==========================================================
Sub Build(x() As Double, Optional link_type As String = "AVERAGE", Optional label As Variant)
Dim i As Long, j As Long, k As Long, m As Long, n As Long, iterate As Long
pn_raw = UBound(x, 1)
pDistance = x
plink_type = UCase(link_type)
ReDim plabel(1 To pn_raw)
If IsMissing(label) = True Then
For i = 1 To pn_raw
plabel(i) = i
Next i
Else
For i = 1 To pn_raw
plabel(i) = label(i)
Next i
End If
ReDim pZ(1 To pn_raw - 1, 1 To 3)
ReDim pZ_height(1 To pn_raw - 1)
If plink_type = "MEDIAN" Or plink_type = "CENTROID" Then
Call Build_Generic(x)
'Call Build_Direct(x)
ElseIf plink_type = "AVERAGE" Or plink_type = "WARD" _
Or plink_type = "COMPLETE" Or plink_type = "WEIGHTED" Then
Call Build_NNChain(x)
ElseIf plink_type = "SINGLE" Then
Call Build_MSTLinkage(x)
End If
Call Find_Parents 'find parents of each node
Call Find_Sizes 'find sizes of each node
Call Calc_Leaf_Order 'get leafs ordering
End Sub
'Create a new node w that parents over u & v
Private Sub Merge_uv(u As Long, v As Long, w As Long, iterate As Long, _
w_height As Double, parent() As Long, height() As Double, node_size() As Long)
parent(u) = w
parent(v) = w
height(w) = w_height
node_size(w) = node_size(u) + node_size(v)
pZ_height(iterate) = w_height
pZ(iterate, 1) = w
If height(u) < height(v) Then
pZ(iterate, 2) = v
pZ(iterate, 3) = u
Else
pZ(iterate, 2) = u
pZ(iterate, 3) = v
End If
End Sub
'=== Direct Implementation of agglomoerative clustering
Private Sub Build_Direct(x() As Double)
Dim i As Long, j As Long, k As Long, m As Long, n As Long, iterate As Long
Dim ii As Long, jj As Long
Dim u As Long, v As Long, w As Long, new_node As Long
Dim distance() As Double
Dim tmp_min As Double, temp As Double
Dim tmp_x As Double, tmp_y As Double, tmp_vec() As Double
Dim height() As Double
Dim node_size() As Long, parent() As Long
Dim node_ptr() As Long, u_addr As Long, v_addr As Long, INFINITY As Double
INFINITY = Exp(70)
distance = x 'Reuse index in distance matrix...
ReDim node_ptr(1 To pn_raw) '...node_ptr(i)=k means row/column i refers to node k
ReDim parent(1 To 2 * pn_raw - 1)
ReDim node_size(1 To 2 * pn_raw - 1)
ReDim height(1 To 2 * pn_raw - 1) 'Keep track of height of each node
For i = 1 To pn_raw
node_size(i) = 1
node_ptr(i) = i
Next i
'==========================
'Start iteration
'==========================
new_node = pn_raw
For iterate = 1 To pn_raw - 1
'Find closest pair of nodes
tmp_min = INFINITY
For i = 1 To pn_raw - 1
m = node_ptr(i)
If m > 0 Then
For j = i + 1 To pn_raw
n = node_ptr(j)
If n > 0 Then
If distance(i, j) < tmp_min Then
tmp_min = distance(i, j)
u = m: u_addr = i
v = n: v_addr = j
End If
End If
Next j
End If
Next i
'=== Create new node to join the pair
new_node = new_node + 1
Call Merge_uv(u, v, new_node, iterate, tmp_min, parent, height, node_size)
node_ptr(u_addr) = -1 'Destroy u and point v to new node
node_ptr(v_addr) = new_node
'=== Calculate distance of the new node to other nodes
ReDim tmp_vec(1 To pn_raw)
Select Case UCase(plink_type)
Case "CENTROID" 'Centroid method
m = node_size(u)
n = node_size(v)
temp = m * n * (distance(u_addr, v_addr) / (m + n)) ^ 2
For w = 1 To pn_raw
If w <> u_addr And w <> v_addr And node_ptr(w) > 0 Then
tmp_x = distance(u_addr, w) ^ 2
tmp_y = distance(v_addr, w) ^ 2
tmp_vec(w) = Sqr((m * tmp_x + n * tmp_y) / (m + n) - temp)
End If
Next w
Case "MEDIAN" 'Median method
temp = (distance(u_addr, v_addr) ^ 2) / 4
For w = 1 To pn_raw
If w <> u_addr And w <> v_addr And node_ptr(w) > 0 Then
tmp_x = distance(u_addr, w) ^ 2
tmp_y = distance(v_addr, w) ^ 2
tmp_vec(w) = Sqr((tmp_x + tmp_y) / 2 - temp)
End If
Next w
Case "AVERAGE" 'Average method
m = node_size(u)
n = node_size(v)
For w = 1 To pn_raw
If w <> u_addr And w <> v_addr And node_ptr(w) > 0 Then
tmp_x = distance(u_addr, w)
tmp_y = distance(v_addr, w)
tmp_vec(w) = (m * tmp_x + n * tmp_y) / (m + n)
End If
Next w
Case "WARD" 'Ward's Method
m = node_size(u)
n = node_size(v)
temp = distance(u_addr, v_addr) ^ 2
For w = 1 To pn_raw
If w <> u_addr And w <> v_addr And node_ptr(w) > 0 Then
k = node_size(node_ptr(w))
tmp_x = distance(u_addr, w) ^ 2
tmp_y = distance(v_addr, w) ^ 2
tmp_vec(w) = Sqr(((m + k) * tmp_x + (n + k) * tmp_y - k * temp) / (m + n + k))
End If
Next w
End Select
For i = 1 To pn_raw
distance(v_addr, i) = tmp_vec(i)
distance(i, v_addr) = tmp_vec(i)
Next i
'===========================================
If iterate Mod 10 = 0 Then
DoEvents
Application.StatusBar = pn_raw - new_node & " nodes remaining..."
End If
Next iterate
Erase distance, height, node_size, parent, node_ptr
Application.StatusBar = False
End Sub
'=== Generic linkage method from Daniel Mullner
Private Sub Build_Generic(x() As Double)
Dim i As Long, j As Long, k As Long, m As Long, n As Long, iterate As Long
Dim u As Long, v As Long, w As Long, new_node As Long
Dim distance() As Double
Dim tmp_x As Double, tmp_y As Double, tmp_min As Double, temp As Double
Dim node_id() As Long, node_size() As Long, iStack() As Long, n_nghbr() As Long
Dim height() As Double, mindist() As Double, tmp_vec() As Double
Dim qHeap As cHeap
Dim INFINITY As Double
INFINITY = Exp(70)
distance = x
ReDim iStack(0 To pn_raw)
ReDim node_size(1 To pn_raw)
ReDim node_id(1 To pn_raw)
ReDim height(1 To pn_raw)
For i = 1 To pn_raw
node_size(i) = 1
iStack(i) = i
node_id(i) = i
Next i
'Initialize candidate list of nearest pairs
ReDim n_nghbr(1 To pn_raw - 1)
ReDim mindist(1 To pn_raw - 1)
For i = 1 To pn_raw - 1
tmp_min = INFINITY: k = -1
For j = i + 1 To pn_raw
If distance(i, j) < tmp_min Then
tmp_min = distance(i, j)
k = j
End If
Next j
n_nghbr(i) = k
mindist(i) = tmp_min
Next i
'Put mindist into a min-Heap
Set qHeap = New cHeap
Call qHeap.Build(mindist, "MIN")
'Start iteration
new_node = pn_raw
For iterate = 1 To pn_raw - 1
'Peep closest pair (u,v)
Call qHeap.Top(tmp_min, u): v = n_nghbr(u)
'Verify that (u,v) is indeed the closest pair
Do While tmp_min <> distance(u, v)
tmp_min = INFINITY: k = -1
For j = 1 To UBound(iStack)
w = iStack(j)
If w > u Then
If distance(u, w) < tmp_min Then
tmp_min = distance(u, w)
k = w
End If
End If
Next j
n_nghbr(u) = k
mindist(u) = tmp_min
Call qHeap.Update(tmp_min, u)
Call qHeap.Top(tmp_min, u): v = n_nghbr(u)
Loop
'Pop closest pair from heap
Call qHeap.Pop_Min(tmp_min, u): v = n_nghbr(u)
'Merge u & v
new_node = new_node + 1
pZ_height(iterate) = tmp_min
pZ(iterate, 1) = new_node
If height(u) < height(v) Then
pZ(iterate, 2) = node_id(v)
pZ(iterate, 3) = node_id(u)
Else
pZ(iterate, 2) = node_id(u)
pZ(iterate, 3) = node_id(v)
End If
height(v) = tmp_min 'height of new node
node_id(v) = new_node 'reuse index v for new node...
Call Array_Remove(iStack, u) '...and destroy u
'Update distance of the new node to other nodes
ReDim tmp_vec(1 To UBound(iStack))
Select Case UCase(plink_type)
Case "CENTROID" 'Centroid method
m = node_size(u)
n = node_size(v)
temp = m * n * (distance(u, v) / (m + n)) ^ 2
For i = 1 To UBound(iStack)
w = iStack(i)
If w <> v Then
tmp_x = distance(u, w) ^ 2
tmp_y = distance(v, w) ^ 2
tmp_vec(i) = Sqr((m * tmp_x + n * tmp_y) / (m + n) - temp)
End If
Next i
Case "MEDIAN" 'Median method
temp = (distance(u, v) ^ 2) / 4
For i = 1 To UBound(iStack)
w = iStack(i)
If w <> v Then
tmp_x = distance(u, w) ^ 2
tmp_y = distance(v, w) ^ 2
tmp_vec(i) = Sqr((tmp_x + tmp_y) / 2 - temp)
End If
Next i
Case "AVERAGE" 'Average method
m = node_size(u)
n = node_size(v)
For i = 1 To UBound(iStack)
w = iStack(i)
If w <> v Then
tmp_x = distance(u, w)
tmp_y = distance(v, w)
tmp_vec(i) = (m * tmp_x + n * tmp_y) / (m + n)
End If
Next i
Case "WARD" 'Average method
m = node_size(u)
n = node_size(v)
temp = distance(u, v) ^ 2
For i = 1 To UBound(iStack)
w = iStack(i)
If w <> v Then
k = node_size(w)
tmp_x = distance(u, w) ^ 2
tmp_y = distance(v, w) ^ 2
tmp_vec(i) = Sqr(((m + k) * tmp_x + (n + k) * tmp_y - k * temp) / (m + n + k))
End If
Next i
End Select
For i = 1 To UBound(iStack)
w = iStack(i)
distance(w, v) = tmp_vec(i)
distance(v, w) = tmp_vec(i)
Next i
Erase tmp_vec
'Update size of new node
node_size(v) = node_size(u) + node_size(v)
'Reference to u should now point to v
For i = 1 To UBound(iStack)
w = iStack(i)
If w < u Then
If n_nghbr(w) = u Then
n_nghbr(w) = v
End If
End If
Next i
'For each node check if new node is a nearer neighbor
For i = 1 To UBound(iStack)
w = iStack(i)
If w < v Then
tmp_x = distance(w, v)
If tmp_x < mindist(w) Then
n_nghbr(w) = v
mindist(w) = tmp_x
Call qHeap.Update(tmp_x, w)
End If
End If
Next i
'Find nearest neighbor candidate of new node
If v < pn_raw Then
tmp_min = INFINITY: k = -1
For i = 1 To UBound(iStack)
w = iStack(i)
If w > v Then
If distance(w, v) < tmp_min Then
tmp_min = distance(w, v)
k = w
End If
End If
Next i
n_nghbr(v) = k
mindist(v) = tmp_min
Call qHeap.Update(tmp_min, v)
End If
If iterate Mod 50 = 0 Then
DoEvents
Application.StatusBar = pn_raw - iterate & " nodes remaining..."
End If
Next iterate
Call qHeap.Reset
Set qHeap = Nothing
Erase distance, height, node_size, iStack, n_nghbr, mindist
Application.StatusBar = False
End Sub
'=== Using Nearest Neighbour Chain algorithm to speed up clustering
Private Sub Build_NNChain(x() As Double)
Dim i As Long, j As Long, m As Long, n As Long, k As Long
Dim iterate As Long
Dim distance() As Double
Dim tmp_min As Double, tmp_max As Double, temp As Double
Dim tmp_x As Double, tmp_y As Double
Dim u As Long, v As Long, w As Long, new_node As Long
Dim tmp_u As Long, tmp_v As Long
Dim parent() As Long
Dim height() As Double
Dim node_size() As Long
Dim iStack() As Long, iChain() As Long
ReDim distance(1 To 2 * pn_raw - 1, 1 To 2 * pn_raw - 1)
For i = 1 To pn_raw - 1
For j = i + 1 To pn_raw
distance(i, j) = x(i, j)
distance(j, i) = x(i, j)
Next j
Next i
ReDim node_size(1 To 2 * pn_raw - 1)
ReDim parent(1 To 2 * pn_raw - 1)
ReDim height(1 To 2 * pn_raw - 1)
For i = 1 To pn_raw
node_size(i) = 1
Next i
'=== Using Nearest Neighbour Chain algorithm to speed up clustering
'=== Start Adding internal nodes for most similar pairs
ReDim iStack(0 To 0)
ReDim iChain(0 To 0)
For i = 1 To pn_raw
Call Array_Push(iStack, i)
Next i
iterate = 0
new_node = pn_raw
Do While UBound(iStack) > 1
iterate = iterate + 1
If UBound(iChain) <= 3 Then
ReDim iChain(0 To 0)
u = iStack(1)
v = iStack(2)
Call Array_Push(iChain, u)
Else
u = iChain(UBound(iChain) - 3)
v = iChain(UBound(iChain) - 2)
For i = 1 To 3
Call Array_Pop(iChain)
Next i
End If
k = 0
Do
'=== Find pair of minimum dissimilarity
tmp_min = 99999999
If parent(v) = 0 Then
w = v
tmp_min = distance(u, v)
End If
For i = 1 To UBound(iStack)
tmp_u = iStack(i)
If tmp_u <> u Then
If distance(u, tmp_u) < tmp_min Then
tmp_min = distance(u, tmp_u)
w = tmp_u
End If
End If
Next i
'==============================================
v = u
u = w
Call Array_Push(iChain, u)
If UBound(iChain) >= 3 Then
If u = iChain(UBound(iChain) - 2) Then k = 1
End If
Loop Until k = 1
'=== Attributes of the new vertex
new_node = new_node + 1
Call Merge_uv(u, v, new_node, iterate, tmp_min, parent, height, node_size)
'===========================================
Call Array_Remove(iStack, u)
Call Array_Remove(iStack, v)
'=== Calculate the distance of the new vertex to other vertices
Select Case UCase(plink_type)
Case "AVERAGE" 'Average Linkage
m = node_size(u)
n = node_size(v)
For i = 1 To UBound(iStack)
w = iStack(i)
tmp_x = distance(u, w)
tmp_y = distance(v, w)
distance(new_node, w) = (m * tmp_x + n * tmp_y) / (m + n)
distance(w, new_node) = distance(new_node, w)
Next i
Case "WEIGHTED" 'Average Linkage
For i = 1 To UBound(iStack)
w = iStack(i)
tmp_x = distance(u, w)
tmp_y = distance(v, w)
distance(new_node, w) = (tmp_x + tmp_y) / 2
distance(w, new_node) = distance(new_node, w)
Next i
Case "COMPLETE" 'Complete Linkage
For i = 1 To UBound(iStack)
w = iStack(i)
distance(new_node, w) = distance(u, w)
tmp_x = distance(v, w)
If tmp_x > distance(new_node, w) Then distance(new_node, w) = tmp_x
distance(w, new_node) = distance(new_node, w)
Next i
Case "SINGLE" 'Single Linkage
For i = 1 To UBound(iStack)
w = iStack(i)
distance(new_node, w) = distance(u, w)
tmp_x = distance(v, w)
If tmp_x < distance(new_node, w) Then distance(new_node, w) = tmp_x
distance(w, new_node) = distance(new_node, w)
Next i
Case "WARD" 'Ward's Method
m = node_size(u)
n = node_size(v)
temp = distance(u, v) ^ 2
For i = 1 To UBound(iStack)
w = iStack(i)
k = node_size(w)
tmp_x = distance(u, w) ^ 2
tmp_y = distance(v, w) ^ 2
distance(w, new_node) = Sqr(((m + k) * tmp_x + (n + k) * tmp_y - k * temp) / (m + n + k))
distance(new_node, w) = distance(w, new_node)
Next i
End Select
'===========================================
Call Array_Push(iStack, new_node)
If iterate Mod 10 = 0 Then
DoEvents
Application.StatusBar = UBound(iStack) & " nodes remaining..."
End If
Loop
Erase distance, node_size, height, parent
Call sort_tree 're-index Z() in order of increasing height
Application.StatusBar = False
End Sub
'=== MST alogrithm used only for single linkage
Private Sub Build_MSTLinkage(x() As Double)
Dim i As Long, j As Long, k As Long, m As Long, n As Long, iterate As Long
Dim u As Long, v As Long, w As Long, new_node As Long
Dim tmp_min As Double, temp As Double
Dim tmp_x As Double, tmp_y As Double
Dim height() As Double
Dim dist() As Double, INFINITY As Double
Dim isProcess() As Long
INFINITY = Exp(70)
ReDim height(1 To 2 * pn_raw - 1) 'Keep track of height of each node
new_node = pn_raw
u = 1
ReDim dist(1 To pn_raw)
ReDim isProcess(1 To pn_raw)
For i = 1 To pn_raw
dist(i) = INFINITY
Next i
For iterate = 1 To pn_raw - 1
isProcess(u) = 1
For i = 1 To pn_raw
If isProcess(i) = 0 Then
If x(i, u) < dist(i) Then dist(i) = x(i, u)
End If
Next i
tmp_min = INFINITY: v = -1
For i = 1 To pn_raw
If isProcess(i) = 0 Then
If dist(i) < tmp_min Then
tmp_min = dist(i)
v = i
End If
End If
Next i
pZ_height(iterate) = tmp_min
pZ(iterate, 1) = pn_raw + iterate
pZ(iterate, 2) = u
pZ(iterate, 3) = v
u = v
If iterate Mod 10 = 0 Then
DoEvents
Application.StatusBar = pn_raw - new_node & " nodes remaining..."
End If
Next iterate
Erase dist
Call Relabel_Z
Application.StatusBar = False
End Sub
Private Sub Relabel_Z()
Dim i As Long, j As Long, n As Long, u As Long, v As Long, w As Long
Dim sort_index() As Long, Z_sorted() As Long, parent() As Long, nxt_label As Long
Dim tmp_x As Double
Call modMath.Sort_Bubble_A(pZ_height, sort_index)
ReDim Z_sorted(1 To pn_raw - 1, 1 To 2)
For i = 1 To pn_raw - 1
j = sort_index(i)
Z_sorted(i, 1) = pZ(j, 2)
Z_sorted(i, 2) = pZ(j, 3)
Next i
ReDim pZ(1 To pn_raw - 1, 1 To 3)
For i = 1 To pn_raw - 1
pZ(i, 1) = pn_raw + i
Next i
ReDim parent(1 To 2 * pn_raw - 1)
nxt_label = pn_raw + 1
For i = 1 To pn_raw - 1
u = Z_sorted(i, 1)
v = Z_sorted(i, 2)
'U.EFFICIENT_FIND(a)
w = u
Do While parent(u) <> 0
u = parent(u)
Loop
Do While parent(w) <> u And parent(w) <> 0
j = w
w = parent(w)
parent(j) = u
Loop
'U.EFFICIENT_FIND(b)
w = v
Do While parent(v) <> 0
v = parent(v)
Loop
Do While parent(w) <> v And parent(w) <> 0
j = w
w = parent(w)
parent(j) = v
Loop
'Append(U.EFFICIENT_FIND(a),U.EFFICIENT_FIND(b)) to L'
pZ(i, 2) = u
pZ(i, 3) = v
'U.UNION(a,b)
parent(u) = nxt_label
parent(v) = nxt_label
nxt_label = nxt_label + 1
Next i
Erase Z_sorted, parent, sort_index
End Sub
Private Sub Init(x() As Double, link_type As String, Optional label As Variant)
Dim i As Long
pn_raw = UBound(x, 1)
pDistance = x
plink_type = UCase(link_type)
ReDim plabel(1 To pn_raw)
If IsMissing(label) = True Then
For i = 1 To pn_raw
plabel(i) = i
Next i
Else
For i = 1 To pn_raw
plabel(i) = label(i)
Next i
End If
ReDim pZ(1 To pn_raw - 1, 1 To 3)
ReDim pZ_height(1 To pn_raw - 1)
End Sub
'=== Direct Implementation of agglomoerative clustering
'Input: label() is the string labels of the N data points
'Input: x() is the distance matrix(N by N), symmetric and zero diagonals
'Input: linkage can be "AVERAGE","COMPLETE","SINGLE","WARD"
Sub linkage(x() As Double, Optional link_type As String = "AVERAGE", Optional label As Variant)
Dim i As Long, j As Long, m As Long, n As Long, k As Long
Dim iterate As Long
Dim distance() As Double
Dim tmp_min As Double, temp As Double
Dim tmp_x As Double, tmp_y As Double
Dim u As Long, v As Long, w As Long, new_node As Long
Dim parent() As Long
Dim height() As Double
Dim node_size() As Long
If IsMissing(label) = True Then
Call Init(x, link_type)
Else
Call Init(x, link_type, label)
End If
ReDim distance(1 To 2 * pn_raw - 1, 1 To 2 * pn_raw - 1)
For i = 1 To pn_raw - 1
For j = i + 1 To pn_raw
distance(i, j) = x(i, j)
distance(j, i) = x(i, j)
Next j
Next i
ReDim node_size(1 To 2 * pn_raw - 1)
ReDim parent(1 To 2 * pn_raw - 1)
ReDim height(1 To 2 * pn_raw - 1) 'Keep track of height of each node
For i = 1 To pn_raw
node_size(i) = 1
Next i
'=== Direct Implementation of agglomoerative clustering
new_node = pn_raw
For iterate = 1 To pn_raw - 1
'Find closest pair of nodes
tmp_min = 999999999
For i = 1 To new_node - 1
For j = i + 1 To new_node
If parent(i) = 0 And parent(j) = 0 Then
If distance(i, j) < tmp_min Then
tmp_min = distance(i, j)
u = i
v = j
End If
End If
Next j
Next i
'=== Create new node to join the pair
new_node = new_node + 1
parent(u) = new_node
parent(v) = new_node
height(new_node) = tmp_min
node_size(new_node) = node_size(u) + node_size(v)
pZ_height(iterate) = tmp_min
pZ(iterate, 1) = new_node
If height(u) < height(v) Then
pZ(iterate, 2) = v
pZ(iterate, 3) = u
Else
pZ(iterate, 2) = u
pZ(iterate, 3) = v
End If
'===========================================
'=== Calculate the distance of the new node to other nodes
Select Case UCase(link_type)
Case "AVERAGE" 'Average Linkage
m = node_size(u)
n = node_size(v)
For w = 1 To new_node - 1
tmp_x = distance(u, w)
tmp_y = distance(v, w)
distance(new_node, w) = (m * tmp_x + n * tmp_y) / (m + n)
distance(w, new_node) = distance(new_node, w)
Next w
Case "WEIGHTED" 'Average Linkage
For w = 1 To new_node - 1
tmp_x = distance(u, w)
tmp_y = distance(v, w)
distance(new_node, w) = (tmp_x + tmp_y) / 2
distance(w, new_node) = distance(new_node, w)
Next w
Case "COMPLETE" 'Complete Linkage
For w = 1 To new_node - 1
distance(new_node, w) = distance(u, w)
tmp_x = distance(v, w)
If tmp_x > distance(new_node, w) Then distance(new_node, w) = tmp_x
distance(w, new_node) = distance(new_node, w)
Next w
Case "SINGLE" 'Single Linkage
For w = 1 To new_node - 1
distance(new_node, w) = distance(u, w)
tmp_x = distance(v, w)
If tmp_x < distance(new_node, w) Then distance(new_node, w) = tmp_x
distance(w, new_node) = distance(new_node, w)
Next w
Case "WARD" 'Ward's Method
m = node_size(u)
n = node_size(v)
temp = distance(u, v) ^ 2
For w = 1 To new_node - 1
k = node_size(w)
tmp_x = distance(u, w) ^ 2
tmp_y = distance(v, w) ^ 2
distance(w, new_node) = Sqr(((m + k) * tmp_x + (n + k) * tmp_y - k * temp) / (m + n + k))
distance(new_node, w) = distance(w, new_node)
Next w
Case "CENTROID" 'Centroid method
m = node_size(u)
n = node_size(v)
temp = m * n * (distance(u, v) / (m + n)) ^ 2
For w = 1 To new_node - 1
tmp_x = distance(u, w) ^ 2
tmp_y = distance(v, w) ^ 2
distance(w, new_node) = Sqr((m * tmp_x + n * tmp_y) / (m + n) - temp)
distance(new_node, w) = distance(w, new_node)
Next w
Case "MEDIAN" 'Centroid method
temp = (distance(u, v) ^ 2) / 4
For w = 1 To new_node - 1
tmp_x = distance(u, w) ^ 2
tmp_y = distance(v, w) ^ 2
distance(w, new_node) = Sqr((tmp_x + tmp_y) / 2 - temp)
distance(new_node, w) = distance(w, new_node)
Next w
End Select
'===========================================
If iterate Mod 10 = 0 Then
DoEvents
Application.StatusBar = pn_raw - new_node & " nodes remaining..."
End If
Next iterate
Erase distance, height, node_size, parent
Call Find_Parents 'find parents of each node
Call Find_Sizes 'find sizes of each node
Call Calc_Leaf_Order 'get leafs ordering
Application.StatusBar = False
End Sub
'=== Using Nearest Neighbour Chain algorithm to speed up clustering
'Input: label() is the string labels of the N data points
'Input: x() is the distance matrix(N by N), symmetric and zero diagonals
'Input: linkage can be "AVERAGE","COMPLETE","SINGLE","WARD"
Sub NNChainLinkage(x() As Double, Optional link_type As String = "AVERAGE", Optional label As Variant)
Dim i As Long, j As Long, m As Long, n As Long, k As Long
Dim iterate As Long
Dim distance() As Double
Dim tmp_min As Double, tmp_max As Double, temp As Double
Dim tmp_x As Double, tmp_y As Double
Dim u As Long, v As Long, w As Long, new_node As Long
Dim tmp_u As Long, tmp_v As Long
Dim parent() As Long
Dim height() As Double
Dim node_size() As Long
If IsMissing(label) = True Then
Call Init(x, link_type)
Else
Call Init(x, link_type, label)
End If
ReDim distance(1 To 2 * pn_raw - 1, 1 To 2 * pn_raw - 1)
For i = 1 To pn_raw - 1
For j = i + 1 To pn_raw
distance(i, j) = x(i, j)
distance(j, i) = x(i, j)
Next j
Next i
ReDim node_size(1 To 2 * pn_raw - 1)
ReDim parent(1 To 2 * pn_raw - 1)
ReDim height(1 To 2 * pn_raw - 1)
'=== Using Nearest Neighbour Chain algorithm to speed up clustering
'=== Start Adding internal nodes for most similar pairs
Dim iStack() As Long, iChain() As Long
ReDim iStack(0 To 0)
ReDim iChain(0 To 0)
For i = 1 To pn_raw
Call Array_Push(iStack, i)
node_size(i) = 1
Next i
iterate = 0
new_node = pn_raw
Do While UBound(iStack) > 1
iterate = iterate + 1
If UBound(iChain) <= 3 Then
ReDim iChain(0 To 0)
u = iStack(1)
v = iStack(2)
Call Array_Push(iChain, u)
Else
u = iChain(UBound(iChain) - 3)
v = iChain(UBound(iChain) - 2)
For i = 1 To 3
Call Array_Pop(iChain)
Next i
End If
k = 0
Do
'=== Find pair of minimum dissimilarity
tmp_min = 99999999
If parent(v) = 0 Then
w = v
tmp_min = distance(u, v)
End If
For i = 1 To UBound(iStack)
tmp_u = iStack(i)
If tmp_u <> u Then
If distance(u, tmp_u) < tmp_min Then
tmp_min = distance(u, tmp_u)
w = tmp_u
End If
End If
Next i
'==============================================