Skip to content

Instantly share code, notes, and snippets.

View arbenson's full-sized avatar
💻
computing

Austin Benson arbenson

💻
computing
View GitHub Profile
@arbenson
arbenson / triangles.jl
Last active April 15, 2020 16:32
Simple triangle enumeration julia
using SparseArrays
function find_triangles(A::SparseMatrixCSC{Int64,Int64})
n = size(A, 1) # number of nodes
d = vec(sum(A, dims=2)) # degree vector
deg_order = zeros(Int64, n)
deg_order[sortperm(d)] = 1:n
triangles = []
for i = 1:n
N_i = findnz(A[:, i])[1] # neighbors of node i
@arbenson
arbenson / UMVC.jl
Created October 25, 2018 14:09
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)
using LightXML
function parse_tags(filename::AbstractString)
tag_map = Dict{String, Int64}()
function get_tag_key(tag::AbstractString)
if !haskey(tag_map, tag)
n = length(tag_map) + 1
tag_map[tag] = n
return n
end
@arbenson
arbenson / tensor_dynsys_alg.jl
Last active November 20, 2018 16:41
Tensor dynamical system for Z-eigenvector computation
using LinearAlgebra
function tensor_apply(T::Array{Float64,3}, x::Vector{Float64})
n = length(x)
y = zeros(Float64, n)
for k in 1:n; y += T[:, :, k] * x * x[k]; end
return y
end
function tensor_collapse(T::Array{Float64,3}, x::Vector{Float64})
n = length(x)
Y = zeros(Float64, n, n)
@arbenson
arbenson / UMVC.jl
Last active March 14, 2018 18:39
Union of minimum vertex covers.
function UMVC(A::SparseMatrixCSC{Int64,Int64}, ncovers=300)
edgevec = 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
cover = zeros(Int64, size(A,1))
edge_queue = shuffle(edgevec)
while length(edge_queue) > 0
i, j = pop!(edge_queue)
@arbenson
arbenson / parse_timestamp.jl
Created February 28, 2018 20:03
Parse W3C email dataset timestamps in Julia
using TimeZones
function parse_timestamp(timestamp::String)
ts = strip(timestamp)
if length(ts) < 10; return -1; end
if ts[1] == '"' && ts[end] == '"'; ts = ts[2:(end - 1)]; end
try @show ts; catch; return -1; end
# Truly terrible cases that we give up on
if contains(ts, "???"); return -1; end
@arbenson
arbenson / motif_spectral.jl
Created May 28, 2017 00:03
Julia implementation of the motif-based clustering algorithm
# Find a motif-based cluster for any directed triangle motif.
function MotifSpectralClust(A::SparseMatrixCSC{Int64,Int64}, motif::AbstractString)
# Form motif adjacency matrix
B = min.(A, A') # bidirectional links
U = A - B # unidirectional links
if motif == "M1"
C = (U * U) .* U'
W = C + C'
elseif motif == "M2"
C = (B * U) .* U' + (U * B) .* U' + (U * U) .* B
@arbenson
arbenson / motif_spectral.jl
Created May 7, 2017 21:14
Julia implementation of the motif-based clustering algorithm
# Find a motif-based cluster for any directed triangle motif.
function MotifSpectralClust(A::SparseMatrixCSC{Int64,Int64}, motif::AbstractString)
# Form motif adjacency matrix
B = min.(A, A') # bidirectional links
U = A - B # unidirectional links
if motif == "M1"
C = (U * U) .* U'
W = C + C'
elseif motif == "M2"
C = (B * U) .* U' + (U * B) .* U' + (U * U) .* B