@@ -74,8 +74,8 @@ cpdef cnp.ndarray[MST_edge_t, ndim=1, mode='c'] mst_from_mutual_reachability(
74
74
Returns
75
75
-------
76
76
mst : ndarray of shape (n_samples - 1,), dtype=MST_edge_dtype
77
- The MST representation of the mutual-reahability graph. The MST is
78
- represented as a collecteion of edges.
77
+ The MST representation of the mutual-reachability graph. The MST is
78
+ represented as a collection of edges.
79
79
"""
80
80
cdef:
81
81
# Note: we utilize ndarray's over memory-views to make use of numpy
@@ -136,8 +136,8 @@ cpdef cnp.ndarray[MST_edge_t, ndim=1, mode='c'] mst_from_data_matrix(
136
136
Returns
137
137
-------
138
138
mst : ndarray of shape (n_samples - 1,), dtype=MST_edge_dtype
139
- The MST representation of the mutual-reahability graph. The MST is
140
- represented as a collecteion of edges.
139
+ The MST representation of the mutual-reachability graph. The MST is
140
+ represented as a collection of edges.
141
141
"""
142
142
143
143
cdef:
@@ -163,6 +163,8 @@ cpdef cnp.ndarray[MST_edge_t, ndim=1, mode='c'] mst_from_data_matrix(
163
163
164
164
current_node = 0
165
165
166
+ # The following loop dynamically updates minimum reachability node-by-node,
167
+ # avoiding unnecessary computation where possible.
166
168
for i in range (0 , n_samples - 1 ):
167
169
168
170
in_tree[current_node] = 1
@@ -194,25 +196,27 @@ cpdef cnp.ndarray[MST_edge_t, ndim=1, mode='c'] mst_from_data_matrix(
194
196
next_node_core_dist,
195
197
pair_distance
196
198
)
197
- if mutual_reachability_distance > next_node_min_reach:
198
- if next_node_min_reach < new_reachability:
199
- new_reachability = next_node_min_reach
200
- source_node = next_node_source
201
- new_node = j
202
- continue
203
199
200
+ # If MRD(i, j) is smaller than node j's min_reachability, we update
201
+ # node j's min_reachability for future reference.
204
202
if mutual_reachability_distance < next_node_min_reach:
205
203
min_reachability[j] = mutual_reachability_distance
206
204
current_sources[j] = current_node
205
+
206
+ # If MRD(i, j) is also smaller than node i's current
207
+ # min_reachability, we update and set their edge as the current
208
+ # MST edge candidate.
207
209
if mutual_reachability_distance < new_reachability:
208
210
new_reachability = mutual_reachability_distance
209
211
source_node = current_node
210
212
new_node = j
211
- else :
212
- if next_node_min_reach < new_reachability:
213
- new_reachability = next_node_min_reach
214
- source_node = next_node_source
215
- new_node = j
213
+
214
+ # If the node j is closer to another node already in the tree, we
215
+ # make their edge the current MST candidate edge.
216
+ elif next_node_min_reach < new_reachability:
217
+ new_reachability = next_node_min_reach
218
+ source_node = next_node_source
219
+ new_node = j
216
220
217
221
mst[i].current_node = source_node
218
222
mst[i].next_node = new_node
@@ -227,8 +231,8 @@ cpdef cnp.ndarray[HIERARCHY_t, ndim=1, mode="c"] make_single_linkage(const MST_e
227
231
Parameters
228
232
----------
229
233
mst : ndarray of shape (n_samples - 1,), dtype=MST_edge_dtype
230
- The MST representation of the mutual-reahability graph. The MST is
231
- represented as a collecteion of edges.
234
+ The MST representation of the mutual-reachability graph. The MST is
235
+ represented as a collection of edges.
232
236
233
237
Returns
234
238
-------
0 commit comments