2

Let's say I have a vector of vectors:

std::vector<std::vector<int> > vec;

Now, in my program, this vector contains groups of nodes that create a cycle in a graph. They create the strongly connected component if they are intersected.

I need to loop over this vector and effectively find those vectors, which are intersected and afterwards, join those. It can be done with sets by brute force set_intersection and set_union, but it would be very slow.

As an example, if I have vectors A-D of simple int elements, where A,B,D are intersected.

A = [1,2,3,4]
B = [5,6,7]
C = [8,9]
D = [3,4,6,10]

and I want this result:

s1 = [1,2,3,4,5,6,7,10]
s2 = [8,9]

Now, I have tried multiple things over last few hours, but simple googling shows that set_intersection would be quite slow approach and I would have to loop over all the sets of nodes multiple times (I think it would be N*(N + 1)/2, so O(N2) without heuristics).

I need to do this effectively, so the thing I have in mind is to loop over all the nodes and note down where I found the original node, then group them together in one search, if possible. However I am unable to find nor design such an algorithm to be effective enough, so I would appreciate some help or ideas on how to approach this.

Thank you.

fatum
  • 21
  • 4
  • 1
    It's unclear what you mean by "intersected". From your example it seems that whenever two sets have nonempty intersection, you want to combine them into a single set containing the union of the two original sets, and repeat this until it is no longer possible. If that's right, then 2 things: 1. This can be done efficiently using a fast union/find data structure. 2. If you're just trying to find strongly connected components, then there are much more efficient algorithms for doing this directly than enumerating *all* cycles and then doing what you're currently doing. – j_random_hacker Sep 05 '14 at 11:59
  • Do you always have 4 vectors ? Is the number of integers relatively small ? Do the integers have consecutive values ? If the answers are all positive, then I have an effective solution. – Nelfeal Sep 05 '14 at 11:59
  • @j_random_hacker - you are correct. Can you elaborate on using a fast union/find data structure? And you may be correct that I could traverse graph again to find SCC instead of doing this (I need both, cycles and SCC). This approach seemed simpler, but you might be right. – fatum Sep 05 '14 at 12:15
  • @Nelxiost, they are not always 4, it might be 1-N, but it's a small number. Numbers are not sorted, as they represent the path in the graph (starting with the lowest number, but can be e.g. [1,3,4,2]) – fatum Sep 05 '14 at 12:15
  • Why not just run BFS on the original graph? – n. m. could be an AI Sep 05 '14 at 12:31
  • @n.m. yes, I just ran Tarjan algorithm and it seems to work flawlessly. It just seemed simpler and more effective to implement this than another traversing of the graph. It does not seem like the case anymore. Will update/close the question in near future, so thank you all. – fatum Sep 05 '14 at 12:41
  • @fatum: The top Google hit for "fast union/find data structure" is http://en.wikipedia.org/wiki/Disjoint-set_data_structure. Read that page, it should be clear what you need to do. – j_random_hacker Sep 05 '14 at 12:57

0 Answers0