This repository was archived by the owner on Dec 16, 2022. It is now read-only.
forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathactor_centrality.jl
70 lines (59 loc) · 1.52 KB
/
actor_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
60
61
62
63
64
65
66
67
68
69
70
type Node
name::UTF8String
n::Set{Node}
Node(name) = new(name, Set{Node}())
end
typealias Graph Dict{UTF8String, Node}
function get(G::Graph, name)
if haskey(G, name)
return G[name]
end
G[name] = Node(name)
end
function centrality_mean(G::Graph, start_node)
dists = Dict{Node,Uint64}()
next = Set([G[start_node]])
cdist = 0
while !isempty(next)
nnext = Set{Node}()
for n in next
if !haskey(dists, n)
dists[n] = cdist
for neigh in n.n
push!(nnext, neigh)
end
end
end
cdist += 1
next = nnext
end
mean([ v for (k,v) in dists ])
end
function read_graph()
G = Graph()
actors = Set()
open(joinpath(JULIA_HOME,"..","..","test","perf","kernel","imdb-1.tsv"), "r") do io
while !eof(io)
k = split(strip(readline(io)), "\t")
actor, movie = k[1], join(k[2:3], "_")
ac, mn = get(G, actor), get(G, movie)
push!(actors, actor)
push!(ac.n, mn)
push!(mn.n, ac)
end
end
G, sort!([ a for a in actors])
end
function actor_centrality()
G, actors = read_graph()
d = Dict{UTF8String, Float64}()
for a in actors[1:50]
d[a] = centrality_mean(G, a)
#print("$a: ", d[a], "\n")
end
vals = sort!([(v,k) for (k,v) in d])
#for i=1:20
# print("$i: ", vals[i], "\n")
#end
# print(centrality_mean(G, "Hoffman, Dustin"), "\n")
end