Grouping lists by common elements

后端 未结 1 1018
北海茫月
北海茫月 2021-01-19 07:39

I\'m looking for a clever/fast C++ algorithm, which would allow me to do grouping of several lists of objects when they contain common objects inside. Let\'s say I have N li

相关标签:
1条回答
  • 2021-01-19 08:23

    For each object, calculate which elements contain it.

    i.e.

    01 -> [E1, E3]
    02 -> [E4]
    03 -> [E2, E5]
    04 -> [E3]
    05 -> [E3, E4]
    06 -> [E5]
    

    These lists induce a graph: there's a vertex per element, and two vertices are connected if the corresponding elements show up in the same list.

    enter image description here

    It seems to me that what you want to calculate are the connected components of the graph.

    0 讨论(0)
提交回复
热议问题