Szemeredi's regularity lemma and its applications to pairwise clustering and segmentation