adds color information for isValidPair() function, optimizes the number of pairs by removing cycles in pair adjacency matrix