-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbetweennes_centrality.rb
60 lines (46 loc) · 1019 Bytes
/
betweennes_centrality.rb
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
60
VERTICES = [1,2,3,4,5,6,7]
EDGES = [
[1,2],
[1,3],
[3,4],
[3,5],
[5,7],
[4,7],
[4,6]
]
def neighbors(v)
EDGES.select {|edge| edge[0]==v}.map {|edge| edge[1]}
end
centrality = Hash.new(0)
VERTICES.each do |s|
stack = []
predecessors = Hash.new {|h,k| h[k]=[]}
sigma = Hash.new(0)
sigma[s]=1
d=Hash.new(-1)
d[s]=0
queue = [s]
while !queue.empty? do
v = queue.shift
stack << v
neighbors(v).each do |w|
if d[w] < 0
queue << w
d[w] = d[v]+1
end
if d[w] == d[v]+1
sigma[w] += sigma[v]
predecessors[w] << v
end
end
end
delta = Hash.new(0)
while !stack.empty? do
w = stack.pop
predecessors[w].each { |v2| delta[v2] += (sigma[v2]/sigma[w].to_f) * (1+delta[w]) }
centrality[w] += delta[w] unless w==s
end
end
centrality.each_pair do |v,c|
puts "centrality of #{v} is #{c}"
end