Skip to content

Instantly share code, notes, and snippets.

@arbenson
Created October 25, 2018 14:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arbenson/27c6d9ef2871a31cbdbba33239ea60d0 to your computer and use it in GitHub Desktop.
Save arbenson/27c6d9ef2871a31cbdbba33239ea60d0 to your computer and use it in GitHub Desktop.
Union of minimum vertex covers algorithm.
using SparseArrays, Random
function UMVC(A::SparseMatrixCSC{Int64,Int64},
ncovers::Int64=300)
edges = filter(e->e[1] < e[2],
collect(zip(findnz(A)[1:2]...)))
umvc = zeros(Int64,size(A,1))
for _ in 1:ncovers
# Run 2-approximation with random edge ordering
vc = zeros(Int64, size(A,1))
for (i, j) in shuffle(edges)
if vc[[i,j]] == [0,0]; vc[[i,j]] .= 1; end
end
# Reduce to a minimal cover
while true
vc_size = sum(vc)
for c in shuffle(findall(vc .== 1))
nbrs = findnz(A[:,c])[1]
if sum(vc[nbrs]) == length(nbrs); vc[c] = 0; end
end
if sum(vc) == vc_size; break; end
end
umvc[findall(vc .== 1)] .= 1
end
degs = vec(sum(A, dims=1)) # node degrees
return sortperm(collect(zip(umvc, degs)), rev=true)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment