Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/model/common/union_find.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const root! = root_path_halving!
@doc doc"""
unify!(u, n1, n2)

Connects `n1` and `n2` nodes and returns the root.
Connects `n1` and `n2` nodes using union by weight and returns the root.
"""
function unify!(u::UnionFind, n1::Integer, n2::Integer)
r1 = root!(u, n1)
Expand All @@ -67,7 +67,7 @@ function unify!(u::UnionFind, n1::Integer, n2::Integer)
r1, r2 = r2, r1
end
u.parents[r2] = r1
u.weights[r2] += u.weights[r1]
u.weights[r1] += u.weights[r2]
end
return r1
end
Expand Down
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const alpha = 0.001
filenames = ["observable.jl",
"classical.jl",
"quantum.jl",
"checkpoint.jl"]
"checkpoint.jl",
"union_find.jl"]
for filename in filenames
t = @elapsed include(filename)
println("$(filename): $t sec")
Expand Down
23 changes: 23 additions & 0 deletions test/union_find.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Test
using SpinMonteCarlo

@testset "unify!" begin
uf = UnionFind(0)
addnode!(uf) # node 1
addnode!(uf) # node 2
addnode!(uf) # node 3
addnode!(uf) # node 4

# merge two pairs
r12 = unify!(uf, 1, 2)
r34 = unify!(uf, 3, 4)
@test uf.weights[r12] == 2
@test uf.weights[r34] == 2
@test uf.nclusters == 2

# merge the resulting sets
r = unify!(uf, 1, 3)
@test r == SpinMonteCarlo.root!(uf, 2) == SpinMonteCarlo.root!(uf, 4)
@test uf.weights[r] == 4
@test uf.nclusters == 1
end
Loading