Algorithms For Building Compact Representatives And Processing Ranking Queries