简体   繁体   中英

add mutually exclusive data into groups from set of data

I have set of data:

offerId  : Variants
  101:     (1)
  102:     (2,3)
  103:     (4)
  104:     (1,2,5)
  105:     (3)
  106:     (4,6)

I want to offerIds in separate groups which are mutually exclusive. Like from above data, I'll get two mutually exclusive groups:

   [101,102,104,105] corresponding to [(1),(2,3),(1,2,5),(3)]

   [103, 106] corresponding to  [(4),(4,6)]

How to I achieve this, pseudocode or logic anything will be appreciated.

PS I am writing my code in GO language. But answer in any language will be appreciated.

Thanks in advance.

You need Disjoint-set data structure. Another name - Union-find data structure.

You can see pseudocode in Wiki page, and real implementations in numerous sources. Arbitrary C# example .

In your case you'll also need to check intersection of groups (sets?). If intersection of two groups is not empty, make Union operations for these groups.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM