Search code examples
c++data-structuresstlsetdata-partitioning

suitable data structure for set (graph) partition


I need to store data grouping nodes of a graph partition, something like:

[node1, node2] [node3] [node4, node5, node6]

My first idea was to have just a simple vector or array of ints, where the position in the array denoted the node_id and it's value is some kind of group_id

The problem is many partition algorithms rely on operating on pairs of nodes within a group. With this method, I think I would waste a lot of computation searching through the vector to find out which nodes belong to the same group.

I could also store as a stl set of sets, which seems closer to the mathematical definition of a partition, but I am getting the impression nested sets are not advised or unnecessary, and I would need to modify the inner sets which I am not sure is possible.

Any suggestions?


Solution

  • Depending on what exactly you want to do with the sets, you could try a disjoint set data structure. In this structure, each element has a method find that returns the "representative" of the set it belongs to.

    A C++ implementation is available in Boost.