Algorithm to sort a set of tuples based on linked pairs....