-
Notifications
You must be signed in to change notification settings - Fork 0
/
edge_betweenness_centrality.jl
59 lines (54 loc) · 1.81 KB
/
edge_betweenness_centrality.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function node_edge_dict(g::AbstractGraph)
node_edge_dict = Dict{Tuple{Int64,Int64},Int64}()
edgess = collect(edges(g))
for e in 1:ne(g)
push!(node_edge_dict,(src(edgess[e]),dst(edgess[e]))=>e)
push!(node_edge_dict,(dst(edgess[e]),src(edgess[e]))=>e)
end
return node_edge_dict
end
function edge_betweenness_centrality(g::AbstractGraph,node_edge_dict::Dict{Tuple{Int64,Int64},Int64})
edge_betweenness = zeros(Float64,ne(g))
for root in vertices(g)
values = zeros(Float64,ne(g))
dist_array = zeros(Int64,nv(g))
n_paths_array = zeros(Int64,nv(g))
dict = Dict{Int64,Vector{Int64}}(1=>[root])
queue = Int64[root]
visited = Int64[root]
dist_array[root] = 0
n_paths_array[root] = 1
while !isempty(queue)
for i in neighbors(g,queue[1])
if !(i in visited)
push!(queue,i)
push!(visited,i)
dist_array[i] = dist_array[queue[1]]+1
if haskey(dict,Int64(dist_array[i]+1))
push!(dict[Int64(dist_array[i]+1)],i)
else
push!(dict,Int64(dist_array[i]+1) => [i])
end
n_paths_array[i] = sum((has_edge(g,i,j) ? n_paths_array[j] : 0) for j in dict[Int64(dist_array[i])])
end
end
popfirst!(queue)
end
weights = zeros(nv(g))
visit = dict[length(keys(dict))]
for i in length(keys(dict))-1:-1:1
nodes = dict[i]
for k in 1:length(nodes)
for j in neighbors(g,nodes[k])
if j in visit
values[node_edge_dict[(j,nodes[k])]] = n_paths_array[nodes[k]]/n_paths_array[j]*(weights[j]+1)
weights[nodes[k]]+=values[node_edge_dict[(j,nodes[k])]]
end
end
end
visit = nodes
end
edge_betweenness.+=values
end
return edge_betweenness./2
end