Repository navigation
feat: add union-find data structure #304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,131 @@ | ||
| # Union-Find (Disjoint Set Union) keeps track of elements split into | ||
| # non-overlapping sets. It supports two main operations: | ||
| # - find: which set does an element belong to (returns the set representative) | ||
| # - union: merge the sets containing two elements | ||
| # | ||
| # Two optimizations make both operations almost constant time: | ||
| # - Path compression: find() points every visited node straight at the root. | ||
| # - Union by rank: the shorter tree is attached below the taller one. | ||
| # | ||
| # Time Complexity: O(alpha(n)) amortized per operation, where alpha is the | ||
| # inverse Ackermann function (below 5 for any practical n) | ||
| # Space Complexity: O(n) | ||
| # | ||
| # Applications: | ||
| # - Kruskal's minimum spanning tree | ||
| # - Detecting cycles in undirected graphs | ||
| # - Connected components | ||
| # - Percolation and image segmentation | ||
| # | ||
| # Example usage: | ||
| # uf <- uf_new(5) # elements 1..5, each in its own set | ||
| # uf <- uf_union(uf, 1, 2) | ||
| # uf <- uf_union(uf, 2, 3) | ||
| # uf_connected(uf, 1, 3)$connected # TRUE | ||
| # uf$count # 3 sets remain | ||
| # | ||
| # Note: R copies values, so functions that change the structure return the | ||
| # updated structure. Always reassign the result (uf <- uf_union(...)). | ||
|
|
||
| #' Create a union-find structure with n singleton sets | ||
| #' @param n Number of elements (labelled 1..n) | ||
| #' @return List with parent, rank and count (number of sets) | ||
| uf_new <- function(n) { | ||
| if (!is.numeric(n) || length(n) != 1 || n < 0 || n != floor(n)) { | ||
| stop("Error: n must be a non-negative integer") | ||
| } | ||
| list(parent = seq_len(n), rank = integer(n), count = as.integer(n)) | ||
| } | ||
|
|
||
| uf_check <- function(uf, x) { | ||
| if (!is.numeric(x) || length(x) != 1 || x < 1 || x > length(uf$parent) || | ||
| x != floor(x)) { | ||
| stop(sprintf("Error: element must be an integer between 1 and %d", | ||
| length(uf$parent))) | ||
| } | ||
| } | ||
|
|
||
| #' Find the representative (root) of the set containing x | ||
| #' @param uf Structure from uf_new() | ||
| #' @param x Element to look up | ||
| #' @return List with root and the updated structure (after path compression) | ||
| uf_find <- function(uf, x) { | ||
| uf_check(uf, x) | ||
| root <- x | ||
| while (uf$parent[root] != root) { | ||
| root <- uf$parent[root] | ||
| } | ||
| # Path compression: point every node on the path directly at the root | ||
| while (uf$parent[x] != root) { | ||
| next_node <- uf$parent[x] | ||
| uf$parent[x] <- root | ||
| x <- next_node | ||
| } | ||
| list(root = root, uf = uf) | ||
| } | ||
|
|
||
| #' Merge the sets containing x and y | ||
| #' @return The updated structure (unchanged if already in the same set) | ||
| uf_union <- function(uf, x, y) { | ||
| fx <- uf_find(uf, x) | ||
| fy <- uf_find(fx$uf, y) | ||
| uf <- fy$uf | ||
| rx <- fx$root | ||
| ry <- fy$root | ||
| if (rx == ry) { | ||
| return(uf) | ||
| } | ||
| # Union by rank: attach the shorter tree below the taller one | ||
| if (uf$rank[rx] < uf$rank[ry]) { | ||
| uf$parent[rx] <- ry | ||
| } else if (uf$rank[rx] > uf$rank[ry]) { | ||
| uf$parent[ry] <- rx | ||
| } else { | ||
| uf$parent[ry] <- rx | ||
| uf$rank[rx] <- uf$rank[rx] + 1L | ||
| } | ||
| uf$count <- uf$count - 1L | ||
| uf | ||
| } | ||
|
|
||
| #' Check whether x and y are in the same set | ||
| #' @return List with connected (TRUE or FALSE) and the updated structure | ||
| uf_connected <- function(uf, x, y) { | ||
| fx <- uf_find(uf, x) | ||
| fy <- uf_find(fx$uf, y) | ||
| list(connected = fx$root == fy$root, uf = fy$uf) | ||
| } | ||
|
|
||
| # =========================== | ||
| # Example Usage & Testing | ||
| # =========================== | ||
| uf <- uf_new(8) | ||
| stopifnot(uf$count == 8) | ||
| stopifnot(!uf_connected(uf, 1, 2)$connected) | ||
|
|
||
| uf <- uf_union(uf, 1, 2) | ||
| uf <- uf_union(uf, 3, 4) | ||
| uf <- uf_union(uf, 2, 3) | ||
| stopifnot(uf$count == 5) | ||
| stopifnot(uf_connected(uf, 1, 4)$connected) | ||
| stopifnot(!uf_connected(uf, 1, 5)$connected) | ||
|
|
||
| # Union of elements already in the same set changes nothing | ||
| uf <- uf_union(uf, 1, 4) | ||
| stopifnot(uf$count == 5) | ||
|
|
||
| # Merge everything into one set | ||
| for (i in 2:8) { | ||
| uf <- uf_union(uf, 1, i) | ||
| } | ||
| stopifnot(uf$count == 1) | ||
| root <- uf_find(uf, 1)$root | ||
| for (i in 1:8) { | ||
| stopifnot(uf_find(uf, i)$root == root) | ||
| } | ||
|
|
||
| # Invalid element raises an error | ||
| stopifnot(inherits(try(uf_find(uf, 9), silent = TRUE), "try-error")) | ||
|
|
||
| cat("Number of sets after all unions:", uf$count, "\n") | ||
| cat("All union-find tests passed\n") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.