Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

option for maximum spanning tree #1081

Merged
merged 4 commits into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
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
11 changes: 7 additions & 4 deletions src/spanningtrees/kruskal.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""
kruskal_mst(g, distmx=weights(g))
Return a vector of edges representing the minimum spanning tree of a connected, undirected graph `g` with optional
kruskal_mst(g, distmx=weights(g); minimum=true)
Return a vector of edges representing the minimum (by default) spanning tree of a connected, undirected graph `g` with optional
distance matrix `distmx` using [Kruskal's algorithm](https://en.wikipedia.org/wiki/Kruskal%27s_algorithm).

### Optional Arguments
- `minimize=true`: if set to `false`, calculate the maximum spanning tree.
"""
function kruskal_mst end
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
@traitfn function kruskal_mst(g::AG::(!IsDirected),
distmx::AbstractMatrix{T}=weights(g)) where {T <: Real, U, AG <: AbstractGraph{U}}
distmx::AbstractMatrix{T}=weights(g); minimize=true) where {T <: Real, U, AG <: AbstractGraph{U}}

connected_vs = IntDisjointSets(nv(g))

Expand All @@ -20,7 +23,7 @@ function kruskal_mst end
push!(weights, distmx[src(e), dst(e)])
sbromberger marked this conversation as resolved.
Show resolved Hide resolved
end

for e in edge_list[sortperm(weights)]
for e in edge_list[sortperm(weights; rev=!minimize)]
if !in_same_set(connected_vs, src(e), dst(e))
union!(connected_vs, src(e), dst(e))
push!(mst, e)
Expand Down
4 changes: 4 additions & 0 deletions test/spanningtrees/kruskal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
]

vec_mst = Vector{Edge}([Edge(1, 2), Edge(3, 4), Edge(2, 3)])
max_vec_mst = Vector{Edge}([Edge(2, 4), Edge(1, 4), Edge(1, 3)])
for g in testgraphs(g4)
# Testing Kruskal's algorithm
mst = @inferred(kruskal_mst(g, distmx))
@test mst == vec_mst
@test @inferred(kruskal_mst(g, distmx, minimize=false)) == max_vec_mst
end
#second test
distmx_sec = [
Expand All @@ -28,8 +30,10 @@

gx = SimpleGraph(distmx_sec)
vec2 = Vector{Edge}([Edge(1, 8), Edge(3, 4), Edge(2, 8), Edge(1, 3), Edge(6, 8), Edge(5, 6), Edge(3, 7)])
max_vec2 = Vector{Edge}([Edge(5, 7), Edge(1, 7), Edge(4, 7), Edge(3, 7), Edge(5, 8), Edge(2, 3), Edge(5, 6)])
for g in testgraphs(gx)
mst2 = @inferred(kruskal_mst(g, distmx_sec))
@test mst2 == vec2
@test @inferred(kruskal_mst(g, distmx_sec, minimize=false)) == max_vec2
end
end