-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathcloseness.m
executable file
·30 lines (24 loc) · 959 Bytes
/
closeness.m
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
% Compute the closeness centrality for every vertex: 1/sum(dist to all other nodes)
%
% INPUTs: adjacency matrix, nxn
% OUTPUTs: vector of closeness centralities, nx1
%
% Source: social networks literature (example: Wasserman, Faust, "Social Networks Analysis")
% Other routines used: simpleDijkstra.m
% Last updated: Sep 28, 2012
function C = closeness(adj)
C = zeros(length(adj), 1); % initialize closeness vector
for i = 1:length(adj)
C(i) = 1 / sum(simpleDijkstra(adj, i));
end
%!test
%!shared T
%! T = load_test_graphs();
%!assert(closeness(T{4}{2})',[1/(1+1+2+3+3), 1/(1+1+2+3+3), 1/(1+1+1+2+2), 1/(1+1+1+2+2), 1/(1+1+2+3+3), 1/(1+1+2+3+3)])
%!assert(closeness([0 1 1; 1 0 0; 1 0 0]),[0.5 1/3 1/3]')
%!assert(closeness(T{13}{2}),[1/(1+1), 1/(1+1), 1/(1+1)]')
%!demo
%! bowtie=[0 1 1 0 0 0; 1 0 1 0 0 0; 1 1 0 1 0 0; 0 0 1 0 1 1; 0 0 0 1 0 1; 0 0 0 1 1 0];
%! closeness(bowtie)
%! adj = [0 1 1; 1 0 1; 1 1 0];
%! closeness(adj)