| Algorithm 2: The method for clustering |
| let T as the set of initial cluster centers; |
| calculate the number of initial cluster centers |
| Repeat |
| assign each remaining common node to the cluster with the nearest medoid; |
| randomly select a common sensor node ; |
| calculate the cost function of swapping node with ; |
| if S<0 then |
| swap with to form the new set of k clusters; |
| Until no change |
| Output: a set of k clusters. |