What's the smart way to solve it? The logic I think is necessary is: Every alloy ID should have a list of alloy IDs that are weaker than it. For every pair of alloys N and M, where N is stronger than M, update list N to include alloy M, update any list K that includes N to also include M, update any list J that includes K to also include M, repeat as necessary. Is there a simpler way to do it?