memgraph/src/data_structures/union_find.hpp
florijan 968aa4926a Add parallel customers/Otto test
Summary:
Looking for connected components in a random graph. This test performs the following:
- Generates a random graph that is NOT sequential in memory (otherwise itertion over edges is 2 or more times faster).
- Connectivity by iterating over all the edges.
- Ditto over vertices.
- Ditto over vertices in parallel.

Not done:
- Edge filtering based on XY. I could/should add that to see how it affects perf.
- Getting component info out from union-find.

Local results are encouraging. Iterating over the graph is the bottleneck. Still, I get connectivity of 10M vertices/edges in <7sec (parallel over vertices). Will test on 250M remote now.

Locally obtained results (20M/20M, 2 threads)
```
I1115 14:57:55.136875   357 otto_parallel.cpp:50] Generating 2000000 vertices...
I1115 14:58:19.057734   357 otto_parallel.cpp:74] Generated 2000000 vertices in 23.9208 seconds.
I1115 14:58:19.919221   357 otto_parallel.cpp:82] Generating 2000000 edges...
I1115 14:58:39.519951   357 otto_parallel.cpp:93] Generated 2000000 edges in 19.3398 seconds.
I1115 14:58:39.520349   357 otto_parallel.cpp:196] Running Edge iteration...
I1115 14:58:43.857264   357 otto_parallel.cpp:199]      Done in 4.33691 seconds, result: 3999860270398
I1115 14:58:43.857316   357 otto_parallel.cpp:196] Running Vertex iteration...
I1115 14:58:49.498181   357 otto_parallel.cpp:199]      Done in 5.64087 seconds, result: 4000090070787
I1115 14:58:49.498208   357 otto_parallel.cpp:196] Running Connected components - Edges...
I1115 14:58:54.232530   357 otto_parallel.cpp:199]      Done in 4.73433 seconds, result: 323935
I1115 14:58:54.232570   357 otto_parallel.cpp:196] Running Connected components - Vertices...
I1115 14:59:00.412395   357 otto_parallel.cpp:199]      Done in 6.17983 seconds, result: 323935
I1115 14:59:00.412422   357 otto_parallel.cpp:196] Running Parallel connected components - Vertices...
I1115 14:59:04.662087   357 otto_parallel.cpp:199]      Done in 4.24967 seconds, result: 323935
I1115 14:59:04.662116   357 otto_parallel.cpp:196] Running Expansion...
I1115 14:59:13.913015   357 otto_parallel.cpp:199]      Done in 9.25091 seconds, result: 323935
```

Reviewers: buda, mislav.bradac, dgleich, teon.banek

Reviewed By: buda, teon.banek

Subscribers: teon.banek, pullbot

Differential Revision: https://phabricator.memgraph.io/D982
2017-11-23 09:20:53 +01:00

91 lines
2.1 KiB
C++

#pragma once
#include <memory>
#include <vector>
template <class uintXX_t = uint32_t>
/**
* UnionFind data structure. Provides means of connectivity
* setting and checking in O(alpha(n)) amortized complexity. Memory
* complexity is linear.
*/
class UnionFind {
public:
/**
* Constructor, creates a UnionFind structure of fixed size.
*
* @param n Number of elements in the data structure.
*/
explicit UnionFind(uintXX_t n) : set_count_(n), rank_(n), parent_(n) {
for (auto i = 0; i < n; ++i) rank_[i] = 0, parent_[i] = i;
}
/**
* Connects two elements (and thereby the sets they belong
* to). If they are already connected the function has no effect.
*
* Has O(alpha(n)) amortized time complexity.
*
* @param p First element.
* @param q Second element.
*/
void Connect(uintXX_t p, uintXX_t q) {
auto rp = Root(p);
auto rq = Root(q);
// if roots are equal, we don't have to do anything
if (rp == rq) return;
// merge the subtree with the smaller rank to the root of the subtree with
// the larger rank
if (rank_[rp] < rank_[rq])
parent_[rp] = rq;
else if (rank_[rp] > rank_[rq])
parent_[rq] = rp;
else
parent_[rq] = rp, rank_[rp] += 1;
// update the number of groups
set_count_--;
}
/**
* Indicates if two elements are connected. Has amortized O(alpha(n)) time
* complexity.
*
* @param p First element.
* @param q Second element.
* @return See above.
*/
bool Find(uintXX_t p, uintXX_t q) { return Root(p) == Root(q); }
/**
* Returns the number of disjoint sets in this UnionFind.
*
* @return See above.
*/
uintXX_t Size() const { return set_count_; }
private:
uintXX_t set_count_;
// array of subtree ranks
std::vector<uintXX_t> rank_;
// array of tree indices
std::vector<uintXX_t> parent_;
uintXX_t Root(uintXX_t p) {
auto r = p;
auto newp = p;
// find the node connected to itself, that's the root
while (parent_[r] != r) r = parent_[r];
// do some path compression to enable faster searches
while (p != r) newp = parent_[p], parent_[p] = r, p = newp;
return r;
}
};