K-medoids represents each cluster by one of its own points, so it needs only an dissimilarity matrix D. Medoids are given as a list of point indices; the cost of a medoid set is . Assignments go to the nearest medoid, ties to the medoid listed first.
kmedoids_voronoi(D, init_medoids, max_iter): the Voronoi-iteration K-medoids algorithm. Repeat up to max_iter times: assign every point to its nearest medoid; then, for each cluster in order, replace by the member of cluster minimising (ties: smaller index). Stop early when no medoid changes. Return (medoids, labels, cost) with labels[n] the position of the assigned medoid in the medoid list, computed for the final medoids.
pam(D, init_medoids): the swap phase of partitioning around medoids. Repeat: for every medoid position (in order) and every non-medoid point (increasing index), compute the cost after replacing by ; if the best such swap (first one found on ties) strictly lowers the cost, apply it, otherwise stop. Return (medoids, labels, cost) as above.
Return medoids and labels as lists of ints and cost as a Python float.