Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

Commit

Permalink
back to using for nonextended functions (#898)
Browse files Browse the repository at this point in the history
* back to using for nonextended functions

* Update greedy_color.jl

whitespace changes

* Update edit_distance.jl

whitespace changes
  • Loading branch information
sbromberger authored Jun 8, 2018
1 parent f19a796 commit 463e432
Show file tree
Hide file tree
Showing 53 changed files with 468 additions and 505 deletions.
40 changes: 20 additions & 20 deletions benchmark/core.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
function bench_iteredges(g::AbstractGraph)
i = 0
for e in edges(g)
i += 1
end
return i
i = 0
for e in edges(g)
i += 1
end
return i
end

function bench_has_edge(g::AbstractGraph)
Random.srand(1)
srand(1)
nvg = nv(g)
srcs = rand([1:nvg;], cld(nvg, 4))
dsts = rand([1:nvg;], cld(nvg, 4))
Expand All @@ -28,18 +28,18 @@ EDGEFNS = [

@benchgroup "edges" begin

for fun in EDGEFNS
@benchgroup "$fun" begin
@benchgroup "graph" begin
for (name, g) in GRAPHS
@bench "$name" $fun($g)
end
end
@benchgroup "digraph" begin
for (name, g) in DIGRAPHS
@bench "$name" $fun($g)
end
end # digraph
end # fun
end
for fun in EDGEFNS
@benchgroup "$fun" begin
@benchgroup "graph" begin
for (name, g) in GRAPHS
@bench "$name" $fun($g)
end
end
@benchgroup "digraph" begin
for (name, g) in DIGRAPHS
@bench "$name" $fun($g)
end
end # digraph
end # fun
end
end # edges
23 changes: 12 additions & 11 deletions src/LightGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ module LightGraphs

using SimpleTraits

import CodecZlib
import DataStructures
import DelimitedFiles
import Distributed
import IterativeEigensolvers
import LinearAlgebra
import Markdown
import Random
import SharedArrays
import SparseArrays
using CodecZlib: GzipCompressorStream, GzipDecompressorStream
using DataStructures: IntDisjointSets, PriorityQueue, dequeue!, dequeue_pair!, enqueue!, heappop!, heappush!, in_same_set, peek, union!
using Distributed: @distributed
using IterativeEigensolvers: eigs
using LinearAlgebra: I, Symmetric, diagm, eigen, eigvals, norm, rmul!, tril, triu
import LinearAlgebra: Diagonal, issymmetric, mul!
# import Markdown
using Random: AbstractRNG, GLOBAL_RNG, MersenneTwister, randperm, randsubseq!, shuffle, shuffle!, srand
using SharedArrays: SharedMatrix, SharedVector, sdata
using SparseArrays: SparseMatrixCSC, nonzeros, nzrange, rowvals
import SparseArrays: blockdiag, sparse

import Base: write, ==, <, *, , convert, isless, issubset, union, intersect,
reverse, reverse!, isassigned, getindex, setindex!, show,
Expand All @@ -25,7 +26,7 @@ export
AbstractGraph, AbstractEdge, AbstractEdgeIter,
Edge, Graph, SimpleGraph, SimpleGraphFromIterator, DiGraph, SimpleDiGraphFromIterator,
SimpleDiGraph, vertices, edges, edgetype, nv, ne, src, dst,
is_directed,
is_directed, IsDirected,
has_vertex, has_edge, inneighbors, outneighbors,

# core
Expand Down
2 changes: 1 addition & 1 deletion src/SimpleGraphs/simpledigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ SimpleDiGraph(n::T) where T<:Integer = SimpleDiGraph{T}(n)
SimpleDiGraph(::Type{T}) where T<:Integer = SimpleDiGraph{T}(zero(T))

# sparse adjacency matrix constructor: SimpleDiGraph(adjmx)
function SimpleDiGraph{T}(adjmx::SparseArrays.SparseMatrixCSC{U}) where T<:Integer where U<:Real
function SimpleDiGraph{T}(adjmx::SparseMatrixCSC{U}) where T<:Integer where U<:Real
dima, dimb = size(adjmx)
isequal(dima, dimb) || throw(ArgumentError("Adjacency / distance matrices must be square"))

Expand Down
2 changes: 1 addition & 1 deletion src/SimpleGraphs/simplegraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ SimpleGraph(::Type{T}) where T <: Integer = SimpleGraph{T}(zero(T))
function SimpleGraph{T}(adjmx::AbstractMatrix) where T <: Integer
dima, dimb = size(adjmx)
isequal(dima, dimb) || throw(ArgumentError("Adjacency / distance matrices must be square"))
LinearAlgebra.issymmetric(adjmx) || throw(ArgumentError("Adjacency / distance matrices must be symmetric"))
issymmetric(adjmx) || throw(ArgumentError("Adjacency / distance matrices must be symmetric"))

g = SimpleGraph(T(dima))
@inbounds for i in findall(triu(adjmx) .!= 0)
Expand Down
30 changes: 12 additions & 18 deletions src/centrality/betweenness.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ bc(v) = \\frac{1}{\\mathcal{N}} \\sum_{s \\neq t \\neq v}
### References
- Brandes 2001 & Brandes 2008
"""
function betweenness_centrality(
g::AbstractGraph,
vs::AbstractVector = vertices(g),
distmx::AbstractMatrix = weights(g);
function betweenness_centrality(g::AbstractGraph,
vs::AbstractVector=vertices(g),
distmx::AbstractMatrix=weights(g);
normalize=true,
endpoints=false)

Expand All @@ -40,7 +39,7 @@ function betweenness_centrality(

betweenness = zeros(n_v)
for s in vs
if degree(g,s) > 0 # this might be 1?
if degree(g, s) > 0 # this might be 1?
state = dijkstra_shortest_paths(g, s, distmx; allpaths=true, trackvertices=true)
if endpoints
_accumulate_endpoints!(betweenness, state, g, s)
Expand All @@ -62,10 +61,9 @@ end
betweenness_centrality(g::AbstractGraph, k::Integer, distmx::AbstractMatrix=weights(g); normalize=true, endpoints=false) =
betweenness_centrality(g, sample(vertices(g), k), distmx; normalize=normalize, endpoints=endpoints)

function parallel_betweenness_centrality(
g::AbstractGraph,
vs::AbstractVector = vertices(g),
distmx::AbstractMatrix = weights(g);
function parallel_betweenness_centrality(g::AbstractGraph,
vs::AbstractVector=vertices(g),
distmx::AbstractMatrix=weights(g);
normalize=true,
endpoints=false)::Vector{Float64}

Expand All @@ -75,7 +73,7 @@ function parallel_betweenness_centrality(

# Parallel reduction

betweenness = Distributed.@distributed (+) for s in vs
betweenness = @distributed (+) for s in vs
temp_betweenness = zeros(n_v)
if degree(g, s) > 0 # this might be 1?
state = dijkstra_shortest_paths(g, s, distmx; allpaths=true, trackvertices=true)
Expand All @@ -100,12 +98,10 @@ end
parallel_betweenness_centrality(g::AbstractGraph, k::Integer, distmx::AbstractMatrix=weights(g); normalize=true, endpoints=false) =
parallel_betweenness_centrality(g, sample(vertices(g), k), distmx; normalize=normalize, endpoints=endpoints)

function _accumulate_basic!(
betweenness::Vector{Float64},
function _accumulate_basic!(betweenness::Vector{Float64},
state::DijkstraState,
g::AbstractGraph,
si::Integer
)
si::Integer)

n_v = length(state.parents) # this is the ttl number of vertices
δ = zeros(n_v)
Expand All @@ -130,12 +126,10 @@ function _accumulate_basic!(
return nothing
end

function _accumulate_endpoints!(
betweenness::Vector{Float64},
function _accumulate_endpoints!(betweenness::Vector{Float64},
state::DijkstraState,
g::AbstractGraph,
si::Integer
)
si::Integer)

n_v = nv(g) # this is the ttl number of vertices
δ = zeros(n_v)
Expand Down
6 changes: 3 additions & 3 deletions src/centrality/closeness.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ function parallel_closeness_centrality(g::AbstractGraph,

n_v = Int(nv(g))

closeness = SharedArrays.SharedVector{Float64}(n_v)
closeness = SharedVector{Float64}(n_v)

Distributed.@sync Distributed.@distributed for u in vertices(g)
@sync @distributed for u in vertices(g)
if degree(g, u) == 0 # no need to do Dijkstra here
closeness[u] = 0.0
else
Expand All @@ -61,5 +61,5 @@ function parallel_closeness_centrality(g::AbstractGraph,
end
end
end
return SharedArrays.sdata(closeness)
return sdata(closeness)
end
2 changes: 1 addition & 1 deletion src/centrality/eigenvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ eigenvector of the adjacency matrix \$\\mathbf{A}\$.
- Mark E. J. Newman: Networks: An Introduction.
Oxford University Press, USA, 2010, pp. 169.
"""
eigenvector_centrality(g::AbstractGraph) = abs.(vec(IterativeEigensolvers.eigs(adjacency_matrix(g), nev=1)[2]))::Vector{Float64}
eigenvector_centrality(g::AbstractGraph) = abs.(vec(eigs(adjacency_matrix(g), nev=1)[2]))::Vector{Float64}
4 changes: 2 additions & 2 deletions src/centrality/katz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ the centrality calculated for each node in `g`.
function katz_centrality(g::AbstractGraph, α::Real=0.3)
nvg = nv(g)
v = ones(Float64, nvg)
spI = SparseArrays.sparse(one(Float64) * LinearAlgebra.I, nvg, nvg)
spI = sparse(one(Float64) * I, nvg, nvg)
A = adjacency_matrix(g, Bool; dir=:in)
v = (spI - α * A) \ v
v /= LinearAlgebra.norm(v)
v /= norm(v)
return v
end
6 changes: 3 additions & 3 deletions src/centrality/radiality.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ function parallel_radiality_centrality(g::AbstractGraph)::Vector{Float64}
n_v = nv(g)
vs = vertices(g)
n = ne(g)
meandists = SharedArrays.SharedVector{Float64}(Int(n_v))
maxdists = SharedArrays.SharedVector{Float64}(Int(n_v))
meandists = SharedVector{Float64}(Int(n_v))
maxdists = SharedVector{Float64}(Int(n_v))

Distributed.@sync Distributed.@distributed for i = 1:n_v
@sync @distributed for i = 1:n_v
d = dijkstra_shortest_paths(g, vs[i])
maxdists[i] = maximum(d.dists)
meandists[i] = sum(d.dists) / (n_v - 1)
Expand Down
15 changes: 6 additions & 9 deletions src/centrality/stress.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The stress centrality of a vertex ``n`` is defined as the number of shortest pat
- Barabási, A.L., Oltvai, Z.N.: Network biology: understanding the cell's functional organization. Nat Rev Genet 5 (2004) 101-113
- Shimbel, A.: Structural parameters of communication networks. Bull Math Biophys 15 (1953) 501-507.
"""
function stress_centrality(g::AbstractGraph, vs::AbstractVector = vertices(g))
function stress_centrality(g::AbstractGraph, vs::AbstractVector=vertices(g))
n_v = nv(g)
k = length(vs)
isdir = is_directed(g)
Expand All @@ -33,17 +33,16 @@ stress_centrality(g::AbstractGraph, k::Integer) =
stress_centrality(g, sample(vertices(g), k))


function parallel_stress_centrality(
g::AbstractGraph,
vs::AbstractVector = vertices(g))::Vector{Int}
function parallel_stress_centrality(g::AbstractGraph,
vs::AbstractVector=vertices(g))::Vector{Int}

n_v = nv(g)
k = length(vs)
isdir = is_directed(g)

# Parallel reduction

stress = Distributed.@distributed (+) for s in vs
stress = @distributed (+) for s in vs
temp_stress = zeros(Int, n_v)
if degree(g, s) > 0 # this might be 1?
state = dijkstra_shortest_paths(g, s; allpaths=true, trackvertices=true)
Expand All @@ -58,12 +57,10 @@ parallel_stress_centrality(g::AbstractGraph, k::Integer) =
parallel_stress_centrality(g, sample(vertices(g), k))


function _stress_accumulate_basic!(
stress::Vector{Int},
function _stress_accumulate_basic!(stress::Vector{Int},
state::DijkstraState,
g::AbstractGraph,
si::Integer
)
si::Integer)

n_v = length(state.parents) # this is the ttl number of vertices
δ = zeros(Int, n_v)
Expand Down
6 changes: 3 additions & 3 deletions src/distance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ function parallel_eccentricity(g::AbstractGraph,
vs::AbstractVector=vertices(g),
distmx::AbstractMatrix{T}=weights(g)) where T <: Real
vlen = length(vs)
eccs = SharedArrays.SharedVector{T}(vlen)
Distributed.@sync Distributed.@distributed for i = 1:vlen
eccs = SharedVector{T}(vlen)
@sync @distributed for i = 1:vlen
eccs[i] = maximum(dijkstra_shortest_paths(g, vs[i], distmx).dists)
end
d = SharedArrays.sdata(eccs)
d = sdata(eccs)
maximum(d) == typemax(T) && warn("Infinite path length detected")
return d
end
Expand Down
Loading

0 comments on commit 463e432

Please sign in to comment.