Trivially improve distance_hierarchy
dkl9

dkl9 commited on 2025-190 04:12:28
Showing 1 changed files, with 3 additions and 2 deletions.

... ...
@@ -82,7 +82,7 @@ def cachey_nearest(distances: DistTable) -> IndSeq:
82 82
         options.remove(best)
83 83
     return seq
84 84
 
85
-# good results, sketchy on a couple trials
85
+# good results, sketchy on a couple trials: O(n^3)
86 86
 def cachey_maximin(distances: DistTable) -> IndSeq:
87 87
     seq = []
88 88
     options = set(range(len(distances)))
... ...
@@ -98,12 +98,13 @@ def cachey_maximin(distances: DistTable) -> IndSeq:
98 98
         options.remove(best)
99 99
     return seq
100 100
 
101
+# empirically O(n^2 log n), but confusingly so
101 102
 def distance_hierarchy(distances: DistTable) -> BinaryTree:
102 103
     n = len(distances)
103 104
     forest = [BinaryTree(i) for i in range(n)]
104 105
     p = None
105 106
     for (i, j) in sorted(
106
-        ((i, j) for i in range(n) for j in range(n)),
107
+        ((i, j) for i in range(n) for j in range(i)),
107 108
         key=lambda ij: distances[ij[0]][ij[1]]
108 109
     ):
109 110
         p = forest[i].merge(forest[j])
110 111