Skip to content

Instantly share code, notes, and snippets.

@ZacCranko
Created July 28, 2015 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZacCranko/16b9523f47450a2e514c to your computer and use it in GitHub Desktop.
Save ZacCranko/16b9523f47450a2e514c to your computer and use it in GitHub Desktop.
function DiGraph{T<:Real}(adjmx::AbstractMatrix{T})
isequal(size(adjmx)...) || error("Adjacency / distance matrices must be square")
g = DiGraph(size(adjmx,1))
for (s,d) in zip(findn(adjmx)...)
add_edge!(g,s,d)
end
return g
end
@sbromberger
Copy link

julia> zip([1,2,3],['a','b','c'])
Base.Zip2{Array{Int64,1},Array{Char,1}}([1,2,3],['a','b','c'])

julia> [x for x in ans]
3-element Array{Any,1}:
 (1,'a')
 (2,'b')
 (3,'c')

@ZacCranko
Copy link
Author

function DiGraph2{T<:Real}(adjmx::AbstractMatrix{T})
    dima,dimb = size(adjmx)
    isequal(dima,dimb) || error("Adjacency / distance matrices must be square")

    g = DiGraph(dima)
    s,d = findn(adjmx)
    @inbounds for i = 1:dima
        add_edge!(g,s[i],d[i])
    end
    return g
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment