I have a series of Key value pairs. Each Key has 2 values. Values of Keys may coincide.
Initially, I start with Key1, ValueKey1, ValueKey2 are stored in a set (maybe array).
Now for each consecutive Keyi, I check if Valuei and Valuei+1 are in the set. If any one of them is in the set, then join the set.
Else if both KeyValues are present in a set, discard that key.
How can I implement this thing using C++, I have very little idea, so if possible a code snippet or a hint would be really helpful.
Are you using or in possession of ‘Algorithms in C++‘ (Sedgewick, 3rd ed.) by any chance?
The first problem that is encountered in the book is that of connectivity (page 7) , and in it there are multiple implementations of a union-find (quick-union, quick-find, weighted etc) algorithm that uses paired keys.
Here’s the quick-find version: (This is just a re-type from the book – not tested)
Just keeps spitting out pairs that aren’t connected (which you could probably invert to get what you want)
The book has a pretty detailed description of how it works – and I’m guessing that you have it for now.