Implement affinity_propagation(S, damping=0.9, max_iter=200), which clusters points by passing messages on a similarity matrix S of shape (n,n). S[i,k] is the similarity of point i to point k and the diagonal S[k,k] is the preference of k to be an exemplar.
Keep a responsibility matrix R and an availability matrix A, both initialised to zeros. For exactly max_iter iterations, update R first and then A (using the freshly updated R):
- Responsibility, for all i,k: rnew(i,k)=S(i,k)−maxk′=k[A(i,k′)+S(i,k′)]
- Availability, for i=k: anew(i,k)=min(0,R(k,k)+∑i′∈/{i,k}max(0,R(i′,k)))
- Self-availability: anew(k,k)=∑i′=kmax(0,R(i′,k))
- Damping with λ =
damping, applied to each matrix: R←λR+(1−λ)Rnew, likewise A←λA+(1−λ)Anew
After the loop, point k is an exemplar if R(k,k)+A(k,k)>0. If no point qualifies, the single exemplar is the point with the largest R(k,k)+A(k,k). Each exemplar is labelled with its own index; every other point i is labelled with the exemplar k maximising S[i,k] (lowest index on ties).
Return the labels as a list of n ints. For n=1, return [0].