Skip to content

Commit

Permalink
reverting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
iaravenas committed Feb 6, 2018
1 parent 677f6fa commit ac1760b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
19 changes: 9 additions & 10 deletions src/spanningtrees/kruskal.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
struct KruskalHeapEntry{T<:AbstractEdge, U<:Real}
edge::T
dist::U
struct KruskalHeapEntry{T<:Real}
edge::Edge
dist::T
end

isless(e1::KruskalHeapEntry, e2::KruskalHeapEntry) = e1.dist < e2.dist
Expand Down Expand Up @@ -29,21 +29,20 @@ distance matrix `distmx` using [Kruskal's algorithm](https://en.wikipedia.org/wi
"""
function kruskal_mst end
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
@traitfn function kruskal_mst{U<:Real, V, AG<:AbstractGraph{V}}(
@traitfn function kruskal_mst{T, U, AG<:AbstractGraph{U}}(
g::AG::(!IsDirected),
distmx::AbstractMatrix{U} = weights(g)
distmx::AbstractMatrix{T} = weights(g)
)

T = edgetype(g)
edge_list = Vector{KruskalHeapEntry{T, U}}()
mst = Vector{T}()
connected_vs = collect(one(V):nv(g))
edge_list = Vector{KruskalHeapEntry{T}}()
mst = Vector{Edge}()
connected_vs = collect(one(U):nv(g))

sizehint!(edge_list, ne(g))
sizehint!(mst, ne(g))

for e in edges(g)
heappush!(edge_list, KruskalHeapEntry{T, U}(e, distmx[src(e), dst(e)]))
heappush!(edge_list, KruskalHeapEntry{T}(e, distmx[src(e), dst(e)]))
end

while !isempty(edge_list) && length(mst) < nv(g) - 1
Expand Down
26 changes: 12 additions & 14 deletions src/spanningtrees/prim.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
struct PrimHeapEntry{T<:AbstractEdge, U<:Real}
edge::T
dist::U
struct PrimHeapEntry{T<:Real}
edge::Edge
dist::T
end

isless(e1::PrimHeapEntry, e2::PrimHeapEntry) = e1.dist < e2.dist
Expand All @@ -13,14 +13,12 @@ distance matrix `distmx` using [Prim's algorithm](https://en.wikipedia.org/wiki/
Return a vector of edges.
"""
function prim_mst end
@traitfn function prim_mst{U<:Real, V, AG<:AbstractGraph{V}}(
g::AG::(!IsDirected),
distmx::AbstractMatrix{U} = weights(g)
)

T = edgetype(g)
pq = Vector{PrimHeapEntry{T, U}}()
mst = Vector{T}()
@traitfn function prim_mst(
g::::(!IsDirected),
distmx::AbstractMatrix = weights(g)
)
pq = Vector{PrimHeapEntry}()
mst = Vector{Edge}()
marked = zeros(Bool, nv(g))

sizehint!(pq, ne(g))
Expand Down Expand Up @@ -52,15 +50,15 @@ function visit!(
g::AbstractGraph,
v::Integer,
marked::AbstractVector{Bool},
pq::AbstractVector{PrimHeapEntry{T, U}},
pq::AbstractVector,
distmx::AbstractMatrix
) where {T<:AbstractEdge, U<:Real}
)
marked[v] = true
for w in outneighbors(g, v)
if !marked[w]
x = min(v, w)
y = max(v, w)
heappush!(pq, PrimHeapEntry{T, U}(T(x, y), distmx[x, y]))
heappush!(pq, PrimHeapEntry(Edge(x, y), distmx[x, y]))
end
end
end

0 comments on commit ac1760b

Please sign in to comment.