|
| 1 | +module GraphsSharedArraysExt |
| 2 | + |
| 3 | +using Graphs |
| 4 | +using SharedArrays: SharedArrays, SharedMatrix, SharedVector, sdata |
| 5 | +using SharedArrays.Distributed: @distributed |
| 6 | + |
| 7 | +# betweenness |
| 8 | +function Graphs.distr_betweenness_centrality( |
| 9 | + g::AbstractGraph, |
| 10 | + vs=vertices(g), |
| 11 | + distmx::AbstractMatrix=weights(g); |
| 12 | + normalize=true, |
| 13 | + endpoints=false, |
| 14 | +)::Vector{Float64} |
| 15 | + n_v = nv(g) |
| 16 | + k = length(vs) |
| 17 | + isdir = is_directed(g) |
| 18 | + |
| 19 | + # Parallel reduction |
| 20 | + |
| 21 | + betweenness = @distributed (+) for s in vs |
| 22 | + temp_betweenness = zeros(n_v) |
| 23 | + if degree(g, s) > 0 # this might be 1? |
| 24 | + state = Graphs.dijkstra_shortest_paths( |
| 25 | + g, s, distmx; allpaths=true, trackvertices=true |
| 26 | + ) |
| 27 | + if endpoints |
| 28 | + Graphs._accumulate_endpoints!(temp_betweenness, state, g, s) |
| 29 | + else |
| 30 | + Graphs._accumulate_basic!(temp_betweenness, state, g, s) |
| 31 | + end |
| 32 | + end |
| 33 | + temp_betweenness |
| 34 | + end |
| 35 | + |
| 36 | + Graphs._rescale!(betweenness, n_v, normalize, isdir, k) |
| 37 | + |
| 38 | + return betweenness |
| 39 | +end |
| 40 | + |
| 41 | +# closeness |
| 42 | +function Graphs.distr_closeness_centrality( |
| 43 | + g::AbstractGraph, distmx::AbstractMatrix=weights(g); normalize=true |
| 44 | +)::Vector{Float64} |
| 45 | + n_v = Int(nv(g)) |
| 46 | + closeness = SharedVector{Float64}(n_v) |
| 47 | + fill!(closeness, 0.0) |
| 48 | + |
| 49 | + @sync @distributed for u in vertices(g) |
| 50 | + if degree(g, u) == 0 # no need to do Dijkstra here |
| 51 | + closeness[u] = 0.0 |
| 52 | + else |
| 53 | + d = Graphs.dijkstra_shortest_paths(g, u, distmx).dists |
| 54 | + δ = filter(x -> x != typemax(x), d) |
| 55 | + σ = sum(δ) |
| 56 | + l = length(δ) - 1 |
| 57 | + if σ > 0 |
| 58 | + closeness[u] = l / σ |
| 59 | + if normalize |
| 60 | + n = l * 1.0 / (n_v - 1) |
| 61 | + closeness[u] *= n |
| 62 | + end |
| 63 | + else |
| 64 | + closeness[u] = 0.0 |
| 65 | + end |
| 66 | + end |
| 67 | + end |
| 68 | + return sdata(closeness) |
| 69 | +end |
| 70 | + |
| 71 | +# radiality |
| 72 | +function Graphs.distr_radiality_centrality(g::AbstractGraph)::Vector{Float64} |
| 73 | + n_v = nv(g) |
| 74 | + vs = vertices(g) |
| 75 | + n = ne(g) |
| 76 | + meandists = SharedVector{Float64}(Int(n_v)) |
| 77 | + maxdists = SharedVector{Float64}(Int(n_v)) |
| 78 | + |
| 79 | + @sync @distributed for i in 1:n_v |
| 80 | + d = Graphs.dijkstra_shortest_paths(g, vs[i]) |
| 81 | + maxdists[i] = maximum(d.dists) |
| 82 | + meandists[i] = sum(d.dists) / (n_v - 1) |
| 83 | + nothing |
| 84 | + end |
| 85 | + dmtr = maximum(maxdists) |
| 86 | + radialities = collect(meandists) |
| 87 | + return ((dmtr + 1) .- radialities) ./ dmtr |
| 88 | +end |
| 89 | + |
| 90 | +# stress |
| 91 | +function Graphs.distr_stress_centrality(g::AbstractGraph, vs=vertices(g))::Vector{Int64} |
| 92 | + n_v = nv(g) |
| 93 | + k = length(vs) |
| 94 | + isdir = is_directed(g) |
| 95 | + |
| 96 | + # Parallel reduction |
| 97 | + stress = @distributed (+) for s in vs |
| 98 | + temp_stress = zeros(Int64, n_v) |
| 99 | + if degree(g, s) > 0 # this might be 1? |
| 100 | + state = Graphs.dijkstra_shortest_paths(g, s; allpaths=true, trackvertices=true) |
| 101 | + Graphs._stress_accumulate_basic!(temp_stress, state, g, s) |
| 102 | + end |
| 103 | + temp_stress |
| 104 | + end |
| 105 | + return stress |
| 106 | +end |
| 107 | + |
| 108 | +# generate_reduce |
| 109 | +function Graphs.distr_generate_reduce( |
| 110 | + g::AbstractGraph{T}, gen_func::Function, comp::Comp, reps::Integer |
| 111 | +) where {T<:Integer,Comp} |
| 112 | + # Type assert required for type stability |
| 113 | + min_set::Vector{T} = @distributed ((x, y) -> comp(x, y) ? x : y) for _ in 1:reps |
| 114 | + gen_func(g) |
| 115 | + end |
| 116 | + return min_set |
| 117 | +end |
| 118 | + |
| 119 | +# eccentricity |
| 120 | +function Graphs.distr_eccentricity( |
| 121 | + g::AbstractGraph, vs=vertices(g), distmx::AbstractMatrix{T}=weights(g) |
| 122 | +) where {T<:Number} |
| 123 | + vlen = length(vs) |
| 124 | + eccs = SharedVector{T}(vlen) |
| 125 | + @sync @distributed for i in 1:vlen |
| 126 | + d = Graphs.dijkstra_shortest_paths(g, vs[i], distmx) |
| 127 | + eccs[i] = maximum(d.dists) |
| 128 | + end |
| 129 | + d = sdata(eccs) |
| 130 | + maximum(d) == typemax(T) && @warn("Infinite path length detected") |
| 131 | + return d |
| 132 | +end |
| 133 | + |
| 134 | +# dijkstra shortest paths |
| 135 | +function Graphs.distr_dijkstra_shortest_paths( |
| 136 | + g::AbstractGraph{U}, sources=vertices(g), distmx::AbstractMatrix{T}=weights(g) |
| 137 | +) where {T<:Number} where {U} |
| 138 | + n_v = nv(g) |
| 139 | + r_v = length(sources) |
| 140 | + |
| 141 | + # TODO: remove `Int` once julialang/#23029 / #23032 are resolved |
| 142 | + dists = SharedMatrix{T}(Int(r_v), Int(n_v)) |
| 143 | + parents = SharedMatrix{U}(Int(r_v), Int(n_v)) |
| 144 | + |
| 145 | + @sync @distributed for i in 1:r_v |
| 146 | + state = Graphs.dijkstra_shortest_paths(g, sources[i], distmx) |
| 147 | + dists[i, :] = state.dists |
| 148 | + parents[i, :] = state.parents |
| 149 | + end |
| 150 | + |
| 151 | + result = MultipleDijkstraState(sdata(dists), sdata(parents)) |
| 152 | + return result |
| 153 | +end |
| 154 | + |
| 155 | +# random greedy color |
| 156 | +function Graphs.distr_random_greedy_color(g::AbstractGraph{T}, reps::Integer) where {T<:Integer} |
| 157 | + best = @distributed (Graphs.best_color) for i in 1:reps |
| 158 | + seq = shuffle(vertices(g)) |
| 159 | + Graphs.perm_greedy_color(g, seq) |
| 160 | + end |
| 161 | + |
| 162 | + return convert(Graphs.Coloring{T}, best) |
| 163 | +end |
| 164 | + |
| 165 | +end |
0 commit comments