Skip to content
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

Pull Request for Issue #719 #723

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 30 additions & 26 deletions src/disjoint_set.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Disjoint-Set
#Disjoint-Set
NehaDalmia marked this conversation as resolved.
Show resolved Hide resolved

############################################################
#
Expand All @@ -12,12 +12,20 @@
# to an integer index
#
############################################################

_intdisjointset_bounds_err_msg(T) = "the maximum number of elements in IntDisjointSet{$T} is $(typemax(T))"

abstract type Scalarness end
struct Scalar <: Scalarness end
struct NotScalar <: Scalarness end

isscalar(::Type{Any}) = NotScalar() # if we don't know the type we can't really know if scalar or not
isscalar(::Type{<:AbstractString}) = NotScalar() # We consider strings to be nonscalar
isscalar(::Type{<:Number}) = Scalar() # We consider Numbers to be scalar
isscalar(::Type{Char}) = Scalar() # We consider Character to be scalar
isscalar(::Type{Symbol}) = Scalar()
isscalar(::Type{T}) where T = hasmethod(iterate, (T,)) ? NotScalar() : Scalar()
"""
IntDisjointSet{T<:Integer}(n::Integer)

A forest of disjoint sets of integers, which is a data structure
(also called a union–find data structure or merge–find set)
that tracks a set of elements partitioned
Expand All @@ -35,7 +43,6 @@ Base.length(s::IntDisjointSet) = length(s.parents)

"""
num_groups(s::IntDisjointSet)

Get a number of groups.
"""
num_groups(s::IntDisjointSet) = s.ngroups
Expand All @@ -62,22 +69,19 @@ end

"""
find_root!(s::IntDisjointSet{T}, x::T)

Find the root element of the subset that contains an member x.
Path compression is implemented here.
"""
find_root!(s::IntDisjointSet{T}, x::T) where {T<:Integer} = find_root_impl!(s.parents, x)

"""
in_same_set(s::IntDisjointSet{T}, x::T, y::T)

Returns `true` if `x` and `y` belong to the same subset in `s` and `false` otherwise.
"""
in_same_set(s::IntDisjointSet{T}, x::T, y::T) where {T<:Integer} = find_root!(s, x) == find_root!(s, y)

"""
union!(s::IntDisjointSet{T}, x::T, y::T)

Merge the subset containing x and that containing y into one
and return the root of the new set.
"""
Expand All @@ -90,7 +94,6 @@ end

"""
root_union!(s::IntDisjointSet{T}, x::T, y::T)

Form a new set that is the union of the two sets whose root elements are
x and y and return the root of the new set.
Assume x ≠ y (unsafe).
Expand All @@ -113,7 +116,6 @@ end

"""
push!(s::IntDisjointSet{T})

Make a new subset with an automatically chosen new element x.
Returns the new element. Throw an `ArgumentError` if the
capacity of the set would be exceeded.
Expand All @@ -130,30 +132,36 @@ end

"""
DisjointSet{T}(xs)

A forest of disjoint sets of arbitrary value type T.

It is a wrapper of IntDisjointSet{Int}, which uses a
dictionary to map the input value to an internal index.
"""
mutable struct DisjointSet{T} <: AbstractSet{T}
intmap::Dict{T,Int}
revmap::Vector{T}
internal::IntDisjointSet{Int}

DisjointSet{T}() where T = new{T}(Dict{T,Int}(), Vector{T}(), IntDisjointSet(0))

function DisjointSet{T}(xs) where T # xs must be iterable
imap = Dict{T,Int}()
rmap = Vector{T}()
n = length(xs)
sizehint!(imap, n)
sizehint!(rmap, n)
id = 0
for x in xs
imap[x] = (id += 1)
push!(rmap,x)
if(typeof(isscalar(typeof(xs)))!=Scalar)
n = length(xs)
sizehint!(imap, n)
sizehint!(rmap, n)
id = 0
for x in xs
imap[x] = (id += 1)
push!(rmap,x)
end
new{T}(imap, rmap, IntDisjointSet(n))
else
id = 0
imap[xs] = (id += 1)
push!(rmap,xs)
new{T}(imap, rmap, IntDisjointSet(1))

end
new{T}(imap, rmap, IntDisjointSet(n))
end
end

Expand All @@ -173,7 +181,6 @@ Base.length(s::DisjointSet) = length(s.internal)

"""
num_groups(s::DisjointSet)

Get a number of groups.
"""
num_groups(s::DisjointSet) = num_groups(s.internal)
Expand All @@ -187,40 +194,37 @@ end

"""
find_root!{T}(s::DisjointSet{T}, x::T)

Finds the root element of the subset in `s` which has the element `x` as a member.
"""
find_root!(s::DisjointSet{T}, x::T) where {T} = s.revmap[find_root!(s.internal, s.intmap[x])]

"""
in_same_set(s::DisjointSet{T}, x::T, y::T)

Returns `true` if `x` and `y` belong to the same subset in `s` and `false` otherwise.
"""
in_same_set(s::DisjointSet{T}, x::T, y::T) where {T} = in_same_set(s.internal, s.intmap[x], s.intmap[y])

"""
union!(s::DisjointSet{T}, x::T, y::T)

Merge the subset containing x and that containing y into one
and return the root of the new set.
"""
Base.union!(s::DisjointSet{T}, x::T, y::T) where {T} = s.revmap[union!(s.internal, s.intmap[x], s.intmap[y])]

"""
root_union!(s::DisjointSet{T}, x::T, y::T)

Form a new set that is the union of the two sets whose root elements are
x and y and return the root of the new set.
Assume x ≠ y (unsafe).
"""
root_union!(s::DisjointSet{T}, x::T, y::T) where {T} = s.revmap[root_union!(s.internal, s.intmap[x], s.intmap[y])]

"""
push!(s::DisjointSet{T}, x::T)

push!(s::DisjointSet{T}, x::T)
Make a new subset with an automatically chosen new element x.
Returns the new element.

"""
function Base.push!(s::DisjointSet{T}, x::T) where T
haskey(s.intmap, x) && return x
Expand Down