This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |