• First impression....a lot of code (and a lot of work).

    I do not have the time to read the lengthy article in full but as the OP rightly articulated, the problem is well known and has a myriad of (often half) solutions. This means I made one for myself not too long ago that supports some extra twists and does not look as complicated while performing well for the datasets it was meant to.

    I will make a proper contributing post and share the code tomorrow, right now I have a deadline to meet, so stay tuned!