I am using scikit-learn to run k-means. I looked the scikit-learn k-means code but I don't understand how k-means pre-computes distances in advance. Which distances k-means pre-computes in advance while it doesn't know in advance the values of the centers?
It does not pre-compute the distance between the centers, it precomputes the distance between a point, say X and all other points in the system and stores them for later use.
Check this line 619 in kmeans which calls _labels_inertia_precompute_dense
, which in turn calls pairwise_distances_argmin_min at line 562.
The documentation of pairwise_distances_argmin_min states that
Compute minimum distances between one point and a set of points. This function computes for each row in X, the index of the row of Y which is closest (according to the specified distance). The minimal distances are also returned.
So it does not need to know the centres, this is just used to precompute distances between all possible pair of points with each other.