A link that adds nothing
transcript
Links arrive one at a time. Most of them join two different groups. Each merge makes one group fewer. But one link tells you nothing new.
The links zero one, one two and two three arrive. Accounts zero to three are one group. Their root is zero. Account four is alone. Zero and three are joined by a chain. So there are two groups in all.
Now the link three and zero arrives. Find three gives root zero. Find zero gives root zero as well. Two finds, and the same answer. Equal roots. They were already connected.
Start the count at the number of accounts. Subtract one each time two groups merge. When the roots are equal, the count stays. A merge changes the count. A loop does not. That link closes a loop in the graph. In graph words, a loop is a cycle.
Union-find spots it the moment it arrives. It works for undirected graphs only. Full breakdown in the lecture.