generated from allenai/python-package-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrating the source files from splex
- Loading branch information
Showing
24 changed files
with
7,225 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
//---------------------------------------------------------------------- | ||
// Disjoint-set data structure | ||
// File: union_find.h | ||
//---------------------------------------------------------------------- | ||
// Copyright (c) 2018 Matt Piekenbrock. All Rights Reserved. | ||
// | ||
// Class definition based off of data-structure described here: | ||
// https://en.wikipedia.org/wiki/Disjoint-set_data_structure | ||
|
||
#ifndef UNIONFIND_H_ | ||
#define UNIONFIND_H_ | ||
|
||
#include <cstddef> // size_t | ||
#include <vector> // vector | ||
#include <numeric> // iota | ||
#include <algorithm> // transform | ||
|
||
struct UnionFind { | ||
using idx_t = std::size_t; | ||
using idx_v = std::vector< size_t >; | ||
idx_t size; | ||
mutable idx_v parent, rank; | ||
|
||
UnionFind(const idx_t _size) : size(_size), parent(_size), rank(_size){ | ||
std::iota(parent.begin(), parent.end(), 0); | ||
} | ||
|
||
// Main operations | ||
void Union(const idx_t x, const idx_t y){ | ||
if (x >= size || y >= size){ return; } | ||
const idx_t xRoot = Find(x), yRoot = Find(y); | ||
if (xRoot == yRoot){ return; } | ||
else if (rank[xRoot] > rank[yRoot]) { parent[yRoot] = xRoot; } | ||
else if (rank[xRoot] < rank[yRoot]) { parent[xRoot] = yRoot; } | ||
else if (rank[xRoot] == rank[yRoot]) { | ||
parent[yRoot] = parent[xRoot]; | ||
rank[xRoot] = rank[xRoot] + 1; | ||
} | ||
} | ||
idx_t Find(const idx_t x) const { | ||
if (x >= size || parent[x] == x){ return x; } | ||
else { | ||
parent[x] = Find(parent[x]); | ||
return parent[x]; | ||
} | ||
} | ||
void AddSets(const idx_t n_sets){ | ||
parent.resize(size + n_sets); | ||
std::iota(parent.begin() + size, parent.end(), size); // parent initialized incrementally | ||
rank.resize(size + n_sets, 0); // rank all 0 | ||
size += n_sets; | ||
} | ||
|
||
// Convenience functions | ||
void UnionAll(const idx_v& idx){ | ||
if (idx.size() <= 1){ return; } | ||
const idx_t n_pairs = idx.size()-1; | ||
for (idx_t i = 0; i < n_pairs; ++i){ Union(idx[i], idx[i+1]); } | ||
} | ||
idx_v FindAll(const idx_v& idx){ | ||
if (idx.size() == 0){ return idx_v(); } | ||
idx_v cc = idx_v(idx.size()); | ||
std::transform(idx.begin(), idx.end(), cc.begin(), [this](const size_t i){ | ||
return(Find(i)); | ||
}); | ||
return(cc); | ||
} | ||
idx_v ConnectedComponents(){ | ||
idx_v cc = idx_v(size); | ||
for (size_t i = 0; i < size; ++i){ cc[i] = Find(i); } | ||
return(cc); | ||
} | ||
}; // class UnionFind | ||
|
||
#endif |
Oops, something went wrong.